Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem with MAVG

Hi all,
I'm trying to implement a simple moving average strategy, but it's causing a runtime error in line 15. I'm not sure what the reason is.

Thank you very much.

def initialize(context):  
        context.stocks = symbols('XLY',  # XLY Consumer Discretionary SPDR Fund  
                           'XLF',  # XLF Financial SPDR Fund  
                           'XLK',  # XLK Technology SPDR Fund  
                           'XLE',  # XLE Energy SPDR Fund  
                           'XLV',  # XLV Health Care SPRD Fund  
                           'XLI',  # XLI Industrial SPDR Fund  
                           'XLP',  # XLP Consumer Staples SPDR Fund  
                           'XLB',  # XLB Materials SPDR Fund  
                           'XLU')  # XLU Utilities SPRD Fund

def handle_data(context, data):  
        for stock in context.stocks:  
                price_hist1 = data.history(stock, 'price', 50, '1d')    ***Runtime error here***  
                ma1 = price_hist1.mean()  
                price_hist2 = data.history(stock, 'price', 200, '1d')  
                ma2 = price_hist2.mean()  
                if ma1 > ma2:  
                        order_target_percent(stock, 0.11)  
                elif ma1 < ma2:  
                        order_target_percent(stock, -0.11)  
2 responses

Hi Andy,

Quantopian has data starting from 1/3/2002. Your algo has two history calls that say "on every bar, give me the last 50 and 200 days of close prices". However on the first day of the backtest, this trailing window is not available. Try starting your backtest on 1/3/2003 and it will run.

If you'd like help learning the API check out the getting started tutorial: https://www.quantopian.com/tutorials/getting-started

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 so much!