Excuse the novice question, I am new to coding.
I'm trying to create an algo that only trades when both the $SPY and the $IWM are uptrending.
I'm assigning a binary variable to define the trends using SMA's
I keep getting an undbloundlocalerror
UnboundLocalError: local variable 'iwmbull' referenced before assignment
USER ALGORITHM:35, in handle_data
if spybull == iwmbull:
def initialize(context):
context.spy = sid(8554)
context.iwm = sid(21519)
context.aapl = sid(24)
def handle_data(context, data):
histspy = data.history(context.spy, 'price', 20, '1d')
log.info(histspy.head())
sma_20spy = histspy.mean()
sma_9spy = histspy[-9:].mean()
if sma_9spy > sma_20spy:
spybull = 1
elif sma_9spy < sma_20spy:
spybear = 1
histiwm = data.history(context.iwm, 'price', 20, '1m')
log.info(histiwm.head())
sma_20iwm = histiwm.mean()
sma_9iwm = histiwm[-9:].mean()
if sma_9iwm > sma_20iwm:
iwmbull = 1
elif sma_9iwm < sma_20iwm:
iwmbear = 1
if spybull == iwmbull:
order_target_percent(context.aapl, 1.0)
elif spybear == iwmbear:
order_target_percent(context.aapl, -1.0)