Portfolio rebalancing algorithm
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:


$p_1(c_1 + x_1) = p_2(c_2 + x_2)$
$p_2(c_2 + x_2) = p_3(c_3 + x_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&gt;-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.