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!
See more
See less

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

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

    I made this strategy which works fine when using the market replay backtest. But when trying it live as soon as it's enabled it immediately enters a single long position for some reason.

    In the first set I have multiple conditions which need to be met and they are not met, yet it still enters long regardless which I don't understand. The output immediately prints:

    Strategy 'New/327706409': An order placed at '06/19/2024 04:30:27' has been ignored since the order was submitted before the strategy property BarsRequiredToTrade had been met.
    ​19/06/2024 04:30:27 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 19/06/2024 04:30:27: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5560.00 SignalName='' FromEntrySignal='' Reason='BarsRequiredToTrade has not been met'
    Last edited by lucyb; 06-24-2024, 10:42 AM.

    #2
    Anyone any ideas with this please? I created a similar strategy and again it works in replay mode but as soon as I connect to live. It immediately goes into a Long position and considers every single bar a long trigger.

    Comment


      #3
      Hello lucyb,

      Thank you for your post.

      I'm not seeing from the output you posted where the strategy is entering a long position - this output it stating that no orders have been placed due to the BarsRequiredToTrade not being met yet.

      In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

      The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

      Below is a link to a post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


      Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

      Let me know if you need any assistance creating a print or enabling TraceOrders.

      Save the output from the output window to a text file and provide this with your reply.

      I'll be happy to assist with analyzing the output.​
      Gaby V.NinjaTrader Customer Service

      Comment


        #4
        Here is a last strategy I wrote. Immediately when enabled it goes into a 1L position and straight onto the 2nd set. This is despite the fact the conditions were not met in the 1st set to trigger any actions. Here is the output, this just repeats over and over because it's the loop in the 2nd set.

        This strategy works perfect in market replay. I dunno why as soon as I connect to live market it ignores all the conditions in the 1st.

        25/06/2024 15:47:31 Strategy 'New/327706409': Entered internal SubmitOrderManaged() method at 25/06/2024 15:47:31: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5526.75 SignalName='' FromEntrySignal='myLong'
        25/06/2024 15:47:31 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 25/06/2024 15:47:31: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5526.75 SignalName='Sell' FromEntrySignal='myLong' Reason='Invalid order price, please see log tab'
        25/06/2024 15:47:31 Strategy 'New/327706409': Entered internal SubmitOrderManaged() method at 25/06/2024 15:47:31: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=5530.75 StopPrice=0 SignalName='' FromEntrySignal='myLong'
        25/06/2024 15:47:31 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 25/06/2024 15:47:31: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=5530.75 StopPrice=0 SignalName='Sell' FromEntrySignal='myLong' Reason='Order already has this stop price/limit price/quantity'
        Current Position: Long
        TriggerBar 1492​

        Regardless of where I enable the strategy with live data it immediately shows 1L position. I'm using the SIM account. I stays in this 1L position for a long time. I assumed that it would only show a 1L position when enabled if my conditions were met in the preceding bars.

        The same does not happen on Market Replay, and the strategy works as expected.
        Attached Files
        Last edited by lucyb; 06-25-2024, 08:16 AM.

        Comment


          #5
          Hello lucy,

          Thank you for the output, however it is missing the info from the prints. This is only telling us that an order was submitted, but in order to know why the condition is evaluating as true we need the information from the prints.

          The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

          I recommend reviewing the page below for instructions on how to create print statements, thee is also a link to a video recorded using the Strategy Builder to add prints.



          If you need assistance creating a print, please let me know. ​
          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            Do I put all the prints in a new set? I'm unsure where to put them. Sorry, I'm using strategy builder as I'm not a coder. I probably need some help with that.
            Last edited by lucyb; 06-25-2024, 08:27 AM.

            Comment


              #7
              The prints should go in a separate set.

              I recommend watching this video that demonstrates debugging in the Strategy Builder:

              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                Hey, so when I put in the prints I can see all the conditions are validating in order to enter a long trade. ie, if I require the current price to be higher than the high of the previous be to enter long. These things validate true.

                The problem seems to be that depending on where I enable the strategy, often it will show already 1L and will not get out of it.

                My strategy has exitLong commands which trigger as soon as the position is Long. These commands both have the same signal as the original order. Let's say my SL and TP is 6 ticks. I can see in the output and on the chart where the long position was entered prior to me enabling the strategy. However, I can also see that since then the price has easily moved 6 ticks in either direction. But yet the position remains Long as if the ExitLong commands where not followed.

                My ExitLong commands are resubmitted every time the price changes to prevent them expiring. I can see these commands looping in the log but it doesn't get out of it. These outputs are the last outputs I passed above.

                During Market replay, or sometimes when I enter live market and 1L is not immediately present. The strategy works. As soon as my trigger occurs, I enter a limit order. As soon as the position goes Long I se the variables for the SL/TP and the strategy will move onto the next set and trigger the ExitLong SL/TP orders which loop until the position is flat and then it starts checking again for the trigger to enter long.

                Sometimes in Market replay I have to move the play head around to make sure 1L is also not there. But obviously I cannot do this in live. And I cannot get it to go flat.


                Comment


                  #9
                  Hello lucyb,

                  Can you please post the output so we can review? The output you posted in your last post does not include the information from the prints.

                  Additionally, what start behavior are you using?
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    What do you mean what start behaviour? OK....I know what you mean.

                    I have it set to 'wait until flat, synconize acc'. If it's set to immediately submit. I get the following errors immediately:
                    Attached Files
                    Last edited by lucyb; 06-26-2024, 07:56 AM.

                    Comment


                      #11
                      ok, here is the log.... and a picture of the chart. I just enabled the strategy and it's showing 1L.

                      Looking at the log, the long position was entered on bar 1316 when the close price of the trigger bar was 5533.25. As I write this the price is currently 5521 and the current bar is 1357.

                      My SL is set to 10 ticks(3 pts) and TP is set to 6 ticks.(1.5pts). I also attach the set that includes the exitLong commands.

                      I just cannot run the strategy live because it's constantly stuck in this 1L position. As it says in the output there is an error in the log. I attach this as well.

                      Update: The position finally went flat @ bar 1384 with the price here. This is 68 bars after the position entered into Long. I have no idea.

                      TriggerBar 1384
                      Tigger Close = 5531.25 // Trigger Median = 5531 // Trigger height = 1.5

                      26/06/2024 16:08:17 Strategy 'New/327706409': Entered internal SubmitOrderManaged() method at 26/06/2024 16:08:17: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5528.25 SignalName='' FromEntrySignal='myLong'
                      26/06/2024 16:08:17 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 26/06/2024 16:08:17: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5528.25 SignalName='Sell' FromEntrySignal='myLong' Reason='There already is a matching order with same prices and quantity'
                      26/06/2024 16:08:17 Strategy 'New/327706409': Entered internal SubmitOrderManaged() method at 26/06/2024 16:08:17: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=5532.25 StopPrice=0 SignalName='' FromEntrySignal='myLong'
                      26/06/2024 16:08:17 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 26/06/2024 16:08:17: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=5532.25 StopPrice=0 SignalName='Sell' FromEntrySignal='myLong' Reason='There already is a matching order with same prices and quantity'
                      Current Position: Long
                      TriggerBar 1384
                      Tigger Close = 5531.25 // Trigger Median = 5531 // Trigger height = 1.5


                      26/06/2024 16:08:19 Strategy '327706409/New: Cancelled pending exit order, since associated position is closed, orderId='fe9caaedc647467580411fc756c60e59' account='Sim101' name='Sell' orderState=Accepted instrument='ES 09-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=5528.25 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=4176 time='2024-06-26 16:06:24' gtd='2099-12-01' statementDate='2024-06-26'
                      Current Position: Flat
                      26/06/2024 16:12:41 Strategy 'New/327706409': Entered internal SubmitOrderManaged() method at 26/06/2024 16:12:41: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=5530.50 StopPrice=0 SignalName='myLong' FromEntrySignal=''
                      Triggerstate 1
                      Current Position: Flat​
                      TriggerBar 1388
                      Tigger Close = 5530.25 // Trigger Median = 5529.375 // Trigger height = 2.25​

                      Once the position goes flat, the strategy works fine and places orders fine with the SL/TP working fine. It's just getting it to go flat when enabling it.
                      Attached Files
                      Last edited by lucyb; 06-26-2024, 08:29 AM.

                      Comment


                        #12
                        I'm having trouble uploading the output from the full log because it's so big but here it is.
                        Attached Files

                        Comment


                          #13
                          Just a theory but it seems like the position went flat around bar 1384 was because the price came back up within the range of the SP/TP and then the log changed it's output and then finally realised the position should be flat.

                          It goes from saying ''There already is a matching order with same prices and quantity' ---> 'Cancelled pending exit order, since associated position is closed"

                          Attached Files
                          Last edited by lucyb; 06-26-2024, 08:48 AM.

                          Comment


                            #14
                            Hello lucyb,

                            It's not clear to me from the output if the conditions are actually evaluating as true or false. For example, your first condition check has several price checks and a condition that checks if your MarketPositon is flat. I am not seeing any of this info in your output.

                            A more informative print for this condition set would be:

                            Code:
                            Print(Times[0][0] + " High[0]: " + High[0] + " > High[1]: " + High[1] + " & MyBar: " + MyBar + " != CurrentBars[0]: " + CurrentBars[0] + " & Low[1]: " + Low[1] + " > Low[2]: " + Low[2] + " & MarketPosition: " + Position.MarketPosition + " = MarketPosition.Flat");
                            You'll need to create similar prints for all condition sets in your strategy.

                            If you are using "Wait until flat, synchronize account" this may be why you are immediately seeing this behavior. However, we would need the prints from your conditions to confirm.

                            When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

                            If you are using the 'Wait until flat' Start Behavior and the strategy calculates a historical position, this means that once the strategy has finished processing the historical data and has transitioned to real-time, it will wait until there is a real-time order submission that will cause the strategy to become flat. This also includes changing from a long position to a short position or vice versa as the position would pass through flat.

                            From the Help Guide:

                            "When your strategy starts it will check for any active orders previously generated by the strategy on your account and cancel those first. Should the strategy be unable to cancel and receive confirmation on the cancellation of these orders within 40 seconds the strategy will not start and an alert will be issued. After the strategy is successful in cancelling any orders that required action it will check your current Account Position and compare it to a flat state.

                            If the Account Position is not flat, NinjaTrader will submit a market order(s) to reconcile the Account Position to a flat state. The strategy will then wait for the Strategy Position to reach a flat state before submitting live orders."



                            Additional information could be found in this forum thread - https://ninjatrader.com/support/foru...ion#post811541

                            If you do not want the strategy to calculate a position from processing historical data, add if (State == State.Historical) return; to the top of your OnBarUpdate() strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders. This would require unlocking the strategy and coding my hand.


                            26/06/2024 16:08:17 Strategy 'New/327706409': Ignored SubmitOrderManaged() method at 26/06/2024 16:08:17: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=5532.25 StopPrice=0 SignalName='Sell' FromEntrySignal='myLong' Reason='There already is a matching order with same prices and quantity'

                            This order is ignored because there is already a matching order placed with the same exact prices and quantity.

                            21/06/2024 11:40:47 Strategy 'New/327706409: Cancelled pending exit order, since associated position is closed, orderId='NT-00001-10' account='Sim101' name='Sell' orderState=Working instrument='ES 09-24' orderAction=Sell orderType='Limit' limitPrice=5539.25 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-06-21 10:13:35' gtd='2099-12-01' statementDate='2024-06-26'

                            This trace tells us that our sell limit order was cancelled because its corresponding position was already closed by another order.
                            Gaby V.NinjaTrader Customer Service

                            Comment


                              #15
                              I will re-write the strategy from scratch and come back to the thread when I have all the prints you need.

                              I'm using strategy builder so I don't think I can add the skip historical part right?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by xfactscout, 07-22-2024, 09:01 PM
                              1 response
                              23 views
                              0 likes
                              Last Post xfactscout  
                              Started by Chrisnasdaq, 08-26-2023, 08:37 PM
                              4 responses
                              1,401 views
                              0 likes
                              Last Post pierodicarlo  
                              Started by GillRymhes, 06-23-2022, 09:16 AM
                              13 responses
                              743 views
                              5 likes
                              Last Post pierodicarlo  
                              Started by aligator, 06-22-2022, 01:10 PM
                              13 responses
                              1,821 views
                              0 likes
                              Last Post pierodicarlo  
                              Started by sumobull, Today, 06:48 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post sumobull  
                              Working...
                              X