Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SPY/TLT rotation strategy - help much appreciated

Hi all,

I am a complete beginner when it comes to Python/Quantopian (which will quickly become evident to anyone who continues reading...). I am wondering whether anyone would be kind enough to explain where I am going wrong with the below code. I am getting a Syntax Error on the first order, however I have no idea how to fix this..

Many thanks,
Dean

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    context.securities = symbol('SPY', # SPDR S&P 500 ETF  
                                'TLT') # iShares 20+ year treasury bond ETF  
# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    record ('cash', context.portfolio.cash, 'leverage', context.portfolio.leverage,  
            'SPY', data.symbol('SPY').price, 'TLT', data.symbol('TLT').price,  
    MA = data[symbol('SPY')].mavg(100),  
    current_price = data[symbol('SPY')].price  
# If current price of SPY is greater than its MA, buy SPY.

    if current_price > MA  
        order_target_percent(symbol('SPY'), 1)  
        order_target_percent(symbol('TLT'), 0)  
        log.info ('Long SPY'),  
# If not, buy TLT  
    elif current_price <= MA:  
        order_target_percent(symbol('TLT'), 1)  
        order_target_percent(symbol('SPY'), 0)  
        log.info ('Long TLT')  
```.
2 responses

Your code used symbol() rather than symbols; the record statement lacked a closing parenthesis; there was a superfluous comma at the end of the MA assignment line; the if statement lacked a colon; etc. Here's a working version for comparison.

Thanks a lot Michael. "A journey of 1000 miles begins with a single step"...