Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trouble with Optimize TargetWeights

So when I give the optimize target weights a pandas series of assets, I would expect it to only order the assets with non-zero weights.

In the attached algorithm, I pass target weights 100 longs and 100 shorts and I set all of the remaining assets to zero. I noticed that the first call to order_optimal_portfolio did what I expected, however, each call after that actually increases the number of open positions instead of only staying at 200. The log.info on line 103 shows that the pandas series passed to the target weights has 100 long, 100 short and all others zero, but the number of open positions grows from 200 to over 1000 within a handful of calls.

Am I using the optimize target weights correctly or do I have a misunderstanding of how it works?

Any help is appreciated.
Thanks.

1 response

Some tools that might help to see what's going on by analyzing the log output.

That's kind of a smoke test as weights in it are all 100% long and 100% short (1 and -1).
More often the sum of longs for example would be 1 or .5 vs 100 above.

It's true that inputs assigned as 0 are at least not always set that way by opt.
Compare 'alpha' and 'new' columns. Opt is setting the position to 'new'. The target_weights input winds up as "alpha" here.
Maybe it could affect the more usual cases sometimes too.

Changes  
   alpha      old         new       pct  
  1.0000   -0.0147 =>   0.0150    -102.1%    BNS  
  0.0000    0.0120 =>   0.0060      50.0%    AIG  
  0.0000   -0.0136 =>  -0.0063      46.2%    BID  
  0.0000    0.0134 =>   0.0060      44.9%    AIRM