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

Intrabar backtest Indicator values

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

    Intrabar backtest Indicator values

    Hello,
    since in backtest I can not use the CalculateOnBarClose=false, I'm looking for a way to calculate in backtest the value of an indicator during the bar forming.
    My goal is to determine in backtest the intersection of two averages during the formation of the bar (10 or 15 min timeframe), without waiting for the Close of the bar.

    In my test strategy I'm using a secondary time frame for the call of OnBarUpdate() at each tick or minute.
    I'm using also a custom DataSeries filled up to the [1] element with the main dataseries and filled the [0] element with the last Close[0] of the 1 tick timeframe. Then I pass the dataseries to the indicators for the calculation, but I can not get the desired result.
    I tried to found a solution for a long time in the forum but nothing.
    Is there a way to get this result?

    Thank You

    #2
    Hello carlot,

    Thanks for your post and welcome to the forums!

    As you've found the strategy analyzer backtesting only works with completed bars effectively making the strategy run as if CalculateOnBarClose = true. To work around the issue, for strategy analyzer backtesting, you can add a faster data series. Since you want to look at a moving average crossover within the 10 or 15 minute time frame, you would need to put the indicator on the faster dataseries in order to see the "intrabar" movement.

    Please see the reference strategy for "Backtesting NinjaScript Strategies with an intrabar granularity": http://ninjatrader.com/support/forum...ead.php?t=6652

    In addition you would want to review all of the information here: http://ninjatrader.com/support/helpG...nstruments.htm

    Alternatively, if you have access to market replay data, you can run your strategy without changing it for backtesting, as Market replay provides live like data (all of the ticks and fill simulation engine), however market replay is slower because of that.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul,
      Thanks for the reply.

      The link you provided I know them well.
      I also do not want to use the MarketReplay because it is slow and I need to use the optimizer.
      Soon I'll send you the code of my test strategy so you can tell me if I'm wrong something.

      Comment


        #4
        This is the Ninja default strategy sample about SMA crossover:

        Code:
        // 
        // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
        // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
        //
        
        #region Using declarations
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Data;
        using NinjaTrader.Indicator;
        using NinjaTrader.Strategy;
        #endregion
        
        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
            /// <summary>
            /// Simple moving average cross over strategy.
            /// </summary>
            [Description("Simple moving average cross over strategy.")]
            public class SampleMACrossOver : Strategy
            {
                #region Variables
                private int        fast    = 10;
                private int        slow    = 25;
                #endregion
        
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
                    SMA(Fast).Plots[0].Pen.Color = Color.Orange;
                    SMA(Slow).Plots[0].Pen.Color = Color.Green;
        
                    Add(SMA(Fast));
                    Add(SMA(Slow));
        
                    CalculateOnBarClose    = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick).
                /// </summary>
                protected override void OnBarUpdate()
                {
                    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                        EnterLong();
                    else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
                        EnterShort();
                }
        
                #region Properties
                /// <summary>
                /// </summary>
                [Description("Period for fast MA")]
                [GridCategory("Parameters")]
                public int Fast
                {
                    get { return fast; }
                    set { fast = Math.Max(1, value); }
                }
        
                /// <summary>
                /// </summary>
                [Description("Period for slow MA")]
                [GridCategory("Parameters")]
                public int Slow
                {
                    get { return slow; }
                    set { slow = Math.Max(1, value); }
                }
                #endregion
            }
        }
        And this is my modified version for testing intrabar backtest:

        Code:
        ............
        
            public class SampleMACrossOver1Tick : Strategy
            {
                #region Variables
                private int        fast    = 10;
                private int        slow    = 25;
                #endregion
        
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
                    SMA(Fast).Plots[0].Pen.Color = Color.Orange;
                    SMA(Slow).Plots[0].Pen.Color = Color.Green;
        
                    Add(SMA(Fast));
                    Add(SMA(Slow));
        
                    CalculateOnBarClose    = true;
        
                    // 1 tick Secondary timeframe
                    Add(PeriodType.Tick, 1);
        
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick).
                /// </summary>
                protected override void OnBarUpdate()
                {
                    if (BarsInProgress == 1)  // called every tick
                    {
                        // the crossovers are found at the Close of the main bar and not intrabar !
                        if (CrossAbove(SMA(BarsArray[0], Fast), SMA(BarsArray[0], Slow), 1) )
                            EnterLong(1 /*BarsArray[1]*/, 1 /*quantity*/, "L"); // ok, the oder will be filled at the next tick, but it's too late
                        else if (CrossBelow(SMA(BarsArray[0], Fast), SMA(BarsArray[0], Slow), 1))
                            EnterShort(1 /*BarsArray[1]*/, 1 /*quantity*/, "S");
                    }
                }
        
        ................
        The little bit better result of my second version with 1 tick resolution is that the order is filled al the end of the crossover bar instead of the open of the next bar, but it is not enough. The crossovers are not found intrabar because the SMAs are not calculated intrabar.

        How to calculate intrabar the SMAs of BarsArray[0] ?

        I tried to use a custom DataSeries instead of BarsArray[0], without success .

        Comment


          #5
          Hello carlot,

          Thanks for your post.

          The issue that you are running into is trying to refer to the 15 (or 10) minute timeframe for the SMA values and these values will only change on the next 15 minute bar. So until you have a new 15 minute bar all the references will continue to point to the previous SMA value at 15 minutes ago. The 1 tick series is only going to give you another OBU in which to process the entry/exit order.

          I'm not sure well how this would work but one suggestion would be to use a lower time frame, such as 1 minute bars, and apply a 15 minute SMA proxy to it. You can test this out visually on a chart first. Have two data series (a 15 minute and a 1 minute) and add an SMA to the 15 minute data series (baseline), next try various lengths of SMA on the 1 minute series until you find as close a fit as possible. Once you have identified the 1 minute equivalents you can use them in the strategy to calculate the approximate position of the 15 minute SMA on the 1 minute bars. Alternatively you could also consider 3 minute or 5 minute bars.

          If you are stuck on a 15 minute strategy and want intrabar options then Market Replay is your best option.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thanks Paul for the suggestion.

            I just verify that using a 1 min time frame, the 10 min SMA(30) is similar the 1 min SMA(30*10).
            I'll try my strategy in this way.

            But I think the lack of the intrabar feature in backtest is very limiting.
            NT8 has the same limit?

            Comment


              #7
              Hello carlot,

              Thanks for your post.

              NinjaTrader 8 offers Tick Replay capability.
              Paul H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by trilliantrader, Yesterday, 03:01 PM
              3 responses
              29 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by cmtjoancolmenero, Yesterday, 03:58 PM
              4 responses
              26 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Brevo, Today, 01:45 AM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by rjbtrade1, 11-30-2023, 04:38 PM
              2 responses
              74 views
              0 likes
              Last Post DavidHP
              by DavidHP
               
              Started by suroot, 04-10-2017, 02:18 AM
              5 responses
              3,022 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X