Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
talib.MACD(price_history[sid]) failing for dates earlier than one year ago

Good Afternoon,

I have been playing for over a week with Quantopian in order to cleanly formulate, automate and test my ETF portfolio investment logic. As long as I run backtests for the last 12 months, everything works wonderfully well (see attached).

However, trying to run a backtest from 1/1/2013 to 6/6/2014, Quantopian produces the following error:

Something went wrong on our end. Sorry for the inconvenience.
Exception: inputs are all NaN
There was a runtime error on line 84

Line 84 of my algo contains:

talib_macd = talib.MACD(price_history[sid])  

I use the price_history with 300 days worth of data since my entry points are based on MACD cross-over. The results I get with Quantopian's normal 34-day rolling window simply don't match what I get on Google Finance or Tradingview.

Increasing the bar_count to 600 still causes the same error:

price_history = history(bar_count=600, frequency='1d', field='price')  

In summary, my questions are:

1) What should I do to back-test my algo with time periods starting earlier than June 2013?

2) Does Quantopian ignore/skip ETFs that didn't exist at a specific date of a backtest period without affecting calculation on available ETFs? Or should I write some test in my algo to check whether the ETF price exists for a given day and skip it if it doesn't?

Thanks in advance for your help.

  • Serge
4 responses

Hi Gary, wouldn't using the built-in ta.MACD() function imply a 34-bar rolling window as opposed to the 300-bar history I'm using now with talib.MACD()? At least this is how I interpret an earlier post on that topic: https://www.quantopian.com/posts/ta-dot-macd-values-dont-match-google-or-yahoo-finance-charts. Is there any way to work with 300 bars history using ta.MACD()?
- Serge

Hi Gary, I have written a tiny head-to-head test between ta.MACD() and talib.MACD() and compared the results with Tradingview (https://www.tradingview.com/x/KeycGQtv) for EMLC (an ETF). The talib.MACD() chart matches exactly what I see in Tradingview while the ta.MACD() bears no similarity. Am I doing anything wrong?

Hi Serge,

Let me try to clarify some of the confusion. In your basket of securities, BNDX first started trading on 6/4/2013 so it is not available to backtest on 1/1/2013 - throwing the error. If you remove the security from your list, you can run a backtest over the date range 1/1/2013 - 6/6/2014 (see attached). By default, the backtest will check your date range to see if the securities were available in that time period. But in your case, if you want to test over a longer time frame, you can turn this feature off in the IDE.

To answer your other questions,

Does Quantopian ignore/skip ETFs that didn't exist at a specific date of a backtest period without affecting calculation on available ETFs?
Securities don't trade every minute, or even every day, depending on their liquidity. If there is trade data missing for a security one bar, the backtest will use the previous known price until there is new trade data available. It won't impact the calculations of the other stocks in your algo.

talib vs. ta.
When you do "import talib", you are referencing the open-sourced python library in your algorithm. When we first started, we created a Quantopian-specific wrapper, called "ta." to help use the library on Quantopian functions. However, as we've grown and built new functions, it's shown some holes and we're not working to support it. I imagine it will be deprecated in the future. I'd suggest to use the syntax "import talib" and "talib.MACD" as you're correctly using in your algo.

Matching MACD values between data sources
Quantopian uses a fixed window length of 34 days every time for the calculation. Yahoo and Google use a window from the first day of the year to the current day, so it grows each day. 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. Because of this discrepancy, the values may not match between Quantopian and external data sources. For more information see this discussion here.

Cheers,
Alisa

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.

Hi Alisa, thank you for your detailed and most useful response. I will keep on using the TAlib version of the algo and remove BNDX for the backtest starting 1/1/2013. Cheers, Serge