I keep getting this error and i have no idea how to fix it . KeyError: 'longs' There was a runtime error on line 22.
def initialize(context):
schedule_function(my_rebalance,date_rules.week_start(),time_rules.market_open(hours=1))
my_pipe= make_pipeline()
attach_pipeline(my_pipe,'my_pipeline')
def my_rebalance(context,data):
for security in context.portfolio.positions:
if security not in context.longs and security not in context.shorts and data.can_trade(security):
order_target_percent(security,0)
for security in context.shorts: <<<<==== error here
if data.can_trade(security):
order_target_percent(security,context.short_weight)
for security in context.longs: <<<<<<===== error here
if data.can_trade(security):
order_target_percent(security,context.long_weight)
def my_compute_weights(context):
if len(context.longs)==0:
long_weight=0
else:
long_weight= 0.5/len(context.longs)
if len(context.shorts)==0:
long_weight=0
else:
short_weight= -0.5/ len(context.shorts)
return(long_weight, short_weight)
def before_trading_starts(context,data):
context.output= pipeline_output('my_pipeline')
#long
context.longs= context.output[context.output['longs']].index.tolist()
#short
context.shorts= context.output[context.output['shorts']].index.tolist()
context.long_weight, context.short_weight= my_compute_weights(context)
def make_pipeline():
#U