iMAOnArray returns different results for static array and indicator buffer

 

Hi guys,

Investigating the logic of one indicator I noticed that iMAOnArray() function returns different results for the same values depending on whether they are represented as indicator buffer or static array. I created a simple test that calculates Simple MA of 3 values with the period of 3.

      int N=3;
      MA[2] = 1.0913700000000015;
      MA[1] = 1.0811866666666683;
      MA[0] = 1.0766433333333350;

      double bufferArrayMA=iMAOnArray(MA,0,N,0,MODE_SMA,0);
      Print("BufferArrayMA "+ DoubleToString(bufferArrayMA,16));

      double array[3];
      array[0] = MA[2];
      array[1] = MA[1];
      array[2] = MA[0];
      double staticArrayMA=iMAOnArray(array,0,N,0,MODE_SMA,0);
      Print("StaticArrayMA "+ DoubleToString(staticArrayMA,16));

 I was expected that both results would be absolutely identical, but they were not:

StaticArrayMA 1.0830666666666682
BufferArrayMA 1.0830667368231886

 As you can see the difference is quite small (starting from the seventh digit), but still it's quite strange. 

 Does anybody have a clue what can cause this? Any help is appreciated. 

 The source code of the test is attached to this message.

 Thanks, Anton 

Files:
test.mq4  1 kb
 
anikulin Does anybody have a clue what can cause this? Any help is appreciated.

Buffers are series arrays; zero is the latest value. so you get (MA[2]+MA[1]+MA[0])/3

Your array isn't.  So you get (array[-2] + array[-1] + array[0])/3 first 2 are garbage.

ArraySetAsSeries - MQL4 Documentation

 
WHRoeder:

Buffers are series arrays; zero is the latest value. so you get (MA[2]+MA[1]+MA[0])/3

Your array isn't.  So you get (array[-2] + array[-1] + array[0])/3 first 2 are garbage.

ArraySetAsSeries - MQL4 Documentation




But the problem is that iMAOnArray with a series array returns a different value from (MA[2]+MA[1]+MA[0])/3, but iMAOnArray with a static array returns correct one (the one that can be simply calculated as (MA[2]+MA[1]+MA[0])/3).
 
anikulin:
But the problem is that iMAOnArray with a series array returns a different value from (MA[2]+MA[1]+MA[0])/3, but iMAOnArray with a static array returns correct one (the one that can be simply calculated as (MA[2]+MA[1]+MA[0])/3).

I also noticed that the result that iMAOnArray returns depends on the order or elements, however it shouldn't as Simple MA is used.

When I tried to place 3 same values in different ways I got 3 different results (but none of them was correct):

BufferArrayMA 3 1.0830667461140955
BufferArrayMA 2 1.0830667464531798
BufferArrayMA 1 1.0830667368231886

 I also tested iMAOnArrayMQL4 function that is suggested as a replacement for iMAOnArray in MQL5 (https://www.mql5.com/en/articles/81) and its calculation on series array gave the same results as iMAOnArray for static array (1.0830666666666682, correct value).

 Any ideas why iMAOnArray works in a different way and why the order of elements influences the final result? 

Files:
test_1.mq4  2 kb
 

Hi,

i want to get iMAOnArray() from another smaller MA, it seems simple but i dont understand where is my mistake, the  iMAOnArray (which called BiggerMA here) get me wrong values in compare to what we have on chart
can someone help me.

//+------------------------------------------------------------------+
double MAonArry(string symbol,int timeframe, int candle)
{
double SmallerMABuffer[];
ArrayResize(SmallerMABuffer,(BiggerMA_period+2));
for(int i=BiggerMA_period+1 ; i>=0 ; i--)
 {
  SmallerMABuffer[i]=iMA(symbol,timeframe,SmallerMA_period,SmallerMA_Shift,SmallerMA_method,SmallerApplied_Price,i);
 }

return iMAOnArray(firstMABuffer,0,BiggerMA_period,BiggerMA_Shift,BiggerMA_method,candle);
}
//+------------------------------------------------------------------+
 
Alireza Yadegar #: the  iMAOnArray (which called BiggerMA here) get me wrong values in compare to what we have on chart
  1. Set your array as series (before filling) so the function knows how to process it.
  2. For SMA(n) or WMA(n) you need n values. Exponential requires 3.45×(Length+1) bars to converge to 1/10 percent. (EMA Period 5 requires 21 initial bars.)
              Moving average - Wikipedia
Reason: