Back to Community
200MA SPY Backtest

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

Clone Algorithm
89
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# 50/200 MA strategy


#initialize the strategy 
def initialize(context):
    context.spy = sid(8554)
    context.price={}
    context.invested = False
    
    context.max_notional = 1000000.1
    context.min_notional = -1000000.0
    
    
def handle_data(context, data):
    
    global lastRebalance
    
    price = data[context.spy].price
    shortSig = data[context.spy].mavg(200)
    longSig = data[context.spy].mavg(200)

    qty = round(context.portfolio.cash/price)
    
                  
    if (price > longSig) and not context.invested:
        order(context.spy , qty) 
        buyAmount = round(context.portfolio.starting_cash / data[sid(8554)].price)
        log.info("Bot {0} shares in SPY at {1}, Short MA  = {2}, Long MA = {3}".format(buyAmount, price, shortSig, longSig))
        context.invested = True
    elif (price < shortSig) and  context.invested:
        # liquidate
        order(context.spy,-(context.portfolio.positions[context.spy].amount))
        log.info("Going to cash, SPY = {0}, Short MA  = {1}, Long MA = {2}".format(price, shortSig, longSig))      
        context.invested = False
          
          
          
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
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.

Clone Algorithm
30
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# 50/200 MA strategy


#initialize the strategy 
def initialize(context):
    context.spy = sid(8554)
    context.price={}
    context.invested = False
    
    context.max_notional = 1000000.1
    context.min_notional = -1000000.0
    
    schedule_function(month_end, date_rules.month_end())
    
def month_end(context, data):
    
    global lastRebalance
    
    
    price = data.current(context.spy, 'price')  
    price_hist = data.history(context.spy, 'price', 200, '1d')  
    SMA200 = price_hist.mean()  
    shortSig = price < SMA200  
    longSig = price > SMA200

    qty = round(context.portfolio.cash/price)
                  
    if longSig and not context.invested:

        order(context.spy , qty)
        buyAmount = round(context.portfolio.starting_cash / data.current(context.spy, 'price'))
        log.info("Bot {0} shares in SPY at {1}, Short MA  = {2}, Long MA = {3}".format(buyAmount, price, shortSig, longSig))
        context.invested = True
        
    elif shortSig and context.invested:
        
        # liquidate
        order(context.spy,-(context.portfolio.positions[context.spy].amount))
        log.info("Going to cash, SPY = {0}, Short MA  = {1}, Long MA = {2}".format(price, shortSig, longSig))      
        context.invested = False
There was a runtime error.