NexusFi: Find Your Edge


Home Menu

 





Showing "tried to reference back more bars" in backtest


Discussion in MultiCharts

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




 
 

Showing "tried to reference back more bars" in backtest

 
 Jeffrey 
Hong Kong
 
Experience: Beginner
Platform: MultiCharts, NinjaTrader
Trading: HSIF, ES
Posts: 10 since Jul 2010
Thanks Given: 2
Thanks Received: 1

I have developed a custom ATR as below. I have no problem using it on my charts.

 
Code
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator{
	
	[UpdateOnEveryTick(false)]
	public class Jeffrey_ATR : IndicatorObject {
	
		/*
		This is the first proven working version that work as a reference and shouldn't be changed.				
		*/		
		
		private int _Period;

		[Input]	
        public int Period
        {
            get { return this._Period; }
            set { 
				
				if (value<1) {
					this._Period = 1;
				} else {
					this._Period = value; 
				}			
			}
        }						
			
		private IPlotObject plot1;
		
		private VariableSeries<Double> atrSeries;

			
		public Jeffrey_ATR(object _ctx):
		base(_ctx){
		
			// default 14 days
			Period = 14;			
		}
		
		protected override void Create() {
			// create variable objects, function objects, plot objects etc.
			plot1 = AddPlot(new PlotAttributes("ATR", EPlotShapes.Line, Color.Cyan, Color.Empty, 0, 0, true));
			atrSeries = new VariableSeries<Double>(this);
			
		}
		
		protected override void StartCalc() {
			// assign inputs 
						
			ExecInfo.MaxBarsBack = 1;
				
			#if DEBUG				
				Output.Clear();
				Output.WriteLine("{0} StartCalc()", this.GetType().ToString());			
				Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2} MaxBarsBack={3} Period={4} Close[0]={5}",
				Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),			
	            Bars.CurrentBar,
				Bars.CurrentBarAbsolute()+1,
	            ExecInfo.MaxBarsBack,
				Period,
				Bars.Close[0]);
			#endif				
			
			#if DEBUG						
				Output.WriteLine("StartCalc()");			
				Output.WriteLine("");
			#endif								
		}
		protected override void CalcBar(){

			#if DEBUG			
				Output.WriteLine("CalcBar()");
				Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2}",
				Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),
				Bars.CurrentBar, 				
				Bars.CurrentBarAbsolute()+1);			
			#endif							
			
			// logic from NT7
			/*
			if (CurrentBar == 0)
				Value.Set(High[0] - Low[0]);
			else
			{
				double trueRange = High[0] - Low[0];
				trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
				Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));
			}
			*/			
			
			double trueRange;						
			
			/* 
			This is not the real first bar, this is the first bar after the MaxBarsBack. 
			The MaxBarsBack has been set to 1, so the absolute position of this bar is 2nd indeed.
			*/						
			if (Bars.CurrentBar == 1) {
				
				double trueRangeOfBarZero = Math.Abs(Bars.High[1] - Bars.Low[1]);
				
				trueRange = Math.Max(Math.Abs(Bars.Low[0] - Bars.Close[1]), Math.Max(Math.Abs(Bars.High[0] - Bars.Low[0]), Math.Abs(Bars.High[0] - Bars.Close[1])));
				
				// we calculate ATR of the first (absolute) and second (absolute)
				atrSeries.Value = (trueRangeOfBarZero + trueRange) / 2;
			}
			
			// Start from 2nd bar (CurrentBar) / 3rd bar (CurrentBarAbsolute)
			else {
				
				trueRange = Math.Max(Math.Abs(Bars.Low[0] - Bars.Close[1]), Math.Max(Math.Abs(Bars.High[0] - Bars.Low[0]), Math.Abs(Bars.High[0] - Bars.Close[1])));
				
				// we will multiply by currentBar. So on the 2nd bar (CurrentBar), we wiill multiply by 2 and divide by 3 since 1st bar (currentbar) has an average of two days.
				atrSeries.Value = (Math.Min(Bars.CurrentBar, Period-1) * atrSeries[1] + trueRange) / Math.Min(Bars.CurrentBar + 1, Period);			
			}
			
			if (Bars.CurrentBarAbsolute()+1 >= Period) {
			
				#if DEBUG				
					Output.WriteLine("Print this bar");		
				#endif				

				// 0 means no displacement to the current bar
				plot1.Set(0, atrSeries.Value);
			}			

			#if DEBUG								
				Output.WriteLine("Close[1]={0} High[0]={1} Low[0]={2}",
				Bars.Close[1],
				Bars.High[0], 
				Bars.Low[0]);
			
				Output.WriteLine("atrSeries[1]={0} trueRange[0]={1} multiplier={2} divider={3} atrSeries.Value={4}",
				atrSeries[1],
				trueRange,
				Math.Min(Bars.CurrentBar, Period-1),
				Math.Min(Bars.CurrentBar + 1, Period),
				atrSeries.Value
				);
			#endif			
			
			#if DEBUG					
				Output.WriteLine("CalcBar()");
				Output.WriteLine("");
			#endif						
		}
	}
}
However I when try to reference it in the below strategy and backtest it, it always throw "tried to reference more bars than allowed by the current max bars back setting".

I have set the MaxBarBack of the strategy to 5 and using Period as 5, but still no luck.

 
Code
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
	
	public class Jeffrey_SimpleIntraDayBreakoutLE : SignalObject {
				
		public Jeffrey_SimpleIntraDayBreakoutLE(object _ctx):base(_ctx){}
		
		private int _Period;

		[Input]	
        public int Period
        {
            get { return this._Period; }
            set { 
				
				if (value<1) {
					this._Period = 1;
				} else {
					this._Period = value; 
				}			
			}
        }	
		
		private IOrderMarket buy_order;
		private IOrderMarket sell_order;		
				
		private TimeSpan morningStartTime;
		private TimeSpan morningEndTime;
		
		private TimeSpan afternoonStartTime;
		private TimeSpan afternoonEndTime;
		
		private TimeSpan nightStartTime;
		private TimeSpan nightEndTime;		
		
		private PowerLanguage.Indicator.Jeffrey_ATR atr;
		
		protected override void Create() {
			
			// create variable objects, function objects, order objects etc.
			buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Buy));
			sell_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Sell));

			atr = (PowerLanguage.Indicator.Jeffrey_ATR)AddIndicator("Jeffrey_ATR");

		}
		protected override void StartCalc() {
						
			#if DEBUG							
				Output.Clear();
				Output.WriteLine("{0} StartCalc()", this.GetType().ToString());			
				Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2} MaxBarsBack={3} Period={4} Close[0]={5}",
				Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),			
	            Bars.CurrentBar,
				Bars.CurrentBarAbsolute()+1,
	            ExecInfo.MaxBarsBack,
				Period,
				Bars.Close[0]);
			#endif				
			
			#if DEBUG						
				Output.WriteLine("StartCalc()");			
				Output.WriteLine("");
			#endif				
		}
		protected override void CalcBar(){
			
			#if DEBUG			
				Output.WriteLine("CalcBar()");
				Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2}",
				Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),
				Bars.CurrentBar, 				
				Bars.CurrentBarAbsolute()+1);			
			#endif			
			
			bool entryCondition1 = Bars.Close[0] - Bars.Close[1] > 1000; 
			bool exitCondition1 = Bars.Close[0] - Bars.Close[1] < 1000; 

			
			if (entryCondition1) {
				buy_order.Send();								
			}

			if (exitCondition1) {
				sell_order.Send();								
			}
			
			
			#if DEBUG					
				Output.WriteLine("CalcBar()");
				Output.WriteLine("");
			#endif					
		}
	}
}
the problem is from the line: atr = (PowerLanguage.Indicator.Jeffrey_ATR)AddIndicator("Jeffrey_ATR"); if I comment this out the strategy is able to work.

Anyone have a clue about what's going on?

Started this thread

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Are there any eval firms that allow you to sink to your …
Traders Hideout
Futures True Range Report
The Elite Circle
Deepmoney LLM
Elite Quantitative GenAI/LLM
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
 
 kevinkdog   is a Vendor
 
Posts: 3,647 since Jul 2012
Thanks Given: 1,890
Thanks Received: 7,338

It is telling you you need more than 5 for the max bars back value. So, try 10, and see if it works. Or, keep increasing it until it doesn't throw the error.

Follow me on Twitter
 
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690



kevinkdog View Post
It is telling you you need more than 5 for the max bars back value. So, try 10, and see if it works. Or, keep increasing it until it doesn't throw the error.

A better approach might be to not specify the number of MaxBarsBack explicitly (either manually or programmatically). That way MultiCharts will use auto-detect for the number of MaxBarsBack, ensuring this value is always big enough for the script's calculations.


 



Last Updated on May 15, 2015


© 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