# Auto balancing portfolio through new purchases

I'd like to have an investment portfolio with a few items, each having a target proportion (e.g. I want to have stocks of Apple and the total value of those should be 25% of my portfolio's total worth). I also would like to invest periodically. My question is: Is there some sort of formula/simulation that I can use to figure out each period how much to invest in each stock so that everything stays balanced? I tried working it out with an LP, but couldn't wrap my head around it.

A little example to make things a bit more clear:

Say that there's currently 3 stocks in my portfolio with the following info:

``````Stock name                | S1     S2     S3
Current # of stock owned  | 10     8      9
Current price             | 10     9      11
Current size of portfolio | 36.9%  26.6%  36.5%
Target size in portfolio  | 40%    20%    40%
``````

If I now want to invest 50 euros (or dollars, or what have you), is there a way to repeatedly determine how much of which stock to buy so that after buying those things, the portfolio is more balanced than before? I'd like to find a way to solve this problem in general, not just solve this particular example.

But I'd guess that a simple "greedy" algorithm should get you close enough. For any given portfolio state, look at which stock is furthest below the target size - e.g. in your example, `S3` is `3.5%` away whereas `S1` is only `3.1%` away and `S2` is over-sized. Then decided to buy one stock of `S3`, recalculate the current proportions, and repeat until you can't buy more stocks because you've invested all the money.