Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help getting model to optimize

I cant figure out how to get the model to optimize correctly. It comes up with impossible results in the backtest. I have tried a bunch of different variable in the optimizer. I think I may need to recode how the securities are weighted, but Im not sure how. Please help.

2 responses

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)  

Thank you so much. I knew there was a simple solution. It runs great now.