Back to Community
Daily Dip and Peak Intraday SPY Trader

Hi everyone, I made this algorithm to buy and sell the SPY at its hypothesized daily dip and peak. It buys one minute before the close and sells at noon. I wanted this to repeat everyday but it seems like it only did it the first day, anyone know how to fix it?

Clone Algorithm
24
Loading...
Backtest from to with initial capital
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
def initialize(context):
    context.spy = sid(8554)
    #This buys into S&P 500 at close which is hypothesized to be a dip in the market, and sells at open 
    #What I hope to do is incorporate something into this
    
    # Rule out trading costs for now
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))
    
    # Ensure the trades get filled
    set_slippage(slippage.FixedSlippage(spread=0))

    # open position right before closing bell
    schedule_function(open_positions,
                      date_rules.every_day(), 
                      time_rules.market_open(hours=6, minutes=29)
                      )
        
    # close position at 12:00      
    schedule_function(close_positions, 
                      date_rules.every_day(),
                      time_rules.market_close(hours=2, minutes=30)
                      )
    
def open_positions(context, data):
    # Position 50% of our portfolio to be short in SPY
    order_target_percent(context.spy, 1.0)

def close_positions(context, data):
    order_target_percent(context.spy, 1.0)
    
    
There was a runtime error.
1 response

I think you meant to have this to close your position:

def close_positions(context, data):
order_target_percent(context.spy, 0.0)