I have a problem with a script. After couple hours trying identifying the problem, I finaly found it come from my List-type variable.
Each time I run the code, I get the following error message :
"Error on calling 'OnBarUpdate' method for indicator 'KeoPivots' on bar 56: 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."
Here is the code :
#region Variables
// Wizard generated variables
private int period = 20; // Default setting for Period
private int rcoeff = 5; // Default setting for Rcoeff
private double rerror = 0.7; // Default setting for Rerror
// User defined variables (add any user defined variables below)
private DataSeries Cvalue;
private DataSeries Hvalue;
private DataSeries Lvalue;
private DataSeries Range;
private double Suplimit = 0.0 ;
private double Inflimit = 0.0 ;
private double Delta =0.0;
private double Delta2 =0.0;
private double lastCvalue =0.0;
private double diff = 0.0;
private int Index;
public struct Paliers
{
public int barNumber;
public double Lprice;
public double Cprice;
public double Hprice;
public int Compteur;
public Paliers(int PbarNumber,double PLprice,double PCprice,double PHprice,int PCompteur)
{
barNumber=PbarNumber;
Lprice=PLprice;
Cprice=PCprice;
Hprice=PHprice;
Compteur=PCompteur;
}
}
private List<Paliers> xx = new List<Paliers>();
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
// Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
CalculateOnBarClose = true;
Overlay = true;
Cvalue = new DataSeries (this);
Lvalue = new DataSeries (this);
Hvalue = new DataSeries (this);
Range = new DataSeries (this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar==0)
{
if (xx.Count==1)
xx.RemoveAt(0);
}
if (CurrentBar < period)
return;
else
{
Cvalue.Set(SMA(Close,period)[0]);
Hvalue.Set(SMA(Close,period)[0]);
Lvalue.Set(SMA(Close,period)[0]);
Range.Set(Hvalue[0]-Lvalue[0]);
Delta = Range[0]*0.3*rcoeff;
if (CrossAbove(High,Cvalue,1) || CrossBelow(Low,Cvalue,1) || (High[0]>=Cvalue[0] && Low[0]<=Cvalue[0]))
{
Paliers Pivot = new Paliers(CurrentBar,Lvalue[0],Cvalue[0],Hvalue[0],0);
lastCvalue=Cvalue[0];
xx.Add(Pivot);
Index=xx.Count;
// Print(Index);
// Print(xx[Index-1].Compteur);
DrawDot("Pivot"+CurrentBar,false,0,Cvalue[0],Color.Gold);
}
Suplimit = lastCvalue + Delta ;
Inflimit = lastCvalue - Delta ;
Delta2 = rerror*Range[0] ;
if ((CrossAbove(Close,Suplimit,1) || CrossBelow(Close,Inflimit,1)) && Index>2)
{
for (int i=0 ; i<Index ;i++)
{
diff = lastCvalue-xx[i].Cprice ;
if(Math.Abs(diff) <= Delta2)
{
xx.RemoveAt(i);
DrawDot("PivotRemove"+CurrentBar,false,0,xx[i].Cprice,Color.Magenta);
}
}
}
}
}
May someone help ?
Comment