mirror of
https://github.com/simonmichael/hledger.git
synced 2024-11-09 10:17:34 +03:00
fix: roi: make sure empty cashflows are skipped when determining first cashflow
Empty cashflows are added when the begin date of the report is before the first transaction.
This commit is contained in:
parent
7063eba13c
commit
75fc6767a9
@ -162,7 +162,9 @@ timeWeightedReturn showCashFlow prettyTables investmentsQuery trans mixedAmountV
|
||||
-- PnL and CashFlow, we would not be able to apply pnl change to 0 unit,
|
||||
-- which would lead to an error. We make sure that we have at least one
|
||||
-- cashflow entry at the front, and we know that there would be at most
|
||||
-- one for the given date, by construction.
|
||||
-- one for the given date, by construction. Empty CashFlows added
|
||||
-- because of a begin date before the first transaction are not seen as
|
||||
-- a valid cashflow entry at the front.
|
||||
zeroUnitsNeedsCashflowAtTheFront
|
||||
$ sort
|
||||
$ datedCashflows ++ datedPnls
|
||||
@ -170,9 +172,14 @@ timeWeightedReturn showCashFlow prettyTables investmentsQuery trans mixedAmountV
|
||||
zeroUnitsNeedsCashflowAtTheFront changes =
|
||||
if initialUnits > 0 then changes
|
||||
else
|
||||
let (leadingPnls, rest) = span (isLeft . snd) changes
|
||||
(firstCashflow, rest') = splitAt 1 rest
|
||||
in firstCashflow ++ leadingPnls ++ rest'
|
||||
let (leadingEmptyCashFlows, rest) = span isEmptyCashflow changes
|
||||
(leadingPnls, rest') = span (isLeft . snd) rest
|
||||
(firstCashflow, rest'') = splitAt 1 rest'
|
||||
in leadingEmptyCashFlows ++ firstCashflow ++ leadingPnls ++ rest''
|
||||
|
||||
isEmptyCashflow (_date, amt) = case amt of
|
||||
Right amt -> mixedAmountIsZero amt
|
||||
Left _ -> False
|
||||
|
||||
datedPnls = map (second Left) $ aggregateByDate pnl
|
||||
|
||||
|
@ -345,3 +345,24 @@ hledger -f - roi --inv saving --pnl dividend
|
||||
+---++------------+------------++---------------+----------+-------------+---------++-------+-------+
|
||||
|
||||
>>>=0
|
||||
|
||||
# 14. Should support begin date before first transaction where first transaction has pnl
|
||||
hledger -f - roi --inv stocks --pnl expenses --value=then,€ -Y
|
||||
<<<
|
||||
P 2022-07-31 A € 1
|
||||
|
||||
2022-08-01 Purchase
|
||||
checking € -101
|
||||
stocks 100 A @ € 1
|
||||
expenses € 1
|
||||
|
||||
P 2022-08-02 A € 2
|
||||
|
||||
>>>
|
||||
+---++------------+------------++---------------+----------+-------------+------++---------+--------+
|
||||
| || 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
|
||||
|
Loading…
Reference in New Issue
Block a user