Need help figuring out how to use Moving Average on buffer in mql5

 

Hey guys,


I'm busting my head a couple of days on this issue,

And I couldn't find the answer wherever I looked,

So I would really appreciate any input you have.


Basically I try to replicate to functionality of mql4's iMAOnArray in mql5

I have tried using both MetaQuotes' built-in function of ExponentialMAOnBuffer() (As seen here- https://www.mql5.com/en/code/77),

And iMAOnArrayMQL4 function as seen here- https://www.mql5.com/en/articles/81


HOWEVER each of the above function seems to give different result, and non of them match the output of the built-in iMA function.


I've also tried 'flipping' the arrays, setting as series and not as series, and changing periods, but it doesn't seem to make any difference.


This is my function-

This is solely for testing the functionality of the above 2, so it is independent from the rest of my code.

It is called in the beginning of OnInit()

void ArrayTest() {

   // Set test parameters
   int iMAPeriod = 14;
  
   // Get values from iMA function for reference
   int iHandle = iMA(Symbol(), Period(), iMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
   
   if(iHandle == INVALID_HANDLE) {
      Print("Failed to create MA Handle");
      ExpertRemove();
   }
   
   double dReferenceValue[];
   
   if(!ArrayResize(dReferenceValue, iMAPeriod)) {
      Print("Failed to resize dReferenceValue array");
      ExpertRemove();
   }
   
   if(CopyBuffer(iHandle, 0, 0, iMAPeriod, dReferenceValue) == -1) {
      Print("Failed to copy MA Value");
      ExpertRemove();
   }
   
   for(int i = 0; i < iMAPeriod; i++) {
      Print("iMA[", i, "] = ", dReferenceValue[i]);
   }
   
   /* Testing MetaQuotes' complimentary OnBuffer function */
   // Getting close prices
   // Make sure dClose has enough data for calculating MA
   double dClose[];
   int iClosePeriod  = (iMAPeriod + 1) * 2;
   
   if(!ArrayResize(dClose, iClosePeriod)) {
      Print("Failed to resize dClose array");
      ExpertRemove();
   }
   
   if(CopyClose(Symbol(), Period(), 0, iClosePeriod, dClose) == -1) {
      Print("Failed to copy close prices");
      ExpertRemove();
   }
   
   // Create a buffer to hold MA values
   double dMAArray[];
   
   if(!ArrayResize(dMAArray, iMAPeriod)) {
      Print("Failed to resize dMAArray");
      ExpertRemove();
   }
   
   ExponentialMAOnBuffer(iMAPeriod, 0, 0, iMAPeriod, dClose, dMAArray);
   
   for(int i = 0; i < iMAPeriod; i++) {
      Print("dMAArray[", i, "] = ", dMAArray[i]);
   }
   
   /* Test iMAOnArrayMQL4 output */
   for(int i = 0; i < iMAPeriod; i++) {
      Print("iMAOnArray[", i, "] = ", iMAOnArrayMQL4(dClose, 0, iMAPeriod, 0, MODE_EMA, i));
   }
   
  ExpertRemove();
  }


This is the output of the above function-

2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[0] = 1.223302767679022
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[1] = 1.223085065321819
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[2] = 1.222911056612243
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[3] = 1.222782915730611
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[4] = 1.222705193633196
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[5] = 1.222475167815437
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[6] = 1.222335812106712
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[7] = 1.222229703825817
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[8] = 1.222143076649041
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[9] = 1.222067999762502
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[10] = 1.222145599794169
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[11] = 1.222123519821613
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[12] = 1.222089717178731
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMA[13] = 1.222003088221567
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[0] = 1.22689
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[1] = 1.226895333333333
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[2] = 1.226802622222222
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[3] = 1.226682272592593
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[4] = 1.226272636246914
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[5] = 1.225950951413992
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[6] = 1.225580157892127
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[7] = 1.22522280350651
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[8] = 1.224854429705642
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[9] = 1.224605839078223
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[10] = 1.224379727201126
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[11] = 1.224185096907643
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[12] = 1.223941750653291
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   dMAArray[13] = 1.223752183899518
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[0] = 1.22444766429655
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[1] = 1.224071920342173
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[2] = 1.223632215779431
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[3] = 1.223237172053189
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[4] = 1.222827506215218
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[5] = 1.222707122556021
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[6] = 1.222529756795408
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[7] = 1.222431257840856
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[8] = 1.222359143662526
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[9] = 1.222343627302914
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[10] = 1.222244185349516
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[11] = 1.222141752326365
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[12] = 1.222022021915036
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   iMAOnArray[13] = 1.221970025286581
2021.08.03 00:33:25.604 Core 1  2021.01.01 00:00:00   ExpertRemove() function called
2021.08.03 00:33:25.604 Core 1  removed itself within OnInit
2021.08.03 00:33:25.604 Core 1  tester stopped because OnInit failed
2021.08.03 00:33:25.608 Core 1  disconnected
2021.08.03 00:33:25.608 Core 1  connection closed


What am I missing guys?

MovingAverages
MovingAverages
  • www.mql5.com
The MovingAverages library contains functions for calculation of different types of moving averages.
Reason: