Back to Community
MA Crossover Backtest QQQ

Simple backtest to 2002 for QQQs: buy when 10MA > 100MA, go to cash when 10MA < 100MA

Clone Algorithm
503
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.etf = sid(19920)
    context.price={}
    context.invested = False
    
    
def handle_data(context, data):
    
    price = data[context.etf].price
    fastMA = data[context.etf].mavg(10)
    slowMA = data[context.etf].mavg(100)
    shortSig = data[context.etf].mavg(10)
    
    qty = round(context.portfolio.cash/price)
    
    #and price > shortSig                  
    if (fastMA > slowMA ) and not context.invested:
        order(context.etf , qty) 
        buyAmount = round(context.portfolio.starting_cash / price)
        log.info("Bot {0} shares at {1}, Short MA  = {2}, Long MA = {3}".format(buyAmount, price, fastMA, slowMA))
        context.invested = True
    elif (fastMA < slowMA) and  context.invested:
        # liquidate
        order(context.etf,-(context.portfolio.positions[context.etf].amount))
        log.info("Going to cash, ETF = {0}, Short MA  = {1}, Long MA = {2}".format(price, fastMA, slowMA))      
        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.