NexusFi: Find Your Edge


Home Menu

 





Help Intrabar backtest


Discussion in NinjaTrader

Updated
    1. trending_up 2,077 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

Help Intrabar backtest

  #1 (permalink)
 
SodyTexas's Avatar
 SodyTexas 
Austin TX
 
Experience: Advanced
Platform: Ninjatrader, Python, & R
Broker: RJO
Trading: Futures, Spreads
Posts: 421 since Sep 2013
Thanks Given: 117
Thanks Received: 1,085

Hey Guys,

I am struggling to make a system with intrabar backtest capabilities. Is this even possible in NT7? I was able to do this in 6.5 a few years back. But now that I no longer have 6.5 and would like to use just NT7; I need a solution.

I tried the following ( Strategy: [AUTOLINK]Backtesting[/AUTOLINK] NinjaScript Strategies with an intrabar granularity - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum) just to get it to work but all this code does is fills the trade at 6:01 opposed to 6:00, meaning it waited 1 minute to run the code.

Has anyone seen this same issue in NT7 or have sample code that actually works to backtest intrabar.

Thanks in advance, Cheers,

SodyTexas

"The great Traders have always been humbled by the market early on in their careers creating a deep respect for the market. Until one has this respect indelibly engraved in their makeup, the concept of money management and discipline will never be treated seriously."
Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Cheap historycal L1 data for stocks
Stocks and ETFs
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
 
  #2 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,661

In real time, there is CalculateOnBarClose = false meaning it will execute the tick that changes your signal to enter a position.

But for backtesting, there is no such thing.

Instead, you'll need to use actual tick data for backtesting. I suggest using a 1 range bar, I found it most accurate. Use that for your execution dataseries, and you can use a secondary bar series for your signal. You'll need to research using multiple time frames in strategies.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
 
SodyTexas's Avatar
 SodyTexas 
Austin TX
 
Experience: Advanced
Platform: Ninjatrader, Python, & R
Broker: RJO
Trading: Futures, Spreads
Posts: 421 since Sep 2013
Thanks Given: 117
Thanks Received: 1,085


Thanks Mike for taking the time, I know you are a busy guy!

I believe that is exactly what I was doing, with the exception of using 1 minute bars on the BarsInProgress == 1 on a 15 min backtest.

 
Code
Add(PeriodType.Minute, 1);
&

 
Code
		if (BarsInProgress == 0)
			{
				if (CrossAbove(EMA(Fast), EMA(Slow), 1))
				{
					//BarsInProgress = 1
					EnterLong(1, 1, "Long: 1min");				
				}
				
				else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
				{
					//BarsInProgress = 1
					EnterShort(1, 1, "Short: 1min");
				}
			}
			
			if (BarsInProgress == 1)
			{
				
			}
When the backtest runs I get the follow execution times:

Entry: 8:01 AM
Exit: 9:31 AM

Entry: 6:01 PM
Exit: 9:01 PM

Entry: 10:31 PM
Exit: 11:31 PM

and so on...

Do you see what I am doing wrong?

Sody

"The great Traders have always been humbled by the market early on in their careers creating a deep respect for the market. Until one has this respect indelibly engraved in their makeup, the concept of money management and discipline will never be treated seriously."
Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #4 (permalink)
 
SodyTexas's Avatar
 SodyTexas 
Austin TX
 
Experience: Advanced
Platform: Ninjatrader, Python, & R
Broker: RJO
Trading: Futures, Spreads
Posts: 421 since Sep 2013
Thanks Given: 117
Thanks Received: 1,085

Here is the entire 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 SampleIntrabarBacktesV2t : 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()
        {
			
			if (BarsInProgress == 0)
			{
				if (CrossAbove(EMA(Fast), EMA(Slow), 1))
				{
					//BarsInProgress = 1
					EnterLong(1, 1, "Long: 1min");				
				}
				
				else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
				{
					//BarsInProgress = 1
					EnterShort(1, 1, "Short: 1min");
				}
			}
			
			if (BarsInProgress == 1)
			{
				
			}
			
			
			
			// 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
    }
}

"The great Traders have always been humbled by the market early on in their careers creating a deep respect for the market. Until one has this respect indelibly engraved in their makeup, the concept of money management and discipline will never be treated seriously."
Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #5 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,661


SodyTexas View Post
Do you see what I am doing wrong?

Not using tick based bars, like a 1-range bar.

Edit: Sorry I see you were trying to do a 15 minute. Entries would figure once per minute.

Print debug output in the loops and you'll see where you went wrong.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #6 (permalink)
 
SodyTexas's Avatar
 SodyTexas 
Austin TX
 
Experience: Advanced
Platform: Ninjatrader, Python, & R
Broker: RJO
Trading: Futures, Spreads
Posts: 421 since Sep 2013
Thanks Given: 117
Thanks Received: 1,085

Thanks Mike,

Debugging with Print helped. I used:

 
Code
Print(ToTime(Time[0]))
And I found the issue with the sample, instead of adding the entry on BarsInProgress == 0, I moved the code to BarsInProgress == 1, doing nothing on the 15 min bar (BarsInProgress == 0).

The backtest smaple provided from Ninjatrader.com is now working correcltly and I can start my project of building out my system. I will also use your suggestion with the range bar with tick granulation.

Here is the corrected code that provides minute granulation:

 
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 SampleIntrabarBacktesV2t : 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()
        {
			
			if (BarsInProgress == 0)
			{
				
			}
			
			if (BarsInProgress == 1)
			{
				if (CrossAbove(EMA(BarsArray[0],Fast), EMA(BarsArray[0],Slow), 1))
				{
					//BarsInProgress = 1
					EnterLong(1, "Long: 1min");	
					//Print(ToTime(Time[0]));
				}
				else if (CrossBelow(EMA(BarsArray[0],Fast), EMA(BarsArray[0],Slow), 1))
				{
					//BarsInProgress = 1
					EnterShort(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
    }
}
Cheers,

Sody

"The great Traders have always been humbled by the market early on in their careers creating a deep respect for the market. Until one has this respect indelibly engraved in their makeup, the concept of money management and discipline will never be treated seriously."
Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:




Last Updated on June 16, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts