Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Moving average crossover versus benchmark

Posted elsewhere, in the 50/200 MA crossover topic, but looks like moving averages are computed from the start date of a backtest, therefore for daily moving averages there is a significant lag before the values are correct so trading not done when it should be if the correct moving averages are available for the stock when first called in the code. This can apparently been seen by buying a stock and watching the moving averages (e.g. 50 DMA and 100 DMA) in the log Meanwhile being compared against the backtest benchmark (SPY) so since trades not made the comparison will be off. Is this the way moving averages are currently computed and could they be available from Day 1 of the analysis?

7 responses

I think you're referring to what we call "warmup" of moving averages. We've evolved as a product in how we think about warmup, and we have a few different behaviors because of that. It's a bit confusing, which I apologize for. In the future we'll "clean it up" and work on making it more consistent.

  • In backtests, simple transforms like mavg() and vwap() have no warmup. If you have mavg(30), the first day is just that price, the second day is a two-day average, the third day is a three-day average and so on up to 30 days.
  • In backtests, batch transforms emit None until the the window is full, aka it is warmed up.
  • In live trading and paper trading, both simple transforms and batch transforms are fully warmed up before the algo actually starts.

The feedback is that the fully warmed up solution is best. We'll retroactively implement it at some point. For now, you need to code around the warmup period in backtesting.

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.

Hello Dan,

Coincidentally I just got this message for the first time when I started to paper trade an algo:

Your algorithm requires a warmup period that is before March 1, 2013. Use a shorter warmup period and redeploy your algorithm  

What are the restrictions on the warmup period?

P.

Hi Peter,

We recently made an additional year of data available for paper-trading algorithms, so you should now be able to paper-trade algorithms that require warm-up as far back as March 1, 2012. The date in the error message you got is incorrect; we forgot to update it when we released the additional year of data. That will be fixed shortly. ;-)

  jik

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.

Thanks, Jonathan.

Thank you for the explanation and for the responsiveness! Very helpful.

Yes, agree that fully warmed up is preferable for all environments including backtesting. Can code around when the first trade occurs while the simple transforms (e.g. moving average) are warming up but then end up with invalid backtesting comparisons against the benchmark. For example, 200 DMA means waiting close to a year before correct moving average and being able to trade off accurate information while, during this same period, the benchmark SPY is fluctuating. Therefore the final comparison for how well a strategy using simple transforms does against the benchmark can be very inaccurate. Would expect that when code asks for a 50 DMA that's what it gets, not a 2 or 3 day average depending on where you are in the cycle.

+1 for making full warmup consistent across backtesting, paper trading, and live trading.

this has the advantages:
- operates like standard manual trading. When one opens a chart for "today" and begins trading on 14 day MA, the data shown are (today + 13 prior days)
- fewer differences between backtesting and production lead to fewer surprises when switching

Thanks, good to see available.