I am trying to make a simple strategy that prints out some values, though using multiple time frames. I keep getting the following error
Error on calling 'OnBarUpdate' method for strategy 'PrintSwing/b122f02ce1144ea8be09dba0c60f5631': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
Any idea why?
Here is the first few lines of code in my strategy
protected override void Initialize()
{
Add(Swing(5)); //add swing indicator with strength of 5, not 5minutes!
Add(PeriodType.Minute, 30); //add second time frame, 30mis
Add(PeriodType.Minute, 1); // add a third timeframe, 1min
Add(EMA(20));
Add(VWAP());
//assumes that the initial time period chosen for strategy is 1min
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{ // opening bracket
// code for calculating fibonacci levels on the opening range 9:30am - 10:30
double opening_high = 0;
double opening_low = 0;
// Resets the highest high and lowest low at the start of every new session
if (Bars.FirstBarOfSession && FirstTickOfBar)
{
opening_high = 0;
opening_low = 0;
}
DateTime startTime;
DateTime endTime;
endTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 10, 30, 0);
startTime = new DateTime(9,30,0);
int startBarsAgo = GetBar(startTime);
int endBarsAgo = GetBar(endTime);
//calculates opening range high and low
opening_high = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];
opening_low = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];
//fib level calculations on opening range
double opening_range_fibo_thirty = opening_low + (opening_high - opening_low) * .382;
double opening_range_fibo_sixty = opening_low + (opening_high - opening_low) * .618;
double opening_range_fibo_fifty = opening_low + (opening_high - opening_low) * .5;
double opening_range_fibo_thirty_extension_high = opening_low + (opening_high - opening_low) * 1.382;
double opening_range_fibo_thirty_extension_low = opening_low - (opening_high - opening_low) * 1.382;

Comment