Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
200MA SPY Backtest

Quick backtest of $SPY, 200MA as buy/sell signal

3 responses

I want to change this backtest do:
- buy amount $x when the monthly candle closes above the 200 SMA
- sell everything when it closes under the SMA 200

I put in "initiate" the line

schedule_function(month_end, date_rules.month_end())  

and changed "handle_data" to "month_end".

Now there are some changes because of this old version I'm a little bit confused with:

This change works:
price = data.current(context.spy, 'price') This one, too:
price_hist = data.history(context.spy, 'price', 200, '1d') SMA200 = price_hist.mean() But this doesn't work:
shortSig = data.current(context.spy, SMA200)

Can anyone help me to get this simple example run?

Thanks a lot!

Alexander,
Try this:

price = data.current(context.spy, 'price')  
price_hist = data.history(context.spy, 'price', 200, '1d')  
SMA200 = price_hist.mean()  
shortSig = price < SMA200  

Thanks a lot!

Maybe someone else want to use the version that works.
Therefore I put my backtest and code in here.