Yes, this may be the issue of data consistency.
See may be the problem because of these factors:
Calculation on incomplete candles
Missing initial historical data.
Candle mismatch because of timing
See we need the both the live and historical data to get the accurate values in EMA and SMA.
Below is the code that you can refer:
def calculate_ema(prices, period=5):
ema = []
k = 2 / (period + 1)
for i, price in enumerate(prices):
if i == 0:
ema.append(price)
else:
ema.append(price * k + ema[-1] * (1 - k))
return ema
# This prices should be have the both live and historical data
prices = [100, 102, 101, 105, 107, 110]
ema_values = calculate_ema(prices, period=3)
print(ema_values)