TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.63k stars 1.76k forks source link

TA-Lib's MACD does not match standard MACD implementation #140

Open ivegner opened 7 years ago

ivegner commented 7 years ago

Here are the inputs:

inputs = {"open": [12.1, 52.6, 24.6...], "high": [some more floats], "low": [more floats], "close": [you guessed it, floats], "volume": [floats, floats, floats]}
c = pd.Series(inputs["close"])

I've been playing around, and have made 2 ways to calculate MACD on a dataset: Way 1:

n = 5
inputs["macd_"+str(n)], inputs["macdsignal_"+str(n)], inputs["macdhist_"+str(n)] = ta.MACD(inputs, n, n*2, n*2/3)

Way 2 (the standard way to do MACD):

n=5
fast_ema = c.ewm(span = n, adjust = False).mean()
slow_ema = c.ewm(span = n*2, adjust = False).mean()
macd1 = fast_ema - slow_ema
macd2 = macd1.ewm(span = n*2/3, adjust = False).mean()
macd3 = macd1 - macd2
inputs["macd_"+str(n)], inputs["macdsignal_"+str(n)], inputs["macdhist_"+str(n)] = macd1.values, macd2.values, macd3.values

My algorithm gives infinitely better predictions with Way 1 over Way 2. Upon further digging, I found that in the beginning, the results given by the two methods are very dissimilar, but over time (a couple hundred data points) converge to approximately close values. What gives?

PS. Is there some kind of backwards rebalancing or adjustment going on in TA-Lib on its EMAs? Could it be that my algorithm is just seeing the future (with the rebalances, it knows what's ahead) and that's why it's so good?

Thanks in advance.

ivegner commented 7 years ago

Does the pandas-based method match up with TA-Lib's method for calculating MACD?

mrjbq7 commented 7 years ago

You might find some information here about that TA-Lib's "unstable period":

http://www.ta-lib.org/d_api/ta_setunstableperiod.html

And you might find more information by looking at the implementation of TA_MACD:

https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_MACD.c

mrjbq7 commented 7 years ago

The "unstable period" was linked from a discussion on this bug report:

https://sourceforge.net/p/ta-lib/bugs/55/