femtotrader / pandas_talib

A Python Pandas implementation of technical analysis indicators
MIT License
736 stars 195 forks source link

Different results from panda_talib.MACD and talib.MACD #18

Open Gabrielvon opened 6 years ago

Gabrielvon commented 6 years ago

Hi everyone,,

I am confused by the results. It seems there are multiple versions of MACD calculation to me. If not, why are they different? Please helps

print(p_df.head())
                 c
timestamp         
2017-04-19  2852.0
2017-04-20  2889.0
2017-04-21  2928.0
2017-04-24  2928.0
2017-04-25  2949.0
p_arr = np.array(p)
from pandas_talib import pandas_talib as pdta
macd1 = pdta.MACD(p_df, 12, 26, price='c')
print(macd1.tail(10))
                 c  MACD_12_26  MACDsign_12_26  MACDdiff_12_26
timestamp                                                     
2017-10-17  3738.0  -27.479893      -44.220449       16.740556
2017-10-18  3688.0  -27.154199      -40.807199       13.653000
2017-10-19  3604.0  -33.289945      -39.303748        6.013803
2017-10-20  3776.0  -23.997889      -36.242576       12.244687
2017-10-23  3699.0  -22.586382      -33.511337       10.924955
2017-10-24  3740.0  -17.952636      -30.399597       12.446961
2017-10-25  3705.0  -16.909496      -27.701577       10.792081
2017-10-26  3649.0  -20.366536      -26.234569        5.868032
2017-10-27  3577.0  -28.586273      -26.704910       -1.881363
2017-10-30  3571.0  -35.179075      -28.399743       -6.779332
import talib
macd2 = np.vstack(talib.MACD(p_arr, 12, 26, 9))
macd2 = pd.DataFrame(macd2.T, index=p_df.index, columns=['macd','macdsignal','macdhist'])
macd2 = p_df.join(macd2)
print(macd2.tail(10))
                 c       macd  macdsignal   macdhist
timestamp                                           
2017-10-17  3738.0 -27.384079  -44.076077  16.691997
2017-10-18  3688.0 -27.065785  -40.674018  13.608233
2017-10-19  3604.0 -33.208822  -39.180979   5.972157
2017-10-20  3776.0 -23.922497  -36.129283  12.206786
2017-10-23  3699.0 -22.516715  -33.406769  10.890054
2017-10-24  3740.0 -17.888063  -30.303028  12.414965
2017-10-25  3705.0 -16.849797  -27.612382  10.762585
2017-10-26  3649.0 -20.311556  -26.152217   5.840661
2017-10-27  3577.0 -28.535880  -26.628949  -1.906930
2017-10-30  3571.0 -35.132875  -28.329735  -6.803141

tim 20171222140328 …]()

femtotrader commented 6 years ago

Thanks @Gabrielvon

You should provide a reusable minimum working example (with data as text and using StringIO and read_csv)

I think you should compare with https://www.ta-lib.org/hdr_dw.html source which is reference implementation.

Pinging @mrjbq7 author of https://github.com/mrjbq7/ta-lib which is Python wrapper for TA-Lib

Gabrielvon commented 6 years ago

@femtotrader Thanks for your reminder. Attached is my sample data file. sample.txt

Honestly, I don't really know which talib I used refer to the link (https://www.ta-lib.org/hdr_dw.html). I simply installed talib using "pip install TA-Lib" following instructions in the main page( https://github.com/mrjbq7/ta-lib) from mrjbq7. The following are my talib version and pands version. (https://github.com/femtotrader/pandas_talib/files/1585183/sample.txt)

talib.__version__
'0.4.10'
pd.__version__
'0.21.1'

Hi, @mrjbq7. Any suggestions? Thanks.


My apologies. I just recalled that I instaledl TA-Lib using a third-party wrapper (TA_Lib‑0.4.10‑cp36‑cp36m‑win_amd64.whl) from here (https://www.lfd.uci.edu/~gohlke/pythonlibs/).

pip show TA-lib Name: TA-Lib Version: 0.4.10 Summary: Python wrapper for TA-Lib Home-page: http://github.com/mrjbq7/ta-lib Author: John Benediktsson Author-email: mrjbq7@gmail.com License: UNKNOWN Location: c:\users\trader\anaconda3\envs\py36\lib\site-packages Requires:

mrjbq7 commented 6 years ago

Wait, so you're comparing talib.MACD (the cython wrapper calling the TA-Lib C library) to pandas_talib.MACD (the python implementation using pandas.ewma?). That's probably the problem. I bet the moving average is slightly different implemented between the two.