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

Issue with same entry order executed twice

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

    Issue with same entry order executed twice

    Dear NT8 team,
    I hope you are doing well.

    I have been testing a strategy that i created and that I use in a Topstep trading account with Rythmic connection that enters with limit orders up to 5 times in 1 min bar when price reach certain levels.

    I keep having the same issue over and over and I don't know how to solve it. For instance, see below today's trade report where you can see how that the entryorder "MMDOL3" is executed twice at 03.50.20 and 03.50.22:
    Click image for larger version

Name:	image.png
Views:	52
Size:	24.0 KB
ID:	1294588

    However, in the code I have an explicit filter using BarsSinceEntryExecution to only create "MMDOL3" once per bar (see below code of OnBarUpdate where i create the entry orders).

    Something I realized is that this issue ALWAYS happens when the "MMDOL1" position is exited before "MMDOL2" and the rest of entry orders are executed. In the case above, you can see how "MMDOL1E" is executed at 3.50.12 and then "MMDOL2" in 3.50.16.

    Do you know how can i solve this issue? Below sharing pieces of code that i believe might have the issue.. However, happy to share other pieces of code if you think the issue is coming from anywhere else.​

    I have the following OnStateChange configuration (where MAXPositionSize = 5):
    Code:
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = MAXPositionSize;
    EntryHandling = EntryHandling.AllEntries;​
    OnBarUpdate code for entry limit creation:
    Code:
    if (Close[0] < Open[0] - 2
                    && (ToTime(Time[0]) >= 090500 && ToTime(Time[0]) <= 160500)
                    && (CurrentBarSec <= LimitSecL)
                    )
                {                                    
                    
                    if (entryOrder1 == null
                        && BarsSinceEntryExecution(0, "MMDOL1", 0) != 0
                        )
                    {
                        EnterLongLimit(0, true, Qty, (LLL1 - EntryFactorL), "MMDOL1");    
                    }
                    
                    if (entryOrder2 == null
                        && BarsSinceEntryExecution(0, "MMDOL2", 0) != 0
                        )
                    {
                        EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL1), "MMDOL2");    
                    }
                    
                    if (entryOrder3 == null
                        && BarsSinceEntryExecution(0, "MMDOL3", 0) != 0
                        )
                    {
                        EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL2), "MMDOL3");    
                    }
                    
                    if (entryOrder4 == null
                        && BarsSinceEntryExecution(0, "MMDOL4", 0) != 0
                        )
                    {
                        EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL3), "MMDOL4");    
                    }
                    
                    if (entryOrder5 == null
                        && BarsSinceEntryExecution(0, "MMDOL5", 0) != 0
                        )
                    {
                        EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL4), "MMDOL5");    
                    }
                }​

    #2
    Hello linkcou,

    Thank you for your post.

    I see you are checking if BarsSinceEntryExecution is not equal to 0, please note, from the Help Guide:

    "A value of -1 will be returned if a previous entry does not exist."



    So this part of the condition could evaluate as true even when the previous entry does not exist yet, since the value could return as -1. If you change this to > 0, this will only evaluate as true if 1 bar has elapsed since the last entry.



    To understand why the script is behaving as it is, such as placing orders or not placing orders (or drawing objects or other actions) when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    This will print to the output window. Backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Output from prints will appear in the NinjaScript Output window.
    NT8: New -> NinjaScript Output

    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 I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
    NT8 —


    It is also helpful to set TraceOrders to true in State.Configure as well as print the order object in OnOrderUpdate().

    TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.
    Printing the order object in OnOrderUpdate() will allow you to track the progression of the order from submitted, to working, to filled, cancelled, or rejected.

    These tools will let you know what happens to the order.
    TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

    I'm also including a link to a forum post with further suggestions on debugging a script.


    Save the output from the output window to a text file. Let me know if you need assistance creating a print or enabling TraceOrders.

    I am happy to assist with analyzing the output from prints and TraceOrders.​

    Please let me know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Thanks Gaby! let me try the first solution you suggested and i will see if this fix the issue.

      Comment


        #4
        Sorry for the double email. I just realized that I need the -1 because if not i will not be able to initiate any new strategy with this condition. If I put >0, then the strategy will never create a single trade, correct?

        Is there anything else that you think can cause this issue at a first glance? I will implement the other things you mentioned but this is not an error that happens everyday...

        Comment


          #5
          Hello linkcou,

          Take a look at the Help Guide page for BarsSinceEntryExecution, it has this code snippet below:

          Code:
          // Only enter if at least 10 bars has passed since our last entry
          
          if ((BarsSinceEntryExecution() > 10 || BarsSinceEntryExecution() == -1) && CrossAbove(SMA(10), SMA(20), 1))
          EnterLong();
          You could do something similar to check if at least 1 bar has passed since your last entry or the value returned is -1.

          Please let me know if you have any further questions. ​
          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            Thanks Gaby! is this not the same as adding the condition "=! 0"?

            Comment


              #7
              Hello linkcou,

              I agree that this would just be a more explicit way to specify you are checking for at least 1 bar since your last entry. If you still want to check for the -1, but are noticing unexpected behavior, then I suggest debugging using prints and enabling TraceOrders so we can see more clearly why the strategy is placing that order twice.

              Please let me know if I can assist further.
              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                Hello Gaby, I hope you are doing well.

                I had the TraceOrder = true in this script and I was able to find the trace files to identify this issue. See attached the trace order txt file regarding the issue I started this thread with (re: duplicate MMDOL3 entry order at 15.50.20 and 15.50.22)

                When I run the same script in the NT8 playback reply, I don't have this issue so I'm really clueless on why this is happening.. Attaching as well the TickReplay log that I get when I do the playback.

                Could you please let me know if you see something wrong? More than happy to provide more parts of the script code if necessary.

                Thanks!
                Attached Files

                Comment


                  #9
                  Hello linkcou,

                  I don't see the information from TraceOrders in your trace file. Additionally, these trace files will not show the output from any print statements you've created.

                  Please provide the output from the prints and TraceOrders - this information will print to the NinjaScript Output window. After running the text, right-click > Save As and you can attach this to your reply.

                  Is this also the same test with the same exact start time and same exact trades being made?

                  Was the script enabled at the same exact time in Playback as it was live?

                  When you are using the Playback, are you testing on historical data or Market replay Data?

                  ​I look forward to assisting further.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello Gaby, thanks for your answer.

                    What should I include in the prints? Should I run this in market replay or wait in live until i get another error?

                    Regarding your questions, see below my answers:
                    Is this also the same test with the same exact start time and same exact trades being made?
                    • Yes, I used the same script in the same period 06/03 between 15:50-15:52. In the Playback I only have 1 entry called "MMDOL3" while in live i had 2 entries both called "MMDOL3"
                    Was the script enabled at the same exact time in Playback as it was live?
                    • No, but it should not affect at all as I have a condition to cancel all entry orders in first tick of the bar and reassess the strategy in each bar
                    When you are using the Playback, are you testing on historical data or Market replay Data?
                    • I used the Market Replay data.

                    Comment


                      #11
                      Hello linkcou,

                      Thank you for your response.

                      In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

                      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 important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.​

                      Also remember to enable TraceOrders.

                      I'm also including a link to a forum post with further suggestions on debugging a script.
                      https://ninjatrader.com/support/foru...956#post671956

                      No, but it should not affect at all as I have a condition to cancel all entry orders in first tick of the bar and reassess the strategy in each bar
                      This isn't necessarily true. For example, if you connect to Playback and enable the strategy, but you have not enabled it at the same time the strategy was enabled live, it would not be an apples to apples test because the data on the chart before we hit the 'Enable' box would be processed as historical data instead of realtime.

                      You can run the test using Playback on the Sim101 account for testing. You should start the playback as the exact same time it was started in live for comparison.

                      Please let me know if I can assist further.
                      Gaby V.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by rbeckmann05, Yesterday, 06:48 PM
                      1 response
                      12 views
                      0 likes
                      Last Post bltdavid  
                      Started by llanqui, Today, 03:53 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post llanqui
                      by llanqui
                       
                      Started by burtoninlondon, Today, 12:38 AM
                      0 responses
                      10 views
                      0 likes
                      Last Post burtoninlondon  
                      Started by AaronKoRn, Yesterday, 09:49 PM
                      0 responses
                      15 views
                      0 likes
                      Last Post AaronKoRn  
                      Started by carnitron, Yesterday, 08:42 PM
                      0 responses
                      11 views
                      0 likes
                      Last Post carnitron  
                      Working...
                      X