Exponential Moving Average Pandas vs Ta-lib
Asked Answered
V

1

7

I'm currently writng a code involving some financial calculation. More in particular some exponential moving average. To do the job I have tried Pandas and Talib:

talib_ex=pd.Series(talib.EMA(self.PriceAdjusted.values,timeperiod=200),self.PriceAdjusted.index) 
pandas_ex=self.PriceAdjusted.ewm(span=200,adjust=True,min_periods=200-1).mean()

They both work fine, but they provide different results at the begining of the array:

200 day EMA - Talib vs Pandas

So there is some parameter to be change into pandas's EWMA or it is a bug and I should worry?

Thanks in advance

Luca

Veronaveronese answered 19/7, 2017 at 9:22 Comment(0)
D
8

For the talib ema, the formula is:

So when using the pandas, if you want to make pandas ema the same as talib, you should use it as:

pandas_ex=self.PriceAdjusted.ewm(span=200,**adjust=False**,min_periods=200-1).mean()

Set the adjust as False according to the document(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ewm.html) if you want to use the same formula as talib:

When adjust is True (default), weighted averages are calculated using weights (1-alpha)(n-1), (1-alpha)(n-2), ..., 1-alpha, 1.

When adjust is False, weighted averages are calculated recursively as: weighted_average[0] = arg[0]; weighted_average[i] = (1-alpha)weighted_average[i-1] + alphaarg[i].

You can also reference here: https://en.wikipedia.org/wiki/Moving_average

PS: however, in my project, i still find some small differences between the talib and the pandas.ewm and don't know why yet...

Dorotheadorothee answered 14/8, 2017 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.