Instead of the original code
for stock in context.long_list:
if stock in context.long_list:
order_target_percent(stock, long_weight)
for stock in context.short_list:
if stock in context.short_list:
order_target_percent(stock, short_weight)
replace it with
for stock in context.long_list.index:
order_target_percent(stock, long_weight)
for stock in context.short_list.index:
order_target_percent(stock, short_weight)
'context.long_list' and 'context.short_list' are both pandas dataframes which are indexed by the security. All you want is an iterable of the securities so use 'context.short_list.index' in the for loop. Also no need to check if the stock is in the dataframe and can't really do it like that anyway.