I realize order_target_percent has been the subject of multiple past posts, so I though someone would have some insight into the following problem:
1.My algo buys from a 50 stock ranking list with weights calculated individually for each of the 50.
2. I recalculate the 50 stock ranking list and rebalance every month with the following logic:
a.If not still on list then sell all shares.
b.If still on list then rebalance using current monthly calculated weights. the code lines are below(context.weights is my ranking list of 50
stocks along with their respective weights.
for security in context.portfolio.positions:
if security not in context.weights:
order_target(security, 0)
for security, weight in context.weights.iteritems():
if data.can_trade(security):
order_target_percent(security, weight)
The first month of the simulation works fine. the portofolio is filled with the 50 stocks from the ranking list with the appropriate weights. The second month(i.e the first rebalancing month) is where things get confusing. If I look at the transactions in the full backtest I see several things that don't make sense:
Example #1: stock A is bought say at 500 shares in the first month, then in the 2nd month it is still in the ranking list thus it undergoes rebalancing. However in the backtest transactions for month 2, it shows all 500 shares being sold in one transaction and then 200 being bought in a separate transaction. I ran Blue Seahawk's track_orders() and it shows the initial 500 share buy in month one, then it shows the 200 buy in month two but no 500 share sell in month 2 as shown in the transactions list of the full backtest.
Example #2. The same phenomenon makes certain stocks appear as if the are being shorted. In other words, the stock is bought in the first month at 400 shares, then in the second month all 400 shares are sold and an additional 10 shares are sold thus appearing as if a short has occurred. However if I isolate this particular stock using Blue Seahawk's track_orders, it shows the original 400 shares buy in month 1 then a sell of 10 shares in month 2 for a net position of 390 shares and these numbers jive perfectly with the weights for month one and for month 2.
So, it seems that Blue Seahawk's track_orders confirm the code is running as I expected and the shares and weights all make sense, however, the fact that the transactions and positions listed in the full backtest screen don't make sense makes me question the validity of the full backtest results.
Can anyone shed some light on this?