Indicator 'SampleGetHighLowByTimeRange': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
Can someone explain, like I'm a 5 year old what this means? To me, it says there are not enough bars on the chart. Well, there are hundreds of bars on the chart.
All I want to do is draw a line at the Premarket High and Premarket Low. Any assistance would be appreciated.
namespace NinjaTrader.NinjaScript.Indicators
{
public class SampleGetHighLowByTimeRange : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Determines the highest high and lowest low in a specified time range";
Name = "Sample get high low by time range";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
StartMonth = 12;
StartDay = 11;
StartHour = 7;
StartMinute = 30;
EndMonth = 12;
EndDay = 12;
EndHour = 15;
EndMinute = 00;
AddPlot(Brushes.Green, "HighestHigh");
AddPlot(Brushes.Red, "LowestLow");
}
}
private DateTime startDateTime;
private DateTime endDateTime;
protected override void OnBarUpdate()
{
// Check to make sure the end time is not earlier than the start time
if (EndHour < StartHour)
return;
//Do not calculate the high or low value when the ending time of the desired range is less than the current time of the bar being processed
if (ToTime(EndHour,EndMinute,0) > ToTime(Time[0]))
return;
// If the stored date time date is not the same date as the bar time date, create a new DateTime object
if (startDateTime.Date != Time[0].Date)
{
startDateTime = new DateTime(Time[0].Year, StartMonth, StartDay, StartHour, StartMinute, 0);
endDateTime = new DateTime(Time[0].Year, EndMonth, EndDay, EndHour, EndMinute, 0);
}
// Calculate the number of bars ago for the start and end bars of the specified time range
int startBarsAgo = Bars.GetBar(startDateTime);
int endBarsAgo = Bars.GetBar(endDateTime);
/* Now that we have the start and end bars ago values for the specified time range we can calculate the highest high for this range
Note: We add 1 to the period range for MAX and MIN to compensate for the difference between "period" logic and "bars ago" logic.
"Period" logic means exactly how many bars you want to check including the current bar.
"Bars ago" logic means how many bars we are going to go backwards. The current bar is not counted because on that bar we aren't going back any bars so it would be "bars ago = 0" */
double highestHigh = MAX(High, endBarsAgo - startBarsAgo + 1)[CurrentBar - endBarsAgo];
// Now that we have the start and end bars ago values for the specified time range we can calculate the lowest low for this range
double lowestLow = MIN(Low, endBarsAgo - startBarsAgo + 1)[CurrentBar - endBarsAgo];
// Set the plot values
HighestHigh[0] = highestHigh;
LowestLow[0] = lowestLow;
//Print("Start Time: " + startDateTime + " End Time: " + endDateTime);
//Print("HighestHigh[0]: " + HighestHigh[0] + " LowestLow[0]: " + LowestLow[0]);
}​

Comment