"Error on calling 'OnBarUpdate' method for strategy Wtrader2: Index was outside the bounds of the array"
I have narrowed it down to a specific area of the script, bhowever I am unable to find the problem. I hope someone can help...
Here is the script in full:
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private bool DLYHF = false;
private bool DLYLF = false;
private bool Retrace61F = false;
private bool Retrace78F = false;
private bool Retrace127F = false;
private bool RetraceF = false;
private bool RetraceClosedF = false;
private double Retrace61P;
private double Retrace78P;
private double Retrace127P;
private double Retrace=0;
private double DLYH=0;
double DLYL=0;
double DLYO=0;
double StudyHigh = 0;
double StudyLow = 0;
double StudyOpen = 0;
double StudyClose = 0;
int Scale=0;
private int fast = 89; // Default setting for Fastfast
private int slow = 36; // Default setting for Fastslow
private int smooth = 12; // Default setting for Fastsmooth
private int fastseries = 0; // Default setting for SlowSeries
private int slowseries = 0; // Default setting for SlowSeries
private double macd;
private double macdAvg;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = false;
int ID = BarsPeriod.Value;
Add(PeriodType.Minute,ID*2);//Scale 1
Add(PeriodType.Minute,ID*4);//Scale 2
Add(PeriodType.Minute,ID*8);//Scale 3
Add(PeriodType.Minute,ID*16);//Scale 4
Add(PeriodType.Minute,ID*32);//Scale 5
Add(PeriodType.Minute,ID*64);//Scale 6
Add(PeriodType.Minute,ID*128);//Scale 7
Add(PeriodType.Minute,ID*256);//Scale 8
Add(PeriodType.Minute,ID*512);//Scale 9
Add(PeriodType.Minute,ID*1025);//Scale 10
Add(PeriodType.Minute,ID*4096);//Scale 11
Add(PeriodType.Minute,ID*8192);//Scale 12
Add(PeriodType.Minute, 1440);//Day
Add(PeriodType.Minute, 7200);//Week
Add(PeriodType.Month, 1);
Add(PeriodType.Year, 1);
Add(MACDCrossFast(fast,slow,smooth));
Add(MACDCrossSlow(fast,slow,smooth));
Scale=1;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if(CurrentBar < 1)return;
DLYH=Highs[13][0];
DLYL=Lows[13][0];
DLYO=Opens[13][0];
DrawHorizontalLine("DailyHighLine",DLYH,Color.Red);
DrawText("DailyHighText","Daily High "+DLYH.ToString("N2"),5,DLYH,Color.Black);
DrawHorizontalLine("DailyLowLine",DLYL,Color.Green);
DrawText("DailyLowText","Daily Low "+DLYL.ToString("N2"),5,DLYL,Color.Black);
DrawHorizontalLine("DailyOpenLine",DLYO,Color.Blue);
DrawText("DailyOpenText","Daily Open "+DLYO.ToString("N2"),5,DLYO,Color.Black);
if (Close[0]>=DLYH)
if (BarsInProgress == Scale)
{
if(DLYHF == false)
{
DLYHF = true;
DLYLF = false;
}
if (Close[0]<=DLYL)
{
if(DLYLF == false)
{
DLYLF = true;
DLYHF = false;
}
}
if (DLYHF == true)
{
if (FirstTickOfBar)
{
StudyHigh = Highs[Scale][0];
StudyLow = Lows[Scale][0];
StudyOpen = Opens[Scale][0];
StudyClose = Closes[Scale][0];
DrawRectangle("Study",1,StudyLow,0,StudyHigh,Color.Blue,Color.Blue,5);
Scale = Scale+1;
if (Scale > 12)
{
Scale = 0;
DLYHF = false;
Print("DLYHF= "+DLYHF);
RemoveDrawObject("RetraceText");
RemoveDrawObject("RetraceIndicator");
}
}
if(Close[0]>Low[1])
{
Retrace = (Close[0]-Low[1])/((High[1]-Low[1])/100);
DrawDiamond("RetraceIndicator",0,Close[0],Color.Blue);
DrawText("RetraceText",Retrace.ToString("N0")+"%",1,Close[0],Color.Black);
if (Retrace >61 && Retrace <78 && Retrace61F == false)
{
RetraceF = true;
Retrace61F = true;
Retrace78F = false;
Retrace127F = false;
}
if (Retrace >78 && Retrace <127 && Retrace78F == false)
{
Retrace61F = false;
Retrace78F = true;
Retrace127F = false;
}
if (Retrace >78 && Retrace <127 && Retrace78F == false)
{
Retrace61F = false;
Retrace78F = false;
Retrace127F = true;
}
}
}
if (DLYLF == true)
{
if (FirstTickOfBar)
{
StudyHigh = Highs[Scale][1];
StudyLow = Lows[Scale][1];
StudyOpen = Opens[Scale][1];
StudyClose = Closes[Scale][1];
Scale = Scale+1;
if (Scale > 12)
{
Scale = 0;
DLYLF = false;
Print("DLYLF= "+DLYHF);
RemoveDrawObject("RetraceText");
RemoveDrawObject("RetraceIndicator");
}
}
if(Close[0]<High[1])
{
Retrace = (Close[0]-Low[1])/((High[1]-Low[1])/100);
DrawDiamond("RetraceIndicator",0,Close[0],Color.Blue);
DrawText("RetraceText",Retrace.ToString("N0")+"%",1,Close[0],Color.Black);
}
}
}
[COLOR=red]if (BarsInProgress == fastseries)[/COLOR]
[COLOR=red]{[/COLOR]
[COLOR=red]if(MACDCrossFast(fast,slow,smooth).CurrentBar < 1)return;[/COLOR]
[COLOR=red]MACDCrossFast(fast,slow,smooth).fastEma.Set((2.0 / (1 + MACDCrossFast(fast,slow,smooth).Fast)) * Input[0] + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Fast))) * MACDCrossFast(fast,slow,smooth).fastEma[1]);[/COLOR]
[COLOR=red]MACDCrossFast(fast,slow,smooth).slowEma.Set((2.0 / (1 + MACDCrossFast(fast,slow,smooth).Slow)) * Input[0] + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Slow))) * MACDCrossFast(fast,slow,smooth).slowEma[1]);[/COLOR]
[COLOR=red]macd = MACDCrossFast(fast,slow,smooth).fastEma[0] - MACDCrossFast(fast,slow,smooth).slowEma[0];[/COLOR]
[COLOR=red]macdAvg = (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Smooth)) * macd + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Smooth))) * MACDCrossFast(fast,slow,smooth).Avg[1];[/COLOR]
[COLOR=red]MACDCrossFast(fast,slow,smooth).Value.Set(macd);[/COLOR]
[COLOR=red]MACDCrossFast(fast,slow,smooth).Avg.Set(macdAvg);[/COLOR]
[COLOR=red]MACDCrossFast(fast,slow,smooth).Diff.Set(macd - macdAvg);[/COLOR]
[COLOR=red]}[/COLOR]
[COLOR=red]if (BarsInProgress == slowseries)[/COLOR]
[COLOR=red]{[/COLOR]
[COLOR=red]Print(slowseries);[/COLOR]
[COLOR=red]if(MACDCrossSlow(fast,slow,smooth).CurrentBar < 1)return;[/COLOR]
[COLOR=red]MACDCrossSlow(fast,slow,smooth).fastEma.Set((2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Fast)) * Input[0] + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Fast))) * MACDCrossSlow(fast,slow,smooth).fastEma[1]);[/COLOR]
[COLOR=red]MACDCrossSlow(fast,slow,smooth).slowEma.Set((2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Slow)) * Input[0] + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Slow))) * MACDCrossSlow(fast,slow,smooth).slowEma[1]);[/COLOR]
[COLOR=red]macd = MACDCrossSlow(fast,slow,smooth).fastEma[0] - MACDCrossSlow(fast,slow,smooth).slowEma[0];[/COLOR]
[COLOR=red]macdAvg = (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Smooth)) * macd + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Smooth))) * MACDCrossSlow(fast,slow,smooth).Avg[1];[/COLOR]
[COLOR=red]MACDCrossSlow(fast,slow,smooth).Value.Set(macd);[/COLOR]
[COLOR=red]MACDCrossSlow(fast,slow,smooth).Avg.Set(macdAvg);[/COLOR]
[COLOR=red]MACDCrossSlow(fast,slow,smooth).Diff.Set(macd - macdAvg);[/COLOR]
[COLOR=red]}[/COLOR]
[COLOR=red]}[/COLOR]
}
}
MACDfast & MACDslow are nearly identical, but have different names so that each can be called by strategy and referenced.
#region Variables
public int fast = 12;
public int slow = 26;
public int smooth = 9;
public int fastseries = 0;
public DataSeries fastEma;
public DataSeries slowEma;
#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.Green, "Macd"));
Add(new Plot(Color.Black, "Avg"));
Add(new Line(Color.DarkGray, 0, "ZeroLine"));
fastEma = new DataSeries(this);
slowEma = new DataSeries(this);
}
/// <summary>
/// Calculates the indicator value(s) at the current index.
/// </summary>
protected override void OnBarUpdate()
{
}
#region Properties
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Avg
{
get { return Values[1]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Default
{
get { return Values[0]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Diff
{
get { return Values[2]; }
}
/// <summary>
/// </summary>
[Description("Number of bars for fast EMA")]
[Category("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Number of bars for slow EMA")]
[Category("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Number of bars for smoothing")]
[Category("Parameters")]
public int Smooth
{
get { return smooth; }
set { smooth = Math.Max(1, value); }
}
#endregion
}
}

Comment