Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy trade price outside range of price bar
Collapse
X
-
This was during a backtest (that is, on historical data). The code I'm using seems to do the right thing almost all the time, but once in a while it does something crazy. I'm using Print(), Trace() and Output window to try and understand. I couldn't glean any clues there.
Thanks much for any comment you can make on this.
Comment
-
Thanks, which order types are you using here? You would need to simplify the code and cirle into those times when things do not work out as you expect and debug what price your orders are submitted at and then compare to what executions are being given. Should you see this in live trading ensure the PC clock is then sync up the execution timestamps would be lining up with the data as expected.
Comment
-
Hi Bertrand,
I'm using Market orders (via the managed approach) to effect a reversal at some condition set. I've done some careful looking at this (and trying to debug as best I can) and am finding something the in Trace info in the output window. It doesn't seem right to me, but perhaps I just don't understand what it's saying.
Here's what I'm seeing:
Several lines which show, correctly, my stops/targets being set
Then several lines showing, correctly, my entry orders being submitted
Now, the odd part:
On a following bar, where I'm still in my position and should be in position, the output window shows the targets (from the SetProfitTarget statement) being canceled. I'm not using any Cancel statements and don't understand what might be going on here.
Comment
-
Further to my above email, I did some more testing. I'm not sure if this means anything, but if you can bear with my wordiness I'll explain as accurately as I can.
I'm in a short position and there is a stop loss set at say 1.3221. Price is currently well below the stop, but I get a signal to reverse. This is effected with EnterLong() along with a new SetStopLoss.
On the chart, the "reversal" shows up as the new long position taking place at the correct price. But the prior short position, which should close at the same price (and in other reversals on the chart it does work correctly) instead closes at the old short stop price of 1.3221. In the trace output, nothing shows up as abnormal.
However, just for the heck of it, before the statements to EnterLong() , I added in the quite unnecessary ExitShort(). Nothing changed on the chart, but in the trace output, there was now a line that said the ExitShort() was rejected because no position existed (which of course there was a position).
I realize it's important to understand in the exact order all these statements are executing, but I've carefully worked through the code, line by line, and I'm baffled as to why, only in certain instances, the exit price of the prior position (only in this case of a reversal) is happening at the wrong price. As you can see in chart snippet at the top of thread, the wrong price can even be outside the range of the bar on which the execution took place.
So far this is only on historical data. I haven't been able to get playback to work so I'm not sure how that might look. Any suggestions or even a guess?
Comment
-
coolmoss, is there any way we could test with your strategy on our end internally to further check into matters? You could contact us directly at support at ninjatrader dot com
You're correct the ExitShort() would be not needed and duplicate in your case, the Enter() method would reverse. The cancelled exit orders are ok as well, since if you reverse the old ones protected the reversed from position are not needed anymore and need to be cancelled, this happens internally for you in the managed approach with the Set() exit methods. Then as you have the new position, a fresh set of targets / stops is sent out.
Comment
-
I've made some progress getting various glitches sorted out. I'm stumped on one though. I have a block of code that is not getting executed on one particular bar where it should. The code does execute correctly elsewhere.
Here is the pertinent code:
The top portion where revToShort is set to true is the very last bit in the code where BarsInProgress == 0. In the output window the first print statement appears, but the second doesn't, indicating that the block of code is not executing. I can't figure out why this BarsInProgress block of code is not executing. The primary data series is a 10 tick range bar, the secondary series is a 1 tick, tick bar. The bar in question goes many ticks lower than the prior primary bar, so should be executing.Code:if (Position.MarketPosition == MarketPosition.Long && CONDITION == true) { revToShort = true; Print("revToShort = " + revToShort + Time[0].ToString()); } if(BarsInProgress == 1) { if( revToShort == true && Close[0] <= Instrument.MasterInstrument.Round2TickSize(Lows[0][0]-entryOffset*TickSize )) { SetStopLoss(CalculationMode.Price,Highs[0][0] + entryOffset*TickSize); SetProfitTarget("FIRSTTARGET",CalculationMode.Ticks,firstTarget); SetProfitTarget("SECONDTARGET",CalculationMode.Ticks,secondTarget); SetProfitTarget("THIRDTARGET",CalculationMode.Ticks,thirdTarget); // overload here is for parameter specifying orders to execute on secondary data series EnterShort(1,1, "FIRSTTARGET"); EnterShort(1,1, "SECONDTARGET"); EnterShort(1,1, "THIRDTARGET"); EnterShort(1,1, "RUNNER"); revToShort = false;// need to reset to false to prevent the reversal block from executing every tick stopControl = true;//need to reset to true if it was false due to prior position first target being met Print("executed short reversal block at " + Time[0].ToString()); }
Any suggestion on how I might further narrow down why, in this one instance (I can't find any others so far) this secondary bar series code is not executing?
Comment
-
Nice work staying on this one's heels coolmoss - you would need to check into this portion then -
Close[0] <= Instrument.MasterInstrument.Round2TickSize(Lows[0][0]-entryOffset*TickSize
That's the second series Close compared to the primary series Low minus your offset - is that the intention?
Would also try this simplified down to for example no offset and no ticksize add, any change? You would need to rule any variable out one by one to understand what causes the unexpected behavior in your code then to address it.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
637 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
366 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
107 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
569 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
572 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment