Error on calling 'OnMarketData' method for indicator 'MyMarketDepth'. You are accessing an index with a value that is invalid since its out of range. IE accessing a series [barsago] with a value of 5 when there are only 4 bars on the chart.
Apparently there are some gaps in some of the data. Once the error occurs, the indicator stops working until I open and close the indicator window. Is there a snipet of code I can add to tell the indicator to ignore the nonexistent data? I should add that this usually happens long after there are many bars in the chart.
Here is the code I'm using;
protectedoverridevoid OnMarketDepth(MarketDepthEventArgs e)
{
if (e.MarketDataType == MarketDataType.Ask)
{
Ask1 = e.MarketDepth.Ask[0].Volume;
Ask2 = e.MarketDepth.Ask[1].Volume;
Ask3 = e.MarketDepth.Ask[2].Volume;
Ask4 = e.MarketDepth.Ask[3].Volume;
Ask5 = e.MarketDepth.Ask[4].Volume;
Ask6 = e.MarketDepth.Ask[5].Volume;
}
if (e.MarketDataType == MarketDataType.Bid)
{
Bid1 = e.MarketDepth.Bid[0].Volume;
Bid2 = e.MarketDepth.Bid[1].Volume;
Bid3 = e.MarketDepth.Bid[2].Volume;
Bid4 = e.MarketDepth.Bid[3].Volume;
Bid5 = e.MarketDepth.Bid[4].Volume;
Bid6 = e.MarketDepth.Bid[5].Volume;
}
//Print(Close[0] + " " + "Bid3 = " + Bid3+ " Bid2 = " + Bid2 + " Bid1 = " + Bid1 + " Ask1 = " + Ask1 + " Ask2 = " + Ask2 + " Ask3 = " + Ask3);
}
protectedoverridevoid OnBarUpdate()
{
if(CurrentBar < BarsRequired)return;
if (Historical)
return;
MyAsk.Set((Ask1 + Ask2 + Ask3 + Ask4 + Ask5 ) / 5);
MyBid.Set((Bid1 + Bid2 + Bid3 + Bid4 + Bid5) / 5);
}
Safetrading
Comment