EMA calculation using history API

Can anyone help me on how does the EMAs are getting calculated which we see on the charts per candle?
I am calculating using below code to calculate 20 EMA in 5 min timeframe -

def calculate_ema(prices, period):
    # prices is an array which contains all previous day close prices of 5 min candles
# as the new prices are coming I am appending them in the array

    # period will be 20
    ema = []
    multiplier = 2 / (period + 1)
    ema.append(sum(prices[:period]) / period)  # Initial EMA (SMA of first period)
    for price in prices[period:]:
        ema.append(((price - ema[-1]) * multiplier) + ema[-1])
    return ema[-1]

1
2 replies