Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy works in market replay but submits an order instantly with live market

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

    #16
    Hello lucyb,

    You could unlock the script in the Builder by clicking the 'Unlock Code' button to edit the code by hand. This would allow you to add the code to skip historical processing.

    If you want to keep the original Builder script, you could even save a copy and unlock the copied version. In the 'Welcome' screen of the Builder, hover over your strategy's name > Save As to save a new copy.

    Comment


      #17
      ok. I would like to keep it in the builder first to identify the issue.

      I will update the thread later. I'm sure this is what's happening though as I just observed it again by disabling and re-enabling the strategy. :

      When I enable the strategy the last historical order is showing sill as having 1 position regardless of the current price. The real time price then has to re-enter the zone between the SL and TP of the last historical order. When the price leaves this zone during the live market, then the position goes flat again.

      By the way do you know an indicator that prints the current bars() on my chart.

      As you know drawing objects on my chart doesn't work, but I can't find an indicator that prints the same numbers as I see in my script output. I see numbers like 1500, the indicators I've downloaded just show numbers like 150.
      Last edited by lucyb; 06-26-2024, 10:29 AM.

      Comment


        #18
        Hello lucyb,

        Please update the thread with your output once you have it.

        Please also post create a new, separate post/start a new thread if you have a separate inquiry unrelated to what we are currently investigating. This will allow our representatives to better track and focus on a single issue per case.

        Comment


          #19
          Hello again. I made a basic strategy with lots of prints so hopefully you can help identify the problem. I attach both the script output and the strategy file.

          All the logic is validating as true. Here is the basics

          SET1
          ====
          Check my entry conditions. If true place Limit order.
          (I've used a myBar variable to prevent resubmitting the same order over and over once conditions are met)

          SET 2
          ====
          If Limit order is filled(Market position = Long). Set Stop Loss and Take Profit variables here.

          SET 3
          =====
          Submit ExitLong orders. Loop until flat. (Looping to prevent ExitLongs expiring)

          SET 4
          =====
          Once Flat. Go back to SET 1.

          ---------------

          As discussed the problem is. When I enable the strategy it shows as being in a 1L position.

          This only happens on historical data. Once flat the strategy runs fine. When the strategy has loaded. I have to wait until the price re-enters the zone between the 2 exit orders and cross over them again.

          Looking at the log it seems that sometimes.....once my Limit order gets filled, the price has already moved literally in the same line. Thus meaning my ExitLong orders are invalid.

          I guess this is a limitation of how the historical data is loaded? as in reality this isn't happening. Is there anything I did wrong in my script?
          Attached Files
          Last edited by lucyb; 06-27-2024, 05:04 AM.

          Comment


            #20
            Hello lucyb,

            Unfortunately, when I click on this attachment it says invalid file specified so I can't take a look at the output. However:

            "When I enable the strategy it shows as being in a 1L position.

            This only happens on historical data. Once flat the strategy runs fine.​"

            This is due to the the start behavior of the strategy and is normal. You stated you are using "Wait until flat, synchronize account". From the Help Guide:

            "If the Strategy Position is not flat, the strategy will place all trades in a "virtual" sense until the Strategy Position reaches or crosses a flat state. Once a flat state is achieved the Strategy Position will be assumed to be in sync with the Account Position and all future orders will be placed live."

            https://ninjatrader.com/support/helpguides/nt8/index.html?syncing_account_positions.htm#WaitUntil Flat

            Strategy position and Account position are two separate things.

            And from the support article:

            "As a strategy is enabled, the strategy will first process all of the historical data loaded on the chart or that has been set to load from the Data Series section of the strategy parameters. As this historical data is processed, NinjaTrader will simulate placing virtual historical orders, following the logic of the strategy. This is the equivalent of a backtest as would be performed in the Strategy Analyzer.

            Once the historical processing has ended and real-time processing begins, any position left open from a historical entry order that was not closed by a historical exit order will be left open as a historical virtual position. The strategy will use this historical virtual position for the start behavior selection."


            Your strategy is first processing all historical data loaded on the chart and placing historical orders according to your strategy logic. At the end of the historical data processing, the 1L position you are seeing was not closing historically so it waits and places trade in "virtual" sense until the strategy position reaches a flat state.

            I highly recommend reading this support article on start behavior to fully understand how this works.

            Comment


              #21
              I will try again with the attachments here.

              The problem is, the strategy could never go back to a flat state in theory because of the problem I described. I could be waiting hours and hours.

              The strategy should not allow the price to go outside the SL and TP. Once it does, if the price doesn't come back there. The strategy will never ever go flat.
              Attached Files
              Last edited by lucyb; 06-27-2024, 05:54 AM.

              Comment


                #22
                Hello lucyb,

                Which problem in particular?

                I have to wait until the price re-enters the zone between the 2 exit orders and cross over them again.
                When I enable the strategy the last historical order is showing sill as having 1 position regardless of the current price. The real time price then has to re-enter the zone between the SL and TP of the last historical order. When the price leaves this zone during the live market, then the position goes flat again.
                This is expected behavior with the start behavior you have selected, the strategy will wait until the real-time conditions allow for the strategy to reach a flat state according to your strategy's logic.

                If you don't want your strategy to calculate historically, you could unlock the script and place the following at the top of OnBarUpdate:

                Code:
                if (State == State.Historical)
                return;


                Or if you wanted the strategy to start submitting orders immediately, you could use Immediately Submit. This behavior should only be used when you are sure your Account Position is the way you want it to be in relation to the Strategy Position prior to strategy start.

                https://ninjatrader.com/support/helpGuides/nt8/index.html?syncing_account_positions.htm#Immediate lySubmit


                The strategy should not allow the price to go outside the SL and TP. Once it does, if the price doesn't come back there. The strategy will never ever go flat.
                Are you saying your strategy is not triggering your SL and TP when expected? If so, you'll need to adjust your prints because currently they are inside the the condition sets, so we are only going to see the result of the prints when the condition is true. The prints should be placed in their own, separate set. This way we can see why the conditions are evaluating as false when you are expecting them to be true.

                Comment


                  #23
                  Originally posted by NinjaTrader_Gaby View Post
                  Hello lucyb,

                  Which problem in particular?

                  Wouldn't the expected behaviour be that the exit orders are successful if the SL or TP is reached in the historical data?

                  What's happening is these orders are being ignored and thus the position is left open, despite the fact the price has gone way outside the SL and TP.


                  Originally posted by NinjaTrader_Gaby View Post

                  Are you saying your strategy is not triggering your SL and TP when expected?
                  It does in real time, but historically it does not. Thus the position is never closed when it should be. My SL and TP is set the moment the trigger conditions are set for the order. Under normal circumstances if the price goes outside of the SL and TP the position is closed. But the loading of historical data is not doing this.

                  It seems the only way to solve this is to unlock the file and manually adjust the code??

                  The conditional evaluating in the strategy is all correct. But the historical loading of the strategy seems to change the current price in a way that SL and TP orders do not get submitted correctly.

                  Here is the problem:

                  "Limit Order has been filled as position is Long, Position Price = 5549.5 & Current Market Price = 5547.75"

                  My SL snd TP are relative to the original order so because the price has moved at the same time the order was filled, one of the orders is considered invalid and the position never closes. This happens over and over again.

                  The Limit order and the SL and TP should be submitted at the same time. But when the historical data is loaded the current price is already 2pts away from the position price immediately as it's filled.

                  Last edited by lucyb; 06-27-2024, 06:40 AM.

                  Comment


                    #24
                    Hello,

                    Thanks for your response.

                    To clarify what you're saying, the SL and TP are getting not ignored completely but just priced so they don't fill in real time since they were priced based on a historical fill, correct?

                    If this is the case, one simple solution we could suggest would be to just add a separate exit condition using something like:

                    Code:
                    if(CurrentBar == Count  - 3)
                    This would just exit 1 bar before the last historical bar.

                    Comment


                      #25
                      I'm saying in the loading of the historical data. The same time the Limit order fills, the current market price at the exact same moment is often 8 ticks different according to the output, thus; Either the SL or TP are submitting an invalid price because it's relative to the fill price of the limit order. So if my SL is 6 ticks, the order price is invalid since you cannot submit a short stop order above the current market price.

                      In reality this never happens, but it seems when loading the previous historical data something is creating this price difference at the time the limit order is filled.

                      Comment


                        #26
                        Hello lucyb,

                        Thanks for clarifying.

                        This could happen in historical because historical data only includes the OHLC of the bar, there is no intrabar data.
                        • This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known
                        • Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.
                        https://ninjatrader.com/support/help...ical_fill_.htm

                        The offset you used resulted in a price where the order was submitted to the wrong side of the market, which led to it being ignored.

                        Try enabling High Order Fill Resolution (Value 1, Type tick) in the strategy's properties - are you seeing any difference in behavior?

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        20 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        120 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        63 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        41 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        45 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X