Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entry and exit in the same bar when backtesting

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

    Entry and exit in the same bar when backtesting

    Hello,

    The strategy I am working on makes two entries in 1 hour time frame, and I setstoplosses individually for each entry. Once in a while, I see one entry is stopped out and the other entry is not stopped out when both should be stopped out. As a result, the backtest isn't as accurate as I'd like. What's the best way to deal with this?


    #2
    Hello op1031,

    Thank you for your post.

    This behavior could be caused by historical overfills occurring when using the Managed Approach in a custom NinjaScript strategy.

    Something you could do is use High Order Fill Resolution along with an added 1-tick series to run backtests with intrabar granularity.

    See this forum post for more information about adding intrabar granularity - https://ninjatrader.com/support/foru...ive#post773377

    Or, you could modify your strategy's logic to use the Unmanaged approach to place orders and take control of those orders with OCO IDs. See the attached example script demonstrating the use of the Unmanaged Approach.

    See the help guide documentation below for more information.
    Discrepancies between realtime vs backtest - https://ninjatrader.com/support/help...ime_vs_bac.htm
    High Order Fill Resolution - https://ninjatrader.com/support/help...fill_.htm#Unde rstandingOrderFillResolution
    Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

    Let us know if we may assist further.
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      Thank you for the response. I am looking to add intrabar granularity. SampleIntrabarBacktest has the following comment:

      /* Add a secondary bar series.
      Very Important: This secondary bar series needs to be smaller than the primary bar series.

      Note: The primary bar series is whatever you choose for the strategy at startup. In this example I will
      reference the primary as a 5min bars series. */
      If my strategy involves 4 HOUR and 1 HOUR time frame, could you confirm I should set up the DataSeries as followed:
      Primary - 4 HOUR
      Secondary - 1 HOUR
      Third - 1 tick

      Then I would submit my by orders in the 1 tick data series. Is this the right approach?

      Comment


        #4
        Hello op1031,

        Thank you for your note.

        Yes, that would be the correct way to set up the added data series. You would add the data series to your script as mentioned and submit orders to the 1-tick data series.

        Please let us know if we may further assist.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon,

          Could you let me know how I can import tick data for backtesting? when I go to Historical Data -> Load, I only see that I can import bar and not tick data.

          For now, I tried using the 1-minute data as my granular intrabar; however, it seems to have the same problem. I still see the profit / stop-loss are filled incorrectly. To avoid the problem of overfill, do I need to detect stoploss and submit sell order manually instead of using setstoploss?

          Comment


            #6
            Hello op1031,

            Thank you for your note.

            Please see this help guide documentation regarding importing data. Note that the data being imported would need to be formatted as Tick data.

            Importing Data - https://ninjatrader.com/support/help...?importing.htm

            To avoid historical overfill High Order Fill Resolution should be used along with an added 1-tick series to run backtests with intrabar granularity. Or, you could modify your strategy's logic to use the Unmanaged approach to place orders and take control of those orders with OCO IDs.

            See the help guide documentation in post number 2 for more information.

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hi Brandon,

              So the correct way to set up the data series is

              Primary - 4 HOUR
              Secondary - 1 HOUR
              Third - 1 tick

              I am interested in knowing what exactly can go wrong if I set up the data series as

              Primary - 1 HOUR
              Secondary - 4 HOUR
              Third - 1 tick

              The reason I want to set it up like that is that the decision to make trades is in 1 HOUR, so I want to see 1 HOUR on the chart when I examine the trades. Could you explain what will go wrong if I use 1 HOUR as the primary data series?



              Comment


                #8
                Hello op1031,

                Thank you for your note.

                You could set up the data series in your script so that it uses a primary series of 1-Hour and adds a secondary 4-Hour series and 1-Tick series.

                The note 'Very Important: This secondary bar series needs to be smaller than the primary bar series.' refers to the secondary series that you will be submitting orders to. If you are submitting orders to a 1-Tick added series, the primary series would need to be greater than 1-Tick.

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Brandon,

                  So if I submit orders at 1 tick series, and only use 1H and 4H to decide whether to trade or not. Does this mean I can set up the data series as the following?

                  Primary - 1 tick
                  Secondary - 1 HOUR
                  Third - 4 HOUR

                  Comment


                    #10
                    Hello op1031,

                    Thank you for your note.

                    Yes, you could have your strategy run on and submit orders to a 1-Tick primary series and use the 1-Hour and 4-Hour added secondary series for your entry order conditions. Note that having the 1-Tick series as your primary series means that the strategy will run on a 1-Tick series. A 1-Tick series would also be what is displayed on the chart.

                    Let us know if we may further assist.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you for the info.

                      Comment


                        #12
                        Hi NinjaTrader team,

                        I am resurrecting this thread because my question is related to setstoploss with High Order fill resolution.
                        I implemented a strategy with a primary data series, and I use setstoploss with High Order fill resolution. Everything is working fine as expected, and I am planning to add a higher time frame to check for trend confirmation. However, if I add a new data series, then I am unable to use High Order fill resolution which also means when using setstoploss I can potentially run into overfill issues as described earlier in this thread.

                        Does this mean that if I add multiple data series, then I am not able to rely on setstoploss anymore?

                        Thank you,

                        Comment


                          #13
                          Hello op1031,

                          Thanks for your note.

                          If you are using a MultiTimeFrame strategy or Tick Replay, a single tick data series would need to be added to your script for intrabar granularity.

                          To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

                          Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

                          High Order Fill Resolution allows for intra-bar order fills with historical processing, but is not compatible with Tick Replay.

                          Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

                          SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

                          TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

                          Developing for Tick Replay -
                          https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

                          Additional information may be found in this NinjaTrader Forum post —
                          https://ninjatrader.com/support/foru...mance?t=102504

                          Please let us know if we may be of further assistance.
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Brandon,

                            Thank you for the info. I will take a look at those.

                            You didn't specifically say it, but I think the answer is "yes, we can't rely on setstoploss if we have multiple data series". I come to this conclusion because setstoploss has to be submitted in the primary series and not in the series with lower granularity. Could you confirm?

                            Comment


                              #15
                              Hello op1031,

                              Thanks for your note.

                              You would be able to use SetStopLoss/SetProfitTarget when submitting an entry order to an added secondary series. For example, if you submit an entry order with a signal name of "myEntry" to the added secondary series and called SetStopLoss/SetProfitTarget using a 'from entry signal' of "myEntry", then the Stop Loss and Profit Target would submit along with the entry order.

                              See the attached example script demonstrating this.

                              Also, see the help guide documentation below for more information.
                              BarsInProgress - https://ninjatrader.com/support/help...inprogress.htm
                              EnterLong - https://ninjatrader.com/support/help.../enterlong.htm
                              SetStopLoss - https://ninjatrader.com/support/help...etstoploss.htm
                              SetProfitTarget - https://ninjatrader.com/support/help...ofittarget.htm

                              Let us know if we may assist further.
                              Attached Files
                              Last edited by NinjaTrader_BrandonH; 06-10-2021, 12:36 PM.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by max1ci6, Today, 02:02 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post max1ci6
                              by max1ci6
                               
                              Started by junkone, Yesterday, 08:06 PM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_EricB  
                              Started by fscabrera03, Yesterday, 07:27 PM
                              1 response
                              5 views
                              0 likes
                              Last Post tradecombine  
                              Started by Rad4633, Yesterday, 05:31 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Rad4633
                              by Rad4633
                               
                              Started by litamm89, Yesterday, 03:51 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post litamm89  
                              Working...
                              X