Hi all,
I'm trying to implement a simple moving average strategy, but it's causing a runtime error in line 15. I'm not sure what the reason is.
Thank you very much.
def initialize(context):
context.stocks = symbols('XLY', # XLY Consumer Discretionary SPDR Fund
'XLF', # XLF Financial SPDR Fund
'XLK', # XLK Technology SPDR Fund
'XLE', # XLE Energy SPDR Fund
'XLV', # XLV Health Care SPRD Fund
'XLI', # XLI Industrial SPDR Fund
'XLP', # XLP Consumer Staples SPDR Fund
'XLB', # XLB Materials SPDR Fund
'XLU') # XLU Utilities SPRD Fund
def handle_data(context, data):
for stock in context.stocks:
price_hist1 = data.history(stock, 'price', 50, '1d') ***Runtime error here***
ma1 = price_hist1.mean()
price_hist2 = data.history(stock, 'price', 200, '1d')
ma2 = price_hist2.mean()
if ma1 > ma2:
order_target_percent(stock, 0.11)
elif ma1 < ma2:
order_target_percent(stock, -0.11)