Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Optimize API - Maintain Existing Shares When Placing A New Trade

Hello.
After a few days grinding on this issue I'm stumped.
Basic question, how do I maintain an existing position's size(NUMBER of shares when the trade was originally put on) using order_optimal_portfolio()?
Put another way, if I already have some trades on and I want to place more trades(different stocks) at a later point in time, how do I leave the existing trades alone(unchanged)?

Example:

-----Go Long XYZ stock @ 25% of portfolio value on today's open. Let's say that is 100 shares of XYZ stock.
Psuedo code on today's open.
objective = opt.TargetWeights({'XYZ': 0.25})
constraints=[]
order_optimal_portfolio(objective, constraints)

-----Go Long ABC stock @ 25% of portfolio value on tomorrow's close AND hold the XYZ position at it's ORIGINAL size(100 shares).
Psuedo code on tomorrow's close.
objective = opt.TargetWeights({'ABC': 0.25, 'XYZ': 0.25}### 'XYZ': 0.25 isn't what I want
constraints=[]
order_optimal_portfolio(objective, constraints)

Now, if you hold the XYZ position for a month and make several other trades in between opening and closing the XYZ trade, you wind up with more XYZ trades than intended if the original weight is used. That makes sense of course, because as your balance changes and the price of XYZ changes, 25% of your balance will be a different number of shares. opt.TargetWeights re-balances that position by some number of shares every time you hit order_optimal_portfolio.

Here's the big rub. Over a two plus year back test, if your algo trades frequently and trades >100 stocks, these micro trades add up.
In my case, this behavior adds >10,000 trades that my contest algos don't really want to make. This results in all kinds of ghost trades, false turnover rates and a dilution of many of my performance metrics.

I would really appreciate any guidance on this, because it's a really big issue for me and I would guess for others too.

Thank you

Bryan

2 responses

Attached is a six month back test that illustrates what I am talking about.
It makes four REAL round trip trades(hard-coded in initialize). Four trade entries on the first two days. And four trade exits on the last two days.
However, everyday in between it hits order_optimal_portfolio() with the same target weights for the same four stocks.
When a tear sheet is ran on this back test 288 round trips are reported. We know the reason why.
If my intention was to only make those four trades I wouldn't be hitting order_optimal_portfolio() every day. In practice though, the algo would be making a lot of other trades(different stocks) and need to refresh the original four stocks with some weight in the dictionary it passes to objective = opt.TargetWeights.
Ideally, the algo would be able to use order_optimal_portfolio() in between the four entries/exits without adjusting the position sizes at all.

Sorry for the wordy posts.

Thank you

@Bryan,
Not sure how to solve your problem, yet here is some collateral that might lead to an answer.
https://www.quantopian.com/posts/optimize-api-frozen-constraint-1
https://www.quantopian.com/posts/does-order-optimal-portfolio-keep-track-of-positions-already-open
alan