# EMA calculation different everywhere

17

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));

}

}

For quick view this is the table of my prices:

Date (2010y)   Close price               EMA 12

 00:00 22/Jul 1.2763 0 00:15 22/Jul 1.27681 0 00:30 22/Jul 1.27608 0 00:45 22/Jul 1.2769 0 01:00 22/Jul 1.2768 0 01:15 22/Jul 1.2761 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.27597 03:00 22/Jul 1.27582 1.27594 03:15 22/Jul 1.27569 1.2759 03:30 22/Jul 1.27562 1.27586 03:45 22/Jul 1.27671 1.27599 04:00 22/Jul 1.27632 1.27604 04:15 22/Jul 1.27682 1.27616 04:30 22/Jul 1.27663 1.27623 04:45 22/Jul 1.27732 1.2764 05:00 22/Jul 1.27683 1.27647 05:15 22/Jul 1.27673 1.27651 05:30 22/Jul 1.27667 1.27653 05:45 22/Jul 1.27719 1.27663 06:00 22/Jul 1.27754 1.27677 06:15 22/Jul 1.27714 1.27683 06:30 22/Jul 1.27723 1.27689 06:45 22/Jul 1.2777 1.27702 07:00 22/Jul 1.2775 1.27709 07:15 22/Jul 1.27819 1.27726 07:30 22/Jul 1.27813 1.27739 07:45 22/Jul 1.27787 1.27747 08:00 22/Jul 1.27525 1.27713 08:15 22/Jul 1.2757 1.27691 08:30 22/Jul 1.27561 1.27671
17

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
48614

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
1583

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.

17

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?...

1583

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:
prices.txt 2 kb
17122

set the array as series before filling
17

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...

1583

sania789:

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