Try "rebalance" this way:
def rebalance(context, data):
long_weight = .5/len(context.longs) if len(context.longs) > 0 else 0
short_weight = -.5/len(context.shorts) if len(context.shorts) > 0 else 0
weights = {}
for sec in context.portfolio.positions:
if sec not in context.longs and sec not in context.shorts: weights[sec] = 0
for sec in context.longs: weights[sec] = long_weight
for sec in context.shorts: weights[sec] = short_weight
objective = opt.TargetWeights(weights)
constraints = []
constraints.append(opt.MaxGrossExposure(MAX_GROSS_EXPOSURE))
constraints.append(opt.PositionConcentration.with_equal_bounds(-MAX_POSITION_CONCENTRATION, MAX_POSITION_CONCENTRATION))
constraints.append(opt.DollarNeutral(tolerance = MAX_NET_DOLLAR_EXPOSURE))
order_optimal_portfolio(objective = objective, constraints = constraints)