hledger/hledger/test/roi.test
Simon Michael e9cd1df048 dev: tests: convert remaining format 1 tests to format 3 (or 2)
Mostly done with an unreleased version of shelltestrunner, plus a few manual fixups.
2023-02-16 11:55:15 -10:00

356 lines
12 KiB
Plaintext

# 1. investment that does not grow has no return
<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=======+=======+
| 1 || 2017-01-01 | 2017-12-31 || 0 | $200 | $200 | 0 || 0.00% | 0.00% |
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
>= 0
# 2. Growth by 12% from single investment transaction should show 12% IRR and 12% TWR
<
2017-01-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $112
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++========+========+
| 1 || 2017-01-01 | 2017-12-31 || 0 | $100 | $112 | $12 || 12.00% | 12.00% |
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
>= 0
# 3. Same as (2), but grow by 1% per month
<
2017-01-01 investment
assets:cash -$100
investment
2017-01-31 investment valuation
investment = $101
pnl
2017-02-28 investment valuation
investment = $102
pnl
2017-03-31 investment valuation
investment = $103
pnl
2017-04-30 investment valuation
investment = $104
pnl
2017-05-31 investment valuation
investment = $105
pnl
2017-06-30 investment valuation
investment = $106
pnl
2017-07-31 investment valuation
investment = $107
pnl
2017-08-31 investment valuation
investment = $108
pnl
2017-09-30 investment valuation
investment = $109
pnl
2017-10-31 investment valuation
investment = $110
pnl
2017-11-30 investment valuation
investment = $111
pnl
2017-12-31 investment valuation
investment = $112
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++========+========+
| 1 || 2017-01-01 | 2017-12-31 || 0 | $100 | $112 | $12 || 12.00% | 12.00% |
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
>= 0
# 4. When $100 invested over 12 month + $100 invested over 6 month yield $220, that's 10% TWR, but 12.73% IRR
<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $220
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++========+========+
| 1 || 2017-01-01 | 2017-12-31 || 0 | $200 | $220 | $20 || 12.72% | 10.00% |
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
>= 0
# 5. When $100 invested over 12 month + $100 invested over 6 month yield $20, that's -90% TWR, but -95.73% IRR
<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $20
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
+---++------------+------------++---------------+----------+-------------+-------++---------+---------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=======++=========+=========+
| 1 || 2017-01-01 | 2017-12-31 || 0 | $200 | $20 | $-180 || -95.73% | -90.00% |
+---++------------+------------++---------------+----------+-------------+-------++---------+---------+
>= 0
# 6. Check that deposits/withdrawals and profit/loss are reflected in the right periods
<
2017-01-01 investment
assets:cash -$100
investment
2017-06-30 investment valuation
investment = $110
pnl
2017-07-01 investment
assets:cash -$100
investment
2017-10-01 withdrawal
assets:cash $50
investment
2017-12-31 investment valuation
investment = $155
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Q
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=========+=========+
| 1 || 2017-01-01 | 2017-03-31 || 0 | $100 | $100 | 0 || 0.00% | 0.00% |
| 2 || 2017-04-01 | 2017-06-30 || $100 | 0 | $110 | $10 || 46.56% | 46.56% |
| 3 || 2017-07-01 | 2017-09-30 || $110 | $100 | $210 | 0 || 0.00% | 0.00% |
| 4 || 2017-10-01 | 2017-12-31 || $210 | $-50 | $155 | $-5 || -11.83% | -11.82% |
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
>= 0
# 7. Check that reporting for the part of the total history of investment works
<
2017-01-01 investment
assets:cash -$100
investment
2017-06-30 investment valuation
investment = $110
pnl
2017-07-01 investment
assets:cash -$100
investment
2017-10-01 withdrawal
assets:cash $50
investment
2017-12-31 investment valuation
investment = $155
pnl
$ hledger -f- roi --inv investment --pnl pnl -b 2017-06 -e 2018
+---++------------+------------++---------------+----------+-------------+-----++-------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=======+========+
| 1 || 2017-06-01 | 2017-12-31 || $100 | $50 | $155 | $5 || 5.24% | 11.45% |
+---++------------+------------++---------------+----------+-------------+-----++-------+--------+
>= 0
# 8. Dont fail where there is no rate-of-return
<
2019/11/01 Example
Assets:Checking 1
Income:Salary -1
$ hledger -f- roi -p 2019-11
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=======+=======+
| 1 || 2019-11-01 | 2019-11-30 || 0 | 0 | 0 | 0 || 0.00% | 0.00% |
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
>= 0
# 9. Fail with a nice error message when commodity can't be valued
<
2019/11/01 Example
Assets:Checking -100 A
Investment 10 B
2019/11/02 Example
Investment -10 B @@ 100 A
Assets:Checking 101 A
Unrealized PnL
$ hledger -f- roi -p 2019-11 --inv Investment --pnl PnL
>2
hledger: Error: Amounts could not be converted to a single cost basis: ["10 B","-10 B @@ 100 A"]
Consider using --value to force all costs to be in a single commodity.
For example, "--cost --value=end,<commodity> --infer-market-prices", where commodity is the one that was used to pay for the investment.
>= 1
# 10. Forcing valuation via --value
<
2019/11/01 Example
Assets:Checking -100 A
Investment 10 B
2019/11/02 Example
Investment -10 B @@ 100 A
Assets:Checking 101 A
Unrealized PnL
$ hledger -f- roi -p 2019-11 --inv Investment --pnl PnL --cost --value=then,A --infer-market-prices
+---++------------+------------++---------------+----------+-------------+-----++----------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++==========+========+
| 1 || 2019-11-01 | 2019-11-30 || 0 | -1 A | 0 | 1 A || 3678.34% | 12.87% |
+---++------------+------------++---------------+----------+-------------+-----++----------+--------+
>= 0
# 11. Use "then" prices. 10000/76.20 = 131.23, 11000/73.88=148.89
<
P 2020-12-01 $ 76.20
P 2021-01-01 $ 73.88
2020-12-02 invest
assets:investment 10000
assets
2021-01-02 get profit
assets:investment =11000
income:investment
$ hledger -f - roi --inv assets:investment --pnl income:investment --value=then,'$'
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=========+=========+
| 1 || 2020-12-02 | 2021-01-02 || 0 | $131 | $149 | $18 || 321.99% | 321.81% |
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
>= 0
# 12. Use "end" prices. 10000/73.88=135.35
<
P 2020-12-01 $ 76.20
P 2021-01-01 $ 73.88
2020-12-02 invest
assets:investment 10000
assets
2021-01-02 get profit
assets:investment =11000
income:investment
$ hledger -f - roi --inv assets:investment --pnl income:investment --value=end,'$'
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=====++=========+=========+
| 1 || 2020-12-02 | 2021-01-02 || 0 | $135 | $149 | $14 || 196.58% | 196.58% |
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
>= 0
# 13. Several PnL transactions on a single date are aggregated together
<
2010-01-01 A1
savingA 100.00 €
checking
2011-12-31 A2
savingA 0.00 €
dividendA -8.00 €
checking
2012-03-29 A3
savingA -100.00 €
checking
# ========================================
2010-01-01 B1
savingB 100.00 €
checking
2011-12-31 B2
savingB 0.00 €
dividendB -8.00 €
checking
2012-03-29 B3
saving -100.00 €
checking
$ hledger -f - roi --inv saving --pnl dividend
+---++------------+------------++---------------+----------+-------------+---------++-------+-------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+=========++=======+=======+
| 1 || 2010-01-01 | 2012-03-29 || 0 | -16.00 € | 0 | 16.00 € || 3.52% | 3.49% |
+---++------------+------------++---------------+----------+-------------+---------++-------+-------+
>= 0
# 14. Should support begin date before first transaction where first transaction has pnl
<
P 2022-07-31 A € 1
2022-08-01 Purchase
checking € -101
stocks 100 A @ € 1
expenses € 1
P 2022-08-02 A € 2
$ hledger -f - roi --inv stocks --pnl expenses --value=then,€ -Y
+---++------------+------------++---------------+----------+-------------+------++---------+--------+
| || Begin | End || Value (begin) | Cashflow | Value (end) | PnL || IRR | TWR |
+===++============+============++===============+==========+=============+======++=========+========+
| 1 || 2022-01-01 | 2022-12-31 || 0 | € 101 | € 200 | € 99 || 410.31% | 98.02% |
+---++------------+------------++---------------+----------+-------------+------++---------+--------+
>= 0