iMAonArray returning incorrect valuesHi all,

 
Hi all,

I'm having a problem calculating the moving average of the RSI. I thought that I had the code right, but, the values I'm returning from the code are different than what I'm getting in the indicator window. I've attached a screenshot.

For the screenshot, look at the first "RSIMA Vals:". The first value should be the current one and should match up with the "MA(8)" value in the indicator window. The subsequent "RSIMA Vals:" values should be previous MA values.

The RSIMA val being calculated on period 0 is 55.9189. But, the indicator window is showing 56.1563. The RSI value is correct.

Also, the solid blue line in the indicator window is the RSI, the dashed orange line is the EMA applied to the RSI.



Following is the code used ...

double rsiMAVal(int shift)
{
  double rsi[8];
  ArraySetAsSeries(rsi,true);
  double mult=MathPow(10,MarketInfo(Symbol(),MODE_DIGITS));
  
  for(int i=0;i<RSIPeriod;i++)
  rsi[i]=MathRound(iRSI(Symbol(),0, RSIPeriod, PRICE_CLOSE,i+shift)*mult)/mult;
  double ma = iMAOnArray(rsi,0,RSIMAPeriod,0,MODE_EMA,0);
  return(ma);
}



I've tried several variations of this, including omitting the ArraySetAsSeries function and the code using MathPow functions. I found the MathPower function example in a previous post where someone was having trouble with iMAOnArray.

Please help. I've searched multiple sites looking for a solution and have not found one yet.

Thanks.

stockwet

 
In the course of doing further testing, I found that if I change the array size to bars and loop backwards, that seems to do the trick. For those interested, the following code seems to work:

double rsiMAVal(int shift)
{
  double rsi[];
  ArrayResize(rsi, Bars);
  ArraySetAsSeries(rsi,true);
  for(int i=Bars; i>=0; i--)
  {
  rsi[i]=iRSI(Symbol(),0, RSIPeriod, PRICE_CLOSE,i+shift);
  }
  double ma = iMAOnArray(rsi,0,RSIMAPeriod,0,MODE_EMA,0);
  return(ma);
}
 
8-items array is too small for moving average calculation
 
8-items array is too small for moving average calculation


Thanks. I just figured it was an 8 period MA so I only needed 8 datapoints. Is there a minimum we should know for the future to calculate MA?
Reason: