Moving Average vs iMAOnArray - page 2

 
Miguel Angel Vico Alba #:
Please attach the full and original code so we can better understand the issue.

If we keep sharing partial snippets, sometimes already modified or outdated, there’s a risk that any proposed solutions won’t work, and we end up stuck in a never-ending loop.

Full code is at the top. That's why this is so frustrating it's not very complicated.

I just added another buffer to calculate the MA.

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_minimum 0

#property indicator_color1 Lime
#property indicator_color2 Red

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


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,val1);
   SetIndexBuffer(1,val2);
   SetIndexBuffer(1,valma);
  
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexStyle(2,DRAW_LINE,0,2,clrMagenta);
   
   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;

//----
   ArraySetAsSeries(val2,True);
   for(i=0; i<limit; i++)
   {
      val1[i]=(Volume[i]+(Close[i]-Open[i])/Point/dXecn)/2;
      val2[i]=(Volume[i]-val1[i]);
   }
   
   for(i=0; i<limit; i++)
   {
     valma[i] = iMAOnArray(val2,0,8,0,MODE_SMA,i);
   }
   

//----
   return(0);
  }
 
sd59 #:

Full code is at the top. That's why this is so frustrating it's not very complicated.

I just added another buffer to calculate the MA.

I think I have solved the issue.

I didn't interpret the built-in MT4 moving average method of calculating correctly.

So when I dropped on the moving average from Indicators\Trend\Moving Average and chose 'First Indicator's Data' then chose 'Previous Indicator's Data' it is actually calculating the moving average of the moving average from the 'First Indicator's Data' as opposed to just using the 2nd buffer value (val2).

Thanks for your replies though.

 
sd59 #:

I think I have solved the issue.

I didn't interpret the built-in MT4 moving average method of calculating correctly.

So when I dropped on the moving average from Indicators\Trend\Moving Average and chose 'First Indicator's Data' then chose 'Previous Indicator's Data' it is actually calculating the moving average of the moving average from the 'First Indicator's Data' as opposed to just using the 2nd buffer value (val2).

Thanks for your replies though.

Thanks for sharing all the details, and I'm glad you were able to get to the bottom of it. 😉

What you said about how MT4 applies "Previous Indicator's Data" makes perfect sense. That setting doesn’t actually apply the moving average directly to the buffer you expect (like val2), but instead to the visual output of the previously drawn indicator in the subwindow. So when you chain indicators like that, you’re often applying the average to a result that’s already been smoothed or transformed, not the raw buffer data you coded.

This kind of misunderstanding is very common; not because it’s illogical, but because MT4 doesn’t really make it clear. The configuration window gives the impression that you’re applying the MA to "the previous buffer" but internally, you don’t have full control over which data it’s referencing.

That’s why the result often doesn’t match what you get with iMAOnArray, which does behave in a fully transparent and predictable way: you choose the exact array it operates on.

As for the solution you shared; it’s perfectly valid in the sense that you correctly identified why the values didn’t match. But it wasn’t really a code issue. It was more about comparing two different things that weren’t equivalent, and that can throw off even experienced developers.

If you’re aiming for a cleaner and more reliable approach, I’d suggest skipping the manually added MA altogether and coding the full logic yourself. For example, calculate the moving average over val2 using iMAOnArray, and if you need to smooth that further, you can apply another MA on top using an extra buffer. This way, you know exactly what you’re working with and remove all ambiguity.

 
Miguel Angel Vico Alba #:

Thanks for sharing all the details, and I'm glad you were able to get to the bottom of it. 😉

What you said about how MT4 applies "Previous Indicator's Data" makes perfect sense. That setting doesn’t actually apply the moving average directly to the buffer you expect (like val2), but instead to the visual output of the previously drawn indicator in the subwindow. So when you chain indicators like that, you’re often applying the average to a result that’s already been smoothed or transformed, not the raw buffer data you coded.

This kind of misunderstanding is very common; not because it’s illogical, but because MT4 doesn’t really make it clear. The configuration window gives the impression that you’re applying the MA to "the previous buffer" but internally, you don’t have full control over which data it’s referencing.

That’s why the result often doesn’t match what you get with iMAOnArray, which does behave in a fully transparent and predictable way: you choose the exact array it operates on.

As for the solution you shared; it’s perfectly valid in the sense that you correctly identified why the values didn’t match. But it wasn’t really a code issue. It was more about comparing two different things that weren’t equivalent, and that can throw off even experienced developers.

If you’re aiming for a cleaner and more reliable approach, I’d suggest skipping the manually added MA altogether and coding the full logic yourself. For example, calculate the moving average over val2 using iMAOnArray, and if you need to smooth that further, you can apply another MA on top using an extra buffer. This way, you know exactly what you’re working with and remove all ambiguity.

Yes I basically ended up using the code I pasted on the previous page SimpleMAOnBuffer which is essentially the MT4 source code, and of course just applied the correct buffer that I wanted.

All good now.👍