Share your insights or perspective on the financial matter presented in the input.

Auto balancing portfolio through new purchases
Finding the "optimal" solution (and even defining what optimal is) would probably take a lot of searching of all the possible combinations of stocks you could buy, given that you can't buy fractional shares. 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. If you have to pay a transaction fee for each kind of stock you purchase you might want to calculate this in larger lot sizes or just avoid making really small purchases.