When I run my script I get the following error message :
Error on calling 'OnBarUpdate' method for indicator 'Pivots' on bar 3118: 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.
After checking the code I finally pinpoint the issue but this is confusing and I don't understand it.
I conditionally stored some price values on a list named xx and at some point I try to get the index of the closest ones below/above the last Close. Here is the code :
var24=999999.00;
var25=999999.00;
for (int i=0 ; i<xx.Count ; i++)
{
var58 = Close[0]-xx[i].Cprice;
if (var58<0 && Math.Abs(var58)< Math.Abs(var24))
{
var24 = var58;
var28 = i;
}
if (var58>=0 && var58 < var25)
{
var25 = var58;
var29 = i;
}
}
Print(var28+" "+var29+" "+xx.Count);
As you can see my index should NEVER reach xx.Count as I specified it in the loop but when I print my var28 & var29 (that contain index values)+xx.Count.........SURPRISE!!!...I found out that at some point index equals xx.Count.
How the hell is it possible ?
Comment