Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Leveraged SPY pullback newbie help

Hi, I'm new to python, and i'm trying to get started with a basic pullback strategy looking for dips in SPY that will trigger a buy for the UPRO 3xETF.

Here is my code, I am getting an execution timeout and I don't know why:

import talib

def initialize(context):
context.security = symbol('SPY')
context.security = symbol('UPRO')

Will be called on every trade event for the securities you specify.

def handle_data(context, data):
current_price = data.current.price
current_high = data.current.high

sma20 = data[context.security].mavg(20)  
sma200 = data[context.security].mavg(200)  
spy_rsi = talib.RSI(current_price, timeperiod=2)[-1]  

current_positions = context.portfolio.positions[symbol('UPRO')].amount  
cash = context.portfolio.cash  

if (spy_rsi<10 and current.high>sma200) and current_positions == 0:  
    number_of_shares = int(cash/current_price)  
    order(context.security.upro, number_of_shares)  
    log.info('Buying shares')  

elif (spy_rsi>85 and current.high>sma20 or rsi>80 and current.high<sma200) and current_positions != 0:  
    order_target(context.security.upro, 0)  
    log.info('selling shares')