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

IsFirstTickOfBar vs OnBarClose for backtest & live

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

    #16
    Has below issue been fixed with new version?

    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.

    Comment


      #17
      Hello connorgrant5,

      This particular issue NTEIGHT-10782 is a tough one and is not resolved and is still a limitation.

      But using an added 1 tick series for intra-bar granularity works fine. Its only the convenience of Order Fill Resolution to not have to code intra-bar granularity yourself that is not available.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Does anyone have a video on how to add intra-bar granularity? I’m new to this and it can’t understand coding that good =(

        Comment


          #19
          Hello rafael_delima86,

          While there are videos on getting started with programming there are not many videos on specific lines of code.


          Intra-bar granularity involves adding a 1 tick series with AddDataSeries(), ensuring logic is processed on the primary series with BarsInProgress, and placing orders to that 1 tick series with the BarsInProgress index of that 1 tick series.



          There is an example script provided for a simplified demonstration.


          Have you started by adding the 1 tick series with AddDataSeries()?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Hi @NinjaTrader_ChelseaB

            Just now I have test the sample from https://ninjatrader.com/support/help...ipt_strate.htm but does not work, could you tell me if there is a new version or what is happening? the message is: Error on calling 'OnBarUpdate' method on bar 49: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

            Regards.​

            Comment


              #21
              Hello J2512,

              I've given this a test and was not able to reproduce.

              Below is a link to a video of the test.


              Be sure to re-download the script (Ctrl + F5 to refresh the cache) to ensure you have the latest version.

              To confirm, you are connected to a data feed that provides historical tick data for the instrument you are viewing?
              (Can you open a 60 second chart with the same date range and see data on the chart for the full date range?)

              If you follow the exact steps shown in the video you are experiencing different behavior?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Hi Chelsea

                Thanks a lot for your response, finally I did the test sucessfully.

                Regards.

                Comment


                  #23
                  I've hit this forum post with the same issue: the sample intrabar backtest runs but doesn't actually trigger any trades. Specifically, it appears that EnterLong(1, ...) just doesn't ever send an order to the second data series (IE: tick data). I see some mention of Continuum for tick level historical data, but it isn't clear if that is a 3rd party subscription, how to sign up for that, etc. Can someone who has gotten this working advise on the specific steps required to add a data feed/subscription that support historical data for tick level backtesting?

                  Comment


                    #24
                    Hello garth,

                    Below is a link to the help guide on the historical data available for each connection type.


                    If you are connected to NinjaTrader Brokerage, with the NinjaTrader connection type, the historical tick data is available.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello fxRichard,

                      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. Instead these are filled based on logical rules from processing historical data.

                      Please review the help guide document on the differences on real-time vs backtest (historical).
                      https://ninjatrader.com/support/help...ime_vs_bac.htm

                      When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar 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.

                      Below is a link to the help guide on Calculate.
                      https://ninjatrader.com/support/help.../calculate.htm


                      Adding intra-bar granularity can help with this.

                      Intra-bar granularity adds a second data series such as a 1 tick series using AddDataSeries() so that the strategy or indicator has the individual ticks in the historical data in between the High and Low of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with. The orders placed will need to use the BarsInProgress (BarsArray) index of that finer resolution series.
                      -Adding a 1 tick series with AddDataSeries() does not enable IsFirstTickOfBar to work correctly in historical data. (IsFirstTickOfBar will always be true)

                      Below is a link to an official reference sample that demonstrates how to add intra-bar granularity.
                      'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/help...ipt_strate.htm

                      And a link to a video demonstrating implementing 1-tick intra-bar granularity.
                      https://drive.google.com/file/d/12F6...usp=share_link

                      Also, here is a link to the differences on real-time vs backtest (historical).
                      https://ninjatrader.com/support/help...ime_vs_bac.htm

                      As well as a link to the help guide on the AddDataSeries() method.
                      https://ninjatrader.com/support/help...dataseries.htm

                      A link to the help guide on BarsInProgress.
                      https://ninjatrader.com/support/help...inprogress.htm

                      And a link to the help guide on Multi-Time Frame & Instruments. Please see the section 'How Bar Data is Referenced', and 'Accessing the Price Data in a Multi-Bars NinjaScript'.
                      https://ninjatrader.com/support/help...nstruments.htm

                      The Fill limit orders on touch option will also change whether a limit order requires a tick through to fill or if it fills on touch, which can change the price at which a limit order fills and changes the end performance.
                      https://ninjatrader.com/support/help...a_strategy.htm
                      https://ninjatrader.com/support/help...mitontouch.htm

                      Also, below I'm including a link to the help guide on Historical Order Backfill Logic.
                      https://ninjatrader.com/support/help...fill_logic.htm
                      https://ninjatrader.com/support/help...lFillAlgorithm


                      In NinjaTrader 8, there have been two new enhancements so that programmers may not have to manually add this secondary series and code the script for high accuracy fills (Order Fill Resolution) or for intra-bar actions (TickReplay) depending on the needs of the script.

                      The first is TickReplay.
                      TickReplay is used for the intra-bar actions. This adds a 1 tick series to the script to be used for triggering OnBarUpdate when Calculate is set to .OnEachTick or .OnPriceChange. This also allows OnMarketData to trigger historically for last market events only (the bid and ask will not trigger).
                      -TickReplay will allow for indicators to update for each tick or price change and for scripts to trigger actions intra-bar.
                      -TickReplay must be enabled for the Calculate setting in State.SetDefaults to take effect in the Strategy Analyzer.
                      -TickReplay allows for IsFirstTickOfBar to work correctly in historical data and will be true for the first tick of the bar only.
                      -TickReplay does not provide fill prices for accurate order fills.

                      Below are links to the help guide on TickReplay.
                      TickReplay - https://ninjatrader.com/support/help...ick_replay.htm
                      Developing for Tick Replay - https://ninjatrader.com/support/help...ick_replay.htm
                      OnMarketData - https://ninjatrader.com/support/help...marketdata.htm
                      IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm
                      Calculate - https://ninjatrader.com/support/help.../calculate.htm
                      OnBarUpdate - https://ninjatrader.com/support/help...nbarupdate.htm


                      The second is Order Fill Resolution.
                      This has two settings, Standard and High. When set to standard, the behavior of order fills will be accurate to the close of the bar without any intra-bar information. When set to High, this allow you to select a secondary data series to be used to provide intra-bar pricing information solely for the purpose of order fill accuracy. The highest you could set this to is 1 tick.
                      -Order fill resolution does not provide intra-bar granularity to OnBarUpdate for actions to be triggered intra-bar.
                      -
                      Order fill resolution does not enable IsFirstTickOfBar to work correctly in historical data.
                      -Order fill resolution does not work with multi-series scripts using AddDataSeries(). If using AddDataSeries(), manually implement 1 tick granularity.

                      Below is a link to the help guide on Order Fill Resolution. (Above this you can also read about the Historical Fill Algorithm)
                      https://ninjatrader.com/support/help...FillResolution


                      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.


                      If you would like to further understand what specifically is different in the data that is causing differences, see the post below.
                      https://ninjatrader.com/support/foru...nce#post100192


                      Note, bar types that are IsRemoveLastBarSupported cannot be used with TickReplay and generally cause inaccurate results when backtesting in historical data.

                      From the help guide:
                      "Note: The system bar types "Line Break" and "Renko" cannot be used with Tick Replay and as a result, the Tick Replay option will be disabled when configured with those bar types. There may be other 3rd party bar types which may also disable Tick Replay by design. If you are a developer, please see the property IsRemoveLastBarSupported for more information"
                      https://ninjatrader.com/support/help...ick_replay.htm

                      Discussion threads.
                      https://ninjatrader.com/support/foru...52#post1100852
                      https://ninjatrader.com/support/foru...636#post839636


                      It is not advised or necessary to change the Calculate mode during run-time.
                      Hello Chelsea,

                      I would like to ask you a small detail, I am trying to obtain in the backtest the most similar results possible to reality, using an indicator with TickReplay (footprint type).

                      In this post in the strategy you don't use "if (BarsInProgress == 0)". However in this other post yes.

                      You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


                      In the logic of my strategy I am waiting for it to calculate at the close of the bar, but with TickReplay update. In this post you are waiting for an intrabar average cross. So, in my case I should use "if (BarsInProgress == 0)", right?​

                      Comment


                        #26
                        Hello rocker84,

                        In the linked sample the purpose of the BarsInProgress == 0 is to show two concepts. The first concept would be that in a backtest you select the primary series that you want your strategy to operate, the BarsInProgress == 0 makes sure that the cross conditions happen for the primary series. The second concept is how the order methods specify BarsInProgress 1 so the fill happens on the more granular secondary series.

                        In your script you would only need BarsInProgress == 0 if you have other series being added. If you have a single series script you don't need the BarsInProgress == 0 condition. If you do have multiple series you would want to make a BarsInProgress condition for the index of the series that you want the logic executed.
                        JesseNinjaTrader Customer Service

                        Comment


                          #27
                          Originally posted by NinjaTrader_Jesse View Post
                          Hello rocker84,

                          In the linked sample the purpose of the BarsInProgress == 0 is to show two concepts. The first concept would be that in a backtest you select the primary series that you want your strategy to operate, the BarsInProgress == 0 makes sure that the cross conditions happen for the primary series. The second concept is how the order methods specify BarsInProgress 1 so the fill happens on the more granular secondary series.

                          In your script you would only need BarsInProgress == 0 if you have other series being added. If you have a single series script you don't need the BarsInProgress == 0 condition. If you do have multiple series you would want to make a BarsInProgress condition for the index of the series that you want the logic executed.
                          Perfect, thank you very much. In the case of the video, where you expect an intrabar mean crossover to enter with series "1" (the smallest, using EnterShort(1, ). What would happen if the averages inside the bar cross over, cross below, several times in the same candle? Multiple orders or does the method ignore the next ones?

                          Comment


                            #28
                            Hello rocker84,

                            If you were using the logic in the intrabar backtest script that would not account for intrabar conditions so you would end up with a lot of fills in a single bar. If your overall plan is to use OnEachTick in realtime or test with TickReplay and OnEachTick you would likely want to add additional conditions to make sure some amount of bars since the entry happen before allowing a reversal. You can use BarsSinceEntryExecution to mitigate additional entries.

                            JesseNinjaTrader Customer Service

                            Comment


                              #29
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello fxRichard,

                              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. Instead these are filled based on logical rules from processing historical data.

                              Please review the help guide document on the differences on real-time vs backtest (historical).
                              https://ninjatrader.com/support/help...ime_vs_bac.htm

                              When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar 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.

                              Below is a link to the help guide on Calculate.



                              Adding intra-bar granularity can help with this.

                              Intra-bar granularity adds a second data series such as a 1 tick series using AddDataSeries() so that the strategy or indicator has the individual ticks in the historical data in between the High and Low of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with. The orders placed will need to use the BarsInProgress (BarsArray) index of that finer resolution series.
                              -Adding a 1 tick series with AddDataSeries() does not enable IsFirstTickOfBar to work correctly in historical data. (IsFirstTickOfBar will always be true)

                              Below is a link to an official reference sample that demonstrates how to add intra-bar granularity.
                              'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/help...ipt_strate.htm

                              And a link to a video demonstrating implementing 1-tick intra-bar granularity.
                              https://drive.google.com/file/d/12F6...usp=share_link

                              Also, here is a link to the differences on real-time vs backtest (historical).


                              As well as a link to the help guide on the AddDataSeries() method.


                              A link to the help guide on BarsInProgress.


                              And a link to the help guide on Multi-Time Frame & Instruments. Please see the section 'How Bar Data is Referenced', and 'Accessing the Price Data in a Multi-Bars NinjaScript'.


                              The Fill limit orders on touch option will also change whether a limit order requires a tick through to fill or if it fills on touch, which can change the price at which a limit order fills and changes the end performance.



                              Also, below I'm including a link to the help guide on Historical Order Backfill Logic.




                              In NinjaTrader 8, there have been two new enhancements so that programmers may not have to manually add this secondary series and code the script for high accuracy fills (Order Fill Resolution) or for intra-bar actions (TickReplay) depending on the needs of the script.

                              The first is TickReplay.
                              TickReplay is used for the intra-bar actions. This adds a 1 tick series to the script to be used for triggering OnBarUpdate when Calculate is set to .OnEachTick or .OnPriceChange. This also allows OnMarketData to trigger historically for last market events only (the bid and ask will not trigger).
                              -TickReplay will allow for indicators to update for each tick or price change and for scripts to trigger actions intra-bar.
                              -TickReplay must be enabled for the Calculate setting in State.SetDefaults to take effect in the Strategy Analyzer.
                              -TickReplay allows for IsFirstTickOfBar to work correctly in historical data and will be true for the first tick of the bar only.
                              -TickReplay does not provide fill prices for accurate order fills.

                              Below are links to the help guide on TickReplay.
                              TickReplay - https://ninjatrader.com/support/help...ick_replay.htm
                              Developing for Tick Replay - https://ninjatrader.com/support/help...ick_replay.htm
                              OnMarketData - https://ninjatrader.com/support/help...marketdata.htm
                              IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm
                              Calculate - https://ninjatrader.com/support/help.../calculate.htm
                              OnBarUpdate - https://ninjatrader.com/support/help...nbarupdate.htm


                              The second is Order Fill Resolution.
                              This has two settings, Standard and High. When set to standard, the behavior of order fills will be accurate to the close of the bar without any intra-bar information. When set to High, this allow you to select a secondary data series to be used to provide intra-bar pricing information solely for the purpose of order fill accuracy. The highest you could set this to is 1 tick.
                              -Order fill resolution does not provide intra-bar granularity to OnBarUpdate for actions to be triggered intra-bar.
                              -
                              Order fill resolution does not enable IsFirstTickOfBar to work correctly in historical data.
                              -Order fill resolution does not work with multi-series scripts using AddDataSeries(). If using AddDataSeries(), manually implement 1 tick granularity.

                              Below is a link to the help guide on Order Fill Resolution. (Above this you can also read about the Historical Fill Algorithm)



                              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.


                              If you would like to further understand what specifically is different in the data that is causing differences, see the post below.
                              https://ninjatrader.com/support/foru...nce#post100192


                              Note, bar types that are IsRemoveLastBarSupported cannot be used with TickReplay and generally cause inaccurate results when backtesting in historical data.

                              From the help guide:
                              "Note: The system bar types "Line Break" and "Renko" cannot be used with Tick Replay and as a result, the Tick Replay option will be disabled when configured with those bar types. There may be other 3rd party bar types which may also disable Tick Replay by design. If you are a developer, please see the property IsRemoveLastBarSupported for more information"


                              Discussion threads.
                              https://ninjatrader.com/support/foru...52#post1100852
                              https://ninjatrader.com/support/foru...636#post839636


                              It is not advised or necessary to change the Calculate mode during run-time.
                              Hello Mr. Chelsea B.,

                              THIS VIDEO about how to implement the 1-tick intra-bar granularity into a strategy has made my day! As I have never learned coding I have to teach myself the hard way and I´ve been searching for this possibility since a long time. I knew it should be simple, but I just couldn't find anything understandable on the topic. Thank you sooooo much!!! By the way, I've read your name - which sounds more like the name of a rapper than a programmer :-) - here 1000 times and your posts have often helped me. You're doing a great job!

                              Comment


                                #30
                                Hello Leafcutter,

                                *mic drop*

                                Thank you so much for the kind words.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by alifarahani, Today, 09:40 AM
                                6 responses
                                32 views
                                0 likes
                                Last Post alifarahani  
                                Started by Waxavi, Today, 02:10 AM
                                1 response
                                17 views
                                0 likes
                                Last Post NinjaTrader_LuisH  
                                Started by Kaledus, Today, 01:29 PM
                                5 responses
                                14 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by Waxavi, Today, 02:00 AM
                                1 response
                                12 views
                                0 likes
                                Last Post NinjaTrader_LuisH  
                                Started by gentlebenthebear, Today, 01:30 AM
                                3 responses
                                17 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Working...
                                X