Page 1 of 1

EMA Accuracy

PostPosted: Wed Mar 27, 2019 12:39 pm
by kelvinocw
Hi guys,How do i code EMA calculations accurately?The values doesn't seem to be accurate or in line with the EMA values from MT4 platforms or Tradingview.com?Please help me out thank you! :D

Re: EMA Accuracy

PostPosted: Wed Mar 27, 2019 1:40 pm
by Apprentice
Can you provide an algorithm used for two mentioned?

We have.
k = 2.0 / (n + 1.0);
EMA= (1 - k) * previous value of EMA + k * source;

Re: EMA Accuracy

PostPosted: Wed Mar 27, 2019 4:42 pm
by kelvinocw
Thanks for replying.The codes that u gave me is great but the problem is what is the starting value of yesterdays EMA.Is there any short cut to grab the value?

Re: EMA Accuracy

PostPosted: Wed Mar 27, 2019 4:52 pm
by kelvinocw
Because the current candle EMA is calculated by the close price which is updated every second with a weighted multiplier which is K + previous candle EMA. The problem is how do i get the previous candle EMA? How do i get the most precise previous day EMA value that would be used in the equation to get the most precise today's EMA?

Re: EMA Accuracy

PostPosted: Fri Mar 29, 2019 4:54 am
by Apprentice
we use this logic.

if (period == first) then
value = source;
else
value = previous value of EMA;
end

You can find other implementations on the internet.
https://stockcharts.com/school/doku.php ... g_averages

Re: EMA Accuracy

PostPosted: Fri Mar 29, 2019 5:17 am
by Apprentice
They use same formula.

Stockcharts version
EMA: {Close - previous} x multiplier + previous.

TS Version
EMA: (1 - multiplier) * previous + multiplier * Close ;
EMA: previous - multiplier*previous + multiplier * Close ;
EMA: previous + multiplier*(Close-previous)
EMA: (Close-previous)*multiplier +previous

Re: EMA Accuracy

PostPosted: Fri Mar 29, 2019 5:22 am
by Apprentice