EMA: how is it calculated in JForex?

Hello there,

I have been trying to reproduce EMA(14) indicator in JForex in java and compare it with the built-in one.
However I am struggling finding how EMA is computed by Dukascopy, since I get discrepancies with my results.

This is my "ema" function: (which is derived from the formula ema[t] = a * x[t] + (1-a) x[t-1])

public double f_ema(double[] val, double k)
{
if (val.length == 1) return val[0];
int t = val.length - 1;
double[] past = Arrays.copyOfRange(val, 0, t);
return k * val[t] + (1 - k) * f_ema(past, k);
}

And I call it in this way:

timeframe = (take m_tperiod + 1 entries from inputs array)
m_output[j] = f_ema(timeframe, 2.0 / (1.0 + m_tperiod));

Do you have an idea about where I am wrong?
Thank you very much in advance.

ipocentro

Translate to English Show original

orto leave comments

Answers: 2

Best Answer

The formula for EMA both in JForex and in Online Charts is as follows:

The first is calculated:
EMA[t] = P[t]

All the others:
EMA(P,N)[t] = (p(N) * P[t]) + ((1 – p(N)) * EMA[t-1])

where
p(N) = 2 / (N + 1) – the degree of weighting decrease
EMA[t-1] — the value of the EMA at the previous time period
P(N) — price (open, close, ... )

Is x in your formula the price? x[t] in your formula should be the price and x[t-1] should be the previous EMA value. There is also a possibility that JForex and your function use different prices. I mean closing and opening prices, for example.

Translate to English Show original
4 Oct. 2016 by

orto leave comments

Hi DRT, thank you for your answer.
Your formula is exactly what I do in "return k * val[t] + (1 - k) * f_ema(past, k);". In JForex sometimes I see discrepancies (around 0.5pip) in some peak values. I was wondering if the EMA in JForex makes some rounding, because I even tried computing EMA by hand and it always matches with my indicator.

PS I always compute EMA by considering closes (both in JForex and in my indicator).

Translate to English Show original
4 Oct. 2016 by

orto leave comments
Please log in or register to post answer.