Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Sample Intrabar Backtest

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

    Using Sample Intrabar Backtest

    I am trying to use the code from the Sample Intrabar Backtest sample code to see if I can get the backtested results look more like my real-time results. I am using minutebars, with limit entries, so my discrepancies are not due to funny bar construction (Renko's,e tc.) I minute bar is a minute bar right?
    I use a limit entry but the example given in the code sample is a market entry. How do I convert this:

    EnterLong(1, 1, "Long: 1min");

    To a limit entry? What I am currently using is:

    EnterLongLimit(DefaultQuantity, Close[0] - ATRTicks(14).ATRinTicks[0] * TickSize, "LAbv_Band");

    Where do I add the reference to the second data stream minute bar. Most of my standard bars for the original strategy are as low as 5 min and as large as 10 min.

    Also when I use the following statement:

    Add(PeriodType.Minute, 1);

    Shouldn't I see a second set of minute data on the chart? At the moment I do not and when I put the Sample Intrabar Backtest strategy on a chart, I do not see a second set of 1 min data on the chart?

    Thanks for your help

    DaveN

    #2
    Hello DaveN,

    That is correct, but please be aware of the discrepancies of Real-Time vs Backtesting. You may read about the discrepancies at the following link to our Help Guide.

    http://www.ninjatrader.com/support/h...ime_vs_bac.htm

    I am not familiar with the "ATRTicks", but it does looks like you are following the EnterLongLimit() overload method correctly.
    Code:
    EnterLongLimit(int quantity, double limitPrice, string signalName);
    http://www.ninjatrader.com/support/h...rlonglimit.htm

    You would want to place the "Add()" method inside the Initialize() method.

    http://www.ninjatrader.com/support/h....html?add3.htm

    When using the Add() method you would not see the secondary data series visually inside the chart, as it will only be referenced inside of the NinjaScript code itself. If you would like to see the secondary data series on a chart you would have to add it manually.

    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      ATRinTicks is my own indicator, used to create order entry offsets equal to the ATR but expressed in ticks. With respect to the sample intrabar code, it shows an entry being made on the 1 min bar, based on conditions with the primary 5 min bar. How do I reference that 1 min bar with a limit entry, is this correct?

      Code:
      EnterLongLimit(1, Close[0] - ATRTicks(14).ATRinTicks[0] * TickSize, "LAbv_Band");
      I don't know if the 1 in the first position is the reference for the second data of 1 min data, or if it is the order entry size. When I use this construct, all my order cancel as soon as they are placed. In the original strategy the limit entry would wait to be filled based on conditions in the previous bar, and it seems to work quite well, but I am getting a big difference between what I see in backtesting and on the chart as historical entries, when the stategy is implemented on the chart, versus what happens in real time. I get a lot more stop outs in real-time, than I see in either backtesting or as historical entries on the chart when I implement the strategy.
      Thanks
      DaveN

      Comment


        #4
        Hello DaveN,

        With that current overload method the "1" would refer to the order quantity. You would have to use the following overload method to reference the 1 Min bar.

        Code:
        EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName);
        So it may try something like the following:
        Code:
        EnterLongLimit(1, true, DefaultQuantity, Close[0] - ATRTicks(14).ATRinTicks[0] * TickSize, "LAbv_Band");
        You may view the following link to our Help Guide that goes over all the different EnterLongLimit() overload methods.

        http://www.ninjatrader.com/support/h...rlonglimit.htm
        JCNinjaTrader Customer Service

        Comment


          #5
          Sample Intrabar Data continued

          Okay, that fixed my immediate cancellation problem and it seems to be running and working properly. Now I'd like to reference the ATRinTicks indicator to the minute bar, so that potentially the average range in ticks is faster adjusting to current conditions. How do I make the following add and indicator, run on the minute data instead of the base five minute data?

          Code:
          Add(ATRTicks(14));
          Code:
          EnterLongLimit(1, true, DefaultQuantity, Close[0] - ATRTicks(14).ATRinTicks[0] * TickSize, "LAbv_Band");
          Thanks, you are really helping me and I appreciate it.
          DaveN

          Comment


            #6
            Hello DaveN,

            You may use the BarsInProgress to reference the Data Series bar from the 1Min time frame. If you view the sample code of the SampleIntrabarBacktest in the commented out code "//" or "/* */" displayed in green when BarsInProgress is 0 it is referencing the Primary Data Series (the data series you have applied the strategy to). When BarsInProgress is equal to 1 in the example it is going to reference the 1 Min data series. So if you would like to reference the ATRTicks inside the 1Min Data Series you may place it in the "BarsInProgress == 1" condition.

            http://www.ninjatrader.com/support/h...inprogress.htm
            JCNinjaTrader Customer Service

            Comment


              #7
              It's all working, unfortunately the smaller time frame made the stop and profit target too small to make the strategy profitable, but it did work and now I know how to accomplish this for future strategies. Thanks for your help.
              DaveN

              Comment


                #8
                daven (or anyone else)--

                I'm working on a weekly and daily swing point strategy. I've built an indicator that confirms swing points. For instance, a swing high would be defined as a 3 bar set up where the middle bar's high is greater than the other two bars and the low of the right most bar is less than the low of the middle bar. In this case, the middle bar would be a swing high and the right most bar would be the "signal" bar.

                Using the above example, the entry price would be 1 tick below the swing high bar's low. In real time with COBC = false, the strategy would see that the signal bar confirmed the swing high, and trigger a limit order entry via EnterLimitShort with a limit price of 1 tick below the swing bar's low.

                The backtesting problem that I am having is that using weekly bars, a backtest only looks at the chart as COBC = true, so the earliest an order would fire would be when the next bar following the signal bar is opened. In the case where the next bar trades through the limit price, the short order would fill--but in the case where price moves down from the signal bar's close and does not trade back through the limit price, the order would be left open waiting for a fill (which I've set via the "true" parameter in EnterShortLimit).

                So I'm looking for a way to get the backtest to fire the order within the signal bar at the limit price and be filled in the signal bar at the limit price (i.e., and not wait until the open of the next bar).

                I tried adding a secondary series, but that doesn't work because even if I specify the BarsInProgress parameter within EnterShortLimit(1, true, 100.34, "short") command, the secondary series is not called until the close of the signal bar, which means that the earliest it would be used would be at the open of the next bar. At least that is my understanding. So adding a secondary bar series doesn't seem to help.

                How were you able to accomplish a representative intrabar backtest?

                Thanks,

                Aventeren

                Comment


                  #9
                  Aventern,

                  You would need to look into using Intrabar Granularity for your strategy in backtesting -
                  http://www.ninjatrader.com/support/f...ead.php?t=6652
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Cal View Post
                    Aventern,

                    You would need to look into using Intrabar Granularity for your strategy in backtesting -
                    http://www.ninjatrader.com/support/f...ead.php?t=6652
                    Cal--

                    I have looked at this indicator.

                    Can you please confirm the following:

                    1. Assume weekly chart
                    2. 3 bar swing high, swing high is confirmed by the rightmost bar at the point in time when the right most bar's low is less than the low of the swing high bar's low (assuming that the leftmost bar's high is less than the swing high bar's high and the swing high bar's high is greater than the signal bar's high).
                    3. However the swing high would be confirmed at the close of the signal bar because backtesting assumes COBC = true (assuming the swing logic was within BIP == 0).
                    4. So from my understanding, the secondary series would be called to fire the order (by using EnterShortLimit(1, true, limitPrice, 1, "short")). However, the earliest that the secondary series would be called would be at the open of the next bar following the signal bar--rather than the secondary series being called and then processed from the opening time of the signal bar forward to the close of the signal bar. Here is the quote from the SampleIntrabarBacktest indicator:

                    Important: Primary bars will always execute before the secondary bar series. If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM bar on the 1min bar series is like this:
                    12:00PM 5min
                    12:00PM 1min
                    12:01PM 1min
                    12:02PM 1min
                    12:03PM 1min
                    12:04PM 1min
                    12:05PM 5min
                    12:05PM 1min
                    5. Given that the timestamp of the signal bar is indexed to the end of the signal bar and that the secondary series will then only be called from the end of signal bar's timestamp forward (per above), intrabar backtesting in NT7 is impossible.

                    I hope that I am missing something, but I don't think so. If this is the case, I need to shelve NT7 until NT8 non-beta is released. I can still use NT7 for forward testing and trading, but all backtesting will need to be on another platform. To say that I am frustrated and disappointed by this fact would be a gross understatement. I hope that you can help, but I am afraid you cannot.

                    Thanks,

                    Aventeren

                    Comment


                      #11
                      Originally posted by aventeren View Post
                      Cal--

                      I have looked at this indicator.

                      Can you please confirm the following:

                      1. Assume weekly chart
                      2. 3 bar swing high, swing high is confirmed by the rightmost bar at the point in time when the right most bar's low is less than the low of the swing high bar's low (assuming that the leftmost bar's high is less than the swing high bar's high and the swing high bar's high is greater than the signal bar's high).
                      3. However the swing high would be confirmed at the close of the signal bar because backtesting assumes COBC = true (assuming the swing logic was within BIP == 0).
                      4. So from my understanding, the secondary series would be called to fire the order (by using EnterShortLimit(1, true, limitPrice, 1, "short")). However, the earliest that the secondary series would be called would be at the open of the next bar following the signal bar--rather than the secondary series being called and then processed from the opening time of the signal bar forward to the close of the signal bar. Here is the quote from the SampleIntrabarBacktest indicator:



                      5. Given that the timestamp of the signal bar is indexed to the end of the signal bar and that the secondary series will then only be called from the end of signal bar's timestamp forward (per above), intrabar backtesting in NT7 is impossible.

                      I hope that I am missing something, but I don't think so. If this is the case, I need to shelve NT7 until NT8 non-beta is released. I can still use NT7 for forward testing and trading, but all backtesting will need to be on another platform. To say that I am frustrated and disappointed by this fact would be a gross understatement. I hope that you can help, but I am afraid you cannot.

                      Thanks,

                      Aventeren
                      No. The way the YOU want to do it is not possible, because you are postulating an impossibility. Intrabar backtesting is eminently possible, as long as you are not expecting the impossible.

                      From your description a swing can only be confirmed by the 3rd bar closing, otherwise there is no way to determine if it is really a swing. Take a Swing high as an example. Assume the first 2 bars are in place, then the second bar low is broken by the third bar. How do you know that you have a swing point? After all the third bar can reverse and go higher than the second bar, negating the potential swing high. What would be your complaint then? That NT incorrectly identified the swing? Or that you acted prematurely? This situation is regardless of the COBC setting, realtime or not.

                      If the swing point can only be defined after the bar closes, then expecting to be able to act before the bar closes is asking for the impossible. You cannot act on a swing until it is definitely established.

                      Now, if you were asking for the entry as soon as a third bar breaks the low after one bar is higher than the previous, that would be a different kettle of fish.
                      Last edited by koganam; 09-27-2014, 07:53 PM.

                      Comment


                        #12
                        Originally posted by koganam View Post
                        No. The way the YOU want to do it is not possible, because you are postulating an impossibility.
                        All due respect, It is not my intention to postulate an impossibility. Rather I am attempting to postulate a situation that I am seeking to backtest: whereby an order fires intrabar--just like it would when the live strat was set to COBC = false. Is this possible?

                        Comment


                          #13
                          Originally posted by aventeren View Post
                          All due respect, It is not my intention to postulate an impossibility. Rather I am attempting to postulate a situation that I am seeking to backtest: whereby an order fires intrabar--just like it would when the live strat was set to COBC = false. Is this possible?
                          My last paragraph says "yes" to that question. That also means the you have an unconfirmed swing which can be negated.

                          What that means is that you have to be more precise in your specification. If you want a swing, and it is defined the way that you have defined it, then you are postulating an impossibility for the reasons that I have already explained.

                          Simply put, you can code the intrabar entry, but it means that you are doing it before the swing is confirmed, and that developing swing point can be negated. You have not answered my question. If that happens what would be the complaint?

                          Comment


                            #14
                            Originally posted by koganam View Post
                            No. The way the YOU want to do it is not possible, because you are postulating an impossibility. Intrabar backtesting is eminently possible, as long as you are not expecting the impossible.

                            From your description a swing can only be confirmed by the 3rd bar closing, otherwise there is no way to determine if it is really a swing. Take a Swing high as an example. Assume the first 2 bars are in place, then the second bar low is broken by the third bar. How do you know that you have a swing point? After all the third bar can reverse and go higher than the second bar, negating the potential swing high. What would be your complaint then? That NT incorrectly identified the swing? Or that you acted prematurely? This situation is regardless of the COBC setting, realtime or not.

                            If the swing point can only be defined after the bar closes, then expecting to be able to act before the bar closes is asking for the impossible. You cannot act on a swing until it is definitely established.

                            Now, if you were asking for the entry as soon as a third bar breaks the low after one bar is higher than the previous, that would be a different kettle of fish.
                            koganam--

                            In the event that the 3rd bar were to first confirm the 3 bar swing high setup by trading 1 tick below the low of the swing high bar's low, at that moment in the time the swing high would be confirmed and I would want to be short.

                            Outside bar's occur somewhere in the neighborhood of 5-20%. I am not seeking advice and clarification on what to do with outside bars within my code--I am seeking clarification on how to backtest 80-95% of my setups...and once I understand how to get accurate historical intrabar backtesting fills on these "vanilla" setups I will turn my attention to the more rare outside bar instances.

                            I understand outside bars will not allow me to run a 100% accurate intrabar backtest with the questions and scenarios that I am seeking clarification on. I am merely seeking clarification on how to obtain an accurate intrabar backtest on a "vanilla" example so that I can then extend my understanding of the NT7 backtesting logic to more "advanced" cases.

                            I appreciate your help and feedback, and I am hopeful that the above will help us to focus in on an answer to my questions.

                            Thanks,

                            Aventeren

                            Comment


                              #15
                              To attempt an intrabar historical fill of a "vanilla" swing high short position, here is what I have done.

                              1. Within Initialize() added a 1 min secondary series

                              Code:
                              Add(PeriodType.Minute, 1);
                              2. Within OnBarUpdate() isolated to the primary series via if BIP == 0
                              3. Within BIP == 0, checking to see if High[2] < High [1] && High[1] > High[0] && Low[0] < Low[1] for a swing high and vice versa for a swing low.

                              Code:
                              if(BarsInProgress == 0)
                              {
                              	if(Position.MarketPosition != MarketPosition.Short && High[2] <= High[1] && High[1] >= High[0] && Low[2] <= Low[1] && Low[1] > Low[0])
                              	{
                              		e = Low[1] - TickSize;
                              		EnterShortLimit(1, true, 1, e, "short");
                              	}
                              				
                              	if(Position.MarketPosition != MarketPosition.Long && Low[2] >= Low[1] && Low[1] <= Low[0] && High[2] >= High[1] && High[1] < High[0])
                              	{
                              		e = High[1] + TickSize;
                              		EnterLongLimit(1, true, 1, e, "long");
                              	}
                              }
                              4. The above code produces a backtest whereby an intrabar fill is not obtained on the bar that confirmed the swing high or swing low on BIP == 0. Rather, the fill is obtained on the bar following the signal bar if price trades through the limit price.

                              5. I am trying to obtain an intrabar fill on the signal bar. If this is possible, how would I need to modify my code to obtain an intrabar fill on the signal bar?

                              Thanks,

                              Aventeren

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              242 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              384 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Started by oviejo, Today, 12:28 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post oviejo
                              by oviejo
                               
                              Working...
                              X