# Portfolio rebalancing algorithm

Quite possibly this problem is already solved and has it's own name, but I was unable to find any directions.

So, I have a portfolio with some items in it:

| Name   | Price | Amount | Share |
|--------|-------|--------|-------|
| Item 1 | 200   | 1      | 25%   |
| Item 2 | 100   | 2      | 25%   |
| Item 3 | 50    | 4      | 25%   |
| Item 4 | 25    | 8      | 25%   |


After some time, prices changed

| Name   | Price | Amount | Share  |
|--------|-------|--------|--------|
| Item 1 | 220   | 1      | 27.36% |
| Item 2 | 130   | 2      | 32.34% |
| Item 3 | 17    | 4      | 8.46%  |
| Item 4 | 32    | 8      | 31.84% |


but I still want all items to have 25% share.

Assuming amount can be decimal, how do I find the optimal way of rebalancing this portfolio back to 25% shares? How to determine how many of each item to sell so we won't have to buy it back again (sell everything and spend 25% on each item is not an option)?

What if I have extra $100 to spend while rebalancing? Upd: what I mean by "optimal" is to keep the amount of transactions as less as possible, assuming each transaction costs something. • Welcome to CS.SE! I don't think the question is well-specified yet. What determines what is "optimal"? What are you trying to minimize? Why is "selling everything and buy back to 25%" not an option? What restriction does that violate? If it's not an option, there must be some requirement it violates; please make sure you've told us that requirement, and make sure you tell us all the requirements/constraints. I don't want to give an answer and then have you say that it isn't an option, either. – D.W. Sep 3 '17 at 16:48 • @D.W. described what I meant by 'optimal'. Selling everything is not an option because it adds unnecessary transactions – eawer Sep 3 '17 at 20:10 • Rather than using "Upd:", please just edit the question to be what it should have been from the start, so that it reads well for someone who encounters the question for the first time. See cs.meta.stackexchange.com/q/657/755. Also, what do you mean by "amount of transactions"? Are you counting the dollar amount? Or the number of shares sold? Or the number of different stocks that have at least one share sold? Or number sold + number bought? Can you be more precise? What is allowed to be in a single transaction? How is the cost of each transaction determined? – D.W. Sep 4 '17 at 3:57 • @D.W. Each operation of selling or buying is considered as a transaction. If you sell 0.5 of 'Item 1' and buy 2 of 'Item 3' - that will make 2 transactions. You can't sell and buy, or sell two different items within one transaction - it will be two separate ones. Each transaction has fee, which equals to 1% of transactions' value (amount * price) – eawer Sep 4 '17 at 8:06 • Thanks for explaining. Please edit the question to include all information in the question, so the question stands on its own and people don't need to read the comments to understand what you are asking. – D.W. Sep 4 '17 at 21:42 ## 3 Answers Your problem specification is incomplete. What do you mean by optimal? Do you want to, say, minimize transactions costs? Do you want to make sure you do not cross the spread? The original portfolio was worth \$800. The new portfolio is worth \$804. Assuming your actions do not change the market price, and assuming no transactions costs, that is a given. You had \$200 allocated to each security. Now, you want \$201 allocated to each security: | Name | Price | Amount | Notional | |--------|-------|--------|----------| | Item 1 | 220 | 1 |$220    |
| Item 2 | 130   | 2      |  $260 | | Item 3 | 17 | 4 |$68    |
| Item 4 | 32    | 8      |  $256 | For every security whose notional is above the desired amount, sell that many units: • Item 1: Sell 19/220 = 0.0863636363636364 units • Item 2: Sell 59/130 = 0.453846153846154 units • Item 4: Sell 55/32 = 1.71875 units With the money you made from those sales, i.e. \$19 + \$59 + \$55 = \$133, buy 7.82352941176471 units of Item 3. Now, you will have \$201 worth of each security, exactly 25% of the new portfolio size of of \$804. The only time the extra \$100 would come in to play is if you are trying to minimize your transaction costs and/or if you are worried about not being able to trade at the spot etc.

With the extra \$100, you want to allocate \$226 to each security. So, buy 6/220 units of Item 1, buy 158/17 units of Item 3, and sell 34/130 units of Item 2 and 30/32 units of Item 4.

However, as far as I can see, you have not specified an optimization problem.

Let $c_i$ denote current units of security $i$ in your portfolio. Let's say you pay a percentage commission of $k$ per transaction. The amount you buy of security $i$ is denoted $x_i$ (negative $x_i$ mean selling). Your objective function is to minimize $\sum_i k p_i |x_i|$.

Since $k$ is a constant, this is equivalent to minimizing $\sum_i p_i|x_i|$ where $|x_i|$ is the absolute value of $x_i$.

This objective is subject to the portfolio allocation constraints:

1. $p_1(c_1 + x_1) = p_2(c_2 + x_2)$

2. $p_2(c_2 + x_2) = p_3(c_3 + x_3)$

3. $p_3(c_3 + x_3) = p_4(c_4 + x_4)$.

Further, the new value of your portfolio, $\sum_ip_i(c_i + x_i)$, plus the amount you spend, $k\sum_ip_i|x_i|$, must be affordable, i.e, must be less than or equal to $B + \sum_ip_ic_i$ which is the current value of your portfolio and the additional balance you have available.

This, of course, ignores borrowing money to finance the rebalancing which might be worthwhile depending on borrowing costs.

I am assuming there is going to be no short selling in a portfolio rebalancing, so $x_i>-c_i$ must also hold.

You might be able to simplify the problem, and possibly save some money, by first spending $B$ on underbought items.

You might be able to drop the absolute values by first deciding which stocks are overbought.

I did not prove the last two statements.

When I get a chance, I will post the solution to the applicable linear program.

• yep, you are right, I meant minimize transactions cost (assuming each transaction has a fee) – eawer Sep 3 '17 at 20:04
• @Shtirlits Is that flat fee, a percentage commission, expected loss due to price impact etc? You need to specify that to get to an optimization problem. – Sinan Ünür Sep 3 '17 at 20:24
• It is a percentage fee. Let's assume it is 1% (if that matters) – eawer Sep 3 '17 at 21:18
• How can you buy 7.82352941176471 units of something? I mean, how do you buy half a product or a quarter of a product?. Is that allowed? – rotia Sep 3 '17 at 22:02
• @rotia "Assuming amount can be decimal." – Sinan Ünür Sep 4 '17 at 0:40

Assuming you are not allowed to buy anything, after selling each of your items will be worth at most the current value of the worst item (of smallest share). It is thus best to keep the worst item as it is, and sell amounts of each other item so that its total worth is the same as the total worth of the worst item. In your example, you would like to sell an amount of each item so that the total worth is $17 \times 4 = 68$.

Suppose now that you are allowed to use $B$ dollars for extra buys (possibly $B=0$, which is not the same as the preceding case). Let's suppose that you sell $x_i$ units worth of item $i$ (the amount possibly negative, in which case you're buying), of which you currently have $c_i$ units, and which has price $p_i$. Then you have the following constraints:

• You cannot have a negative quantity: $x_i \leq c_i$.
• The total worth of each item is the same: $(c_i - x_i)p_i = (c_j - x_j)p_j$ for all $i,j$.
• The total expenditure is at most the budget: $-\sum_i x_i p_i \leq B$.
• The goal (my guess, which turned out to be wrong) is to maximize the total worth of the remaining portfolio, which is $\sum_i (c_i - x_i) p_i$.

(As Sinan Ünür notes in the comments, my objective function is meaningless, since it amounts to the equation $-\sum_i x_ip_i = B$.)

This is a linear program, which you can solve using any of the well-known algorithms. It is also possible that you can find the solution explicitly by going over all basic feasible solutions (the case $B=0$ sounds especially promising), but I'll leave that to you.

• Assuming prices are given, $\sum_i (c_i - x_i) p_i\equiv B+ \sum_i c_i p_i$ must hold. – Sinan Ünür Sep 3 '17 at 18:40
• @SinanÜnür You're probably right. Your equation is equivalent to $-\sum_i x_ip_i = B$, whereas I only had the inequality. But my formulation is probably correct as well. – Yuval Filmus Sep 3 '17 at 21:01
• Maybe I wasn't obvious enough: In the absence of transactions costs, the value of the portfolio is constant, so your objective is a constant function. That is, you haven't specified an optimization problem either. – Sinan Ünür Sep 5 '17 at 12:28
• Right, I see what you mean. – Yuval Filmus Sep 5 '17 at 12:33

I think the optimization problem is to minimize the portfolio variance from the target allocation. I'm going to assume we are trading equities or etfs in which case the trade quantity can only be integers. (Mutual funds can have fractional shares but we can get into that later).

Min Sumofall Ending allocation% - target allocation %

Each security would be traded 0 or 1 time. The extra 100$should actually be a 5th security called cash with a target allocation of 0%. From the example, the transaction cost would be the price*quantity*1%. Thats not how it works in the real world though so let's throw that out. The transaction cost is going to be a flat amount per trade, lets say 5$ because that is fairly common. That 5\$will be the same regardless of the price. So if you ran all the combinations of trades you can make there is going to come a point where that 5$ per trade is going to reduce the total variance by less than making a trade will. For his example, you probably wouldn't make very many trades. Especially for security 1 or 2, because the prices are really high and a single trade would greatly increase the total portfolio variance.

I'm getting tired so I'll check back in later.. but the answer to the algorithm is to not trade security 1 or 2, sell security 4 and 5(cash), and buy security 3.

Doh... I think I may have it wrong, the optimization problem is really maximize the portfolio value given a limit to the total portfolio variance. Or, some sort of ratio of portfolio value to variance. If you simply minimized variance then it would make trades in the overweight securities just so that you would incur trading costs and reduce your overweight in that security.

Edit: If you want a real brain buster try to figure it out when you add in all the real world considerations and constraints.... some securities have 0 commissions, some 5, some 20\$. Some incur 2% fee if sold within 60 days. Some trade in only whole quantities(equities) and some trade in fractional quantities(mutual funds). Most people have more than 1 account so that adds to the complexity. Each security can have vastly different costs based on which account you put it in because of all the tax rules.

• I'm getting tired so I'll check back in later - perhaps it's better to write an answer once you have enough time for it. – Yuval Filmus Sep 7 '17 at 10:51