Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest OnEachTick strategy

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

    Backtest OnEachTick strategy

    Hi NT Team,

    I have a strategy that operates NQ in 5 min bars with limit orders. This is a strategy that requires OnEachTick updates because it uses a volume filter that is updated on every tick.

    I am trying to see if I can backtest this strategy with "Order fill resolution" = High (1 second). When I set everything, i received the following message:
    Click image for larger version

Name:	image.png
Views:	308
Size:	5.6 KB
ID:	1281289

    I am not sure why this is the case because i am using a single-series (the NQ 5 min bar). Any idea why this is happening?

    Thanks!​

    #2
    Are you using AddDataSeries in your strategy?

    Comment


      #3
      Hello linkcou,

      You will need to enable TickReplay to use Calculate.OnEachTick, and add a 1 tick series to submit orders to for intra-bar fills.

      This forum thread goes into detail.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thank you both, really helpful!

        I enabled the option "TickReplay" and add the piece of code in the screenshot below as per instructions in one of the videos:
        Click image for larger version

Name:	image.png
Views:	267
Size:	5.4 KB
ID:	1281483

        However, when I go to the Strategy Analyzer and select the option Fill Resolution "High", i received the error below and it forces me to do the backtest with the "Standard" option:
        "Order Fill Resolution is not available when Tick Replay is enabled"
        Click image for larger version

Name:	image.png
Views:	259
Size:	80.4 KB
ID:	1281484

        When doing the backtest with the "Standard" option the trades taken don't replicate exactly the strategy.

        Additionally, I realized in the backtest that one volume filter that I have "OnEachTick" is not considered when entered the trades. Do I need to declare in the code the filter in any other place to perform the backtest?

        Thanks!​

        Comment


          #5
          To give a bit more of context, the volume filter i have calculated it in "OnMarketData". See below:
          Click image for larger version

Name:	image.png
Views:	265
Size:	19.9 KB
ID:	1281487

          Comment


            #6
            Hello linkcou,

            Currently, at this time, there is a known issue (tracked with ID# NTEIGHT-10782) where the High Fill Order Resolution and TickReplay cannot be used together. These work great when used on their own, however, when used together a complication arises that prevents the script from processing. Our developers are working hard to have this corrected in an upcoming build of NinjaTrader 8.
            In the meantime, if it is necessary to have both, it is still possible to add intra-bar granularity to a script in the code itself with AddDataSeries() for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically and/or have IsFirstTickOfBar work correctly in historical data.​


            Placing orders to the barsInProgressIndex of the 1 tick series is the same as high order fill resolution and works with TickReplay.

            There is a specific way of coding in OnMarketData() for TickReplay.
            See the code sample in the help guide linked below.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thank you, Chelsea! I think I am getting closer to solve this issue

              I have implemented the barsInProgressIndex = 1 and now it enters/exit in the right levels. However, I am still having troubles with the volume filter.

              The filter is implemented as a condition to enter long and short in the "OnBarUpdate"
              Filter = ProjectedVolume < SMA(Volume, 10)[0]

              Where:
              - ProjectedVolume is a calculation using the time remaining in the bar and the current volume.

              I implemented in the projected volume the "marketDataUpdate.Volume" as you suggested. However, i dont know how to create a live SMA of 10 period of the current volume.

              Any idea how to do this?

              I tried SMA(marketDataUpdate.Volume, 10) in the OnBarUpdate but it doesnt work.

              Thanks!

              Comment


                #8
                Hi Chelsea,

                After a while I found the way to backtest the projected volume and now it's working well

                However, I realized in the backtest there are a few missing trades that are right next to the exit bar of the the last exit trade. Example below left screenshot is the backtest and right screenshot the actual trades generated by the strategy in real time:
                Click image for larger version

Name:	image.png
Views:	263
Size:	3.5 KB
ID:	1281546Click image for larger version

Name:	image.png
Views:	254
Size:	6.0 KB
ID:	1281547​​


                In the real-time strategy, I have a filter with Barssinceexitexecution to just have one Long or short entry/exit per bar. Taking into consideration what you told me previously I added 1 in the BarsInProgress index in this function. See below the filter code:

                "&& (BarsSinceExitExecution(1, "ExitStopShort", 0) != 0 && BarsSinceExitExecution(1, "ExitLimitShort", 0) != 0)"

                Do you know why or how i can make the backtest to generate as well the trades in the next bar of the last exit?

                Thank you in advance!

                Comment


                  #9
                  Hello linkcou,

                  If there is a condition that is not evaluating as true when expected, print the time of the bar and ALL values used in the full condition set along with labels for each value and comparison operator.
                  Enable TraceOrders in State.Configure.


                  Save the output to a text file and attach this to your next post.
                  Let me know the specific bar time the order is expected on.

                  I'll be happy to analyze the output to see why the order is not being submitted when expected.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello Chelsea, thanks for your quick answer!

                    I have generated a text file of 3 Mb and i can't upload it here. Do you know how can i attache it to you?

                    The file contains data for NQ future on December 6th at 12:30 when this condition should be true. The 3 conditions i am adding are the below one from the code
                    Click image for larger version

Name:	image.png
Views:	229
Size:	6.2 KB
ID:	1281666

                    I checked the output file and indeed the last exit order is generated at 12:20:04 as per below:
                    Click image for larger version

Name:	image.png
Views:	222
Size:	12.9 KB
ID:	1281667
                    So for the bar time 122500 the "BarssinceExitExecution" should be False as it is in the screenshot above. However, for the timestamp 123000 it should be already true again and i have checked and it's always false for the whole bar. See below the output:
                    Click image for larger version

Name:	image.png
Views:	265
Size:	2.5 KB
ID:	1281668​​

                    Comment


                      #11
                      Hello linkcou,

                      The BarsSinceExitExecution is an integer, which is a number, and not a true or false value.

                      The print should appear:
                      Print(string.Format("{0} | Close[0]: {1} <= LLL1: {2} && MyProjectedVOL[0]: {3} < SMA(Volume, 10)[0]: {4} && BarsSinceExitExecution(1, "ExitStopLong", 0): {5} != 0 && BarsSinceExitExecution(1, "ExitLimitLong", 0): {6} != 0", Time[0], Close[0], LLL1, MyProjectedVOL[0], SMA(Volume, 10)[0], BarsSinceExitExecution(1, "ExitStopLong", 0), BarsSinceExitExecution(1, "ExitLimitLong", 0)));
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Chelsea, thanks!

                        I added your code and run it. I still can't send it to you because of the size of the text file. Let me know if there is a way to get it to you.

                        I can send you some screenshots in the meantime. The strategy is OnEachTick that's why is generating so many lines.

                        Click image for larger version

Name:	image.png
Views:	261
Size:	17.5 KB
ID:	1281689
                        Sending you the text file for the timestamp 12.25 (text v1) and for the timestamp 12.30 (text v2) when it should generate the order.

                        Let me know if you see any issue.

                        Comment


                          #13
                          Hello linkcou,

                          You can use a cloud transfer service.


                          If there is a lot of output, you may also consider writing this to a text file.

                          Below is a link to an example.
                          Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Chelsea,

                            Thanks for your answer.

                            Below the link to download the output from the backtest:
                            https://wetransfer.com/downloads/bf15d234df26d0006cfcf8f9d15e240720231211161750/542eb115550cdcc3e9522a0c9856c5d220231211161750/01ee07

                            Let me know if you can't access.
                            Thanks!

                            Comment


                              #15
                              Hello linkcou,

                              Are you certain you have TraceOrders enabled in State.Configure as advised in post # 9?

                              The file doesn't have any prints for 12:30. The prints start at 12:55.

                              You may need to write to a file to include the full output, or use a time filter to only print between a specific range of minutes.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              71 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              143 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              76 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              47 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              51 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X