Open alensiljak opened 6 years ago
The easiest implementation of the first-in, first-out (FIFO) calculation using virtual lots only (no real lots needed in GnuCash book) is to calculate backwards:
In the chart above:
Note that this calculation has to be done per each holding account. It also does not take into account any stock splits or repricings.
There are only two cases - the one above where we split the last lot, and this one, where we have full lots before reaching the one to be split.
In this, more complex example, we will take as many as full lots as we have (slot E, Lot 3 and/or more), before reaching Lot 2, which gets split. Then we calculate the value for the remaining part of Lot 2 and add to the other, previously calculated ones.
Buy 100 @ 10 = €-1000 Sell 100 @ 15 = €1500 Calculate CG, find matching transactions, if any. €500
Buy 100 @ 10 = €-1000 Sell 50 @ 20 = €1000 Calculate realized CG = €1000 Calculate unrealized CG = €1000
Buy 100 @ 10 = €-1000 Sell 50 @ 20 = €1000 Calculate realized CG = €1000 Calculate unrealized CG = €1000 Sell 50 @ 15 = €750 Calculate realized CG = €250. Unrealized = €0.
As the Lots feature in GnuCash modifies the original records, and is therefore risky to use multiple times, the same concept could be recalculated on the fly. This would create virtual lots, used just for the purpose of calculating the capital gains on the sold stock, and average price for the remaining stock.
The calculation would be based on the current state in the account. Meaning that, if there are any changes in the original transactions, the numbers would be recalculated based on the new state. This is not the case with Lots in GnuCash, where transactions get duplicated when Scrubbing is used, effectively making it extremely difficult to track the changes in the account visually.
Instructions:
Note: this calculation already exists in GnuCash Lots view. It is shown in Splits Free window.
The Virtual Lots model should be the base for any calculation on the remaining portion of the stock.