**megatr0n:**I am seeking some clarification on calculating the EMA(exponential moving average). I would like the manual calculation to get the same EMA result as the iMA function.

- The formula to calculate the Exponential moving average = [Close - previous EMA] * (2 / n+1) + previous EMABe careful with precedence. The formula is
*c=p+(v-p) * 2/(n+1).*You wrote*p+(v-p) * (1 + 2/n)*but correctly calculated*2/(n+1).*Never used the equation 'c=(1-a)p + (a)v', as it amplifies round off errors. -
double arr[3] = {1.3051,1.3041,1.3047};//last three bars' closing prices array double ema1 = iMAOnArray2(arr,2,MODE_EMA,0);// result is 1.30476667

Be careful with ordering. Since you didn't set the direction (*SetAsSeries*) of your array,*OnArray*is assuming that 1.3051 (index zero) is the latest value. [(1.3051 - 1.3041) * (2 / 2+1)] + 1.3041 = [0.001 * (2/3)] + 1.3050 = 1.30476666667

You are calculating as if 1.3041 was the previous EMA value. It is not.- In the exponential moving average (EMA,) the initial value is some what arbitrary, even a constant of zero works given enough future bars. To get within 0.1 percent of true value requires 3.45 * (Length+1) bars. To get an accurate average sooner requires a better initial value. Metaquotes uses SMA(L). Still, don't expect a valid EMA(2) until you have at least 10 bars.
1.3050 shift=0 1.3051 shift=1 (I am using this shift) 1.3041 shift=2 1.3047 shift=3

Your calculation should be- EMA4=(1.3047+1.3041)/2=1.3044
- EMA3=1.3044+2/3(1.3047-1.3044)=1.3046
- EMA2=1.3046+2/3(1.3041-1.3046)=1.304266666666667
- EMA1=1.304266666666667+2/3(1.3051-1.30426666666666771=1.304822222222222

Returns a result of "1.30487345". Now my function returns "1.30476667" and I can't figure out why the result is different if both functions are correct.

Here is my function:

I decided to manually copy the chart's data set the iMA function is using and I observed the following closing prices: