Another, easier approach is using market orders to reverse here - they would not be impacted by the order handling rules in the managed approach.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Stop loss not working
Collapse
X
-
You could set for example a bool / flag variables if you see those having OrderState.Cancelled in your OnOrderUpdate() and only then do the reverse orders with limits.
Another, easier approach is using market orders to reverse here - they would not be impacted by the order handling rules in the managed approach.
-
I don't see how this applies. I'm not having a problem entering in the opposition direction: when my stop fires off exiting me from a long, my position does reverse and I go short. What doesn't happen is my stop loss exit for the short position isn't entered.Originally posted by NinjaTrader_Bertrand View PostAnother, easier approach is using market orders to reverse here - they would not be impacted by the order handling rules in the managed approach.
Comment
-
I attempted this with the following code:Originally posted by NinjaTrader_Bertrand View PostYou could set for example a bool / flag variables if you see those having OrderState.Cancelled in your OnOrderUpdate() and only then do the reverse orders with limits.
I'm still seeing the same behavior. My entry order goes through, but my ExitShortStop does not.//use this method to listen for stop order execution and execute reversal orders as appropriate
protected override void OnExecution(IExecution execution)
{
if(longStopOrder != null && longStopOrder == execution.Order && shortOnReversal)
{
CancelOrder(longTargetOrder);
}
if(shortStopOrder != null && shortStopOrder == execution.Order && longOnReversal)
{
CancelOrder(shortTargetOrder);
}
}
/// <summary>
/// Called on each incoming order event
/// </summary>
protected override void OnOrderUpdate(IOrder order)
{
// When a position is stopped out, the target order is canceled in OnExecution. Check for that condition, and then place entry orders
if (longTargetOrder != null && longTargetOrder == order && shortOnReversal
&& order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
shortStopOrder = ExitShortStop(shortStop, "short_entry");
shortTargetOrder = ExitShortLimit(shortPT, "short_entry");
}
if (shortTargetOrder != null && shortTargetOrder == order && longOnReversal
&& order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
EnterLongStopLimit(longEntry, longEntry, "long_entry");
longStopOrder = ExitLongStop(longStop, "long_entry");
longTargetOrder = ExitLongLimit(longPT, "long_entry");
}
}
I've attached the strategy. Please advise.Attached Files
Comment
-
I thought I'd try using the Set's for exits, and not issuing those commands until after the entry orders fired via OnExecution:
For some reason when I run this code in a strategy analyzer, NT freezes up and never returns, even if I let it run for hours, and performance on my laptop comes to a stand still. I have to kill NT via task manager to go out of it. Not sure why. Any ideas?//use this method to listen for stop order execution and execute reversal orders as appropriate
protected override void OnExecution(IExecution execution)
{
if (execution.Name == "Stop loss")
{
if (shortOnReversal)
shortEntryOrder = EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
else if (longOnReversal)
longEntryOrder = EnterLongStopLimit(longEntry, longEntry, "long_entry");
}
else if (execution.Name == "short_entry")
{
SetStopLoss("short_entry", CalculationMode.Price, shortStop, false);
SetProfitTarget("short_entry", CalculationMode.Price, shortPT);
}
else if (execution.Name == "long_entry")
{
SetStopLoss("long_entry", CalculationMode.Price, longStop, false);
SetProfitTarget("long_entry", CalculationMode.Price, longPT);
}
}Attached Files
Comment
-
You could set a bool flag when you cancel the order, then use OnMarketData() to check the flag, order state and MarketPosition, and then place the reverse order. Remember to reset the flag after you place the order.Originally posted by Style View PostI'm trying to make a go of this via the managed approach. I've went through the code, making sure that I don't have orders that will be ignored because of the order management rules detailed by Adam in the 6th post of this thread. However, I'm still seeing some cases where my stop is being ignored.
So far I've been using the functions ExitLongStop and ExitShortStop for stops. I listen for their execution via OnExecution, and when they are executed I submit my stop and reverse orders, both entry and exit:
But the stop is some times ignored.
I could try using SetStopLoss and SetProfitTarget, but I'm not sure how to listen for their execution via OnExecution, since the set commands do nor return an iorder object.
Any ideas what I could try next? Thanks!
Comment
-
Couldn't you just use OnOrderUpdate to check for when the order was canceled rather than OnMarketData?Originally posted by koganam View PostYou could set a bool flag when you cancel the order, then use OnMarketData() to check the flag, order state and MarketPosition, and then place the reverse order. Remember to reset the flag after you place the order.
In the latest version of the code I'm using SetProfitTarget. You don't even need to cancel that, right? Is there something wrong with what I do below in post 19, checking via OnExecution for the order "Stop Loss" and then issuing the entry order?
Comment
-
Style, looking into the code I'm not exactly clear what you wish to do - why do you filter for BarsInProgress if only one time frame is used in the strategy?
If you have a resting set order protecting an open position, it would be preventing you from reversing with anything other than market orders.
I didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.
Comment
-
It will eventually be a multi-time frame strategy. I’ve not yet added that additional logic because I can’t get passed this stop loss issue. It doesn’t seem wise to add more complexity to a strategy that isn’t working in a simpler form. When I get the simpler strategy working I will add more layers.Originally posted by NinjaTrader_Bertrand View PostStyle, looking into the code I'm not exactly clear what you wish to do - why do you filter for BarsInProgress if only one time frame is used in the strategy?
Even if the stop loss has fired off? I use the EnterShortStopLimit/EnterLongStopLimit for reversals in the OnExecution function when execution.Name == "Stop loss”. At the point that I’m entering the limit entry order, I should be flat, since the stop has already fired. Is that not correct?Originally posted by NinjaTrader_Bertrand View PostIf you have a resting set order protecting an open position, it would be preventing you from reversing with anything other than market orders.
By the way, I have good reason for not using EnterLong/EnterShort to reverse, instead using the stop limit. The reason is sometimes the level to go long and the level to exit a short do not align. For example, sometimes the level to stop out my short is 1.3000, but the level to enter long may be 1.3010. When the stop fires for the short I do NOT want to immediately go long! I just want to submit a stop limit entry order. Make sense?
I will investigate.Originally posted by NinjaTrader_Bertrand View PostI didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.
Thanks!Last edited by Style; 03-20-2012, 05:32 PM.
Comment
-
The reverse was working in the same bar back when I was still using ExitLongStop/ExitShortStop, and listening for its execution via OnExecution. Since switching to the Set() method, NT freezes up when I run a backtest. I no longer get a successful completion of the backtest.Originally posted by NinjaTrader_Bertrand View PostCorrect, if the Set() method is not working anymore and the position is closed, it should not lead to ignored orders. Is your reverse happening on the same bar?
Comment
-
You probably could do it that way, but remember that because of the asynchronous nature of NT, you cannot tell the order in which events happen. Except that as market data is what drives everything else, the only thing you can be certain of is that an OnMarketData() event has caused a change in the market, so any test there is testing at the first point of a market event.Originally posted by Style View PostCouldn't you just use OnOrderUpdate to check for when the order was canceled rather than OnMarketData?
In the latest version of the code I'm using SetProfitTarget. You don't even need to cancel that, right? Is there something wrong with what I do below in post 19, checking via OnExecution for the order "Stop Loss" and then issuing the entry order?
Comment
-
I added TraceOrders to the latest version (which is attached). It's had no effect. The strategy analyzer just freezes up, and eventually I have to close the window.Originally posted by NinjaTrader_Bertrand View PostI didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.
I do get the following error when I click the red X to close the window:
What could be happening here? I could use TraceOrders and see what is going on with the ExitLongStop/ExitShortStop version of the strategy, which does not freeze up. Should I give up on the Set() Method? I got the impression that using the Set() method was the preferred way.3/20/2012 7:51:59 PM|0|4|Error on running backtest: Object reference not set to an instance of an object.
Please advise. Thanks!Attached Files
Comment
-
OK, just for grins I tried running one of my older versions of the code that used ExitLongStop/ExitShortStop instead of Set(). It also froze up and gave the "Object reference not set to an instance of an object" error. This was working before, and I made no changes to the code. It's also running on the same data. How is this possible? Did my installation of NT some how get hosed? What should I try now?
Comment
-
OK, I uninstalled NT, reinstalled it, yet the problem of the back test hanging still persists. I tried running some of the sample strategies that come with NT on the same data set, and they worked with out issue. So my install appears to be ok. It is a code issue.
I even went back several versions of this same strategy, and it works. I will go back to the latest working version and start over from there. The latest 4 versions of the code do not work, but the 5th newest does.
It is so weird that this issue of hanging did not surface until the two latest versions of the code, but now none of the 4 latest versions work. Why did these issues not surface earlier? I can't explain it.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
656 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
371 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
579 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment