Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Intra-bar Backtesting

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

    Intra-bar Backtesting

    Hello NT and NT fans,

    I am running into a situation that many of you have probably solved. Intra-bar backtesting.

    What I know is that claims have been made that it is possible using multiple-time frames, and even samples put out there referencing it.

    http://www.ninjatrader.com/support/f...ead.php?t=6652

    But I can't get this to work,

    If you look at the attached image, you can clearly see that the fills are taking place on the close.

    Does anyone have a working example, or reason why this no longer works?

    Cheers,

    SodyTexas
    Attached Files

    #2
    Hello SodyTexas,

    Thanks for your note.

    What bars in progress are you submitting the order on?

    The orders are always going to show on a primary bar, how are you determining the orders are only submitted at the end of the primary bar? Are you comparing the time of the order as it shows in the Orders tab to the close time of the bar?

    What is the secondary series interval?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Both

      I have tried it both ways with the following provided code:

      Code:
      #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.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Reference sample demonstrating how to achieve intrabar backtesting.
          /// </summary>
          [Description("Reference sample demonstrating how to achieve intrabar backtesting.")]
          public class SampleIntrabarBacktest : 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()
              {
      			/* 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. */
      			Add(PeriodType.Minute, 1);
      			
      			// Add two EMA indicators to be plotted on the primary bar series
      			Add(EMA(Fast));
      			Add(EMA(Slow));
      			
      			/* Adjust the color of the EMA plots.
      			For more information on this please see this tip: http://www.ninjatrader-support.com/vb/showthread.php?t=3228 */
      			EMA(Fast).Plots[0].Pen.Color = Color.Blue;
      			EMA(Slow).Plots[0].Pen.Color = Color.Green;
      			
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
      			/* When working with multiple bar series objects it is important to understand the sequential order in which the
      			OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and
      			so on.
      			
      			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 
      			
      			When the OnBarUpdate() is called from the primary bar series (5min series in this example), do the following */
      			if (BarsInProgress == 0)
      			{
      				// When the fast EMA crosses above the slow EMA, enter long on the secondary (1min) bar series
      				if (CrossAbove(EMA(Fast), EMA(Slow), 1))
      				{
      					/* The entry condition is triggered on the primary bar series, but the order is sent and filled on the
      					secondary bar series. The way the bar series is determined is by the first parameter: 0 = primary bars,
      					1 = secondary bars, 2 = tertiary bars, etc. */
      					EnterLong(1, 1, "Long: 1min");
      				}
      				
      				// When the fast EMA crosses below the slow EMA, enter short on the secondary (1min) bar series
      				else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
      				{
      					/* The entry condition is triggered on the primary bar series, but the order is sent and filled on the
      					secondary bar series. The way the bar series is determined is by the first parameter: 0 = primary bars,
      					1 = secondary bars, 2 = tertiary bars, etc. */
      					EnterShort(1, 1, "Short: 1min");
      				}
      			}
      			
      			// When the OnBarUpdate() is called from the secondary bar series, do nothing.
      			else
      			{
      				return;
      			}
              }
      
              #region Properties
      		/// <summary>
      		/// </summary>
      		[Description("Period for fast MA")]
      		[Category("Parameters")]
      		public int Fast
      		{
      			get { return fast; }
      			set { fast = Math.Max(1, value); }
      		}
      
      		/// <summary>
      		/// </summary>
      		[Description("Period for slow MA")]
      		[Category("Parameters")]
      		public int Slow
      		{
      			get { return slow; }
      			set { slow = Math.Max(1, value); }
      		}
              #endregion
          }
      }
      And then my own attempt:

      Code:
      #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.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Enter the description of your strategy here
          /// </summary>
          [Description("Enter the description of your strategy here")]
          public class OldNumber7 : Strategy
          {
              #region Variables
      		
      		//Add Period Type Varaiables
      		private string ContractDate = "##-##";
      		private int boxSize = 7;
      		private int reversal = 3;
      		private int BIP1 = 1;
      		private int count45 = 0;
      		private int count1 = 0;
      		
              
              #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()
              {
                  CalculateOnBarClose = true;
      			Add(Instrument.MasterInstrument.Name.ToString()+" "+ContractDate,PeriodType.Minute,BIP1);
              }
      
              
              protected override void OnBarUpdate()
              {
      			if(BarsInProgress == 0)
      			{
      				count45 = count45 + 1;
      			}
      			
      
      			if(BarsInProgress == 1)
      			{
      				
      				count1 = count1 + 1;
      				DrawTextFixed("c34","\nC45: " + count45,TextPosition.TopLeft);	
      				DrawTextFixed("c1","\n\n\nC1: " + count1,TextPosition.TopLeft);
      				
      				DrawTextFixed("EMA","EMA 10: " + EMA(BarsArray[0],10)[0],TextPosition.TopRight);
      				if(EMA(BarsArray[0],10)[0] > EMA(BarsArray[0],20)[0] && Close[0] > MaxMin(BarsArray[0],10).Max[0])
      				{
      					EnterLong(1,"Buy");
      				}
      				
      				if(EMA(BarsArray[0],10)[0] < EMA(BarsArray[0],20)[0] && Close[0] < MaxMin(BarsArray[0],10).Min[0])
      				{
      					EnterShort(1,"Sell");
      				}
      				
      			}
      			
              }
      
              #region Properties
              
              #endregion
          }
      }
      Both are filling on close of BarsInProgress 0, i.e. the close of the 30 minute bar and not the 1 minute granularity.

      Comment


        #4
        Sorry

        Nevermind, the second set of code, executing on BarsInProgress == 1 , is intra-bar fills.. You can close the tread out.

        Thanks for your help

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by chbruno, Today, 04:10 PM
        0 responses
        3 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        6 views
        0 likes
        Last Post FAQtrader  
        Started by rocketman7, Today, 09:41 AM
        5 responses
        19 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by frslvr, 04-11-2024, 07:26 AM
        9 responses
        127 views
        1 like
        Last Post caryc123  
        Working...
        X