Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ta.MACD values don't match Google or Yahoo finance charts?

Hi everyone - I'm new and trying out a few of the basics. I'm currently trying out taLib's MACD implementation. It seems the values I get from the function don't match those at Google or Yahoo finance. Can someone tell me what I may have done wrong?

For instance, Google and Yahoo both display the same MACD values
for SPY on March 7, 2013:

MACD: 1.24 Signal: 1.06 Divergence: 0.18

taLib MACD values (macd, macdSignal, macdHist):

(1.121, 0.838, 0.284)

# Plot the MACD and signal for SPY to compare to  
# Google Finance and Yahoo Finance charts

# Example:  
# Google and Yahoo both display the same MACD values  
# for SPY on March 7, 2013:  
# MACD: 1.24    Signal: 1.06    Divergence: 0.18  
#  
# taLib MACD values:  
# (macd, macdSignal, macdHist)  
# (1.121, 0.838, 0.284)


macd = ta.MACD()

def initialize(context):  
    context.spy = sid(8554)  


def handle_data(context, data):

    #Dictionary of sid to tuples, where each tuple  
    #is three floats: (macd, macdSignal, macdHist).  
    macd_data = macd(data)  
    values = macd_data[context.spy]  
    log.info(macd_data)  
    record(macd=values[0], signal=values[1])

14 responses

Oh, just a clarification, MACD is calculated using the same periods on Google and Yahoo as taLib (12, 26, 9)

I've noticed the same issue. Any help would be appreciated.

There does seem to be something amiss here. I've put the Yahoo and Quantopian prices into Excel with the TA-Lib Add-In and got these results:

P.

Peter, thanks for putting that spreadsheet. Based on it, it seems like the problem is data related (i.e. divergence between daily closing prices from Yahoo and Quantopian)?

Chris, question, did you try to use TA's MACDEXT to use an average type of exponential instead of the simple one, and see if there was a difference in your case?

i my_macd_func = ta.MACDEXT(fastmatype=1,slowmatype=1,signalmatype=1)

Carlos

Hello Carlos,

"....it seems like the problem is data related (i.e. divergence between daily closing prices from Yahoo and Quantopian)?"

I'm a bit confused now. I'm not sure if the column names in the spreadsheet are right and I just deleted it! I'll try to recreate it.

My first thoughts were that TA-LIb with Quantopian data in Excel gives the 'right' answer and that backtesting in Quantopian gives the 'wrong' answer.

P.

Apologies. I might have mis-labelled a Yahoo column in the above spreadsheet. This is now correct.

My first thoughts are that TA-LIb with Quantopian data in Excel gives the 'right' answer and that backtesting in Quantopian gives the 'wrong' answer.

The spreadsheet and the Excel Add-In are here:

https://github.com/pcawthron/TALib_And_Excel

The Add-In didn't work for me with Excel 2013 so I reverted to Excel 2003 !

P.

Regarding the slightly differing values, see Dan's post at the bottom of this thread: https://www.quantopian.com/posts/is-it-possible-to-do-live-trading-with-a-daily-trading-frequency. That seems to explain the difference in values here.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

So we have consensus that the minor divergences in closing price as explained by Gus/Dan in the post above are the culprit of the MACD output differences or we still believe that ta-lib / zipline might have something to do?

I think Peter's sheet shows that the difference in the closing price is not a big factor. We opened an issue in our tracker and I'm digging deeper.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Dan, no new developments on this?

Thank you,
Carlos

Not yet. We've been working on it this afternoon, but no resolution yet.

The reason that Yahoo and Google have different MACD values is that they are using different window lengths.  Yahoo and Google don't use 34 days every time - they use all the way back to the first of the year, a window that grows in size each trading day.  Quantopian is doing the calculation with a 34 day window exactly, every time.

MACD is one of the TALib functions that has a memory. The TALib documentation on function memory can be found here, http://ta-lib.org/d_api/ta_setunstableperiod.html Functions with memory will use all available data passed to the function, i.e. the calculation is not constrained to the "lookback" defined by the function's abstract. Our use of the TALib library falls into the "crazy/advanced" use of memory functions prescribed in the TA_SetUnstablePeriod documentation. The TALib wrapper provided in the Quantopian interface uses batch transform under the hood, so 34 bars are seen, the exact window of 34 bars is always passed to the MACD function.

The main advantage of always using a fixed window, instead of all the data available, is that the signal for a given day will be the same across different backtest ranges. With algorithm discussed here, the same value for MACD for March 7th, 2013 will be returned, if the backtest start date is set to January 2nd, 2013 or December 1st 2012 or any other date that is 34 days before March 7th; whereas, if all bars in history are used, the value will change with the backtest range.

Attached is a screen shot of a notebook (inspired by Peter Cawthron's spreadsheet) that shows the difference using the full history and a fixed window.
(Also, Peter it would be interesting to see results from your spreadsheet if the first result is defined as MACD(C3:C36) and that equation was copied into the following rows, versus the current calculation which appears to be doing MACD once over the entire row.)

IPython MACD memory screenshot

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

If someone has their own macd function (def) they have written to simplify, one you trust, can you post that example here?

Hello Gary,

There is some additional information on TA-Lib and it's implementation in Quantopian at https://www.quantopian.com/posts/ta-lib-rsi-using-something-other-than-close-price

P.