The code must be data based: arrays / objects of data.
Rebalancing is when you define a portfolio of x% of stock A, y% of stock B, and z% of stock C.
After a while, stocks prices change, and we need to sell some stocks and buy others, to rebalance.
We have 1000$.
Stock A - cost 10$, we want our portolio to have 20% of stock A - 20 stocks total value 200$.
Stock B - cost 15$, we want our portolio to have 30% of stock B - 20 stocks total value 300$.
Stock C - cost 50$, we want our portolio to have 50% of stock C - 10 stocks total value 500$.
a year has passed, now the prices are:
Stock A - 5$, worth 100$ (5*20).
Stock B - 15$, still worth 300$.
Stock C - 40$, worth 400$ (10*40).
Total worth: 800$.
Now, stock A is 12.5% of the portfolio, so we need to buy more of it to make it 20%.
stock B is 37.5% of the portfolio, so we need to sell some of it to make it 30%.
stock C is 50% which is the goal, so we need to do nothing.
Then another year will pass, and the same calculation again...