I am having trouble with an indicator that I have written. It terminates at random times and gives a totally inapplicable error.
The error message is: "Indicator 'MyIndicator': Error on calling 'OnBarUpdate' method on bar 435: 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.
The problematic code seems to be this:
endBarsAgo = Bars.GetBar(Time[0]);
if(EntryTime == Time[0])
{ PriceVar3 = Close[0]; }
else
{
startBarsAgo = Bars.GetBar(new DateTime(EntryTime.Year, EntryTime.Month, EntryTime.Day, EntryTime.Hour, EntryTime.Minute + 1, 0));
PriceVar3 = MAX(High, endBarsAgo - startBarsAgo + 1)[CurrentBar - endBarsAgo];
}
I am trying to get the High of the range between when the indicator first triggers and when the exit conditions appear, but I want to exclude the first (trigger) bar from the range. So, I set the startBarsAgo variable equal to the time of the bar after the trigger bar. (This is designed exclusively for the 1-minute chart.)
It works perfectly fine for 434 bars, and there is absolutely nothing about bar 435 that would distinguish it in any way from the other bars. Since I am already doing a comparison to see that at least one bar had been stamped since the entry, there must be a valid range. And in bar 435's particular case, there have been several bars printed since the entry trigger occurred, so the assertion that there is an invalid range is total nonsense.
Any idea what might be going on here?
Thanks in advance!

Comment