EMA calculation formula - Zerodha

renishkhunt
Hello all,

I am trying to calculate the 5 days EMA calculation using code but I got the wrong output than Zerodha showing on the chart.

I take the EMA calculation formula from here
https://www.investopedia.com/terms/e/ema.asp

Here is my code for calculating the EMA formula.

$length = 5;
$multiplier = 2 / ( $length + 1 );

$todayPrice = 552.3;

// Wipro closing price 28 January 2022 15:00 to 28 January 2022 14:00
$averageSum = 551.2 + 555 + 555.25 + 553.75 + 553.6;
$average = $averageSum / $length;

print ($todayPrice * $multiplier) + ( $average * ( 1 - $multiplier ) );


I am getting the output 553.27 and Zerodha showing 553.4694 ( 2022/01/28 15:15:00 ).

Can you please help me where I did a mistake in calculating the EMA formula?

Thanks
Sign In or Register to comment.