Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unmanaged Strategy Bug

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

    Unmanaged Strategy Bug

    Hello,

    I would like to report a bug.

    Version:

    8.0.27.1 (Tested on a clean installation)

    Description

    When enabling an Unmanaged strategy, it will often not respect the Trading Hours Template. It will enter a position immediately after enabling the strategy. Even if the market is closed or when chart is showing historical bars from the past.

    Steps to Reproduce

    1. Open a new Chart and set the data Series to the following values:
    Instrument: NQ
    Timeframe: 1 minute
    Days to Load: 3
    End Date: 24/02/2023
    Trading Hours: CME US Index Futures RTH
    2. Add the Ninjatrader Unmanaged Template Example Strategy (attached) to the chart and enable.

    Expected Result

    The strategy should not submit live orders because the chart is outside of the trading hours template: it is after close on a Friday. Furthermore, the chart is displaying historical bars from the past so it should not enter a position based on these bars.

    Actual Result

    The strategy Immediately places limit orders to enter the market, The Orders are based on the historical bars. The market has moved since then and therefore the order entry price is wrong and the orders are placed on the wrong side of the market, they fill immediately and place the strategy in an unexpected erroneous position.

    Additional Information

    1. The same result occurs if the chart shows today's bars but the market is closed. For example when using the US Equities RTH template which ends at 4pm ET. If you enable a strategy after 4pm ET with this trading hours template, it will submit an order.
    2. If the strategy is updated to add "Print(State)" to OnBarUpdate, the strategy State does not reach Transition or Realtime. The output of this is below:


    Code:
    ...
    Historical
    Historical
    Historical
    Historical
    Historical
    Historical
    Enabling NinjaScript strategy 'UnmanagedTemplate/288971050' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=True MaxRestarts=4 in 5 minutes
    NinjaScript strategy 'UnmanagedTemplate/288971050' submitting order
    NinjaScript strategy 'UnmanagedTemplate/288971050' submitting order
    Attached Files
    Last edited by kevinenergy; 02-27-2023, 08:53 AM.

    #2
    Hello kevinenergy,

    This would be expected, you can also see this using the managed approach or when using any trading hour template.

    Any historical order which is marked IsLiveUntilCancelled would be transitioned to realtime if it had not filled or been cancelled before transitioning. You would see this with limit orders for managed if you used IsLiveUntilCancelled = true and GetRealtimeOrder. Unmanaged orders are always live until cancelled.

    You are still seeing the transition to realtime because you are using a realtime chart and there is currently data coming in for the instrument. To see that transition you would have to Print(State) from OnStateChange. The only way this would not submit an order in realtime would be if you are currently in realtime outside of trading hours and there is no realtime data to drive logic.

    No realtime OnBarUpdate events will be observed because of the date selection however the transition of historical to realtime for orders will still happen because the script is running in realtime. You will still see any other realtime override like OnMarketData working meaning the script is still running in realtime, the script does not require OnBarUpdate to process to run in realtime.

    To avoid this the easiest way is to use the backtest tool instead of a chart, backtests don't allow a realtime transition. Another way would be to detect the bar count in the scripts code and cancel the order before the last historical bar so the script is flat and has no orders to transition.



    Last edited by NinjaTrader_Jesse; 02-27-2023, 10:49 AM.

    Comment


      #3
      Hi Jesse...

      This is not expected behavior. Not from any reasonable point of view.

      Expected behavior would be that the Historical order gets cancelled at the end of the session (in Line with the ExitOnSessionClose property). If you add a strategy to a chart that spans multiple sessions, historical entry orders get cancelled and positions closed at the end of each historical session. Why is there an exception to this behavior for the final session-close on the chart? It would be expected for "ExitOnSessionClose" to always close orders at the end of the session. I would not expect the behavior to be different for the final session before a transition to realtime.

      Furthermore, in the scenario I'm dealing with for my strategy, the entry order is a market order. The market order doesn't get transitioned into real-time. So I don't get a position entered outside of market hours. However my stop and target orders do get submitted for the non-existent position. This causes a situation where my stop and target orders risk entering my strategy into an opposite position than expected.

      I would submit that this behavior is NOT expected. Nobody expects orders to be submitted when the market is closed and ExitOnSession should always close orders (historical and realtime) at the end of the session, without exception.

      The behavior is undesirable and not expected and should be logged as a bug.
      Last edited by kevinenergy; 02-27-2023, 01:20 PM.

      Comment


        #4
        Hello kevinenergy,

        A market order won't transition as you mentioned but its targets will be transitioned if they are still active. If a LiveUntilCancelled order is submitted in historical and is not filled or cancelled before the transition happens it is submitted live. The problem you are seeing relates to the LiveUntilCancelled orders used in addition to running a realtime strategy that omits some historical data.

        By setting the date back to a previous date in the chart you are only excluding the historical bars past that date, that will skip any days between the end date and realtime like they don't exist. By skipping those days you also miss any kind of exit logic related to the trading hours like exit on session close which can leave your targets working. Strategies will still transition into realtime because they are a realtime tool and you have enabled it to trade in realtime.

        If you have any working orders when the strategy transitions to realtime then the strategy won't care that you are not looking at the right date in the chart, its still going to transition those orders because its actually working in realtime at that point. The live strategy lives in the control center and will run in realtime, it can place trades even though you won't be able to view them on the chart due to the chart settings.

        You can avoid this situation in a few ways. One is by cancelling the targets before entering realtime as part of your logic, that would be if you plan to backtest the strategy on the chart in that way. I would otherwise suggest to not live enable a strategy for the purpose of backtesting when using LiveUntilCancelled orders. In this case the strategy analyzer would be recommended so you don't need to change logic account for the excluded historical data.



        Comment


          #5
          So explain to me why "ExitOnSessionClose" works for all sessions except the last one before the transition to Realtime?

          I don't think you fully understand what the issue is and I don't think you've even bothered to follow the steps I took the trouble to write out.

          Comment


            #6
            I have made a video which shows what the problem is: https://screencast-o-matic.com/watch/c0ntYwVyETF

            Comment


              #7
              Hello kevinenergy,

              ExitOnSessionClose relies on the next session to open, you won't see exit on session close for the last session in the chart because its not closed. I will forward this to development for further review, if anything comes of this I will put a note about a tracking id in a later post.

              Comment


                #8
                Hi Jesse.
                Appreciate you reporting this to development for review.
                I made another video to validate the expected behavior of ExitOnSession close.

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post
                  if anything comes of this I will put a note about a tracking id in a later post.
                  Hi Jesse, any update?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  77 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  27 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  32 views
                  0 likes
                  Last Post TheRealMorford  
                  Started by Mindset, 02-28-2026, 06:16 AM
                  0 responses
                  62 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Working...
                  X