I have a pipeline in which is attached and producing output (as I've printed the context.output[context.output['longs']].index but when I try to create a weight, it errors out (error to come after code). The code I use looks like this:
def my_rebalance(context,data):
"""
Execute orders according to our schedule_function() timing.
"""
long_secs = context.output[context.output['longs']].index
port_weight = 1/len(long_secs)
for stock in long_secs:
if data.can_trade(stock) & (context.account.buying_power > 0):
order_target_percent(stock, port_weight)
for stock in context.portfolio.positions:
if data.can_trade(stock) & (stock not in long_secs):
order_target_percent(stock, 0)
record(leverage = context.account.leverage)
The problem I'm getting is at the 'port_weight' because the error is a "division by zero" error in which there shouldn't be a 0 in the first place. Anyone understand why len(long_secs) is returning 0 when print long_secs prints multiple equities to the console?