Back to Community
Does order_optimal_portfolio keep track of positions already open?

Hi everyone,
Let's say my algorithm executes order_optimal_portfolio (OOP) once a day and it opened a few positions on day 1 and I leave them open overnight. On the second and subsequent days, is OOP smart enough to know that there might be positions that were opened previously within the suggested target portfolio and only open the additional shares? E.g. On day 1, algo opened positions in S1 & S2 for 100 and -80 shares. On day 2, OOP suggests having 120 and -35 shares in S1 and S3, will OOP be smart to open only the additional 20 shares for S1?

Also, how do you close out/change the open positions that are not in the suggested target portfolio from OOP? In the above example, since S2 does not figure in the suggested portfolio from OOP, is there an automated way in QT to close out the position in S2? BTW, this will be easy to do for a 2 stock portfolio, but is there a QT solution if my portfolio contains 500 names?

Thanks

3 responses

Here's some insight on the 'order_optimal_portfolio' method....

'is OOP smart enough to know that there might be positions that were opened previously within the suggested target portfolio and only open the additional shares?' Yes, the 'order_optimal_portfolio' method looks at current positions AND current outstanding orders to determine the quantity of shares to purchase. It will only create orders for the delta between current positions + current open orders and the target position size.

'how do you close out/change the open positions that are not in the suggested target portfolio from OOP?' By default the 'order_optimal_portfolio' will close any positions which aren't included in the objective series. So, one doesn't need to do anything to close open positions. Simply do not include them in the series passed to the objective (ie the TargetWeights or MaximizeAlpha objectives). At times it may be easier/more desirable to explicitly close or freeze existing positions. A couple of ways to do this. Add constraints.

# constraint to explicitly close existing positions  
# this will also keep from ordering these securities  
close_do_not_open = CannotHold(list_of_assets)

# constraint to freeze existing positions  
# this will also keep from ordering these securities  
do_not_change = Frozen(list_of_assets)

Dan,
Just to confirm: Let's say my algo runs the objective function once everyday with the Q1500US securities and selects an optimal portfolio after adding constraints. On the first day, OOP selects 50 stocks and sends orders for all 50 securities listed in the OOP. The second and subsequent days, my algo runs OOP to find the optimal portfolio for that day and hence, the objective function will contain all Q1500US securities and OOP should find the new optimal portfolio. In this case:

Will OOP close any positions opened the previously, but not in the optimal portfolio list today? From your previous post, looks like the algo will have to close these manually using order_target_percent since objective function will include all the Q1500US stocks.

"Will OOP close any positions opened previously, but not in the optimal portfolio list today?" Yes, any currently held securities not selected by the 'order_optimal_portfolio' method will be closed. Or, more specifically, a market order will be placed in an attempt to close any existing positions (ie sell any long positions / buy any short positions). Existing open orders are also included in determining the quantity of shares to order.

"From your previous post, looks like the algo will have to close these manually using order_target_percent since objective function will include all the Q1500US stocks". No. One never needs to close any positions 'manually'. The correct way of thinking about 'order_optimal_portfolio' is that it first determines a 'desired state' of securities and associated weights which meet the objective and all the constraints. Next, it sums the current positions and any currently outstanding orders to get a 'current state' of securities and associated weights. Finally, it simply places all the orders necessary to move the portfolio from the 'current state' to the 'desired state'. If a currently held security is not in the 'desired state' then the method will attempt to close the position by placing an order equal to the current shares less any currently outstanding ordered shares.

One important point. Just because a security is in the set of securities given to the objective function (say all Q1500US securities), that alone doesn't require the optimize method to include all those securities in the optimized 'desired state'. An 'optimal' solution meeting the objective and all constraints may be only a sub-set of the Q1500US. It could also actually be a larger set and include some securities NOT in Q1500US if they were specified somehow in a constraint. The point to remember is that the optimize function simply determines a 'desired state' of securities. Any securities not in that desired state are closed.