I have a question about the "iMAOnArray". So strange!

 

I wrote the code like this:

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 White

extern int m=5;

double roc[];
double PosBuffer[];

int init()
{
IndicatorBuffers(2);
SetIndexBuffer(0,roc);
SetIndexBuffer(1,PosBuffer);  
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,roc);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,PosBuffer);
  return(0);
}

int start()
{
  int i,counted_bars=IndicatorCounted();
 for (i=0;i<Bars; i++)
   {
    roc[i]=iClose(NULL,0,i)/iClose(NULL,0,i+m);
   }
 for (i=0;i<Bars; i++)
   {
    PosBuffer[i]=iMAOnArray(roc,Bars,m,0,MODE_EMA,i);
     } 
   return(0);
  }

There is only one line for "roc" showed in the chart, and the other one is missing.

but,if I change the code like this:

 for (i=0;i<Bars; i++)
   {
    roc[i]=iClose(NULL,0,i);
   }

and both two lines are appeared in the chart.


Why the first code doesn't work at all?

I am so confused

 
Replace
roc[i]=iClose(NULL,0,i)/iClose(NULL,0,i+m);
to
if(iClose(NULL,0,i+m)!=0)roc[i]=iClose(NULL,0,i)/iClose(NULL,0,i+m);else roc[i]=1;
 
Roger:
Replace
to

It does work.

Thank a lot.

 
crazywoo:

It does work.

Thank a lot.

Problem is in the for loops. They should be like this:

for (i=0;i<Bars-m; i++)