Stop Order isn't triggering on Entry Bar
28.05.2020 17.17

Algorithmic Trading with NinjaTrader

NinjaCoding friend

Hi Uri,

Thank you for the awesome course. I am on NT8 and tried to add the logic to my strategy but I am finding issues with Stoploss order.

I tried both the StopMarket and Limit variation but seems like they aren't working on Live/Playback Real-time data.

In backseat, it is working fine.


Now when I try the same on real-time :

1st:

2nd:

I am getting this error.

The stop loss order isn't triggering on the entry bar! While the profit target order is working fine and triggering on the same candle.


Update :

URI,

I have tried your script "MoneyManagement" one too. It is throwing the same error.

Please have a look at this.

  • Yuri Zolotarev
    28.05.2020 17:48

    Hey!

    This is a very common issue in NinjaTrader. Basically, this happens because orders get placed above the market. It's quite a long topic that I have covered in these articles. Have a read.

    https://ninjacoding.net/ninjatrader/blog/stopordersabovemarket

    https://ninjacoding.net/ninjatrader/blog/stopordersabovemarketparttwo

    Anyhow, the code will need edits so you have some challenges to face) Take care!

    P.S. If you want to dig into this more there are parts of this issue also explored in my new course on Trade System Architecture

    https://ninjacoding.net/ninjatrader/courses/tsarch

    In there you will find protection for replay mode where you would need to check your current bid and ask before placing the order to make sure you are placing it correctly in respect to the price present at that moment on the live market.

    Hope this helps!


  • NinjaCoding friend
    28.05.2020 18:28

    Hi Uri,

    Just to make sure we are on same page let me explain the issue in simple words. The NT isn't accepting the stoploss order on Entry bar. 

    1st : The Short trade is executed at this bar.

     

    2nd : According to the code mentioned in the previous post it should have placed Entry and Stoploss order just after the fill. I am placing two orders here, 1st one is Stoploss which is equal to fill_price + 4 * tick_size and other is Target which is = fill_price + 4* ticksize. The Target order is working fine but even though the price stays long below the entry price the Stoploss order isn't working. 

    See, It is placing stop order on next bar instead of the current bar.


    So in short it isn't taking the stoporder on entry_bar no mater where the stop_price is. I tried placing stop order above the previous swing high which is way higher than the fill price but still it isn't taking the stoploss order. It is somehow ignoring the stoporder on Entry_Bar.

  • Yuri Zolotarev
    28.05.2020 19:24

    You should try setting TraceOrders to true and see what the output window prints out. Debug the issue deeper and you will understand. Let us know how it goes.

  • NinjaCoding friend
    29.05.2020 04:04
    I tried it and here is the output of the scenario.
    22/05/2020 10:06:04 Strategy 'RJ/201627762': Entered internal SubmitOrderManaged() method at 22/05/2020 10:06:04: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=2920.25 SignalName='Short' FromEntrySignal=''
    22/05/2020 10:06:04 Strategy 'RJ/201627762': Amended matching order at 22/05/2020 10:06:04: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=2920.25 SignalName='Short' FromEntrySignal=''
    22/05/2020 10:06:14 Strategy 'RJ/201627762': Entered internal SubmitOrderManaged() method at 22/05/2020 10:06:14: BarsInProgress=0 Action=BuyToCover OrderType=StopLimit Quantity=0 LimitPrice=2923.75 StopPrice=2921.25 SignalName='ShortStop' FromEntrySignal='Short'
    22/05/2020 10:06:14 Strategy 'RJ/201627762': Ignored SubmitOrderManaged() method at 22/05/2020 10:06:14: BarsInProgress=0 Action=BuyToCover OrderType=StopLimit Quantity=0 LimitPrice=2923.75 StopPrice=2921.25 SignalName='ShortStop' FromEntrySignal='Short' Reason='This was an exit order but no position exists to exit'

    It isn't acknowledging the filled "Short" entry and throwing error of "This was an exit order but no position exists to exit".



  • Yuri Zolotarev
    29.05.2020 12:56

    Let us see how you place your entry order. What is the name for your entry order? If it's not named "Short" this will happen because it is trying to find a "From Entry Signal" position that was opened using an entry short order with name "Short" set like this

    EnterShort(PositionSize, "Short");

  • NinjaCoding friend
    29.05.2020 15:21

    Hi Yuri,

    I have mentioned the complete order list above and if you look at the first output.

    22/05/2020 10:06:04 Strategy 'RJ/201627762': Entered internal SubmitOrderManaged() method at 22/05/2020 10:06:04: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=2920.25 SignalName='Short' FromEntrySignal=''

    As we can see the "SignalName" is "Short" and the code is working fine in backtesting but not in execution.

    This isn't the issue for sure. Also I am getting the same issue with the BollingerBand strategy ( In Algo Trading Course) so not sure if this is a logical error.

  • Yuri Zolotarev
    29.05.2020 15:54

    You have described 2 different problems.

    1) Your order gets rejected because it is being placed above the market.

    2) The order is ignored because the position does not exist.

    It is hard to say anything else. There is probably a mistake somewhere in the logic of the code.

  • NinjaCoding friend
    29.05.2020 16:03

    There is only one issue:

    ExitStopOrder isn't triggering on the entry bar.

    In the above TraceOrder update please look, "Short" order is filled but the Stoploss order getting rejected because it doesn't find open-position name "Short".

    I have also posted my exit code too. ( I am not sure then why the target order is getting filled ) 

  • Yuri Zolotarev
    29.05.2020 16:06
    Paste the whole strategy code here, please use style code in the editor whilst doing it.
  • Yuri Zolotarev
    29.05.2020 17:33
    ExitShortStopLimit(_stop + 10 * TickSize, _stop, "ShortStop", "Short");
    These orders guarantee price but do not guarantee exit. Therefore during live execution even if you saw a bar move by this price you are not guaranteed a fill.
    I suggest you switch to ExitShortMarket orders to exclude this scenario and see if the problem continues.

    Also

    1) Don't set your stop and profit in the OnOrderUpdate, set them in the OnExecutionUpdate

    2) Don't reset them on every bar close, place a good until cancelled order once.

    I suggest you review the framework from this course where this is shown in more detail

    https://ninjacoding.net/ninjatrader/courses/strategydesignessentials

  • NinjaCoding friend
    29.05.2020 17:56

    Yuri, Thanks for the suggestions.

    I tried the ExitShortMarket order too earlier and the problem is same. 

  • Yuri Zolotarev
    29.05.2020 18:00

    I don't see any obvious reason of why this might be occurring. I can suggest two more improvements.

    1) Move aways from OnOrderUpdate as suggested earlier.

    2) Use GTC orders

    3) Use and if position is active condition before set entry orders to avoid them getting set over the existing positions

  • NinjaCoding friend
    29.05.2020 18:04

    I will surely implement these soon.


    Do you think there could be a bug in NinjaTrader itself, as in TraceOrder we clearly see the order.

  • Yuri Zolotarev
    29.05.2020 18:06

    I doubt it, I am sure whilst properly debuged the problem can be solved and is 99% human factor.