Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exponential Moving Average Pandas vs Ta-lib

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

like image 989
LucaG Avatar asked Jul 19 '17 09:07

LucaG


1 Answers

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...

like image 59
maxSonic Avatar answered Sep 18 '22 18:09

maxSonic