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

Hi,
I would like to backtest
SPY crosses over SMA(200says) close all positions and buy SPY
SPY crosses bellow SMA(200days), close out all positions and buy SHY

Thanks
Brandon

3 responses

@Brandon,

Try something like this:

# PMAC 

equity, bill = symbol('SPY'), symbol('SHY')  
ma = 200

def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 60))

def trade(context,data):  
    if get_open_orders(): return

    price = data.current(equity, 'price')  
    mavg = data.history(equity, 'price', ma , '1d').mean() 

    wt_eqt = 1.0 if price > mavg else 0  
    wt_bill = 1.0 - wt_eqt  

    if all(data.can_trade([equity, bill])):  
        order_target_percent(equity, wt_eqt)  
        order_target_percent(bill,  wt_bill)  

    record(wt_eqt = wt_eqt,  wt_bill = wt_bill, Leverage = context.account.leverage )  

@Q,

For some unknown reason during last 2 weeks I unable to attach backtest to post reply.
Please respond.

@Vladimir

Thanks so much for your help. If not too much trouble can you please help with this as well

If Relative Strength(200 days) of SPY > SHY, close all positions and buy SPY
If Relative Strength(200 days) of SPY < SHY, close all positions and buy SHY

Thanks,
Brandon

@ Vladimir

what if I want to change it from less than (<) to equals to? aka when 6SMA equals 10SMA.