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
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
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.BertrandNinjaTrader Customer Service
-
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
-
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.
//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:
//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
-
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
-
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.
BertrandNinjaTrader Customer Service
Comment
-
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?
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?
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
-
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
-
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
-
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:
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
-
Hi Style, if you would like - I can offer to run your latest non working version here on my end to see if I can duplicate the 'freezing' issue you saw on your end - for this please contact me directly here at support at ninjatrader dot com via email.
Thanks,BertrandNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by AaronKoRn, Today, 09:49 PM
|
0 responses
6 views
0 likes
|
Last Post
by AaronKoRn
Today, 09:49 PM
|
||
Started by carnitron, Today, 08:42 PM
|
0 responses
9 views
0 likes
|
Last Post
by carnitron
Today, 08:42 PM
|
||
Started by strategist007, Today, 07:51 PM
|
0 responses
10 views
0 likes
|
Last Post Today, 07:51 PM | ||
Started by StockTrader88, 03-06-2021, 08:58 AM
|
44 responses
3,976 views
3 likes
|
Last Post
by jhudas88
Today, 06:59 PM
|
||
Started by rbeckmann05, Today, 06:48 PM
|
0 responses
9 views
0 likes
|
Last Post
by rbeckmann05
Today, 06:48 PM
|
Comment