Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Orders closing mysteriously in backtest...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Orders closing mysteriously in backtest...

    I will do my best to explain my complicated situation as briefly as I can.

    I am testing my own strategy that uses a third party indicator as well as my own.

    There are advantages to being able to determine entry conditions on a lower timeframe than the chart timeframe. I am running the "chart" data series on 120 minute (BarsInProgress 0) but I have added other timeframes for entry calculations. The timeframe that I wish to enter trades on is the 5 minute. I also use the 1 minute and 3 minute because my third party indicator requires them.

    When I enter a trade I place limit orders on the 5 minute data series (BarsInProgress 3). These trades enter as expected, however they all "close" at the beginning/end of a new 120 min candle unexpectedly. It is not always the subsequent 120 minute candle, it may be 2 or 3 chart timeframe candles after the entry, and it is not "closing" at end of session or end of day. This seems to be random. I say "close" in quotes because it is not exactly exiting the trades as you would expect.

    To clarify further, when I enter a trade I set target and stop loss orders. However, the trade closes without price reaching either of these exits. For example, conditions allow my strategy to place a limit order entry at 6:45PM on BIP 3 (5 min data series), with target and stop loss orders set. Trade is eventually triggered at 8:55PM. Things look normal as the trade plays out until at 10:00PM I get this log (from TraceOrders)

    Cancelled pending exit order, since associated position is closed

    It then proceeds to cancel my target and stop loss and the Position.MarketPosition moves from Short to Flat. But the trade is still active despite the market position saying otherwise. This then causes my strategy to enter a different trade, however the original entry is still active and naked with no exit orders. According to the backtest results, the first trade closes when the second trade also has the Cancellation as above at (12:00AM) and the cycle repeats for the length of the backtest.

    Note the screenshot attached. Each trade closes on the open (close?) of a 120 minute bar, yet was opened on the 5 minute timeframe as intended. Almost all of these trades closed at a random value that was often an overshoot of the original stop loss, and the one trade that went to target somehow closed at the right place, however its exit orders were also closed 2 hours prior according to the logs for the same reason as all the others.

    What could be closing these orders? It seems like the order handling thinks the trade has closed but the Strategy Analyzer still has them open?

    I should also note that I have Exit on session close unselected and there are no errors in the logs. This strategy also works if I make the chart timeframe = the entry timeframe. It would just be better if I could make it work with the chart timeframe larger than the entry timeframe for reasons that would take me a long time to explain.
    Attached Files

    #2
    Hello Slappy,

    Are you printing the order.ToString() at the top of OnOrderUpdate()?

    Below is a link to a support article on adding Print() and enabling TraceOrders to understand behavior.


    What is the order that is exiting the position?

    If the order that is exiting the position is a stop or limit order, what is the original stop or limit price of the order and what is the fill price?

    Have you implemented 1-tick intra-bar granularity for improved order fill accuracy? (This would require using exit order methods instead of set methods such as SetStopLoss()).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chels thanks for getting back to me!

      I am indeed using the order.ToString() prints to try to understand what is going on. I will post a section of the logs that my strategy prints out. It is quite extensive but hopefully wont be too confusing.

      As you can see below, at 6:45pm my strategy finds a valid entry and submits it as a limit order. It has a TP and SL. This order is pending for a few bars before executing on the 8:55pm bar. The trade then plays out until it strangely cancels at 10:00pm. Although the exit time of the trade according to the Strategy Analyzer is 12:00am, 2 hours later (which is 1 chart bar).

      Something is making the strategy think the associated position is closed, it is then closing out the TP and SL and then I assume the Strategy Analyzer is closing them out on the next 120 minute bar that traded through the original SL and TP maybe? Not fully sure on why it picks the exit bars that it does but I think that should be resolved once we determine what is "closing" out this position early.

      Log Sample (I removed some bars logs in between events due to character limit for this post):
      27/06/2023 6:45:00 PM: SHORT ENTRY FOUND | Entry = 4672.75 | Stop = 4679.25 | Target = 4630.5 | Contracts = 1
      27/06/2023 6:45:00 PM: Placing Simulated Short Trade
      27/06/2023 6:45:00 PM: Trade Name is LTF:buyT | STF:sellT 9% | ETF:sellT
      27/06/2023 6:45:00 PM: Stop Type is Fixed
      27/06/2023 6:45:00 PM Strategy 'Matthew633/-1': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='LTF:buyT | STF:sellT 9% | ETF:sellT' Mode=Price Value=4679.25 IsSimulatedStop=False IsMarketIfTouched=False
      27/06/2023 6:45:00 PM Strategy 'Matthew633/-1': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LTF:buyT | STF:sellT 9% | ETF:sellT' Mode=Price Value=4630.5 IsSimulatedStop=False IsMarketIfTouched=False
      27/06/2023 6:45:00 PM Strategy 'Matthew633/-1': Entered internal SubmitOrderManaged() method at 27/06/2023 6:45:00 PM: BarsInProgress=3 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=4672.75 StopPrice=0 SignalName='LTF:buyT | STF:sellT 9% | ETF:sellT' FromEntrySignal=''
      ORDER UPDATE: orderId='NT-00002-62' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Submitted instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00002-62' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Accepted instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00002-62' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Working instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-15'
      27/06/2023 6:50:00 PM: ETF Bar
      27/06/2023 6:50:00 PM: Limit order pending...
      ...
      27/06/2023 8:45:00 PM: Limit order pending...
      27/06/2023 8:50:00 PM: ETF Bar
      27/06/2023 8:50:00 PM: Limit order pending...
      ORDER UPDATE: orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Submitted instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Accepted instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Submitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Accepted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00002-62' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Filled instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=4672.75 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-15'
      27/06/2023 8:55:00 PM: ETF Bar
      27/06/2023 8:55:00 PM: Currently in a trade...
      27/06/2023 9:00:00 PM: ETF Bar
      27/06/2023 9:00:00 PM: Currently in a trade...
      27/06/2023 9:05:00 PM: ETF Bar
      27/06/2023 9:05:00 PM: Currently in a trade...
      27/06/2023 9:10:00 PM: ETF Bar
      27/06/2023 9:10:00 PM: Currently in a trade...
      27/06/2023 9:15:00 PM: ETF Bar
      27/06/2023 9:15:00 PM: Currently in a trade...
      27/06/2023 9:20:00 PM: ETF Bar
      27/06/2023 9:20:00 PM: Currently in a trade...
      27/06/2023 9:25:00 PM: ETF Bar
      27/06/2023 9:25:00 PM: Currently in a trade...
      27/06/2023 9:30:00 PM: ETF Bar
      27/06/2023 9:30:00 PM: Currently in a trade...
      27/06/2023 9:35:00 PM: ETF Bar
      27/06/2023 9:35:00 PM: Currently in a trade...
      27/06/2023 9:40:00 PM: ETF Bar
      27/06/2023 9:40:00 PM: Currently in a trade...
      27/06/2023 9:45:00 PM: ETF Bar
      27/06/2023 9:45:00 PM: Currently in a trade...
      27/06/2023 9:50:00 PM: ETF Bar
      27/06/2023 9:50:00 PM: Currently in a trade...
      27/06/2023 9:55:00 PM: ETF Bar
      27/06/2023 9:55:00 PM: Currently in a trade...
      27/06/2023 10:00:00 PM: STF Bar | HIGH = 4673.25 | LOW = 4663.25
      27/06/2023 10:00:00 PM: Check for VTC candle using Trendline of 4680.75
      27/06/2023 10:00:00 PM: ETF Bar
      27/06/2023 10:00:00 PM: Higher Timeframe trend check confirms this is not a VTCL, Trendline = 4680.75, Low = 4663.75
      27/06/2023 10:00:00 PM: Currently in a trade...
      27/06/2023 10:00:00 PM Strategy 'Matthew633/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelPending instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelSubmitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      ORDER UPDATE: orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      27/06/2023 10:00:00 PM Strategy 'Matthew633/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
      27/06/2023 10:05:00 PM: ETF Bar​

      Comment


        #4
        Hello Slappy,

        The profit target order was cancelled because the position was exited by the stop loss being filled.

        ORDER UPDATE: orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-15'
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ahh I did miss that Filled section. So that explains why the orders were cancelled, however that doesn't explain the problem.


          See this screenshot below of the actual trade that my Strategy is taking.

          The entry is the candle on the 5 min chart with the red down triangle. The candle that supposedly filled the stoploss is the green arrow candle. This "fill" was supposed to happen at a price of 4679.25, however the 10:00PM candle is no where near that price.

          Click image for larger version

Name:	image.png
Views:	86
Size:	71.2 KB
ID:	1324865
          Moreover, the Strategy Analyzer says the trade was exited at 12:00AM not 10:00PM, which is even stranger. (see my original post for the Analyzer details.)

          Got any ideas why this stop loss would be getting filled on the wrong candle?

          Comment


            #6
            Hello Slappy,

            I'm seeing the order time of the stop order as filled at 20:00.

            time='2023-06-27 20:00:00'

            This would be 8:00 PM.

            The chart screenshots unfortunately don't provide much helpful information about what a script is doing internally in the code.

            If you feel an order should not have filled, print the State (so we know if this is historical or real-time), print the GetCurrentAsk(), GetCurrentBid(), and the Open[0], High[0], Low[0], and close in OnOrderUpdate() when the order.OrderState == OrderState.Filled.


            Also, synchronize the PC clock with internet time.
            • Shutdown NinjaTrader *important
            • Windows 10
              • Navigate to the Windows Control Panel
              • Select Clock and Region -> then click 'Set the time and date'
                (Or click 'Date and Time' if this is visible)
              • Click on Internet Time tab
              • In the Server: drop-down select time.nist.gov
              • Click Update
            • Windows 11
              • Open Settings
              • Click on Time & language.
              • Click the Date & time page on the right side.
              • Under the “Additional settings” section, click the Sync now button.
            • If the message that appears says 'successful', your PC clock should now be updated
              If not, select a different server from the Server: drop-down and try again (repeat until one of the servers is successful)
            • Restart NinjaTrader
            • Connect to a data feed
            • Reload All Historical Data *important
            Below are a few links to 3rd party educational sites with steps to synchronize with internet time.

            Windows 7 - http://mintywhite.com/windows-7/7mai...t-time-how-to/

            Windows 10 - https://www.windowscentral.com/how-m...ers-windows-10
            - http://www.windows10update.com/2015/...t-time-server/

            Windows 11 - https://pureinfotech.com/fix-clock-out-sync-windows-11/
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I have my PC clock synced with the internet regularly and I have manually done it just before running this test. The result seems to be the same.

              This time thing is a bit strange. I print the current bar's timestamp with each log, and I have seen through many many tests that it is accurate to which bar we are looking at on the chart when the Analyzer processes it. My screenshot above is inline with the data in the test.

              The original submission of the limit orders is on the 18:45 candle and that matches the order printout:

              ORDER UPDATE: orderId='NT-00002-462' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Submitted instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00002-462' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Accepted instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00002-462' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Working instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-19'

              Strangely, when the order is filled at 20:55 the "time" field in the logs refers to the Chart bar rather than the entry bar. Remember I am testing on a 120 minute chart, and entering on a 5 minute data series. The updated SL and TP orders are modified with a timestamp of 20:00 which is the prior 120 minute bar's time, not the current one forming. The filled order is stamped with the original submission time of 16:45. (Note the Bid, Ask, State and OHLC printed after it, which also corresponds to the chart timeframe not the entry time frame)

              ORDER UPDATE: orderId='NT-00003-462' account='Backtest' name='Stop loss' orderState=Submitted instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00003-462' account='Backtest' name='Stop loss' orderState=Accepted instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00003-462' account='Backtest' name='Stop loss' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=Submitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=Accepted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00002-462' account='Backtest' name='LTF:buyT | STF:sellT 9% | ETF:sellT' orderState=Filled instrument='MES 12-24' orderAction=SellShort orderType='Limit' limitPrice=4672.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=4672.75 onBehalfOf='' id=-1 time='2023-06-27 18:45:00' gtd='2099-12-01' statementDate='2024-11-19'
              State: Historical
              O: 4672.75 H: 4675 L: 4667.75 C: 4669.25
              Bid: 4669.25 Ask: 4669.25​

              The stop loss is then unusually triggered at 22:00, yet the orders are stamped with that 20:00 time still. This doesn't make sense to me because by 10:00PM the new 120 min bar has formed as you can see by the OHLC below being updated (and also matching the screenshot of my chart). This was also prior to the trade being entered as I check the market position every 5 minute bar and this is when it moves from flat to short and back to flat again..

              27/06/2023 10:00:00 PM Strategy 'Matthew633/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=CancelPending instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=CancelSubmitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00004-462' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-462' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              ORDER UPDATE: orderId='NT-00003-462' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-462' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
              State: Historical
              O: 4669.25 H: 4673.25 L: 4663.25 C: 4665
              Bid: 4665 Ask: 4665

              I will point out again, the High of this bar is 4673.25, yet the stop loss is "filled" at 4679.25. How is this possible?

              Comment


                #8
                Hello Slappy,

                Is this from SetStopLoss() or are you calling an ExitShortStopMarket() with the 1 as the barsInProgressIndex?


                If the order is submitted with ExitShortStopMarket() / SubmitOrderUnmanaged(), the information about the 5 minute bar used as the barsInProgressIndex of the orders should be printed.

                I should have specified to print the Times[3][0], Opens[3][0], Highs[3][0], Lows[3][0], Closes[3][0], BarsArray[3].GetAsk(CurrentBars[3]), and BarsArray[3].GetBid(CurrentBars[3]).

                Print(string.Format("{0} | Opens[3][0]: {1}, Highs[3][0]: {2}, Lows[3][0]: {3}, Closes[3][0]: {4}, BarsArray[3].GetAsk(CurrentBars[3]): {5}, and BarsArray[3].GetBid(CurrentBars[3]): {6}", Times[3][0], Opens[3][0], Highs[3][0], Lows[3][0], Closes[3][0], BarsArray[3].GetAsk(CurrentBars[3]), BarsArray[3].GetBid(CurrentBars[3])));
                Print(order.ToString();

                If the order is submitted with SetStopLoss(), the information about the primary 120 minute bar series should be printed as set methods only update with the primary series.

                Print(string.Format("{0} | Opens[0][0]: {1}, Highs[0][0]: {2}, Lows[0][0]: {3}, Closes[0][0]: {4}, BarsArray[0].GetAsk(CurrentBars[0]): {5}, and BarsArray[0].GetBid(CurrentBars[0]): {6}", Times[0][0], Opens[0][0], Highs[0][0], Lows[0][0], Closes[0][0], BarsArray[0].GetAsk(CurrentBars[0]), BarsArray[0].GetBid(CurrentBars[0])));
                Print(order.ToString();

                That said, the output does appear unusual, but might be the wrong series. The print above should ​clarify.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Okay here are the logs with your print statements included.


                  ORIGINAL ORDER STOP LOSS "FILLED"
                  ​27/06/2023 10:00:00 PM Strategy 'Matthew633/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[3][0]: 4664.75, Highs[3][0]: 4665.75, Lows[3][0]: 4663.75, Closes[3][0]: 4665, BarsArray[3].GetAsk(CurrentBars[3]): 4665.25, and BarsArray[3].GetBid(CurrentBars[3]): 4664.75
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelPending instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[0][0]: 4669.25, Highs[0][0]: 4673.25, Lows[0][0]: 4663.25, Closes[0][0]: 4665, BarsArray[0].GetAsk(CurrentBars[0]): 4669.75, and BarsArray[0].GetBid(CurrentBars[0]): 4669.25
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelPending instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[3][0]: 4664.75, Highs[3][0]: 4665.75, Lows[3][0]: 4663.75, Closes[3][0]: 4665, BarsArray[3].GetAsk(CurrentBars[3]): 4665.25, and BarsArray[3].GetBid(CurrentBars[3]): 4664.75
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelSubmitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[0][0]: 4669.25, Highs[0][0]: 4673.25, Lows[0][0]: 4663.25, Closes[0][0]: 4665, BarsArray[0].GetAsk(CurrentBars[0]): 4669.75, and BarsArray[0].GetBid(CurrentBars[0]): 4669.25
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=CancelSubmitted instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[3][0]: 4664.75, Highs[3][0]: 4665.75, Lows[3][0]: 4663.75, Closes[3][0]: 4665, BarsArray[3].GetAsk(CurrentBars[3]): 4665.25, and BarsArray[3].GetBid(CurrentBars[3]): 4664.75
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[0][0]: 4669.25, Highs[0][0]: 4673.25, Lows[0][0]: 4663.25, Closes[0][0]: 4665, BarsArray[0].GetAsk(CurrentBars[0]): 4669.75, and BarsArray[0].GetBid(CurrentBars[0]): 4669.25
                  orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[3][0]: 4664.75, Highs[3][0]: 4665.75, Lows[3][0]: 4663.75, Closes[3][0]: 4665, BarsArray[3].GetAsk(CurrentBars[3]): 4665.25, and BarsArray[3].GetBid(CurrentBars[3]): 4664.75
                  orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM | Opens[0][0]: 4669.25, Highs[0][0]: 4673.25, Lows[0][0]: 4663.25, Closes[0][0]: 4665, BarsArray[0].GetAsk(CurrentBars[0]): 4669.75, and BarsArray[0].GetBid(CurrentBars[0]): 4669.25
                  orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'
                  27/06/2023 10:00:00 PM Strategy 'Matthew633/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00004-62' account='Backtest' name='Profit target' orderState=Cancelled instrument='MES 12-24' orderAction=BuyToCover orderType='Limit' limitPrice=4630.5 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-62' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'

                  Comment


                    #10
                    I should also clarify that I’m using SetStopLoss() as its easier in my strategy to determine a stop price at entry, rather than tracking the trade as it progresses while it is open.

                    Comment


                      #11
                      Hello Slappy,

                      I'm seeing the ask was 4679.75.

                      27/06/2023 10:00:00 PM | Opens[0][0]: 4669.25, Highs[0][0]: 4673.25, Lows[0][0]: 4663.25, Closes[0][0]: 4665, BarsArray[0].GetAsk(CurrentBars[0]): 4669.75, and BarsArray[0].GetBid(CurrentBars[0]): 4669.25

                      orderId='NT-00003-62' account='Backtest' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.25 quantity=1 tif=Gtc oco='NT-00000-62' filled=1 averageFillPrice=4679.25 onBehalfOf='' id=-1 time='2023-06-27 20:00:00' gtd='2099-12-01' statementDate='2024-11-19'​
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hey Chels,

                        Sorry, but I think you may have made a typo. The ask is at 4669.75 not 4679.75. The stop is 4679.25 and the ask is 4669.75 so I am still confused as to why price would be filling the stop order.

                        Comment


                          #13
                          Hello Slappy,

                          Thanks for highlighting that.

                          Is this the full output, and may I confirm you have not removed anything from the output below this order fill?

                          In the future, please provide the output saved as a text file. Right-click the output window, then select Save as. Attach the text file to your post by clicking the paper clip icon to upload an attachment.


                          Is 27/06/2023 10:00:00 the last bar on the chart in the Strategy Analyzer?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Here is the full test. There are a lot of logs of my own that you will have to search through.

                            This issue occurs on multiple trades throughout the test. There are no additional logs in relation to the trade we have discussed above. The subsequent logs involve the strategy placing the next trade's orders.
                            Attached Files

                            Comment


                              #15
                              Hello Slappy,

                              When debugging it is helpful to focus on one thing at a time. Often I recommend creating a new test script with only the bare code necessary to reproduce the issue.
                              Temporarily, comment all other prints other than the ones we need to diagnose this specific issue.

                              We may be looking at the wrong bar's information.

                              From comparing the chart, it appears your set to UTC+9 as the timezone, is this correct?
                              (This is 17 hours ahead of me. So the bar close at 20:00 would have been at 3:00 on my end.)

                              The order was still working after the 20:00 primary bar closed, is this correct?
                              This means the order would have filled with the 22:00 primary bar information.


                              I attempted to create reduced test script to test.
                              In the script I am also printing the execution information from OnExecutionUpdate() to see the time the execution is happening.

                              6/27/2023 1:00:00 AM | orderId='NT-00001-86' account='Sim101' name='Stop loss' orderState=Working instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.75 quantity=1 tif=Gtc oco='NT-00000-86' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-27 01:00:00' gtd='2099-12-01' statementDate='2024-11-21'

                              6/27/2023 5:00:00 AM | BarsInProgress: 0, CurrentBars[0]: 18, Opens[0][0]: 4669.25, Highs[0]: 4673.25, Lows[0]: 4663.25, Closes[0]: 4665, BarsArray[0].GetCurrentAsk(18): 4669.75, BarsArray[0].GetCurrentBid(18): 4669.25

                              6/27/2023 1:00:00 AM | orderId='NT-00001-86' account='Sim101' name='Stop loss' orderState=Filled instrument='MES 12-24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4679.75 quantity=1 tif=Gtc oco='NT-00000-86' filled=1 averageFillPrice=4679.75 onBehalfOf='' id=-1 time='2023-06-27 01:00:00' gtd='2099-12-01' statementDate='2024-11-21'

                              6/27/2023 7:00:00 AM | execution='NT-00001-86' instrument='MES 12-24' account='Sim101' exchange=Default price=4679.75 quantity=1 marketPosition=Long orderId='NT-00001-86' time='2023-06-27 07:00:00' sod=False statementDate='0001-01-01'

                              6/27/2023 7:00:00 AM | BarsInProgress: 0, CurrentBars[0]: 19, Opens[0][0]: 4665, Highs[0]: 4680.5, Lows[0]: 4662.75, Closes[0]: 4674.75, BarsArray[0].GetCurrentAsk(19): 4665.5, BarsArray[0].GetCurrentBid(19): 4665

                              I've attached the test script and output information that highlights this.
                              NinjaScript Output 11_21_2024 8_30 AM.txt
                              SetStopWithSecondaryFillTest_NT8.zip

                              Please test the script on your end, and see if this provides clarifying information.

                              I'm also seeing something odd, where there are two updates for the order fill, which is unexpected. This may be due to logic in the code as the test script I've made does not experience this.
                              Attached Files
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              95 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              153 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              80 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              54 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              70 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X