Moving Average vs iMAOnArray

 

Hello guys,

I have a very simple indicator drawn in a subwindow, the code of which is below:

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_minimum 0

#property indicator_color1 Lime
#property indicator_color2 Red

double val1[];
double val2[];
double dXecn = 1;
int nbar;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,val1);
   SetIndexBuffer(1,val2);
  
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   if(Digits==3||Digits==5){dXecn=10;
   }
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+


int start()
  {
  
   int i,counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;

//----
   for(i=0; i<limit; i++)
   {
      val1[i]=(Volume[i]+(Close[i]-Open[i])/Point/dXecn)/2;
      val2[i]=(Volume[i]-val1[i]);
   }
  
   
   if(nbar != iBars(NULL,0))
  {
  ArraySetAsSeries(val2,True);
  Print("val2 = ",iMAOnArray(val2,0,8,0,MODE_SMA,5));
  nbar = iBars(NULL,0);
  }

//----
   return(0);
  }

An example of the output is shown below with an 8SMA (applied price = previous indicators data) dropped into the subwindow.

(note: there is an 8SMA also attached with is not shown in colour that uses applied price = first indicator data.)


I now want to get the values of the moving average so I use the iMAOnArray function.

The results of the print statement do not match the values of the moving average shown in the data window - so for a bar shift of 5 for example:

print statement = 533.87    data window = 544.46

I have checked many more values but the two do not agree with each other. It also doesn't seem to make much difference if I set ArraySetAsSeries to either True or False.

Can anybody see any errors please or suggest why the two values do not match?

thank you.

 
sd59:

why are you trying to "reinvent the wheel"? There are many indicators on codebase that you could modify to suite your needs; using ima array and separate chart.

 
sd59:
You're almost there. The issue is likely due to a mismatch between the array order and how you're feeding data into it.

When using iMAOnArray, make sure the array is either fully processed in reverse using ArraySetAsSeries(true) with a loop from Bars minus one to zero, or keep it in default order with ArraySetAsSeries(false) and adjust the shift index accordingly.

Right now you're mixing both, which causes the discrepancy you're seeing.
 
Michael Charles Schefe #why are you trying to "reinvent the wheel"? There are many indicators on codebase that you could modify to suite your needs; using ima array and separate chart.
You're right that there are many ready-made indicators out there, and they're great for quick use. But here we're also trying to support people who want to learn how things work under the hood.

Understanding functions like iMAOnArray and how buffers behave is part of the learning curve, and sometimes that means digging deeper rather than just reusing code.

This forum isn’t just about finding shortcuts but also about helping others grow as developers.