How can I get my exponential moving average to reflect the same value as the one I defined on Yahoo Finance?

Viewed 300

How can I get my exponential moving average to reflect the same value as the one I defined on Yahoo Finance?

I've tried using the pandas_ta package to calculate them as follows:

df['ema48'] = ta.ema(df.close, length=48)

Tried using the pandas package:

df['ema48'] = pd.Series.ewm(df['close'], span=48).mean()

When on Yahoo Finance, I click on on 'Moving average', change period to 48, and change type to 'Exponential' (and 'Close' is selected as field) but my code doesn't return the same value as seen on YF. Using both calculations, I got 1991.70 today at 08:39 whilst YF displayed 1992.00.

minimal reproducible example

from datetime import date, timedelta
import pandas as pd
import pandas_ta as pdta
import yfinance as yf

pair = 'ETH-GBP'
today = str(date.today())
yesterday = str(date.today() - timedelta(days=1))


def get_historical_data(symbol, start_date, interval):
    df1 = yf.download(symbol, start=start_date, interval=interval)
    df1 = df1.drop(columns=['Open', 'High', 'Low', 'Adj Close', 'Volume'])
    df1 = df1.astype(float)
return df1


df = get_historical_data(pair, yesterday, '1m')

df['ema48'] = pdta.ema(df.Close, length=48)

df['ema48two'] = pd.Series.ewm(df['Close'], span=48).mean()

print(df.tail(1))
1 Answers

I got the same value in both columns, did you check it twice?

I just copied your code and ran it on my computer.

enter image description here

By the way, this is the code of the pandas_ta function, take a look to it in case that you want to know further, it seems to me that if you have TA installed you might see little differences but at the end both columns should converge to the same value.

https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/ema.py

Related