Repository navigation
Closed
Labels
A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.Something wrong, confusing or sub-standard in the software, docs, or user experience.roi
Description
I am observing some results that I can't explain in the roi's TWR calculation.
With the following example ledger, the roi output looks plausible:
commodity 1,000.00 CHF
P 2024-11-08 B 1 CHF
2024-11-26 buying B
assets -20000 CHF
investments:B 19986 B @@ 20000 CHF
P 2024-12-31 B 1.0018 CHF
$ hledger roi -Q --value then,CHF --inv '"investments:"' --pnl STR
+---++------------+------------++---------------+---------------+---------------+-----------++-------++------------+----------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR || TWR/period | TWR/year |
+===++============+============++===============+===============+===============+===========++=======++============+==========+
| 1 || 2024-10-01 | 2024-12-31 || 0 | 20,000.00 CHF | 20,021.97 CHF | 21.97 CHF || 1.12% || 0.18% | 0.72% |
+---++------------+------------++---------------+---------------+---------------+-----------++-------++------------+----------+
If I now add a small other investment that doesn't move in price at all, TWR goes to almost -100%?
commodity 1,000.00 CHF
P 2024-11-07 A 1 CHF
P 2024-11-08 B 1 CHF
2024-11-07 buying A
assets -20 CHF
investments:A 20 A @@ 20 CHF
2024-11-26 buying B
assets -20000 CHF
investments:B 19986 B @@ 20000 CHF
P 2024-12-31 B 1.0018 CHF
$ hledger -f test.ledger roi -Q --value then,CHF --inv '"investments:"' --pnl STR
+---++------------+------------++---------------+---------------+---------------+-----------++-------++------------+----------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR || TWR/period | TWR/year |
+===++============+============++===============+===============+===============+===========++=======++============+==========+
| 1 || 2024-10-01 | 2024-12-31 || 0 | 20,020.00 CHF | 20,041.97 CHF | 21.97 CHF || 1.12% || -69.95% | -99.16% |
+---++------------+------------++---------------+---------------+---------------+-----------++-------++------------+----------
That doesn't seem right?
I compiled hledger from git (8b027a4).
Metadata
Metadata
Assignees
Labels
A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.Something wrong, confusing or sub-standard in the software, docs, or user experience.roi
Activity
simonmichael commentedon May 24, 2025
adept commentedon May 25, 2025
@TPolzer this should be fixed by #2392
@simonmichael yet another "computing intra-day valuations are hard" bug :( I hope that the day will come when finally all of them are properly squashed
lib: roi: fix a bug with PnL attributed to the wrong period (fixes hl…
TPolzer commentedon May 25, 2025
I've tried 306e942 and it fixes the initial example, but I think something is still off. Note the difference between these two cases (again, adding just a purchase of a fixed value investment B):
adept commentedon May 25, 2025
I dont see any issues with this at a first glance
In the first case:
(0.972/1 - 1)*100 = -2.8%Chaining it all up:
((1+0)*(1+0.03)*(1+0)*(1-0.028)*(1+0)-1) = 0.00116, or multiplying by 100 and rounding up to two digits,0.12%for 1/4 year, which gives 0.46% annualized.In the second case:
(1.972/2-1)*100 = -1.4%Chaining it all up:
((1+0)*(1+0.03)*(1+0)*(1+0)*(1-0.014)*(1+0)-1) = 0.01558, or multiplying by 100 and rounding up to two digits,1.56%for 1/4 year, which gives 6.34% annualized.The difference between two cases is that both A and B contribute to the value of your investment, and in the second case the underperformance of A was compensated somewhat by the stable B, which leads to better TWR.
TPolzer commentedon May 25, 2025
Hm, it's entirely possible that my intuition of how TWR "should" behave is off... In that case, thank you for the quick fix and explanation.
simonmichael commentedon May 27, 2025
Thank you both!
lib: roi: fix a bug with PnL attributed to the wrong period (fixes #2391