EMA calculation different everywhere

 

Hello, I am quite new to MQL and trying to create my first EA and I got some strategy where I need to calculate EMA. I am working on 15m period, EUR/USD, calculating on close prices. I got sheet with historic prices where I calculated EMA myself. However the result is different from what i get with MQL

When doing iMAOnArray(arr, 0, 12, 0, MODE_EMA, 0) on these prices I get final EMA = 1.276702082774568 or 1.276246812078973 if set as series.

When I am geting EMA with  

EMA is equal = 1.276701681147687

While in my excel I calculated EMA as 1.276706828

I believe my excel is correct. Why do I have different EMAs with  iMAOnArray and iMA and my excel? Anybody could help? Struggling with this for 3 days already......

 

void OnTick()

 if (TimeHour(TimeCurrent()) == 8 && TimeMinute(TimeCurrent()) == 45) //to get ema for 8:30 close price. (date is correct)
      {
         Print("EMA ",iMA(NULL, PERIOD_M15, 12, 0, MODE_EMA, PRICE_CLOSE, 1));

      }

}

Cannot attach excel file here, so uploaded it here - https://ufile.io/acada

For quick view this is the table of my prices:

    Date (2010y)   Close price               EMA 12

00:00 22/Jul 1.27630 0
00:15 22/Jul 1.27681 0
00:30 22/Jul 1.27608 0
00:45 22/Jul 1.27690 0
01:00 22/Jul 1.27680 0
01:15 22/Jul 1.27610 0
01:30 22/Jul 1.27512 0
01:45 22/Jul 1.27588 0
02:00 22/Jul 1.27642 0
02:15 22/Jul 1.27522 0
02:30 22/Jul 1.27519 0
02:45 22/Jul 1.27477 1.2759658333
03:00 22/Jul 1.27582 1.275943397
03:15 22/Jul 1.27569 1.275904413
03:30 22/Jul 1.27562 1.275860657
03:45 22/Jul 1.27671 1.275991325
04:00 22/Jul 1.27632 1.276041891
04:15 22/Jul 1.27682 1.2761616
04:30 22/Jul 1.27663 1.276233661
04:45 22/Jul 1.27732 1.27640079
05:00 22/Jul 1.27683 1.276466823
05:15 22/Jul 1.27673 1.276507311
05:30 22/Jul 1.27667 1.27653234
05:45 22/Jul 1.27719 1.276633519
06:00 22/Jul 1.27754 1.276772978
06:15 22/Jul 1.27714 1.276829443
06:30 22/Jul 1.27723 1.276891067
06:45 22/Jul 1.27770 1.277015518
07:00 22/Jul 1.27750 1.277090054
07:15 22/Jul 1.27819 1.277259276
07:30 22/Jul 1.27813 1.277393234
07:45 22/Jul 1.27787 1.277466582
08:00 22/Jul 1.27525 1.27712557
08:15 22/Jul 1.27570 1.276906251
08:30 22/Jul 1.27561 1.276706828
 
Roberto Jacobs:
Because iMA and iMAOnArray are two different functions.
I understand that. I am trying to calculate EMA for the same values with iMA and iMAOnArray, but i get different result
 
sania789:
I understand that. I am trying to calculate EMA for the same values with iMA and iMAOnArray, but i get different result
Try to use MovingAverages.mqh
 
sania789:
I understand that. I am trying to calculate EMA for the same values with iMA and iMAOnArray, but i get different result

What is the amount of the difference?

An EMA is affected by every single price in the history (albeit by increasingly tiny amounts), not just the period such as 12 bars. Therefore, a calculation using iMA(), with e.g. 10,000 bars in the available history, will give a slightly different answer to iMAOnArray(), with e.g. 1,000 bars in the array.

... but only usually by a tiny amount. 

 
Roberto Jacobs:
Try to use MovingAverages.mqh

What is this?

 

JC:

What is the amount of the difference?

An EMA is affected by every single price in the history (albeit by increasingly tiny amounts), not just the period such as 12 bars. Therefore, a calculation using iMA(), with e.g. 10,000 bars in the available history, will give a slightly different answer to iMAOnArray(), with e.g. 1,000 bars in the array.

... but only usually by a tiny amount. 

 Ah it makes sense then. However I still do not see why iMAOnArray is different from my excel calculations, I think I am using correct formula.... Anybody?...

 
sania789:

Ah it makes sense then. However I still do not see why iMAOnArray is different from my excel calculations, I think I am using correct formula.... Anybody?...

Your Excel calculation looks correct, and I get the same result - 1.2891614 - if I read that list of prices into an array and then do iMAOnArray() on it.
Files:
 
set the array as series before filling
 
JC:
Your Excel calculation looks correct, and I get the same result - 1.2891614 - if I read that list of prices into an array and then do iMAOnArray() on it.

Sorry, where did you get that number from?.. I am getting different numbers, I don't get 1.2891614 anywhere

 

whroeder1:
set the array as series before filling

I tried filling array from both sides (as far as I understand its the same as setting as series or not series) and still not getting number as in my excel... 

 
sania789:

Sorry, where did you get that number from?.. I am getting different numbers, I don't get 1.2891614 anywhere

It's the final cell K122 in your spreadsheet at https://ufile.io/acada