Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TargetWeights short orders going long for no reason

Hello,

I'm trying to debug a target weight issue where quite a few of my shorts are going long and a couple of my longs are going short.

I'm buying and selling off data from a csv, and it's being read into and creating a dataframe for longs and shorts along with weighting for each stock. As I debug I get the list of longs and shorts with it's weighting (longs dataframe is separate from shorts dataframe) just before it goes into the opt.TargetWeights() and it looks as it should be in there. But once the orders actually go through most of the time there are always a 2-4 positions where the longs should be shorts.

I'm quite confused on this and the only thing I can come up with is that it is done so as a means to coming up with a solution for the portfolio made up with the stocks which I give it. I have constraints set as well which are required to meet the characteristics.

Also is there a way to print out the output of the opt.TargetWeights() function?

Help would be much appreciated!