I'm using the binance connector API in python (https://binance-connector.readthedocs.io/en/latest/) to calculate short, medium and long term EMA on the 1 minute chart. I have access to real time data from binance and so does my trading view account. I'm using the closing prices of candlesticks on both my program and trading view and I'm using the same lengths for the EMAs: 10 20 and 40:
This is an example of one EMA. The other two have 20 and 40 in the length.
I'm waiting for the short term EMA to cross over the medium and long term EMA but the problem that I have is that my EMA calculations cross over 4 to 5 minutes later compared to the ones on tradingview, even if we use the exact same data.
This is the code (STMA, MTMA and LTMA stand for short, medium and long term moving average): I'm using the kline[4] because that's the closing price of the candle stick: https://binance-docs.github.io/apidocs/spot/en/#compressed-aggregate-trades-list
closing_prices: list[float] = [float(kline[4]) for kline in klines]
SMALTMA = sum(closing_prices) / self.LTMA
SMAMTMA = sum(closing_prices[len(closing_prices)-self.MTMA:]) / self.MTMA
SMASTMA = sum(closing_prices[len(closing_prices)-self.STMA:]) / self.STMA
EMALTMA = SMALTMA
EMAMTMA = SMAMTMA
EMASTMA = SMASTMA
LTMA_ALPHA = 2 / (1 + self.LTMA)
MTMA_ALPHA = 2 / (1 + self.MTMA)
STMA_ALPHA = 2 / (1 + self.STMA)
for i in range(len(closing_prices)):
EMALTMA = (closing_prices[i] * LTMA_ALPHA) + (EMALTMA * (1 - LTMA_ALPHA))
if i >= self.MTMA:
EMAMTMA = (closing_prices[i] * MTMA_ALPHA) + (EMAMTMA * (1 - MTMA_ALPHA))
if i >= self.STMA:
EMASTMA = (closing_prices[i] * STMA_ALPHA) + (EMASTMA * (1 - STMA_ALPHA))
This is how I calculate the short, medium and long term EMA. Are my calculations wrong? (I've used this : https://www.steema.com/docs/financialFunctionsRef/expMovingAverageFunction.htm to get the formula)
In this example, as I've said, my EMA lengths have the same lengths as in trading view, so:
self.STMA = 10
self.MTMA = 20
self.LTMA = 40
What am I doing wrong?
EDIT:
I've seen that the last value that I get from the klines is the current value of the current kline that hasn't closed yet. I've read that the EMA is, usually, calculated by only using the closing values of the klines, so I thought to ignore the last value that I get because that is not a value from a closed kline, so I will just get the EMA from the last candle:
closing_prices: list[float] = [float(kline[4]) for kline in klines][:-1]
The further calculations are the same as above. This still doesn't work. What else should I try/how should I fix this?
Example of the problem:
My script showed me that the short term moving average went above the medium and long term moving average and the red candle stick I have my cursor on. This is, as you can see from the EMAs on tradingview, not true. It was late by 5 whole minutes.
UPDATE: I've also tried to set the starting point of the EMA to the first value of the closing_prices and to 0 and both of them didn't work. I've also tried to not use the value of the last price in the klines since that is not the closing price of a candle, but the current price of the current candle. It still didn't work.