my strategy is based around the market doing a certain thing within a certain time. Is there anyway I can program a time based exit? Basically I want to exit my trade after 3 periods if profit isn't at least at a certain level x. Is this possible? could somebody give me a few hints to how I should code this? Thanks for your time
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Time Based Exit
Collapse
X
-
Time Based Exit
Hi Guys,
my strategy is based around the market doing a certain thing within a certain time. Is there anyway I can program a time based exit? Basically I want to exit my trade after 3 periods if profit isn't at least at a certain level x. Is this possible? could somebody give me a few hints to how I should code this? Thanks for your timeTags: None
-
Coolmoss, thanks I will try it out. Could I ask you another question, still related to stoploss. I basically have a strategy that enters on a breakout with stoploss on the daily high, but when backtesting it always stops me out, because working on daily periods NT doesn't recognize the intraday price pattern. Would adding Add(PeriodType.Minute, 1); in the Initialize() section solve my problem? or do I need to do something more complex? Thanks
Comment
-
basically this is the code I'm using, If you run it on a daily chart, you will notice that often your stopped on the same candle you enter. However if you use daily candle, NT has no memory to know if the Entry occurred before or after the Stop level was traded, I suspect the only way to know this is using MTF, what you think?
Moreover, I would like to add the following condition:Code:if (Position.MarketPosition == MarketPosition.Flat) { if (//Long Entry Condition) { SetStopLoss(Low[0]-TickSize); EnterLongStop(1,High[0]+TickSize); } }
where R = (High[0] - Low[0] + 2*TickSize) is defined each time once an EntryLong is taken. Would you know how to code that? It's driving me crazyCode:if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() <= R) ExitLong; else if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() > R) SetStopLoss(Position.AvgPrice + R)
Also please notice I've sent you a private message
Comment
-
Most of my work is using intraday bars and it's been awhile since I coded with daily bars. You might be right that a second data series is necessary. That certainly would allow for what you want to do. However, some experimenting with your current code might reveal what's missing to get your stops to work correctly. I suspect using some sort of bool flag to prevent an early stop might be a viable approach.
Regarding your second question: take the R = (...... bit of code and place it just before your if statment. That way R will be recalculated immediately before each running the if/else sequence
HTH
Comment
-
You mean I should place the R like this?
But shouldn't I somehow declare R before? I am getting an error, as I says R doesn't exist in the current contextCode:if (Position.MarketPosition == MarketPosition.Flat) { if (//Long Entry Condition) { SetStopLoss(Low[0]-TickSize); EnterLongStop(1,High[0]+TickSize); R = (High[0] - Low[0] + 2*TickSize); } } if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() <= R) ExitLong; else if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() > R) SetStopLoss(Position.AvgPrice + R)
Comment
-
I removed the formula for R from the "flat" block, and placed it immediately before the "not flat" block. Normally, I would declare R as a variable in the variable section like this:
double R = 0;
I set it equal to zero just to initialize it to some value. However, there is nothing wrong with declaring the variable right in the logical code itself. It's just often considered sloppy programming practice unless you're declaring a variable with limited scope (ie the variable is only used inside a method and is not to be available to all the code).
Code:if (Position.MarketPosition == MarketPosition.Flat) { if (//Long Entry Condition) { SetStopLoss(Low[0]-TickSize); EnterLongStop(1,High[0]+TickSize); } } double R = (High[0] - Low[0] + 2*TickSize); if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() <= R) ExitLong; else if(Position.MarketPosition != MarketPosition.Flat && BarsSinceEntry >= 3 && Position.GetProfitLoss() > R) SetStopLoss(Position.AvgPrice + R)Last edited by coolmoss; 02-24-2013, 12:54 PM.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
663 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
376 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment