Suppose you have a portfolio with 3 funds (A, B, C) you have 30.000€ in each one but you have done 3 buys in each one (10.000€ each) and each one of the 9 positions (3×3) have diferent gains.
enter image description here
Knowing that if you sell from a fund it will sell first the first position you bought i.e. the oldest (FIFO). How can you determine which ones you should sell to generate 5.000 € while keeping the gains to the lowest to reduce taxes?
It seems like the backpack problem but weirder.. I need to implement an algorithm to solve this in the general case
I’ve tried an algorithm that calculate a score as gains/value and for the newer positions the sum of the previous gains over the sum of the previous values to see which one is better to sell for each unit.
Next I “sell” the position with minimun score and recalculate everything without the position sold and all the older ones of the same fund.
And repeat until I reached the aimed value to generate.
However this sometimes doesnt find the optimal combination.
Rodrigo Lopez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.