I'm attempting to implement something similar to Joel Greenblatt's Magic Formula with this algorithm. The strategy should not involve any leverage and every time I rebalance (which is once a year), I first sell off all my current positions and then buy a new set of 50 positions based on the formula for value.
The strange thing is that the number of positions creeps up beyond 50 over time. I'm not sure why, but I can only assume that I'm not fully selling off all my positions each time I attempt to. Is there something I'm doing wrong in my code? Are the orders being cancelled for some reason?