Back to Community
SPY 7 days strategy

Just a simple strategy learnt from a book. Performance would be better if leverage is 2.

Entry rule:
SPY above 200days sma
and SPY close at 7 days low

Exit rule:
SPY below 200 days sma
or SPY close at 7 days high

Clone Algorithm
26
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
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS, Q500US
from quantopian.pipeline.factors import SimpleMovingAverage, RSI, CustomFactor, Returns, Latest
from quantopian.algorithm import attach_pipeline, pipeline_output

def initialize(context):
    set_commission(commission.PerTrade(cost=0.00))
    set_slippage(slippage.FixedSlippage(spread=0.00))
    set_long_only()

    schedule_function(rebalance_sell, date_rules.every_day(), time_rules.market_close(minutes=30), half_days=False)
    schedule_function(rebalance_buy, date_rules.every_day(), time_rules.market_close(), half_days=False)
    context.stock = symbol('SPY')

def make_pipeline():
    pass

def before_trading_start(context, data):
    pass
              
def rebalance_sell(context, data):  
    cl200 = data.history(context.stock, 'price', 200, '1d')
    cl = data.current(context.stock,'price')
    cl7 = data.history(context.stock, 'price', 7, '1d')
    
    if cl < cl200.mean():
       order_target(context.stock, 0)
    if cl7[-1] == max(cl7):
       order_target(context.stock, 0)
             
def rebalance_buy(context, data): 
    cl200 = data.history(context.stock, 'price', 200, '1d')
    cl = data.current(context.stock,'price')
    cl7 = data.history(context.stock, 'price', 7, '1d')
    
    if cl > cl200.mean():
        if cl7[-1] == min(cl7):
            order_target_percent(context.stock, 1.0)
    
def my_record_vars(context, data):
    pass
There was a runtime error.