I am creating an indicator that will plot the ratio of EMAs of 2 separate instruments. What i am doing is that i put the dataseries of the 2 instruments on the chart. In my code:
double ema1 = EMA(BarsArray[0],_ema1Length)[0];
double ema2 = EMA(ChartControl.BarsArray[1],_ema2Length)[0];
I calculate the ratio and then plot it.
I get some very peculiar and inconsistent behaviour. The above code somethimes work and sometimes it doesnt. Changing the 2nd line to:
double ema2 = EMA(ChartControl.BarsArray[1],_ema2Length)[-1];
works. And then after some time of running, it stops working again. And when i change the statement to 0 offset, it starts to work.
The exception that i get is:
4/17/2013 7:36:11 PM|3|4|Error on calling 'OnBarUpdate' method for indicator 'EMARatio' on bar 0: 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.
Can you please explain the behaviour or what i am supposed to do.
Thanks.
Umer
Comment