hledger/hledger/test/roi.test

348 lines
12 KiB
Plaintext

# 1. investment that does not grow has no return
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
<<<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
>>>
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
<<<
2017-01-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $112
pnl
>>>
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
<<<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $220
pnl
>>>
+---++------------+------------++---------------+----------+-------------+-----++--------+--------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Y
<<<
2017-01-01 investment
assets:cash -$100
investment
2017-06-01 investment
assets:cash -$100
investment
2017-12-31 investment valuation
investment = $20
pnl
>>>
+---++------------+------------++---------------+----------+-------------+-------++---------+---------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017 -e 2018 -Q
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || 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
hledger -f- roi --inv investment --pnl pnl -b 2017-06 -e 2018
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++-------+--------+
| || 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
hledger -f- roi -p 2019-11
<<<
2019/11/01 Example
Assets:Checking 1
Income:Salary -1
>>>
+---++------------+------------++---------------+----------+-------------+-----++-------+-------+
| || 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
hledger -f- roi -p 2019-11 --inv Investment --pnl PnL
<<<
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
>>>2
hledger: 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
hledger -f- roi -p 2019-11 --inv Investment --pnl PnL --cost --value=then,A --infer-market-prices
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++----------+--------+
| || 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
hledger -f - roi --inv assets:investment --pnl income:investment --value=then,'$'
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || 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
hledger -f - roi --inv assets:investment --pnl income:investment --value=end,'$'
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+-----++---------+---------+
| || 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
hledger -f - roi --inv saving --pnl dividend
<<<
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
>>>
+---++------------+------------++---------------+----------+-------------+---------++-------+-------+
| || 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