lib: roi does not fail on empty input data (+test)

This commit is contained in:
Dmitry Astapov 2019-11-16 15:42:15 +00:00 committed by Simon Michael
parent e49da20f8c
commit 87b82b6839
2 changed files with 27 additions and 8 deletions

View File

@ -148,7 +148,8 @@ timeWeightedReturn showCashFlow prettyTables investmentsQuery trans (OneSpan spa
cashflow
let finalUnitBalance = if null units then initialUnits else let (_,_,_,u) = last units in u
finalUnitPrice = valueAfter / finalUnitBalance
finalUnitPrice = if finalUnitBalance == 0 then initialUnitPrice
else valueAfter / finalUnitBalance
totalTWR = roundTo 2 $ (finalUnitPrice - initialUnitPrice)
years = fromIntegral (diffDays spanEnd spanBegin) / 365 :: Double
annualizedTWR = 100*((1+(realToFrac totalTWR/100))**(1/years)-1) :: Double
@ -202,6 +203,9 @@ internalRateOfReturn showCashFlow prettyTables (OneSpan spanBegin spanEnd valueB
(map ((:[]) . show) amounts))
-- 0% is always a solution, so require at least something here
case totalCF of
[] -> return 0
_ ->
case ridders
#if MIN_VERSION_math_functions(0,3,0)
(RiddersParam 100 (AbsTol 0.00001))

View File

@ -210,3 +210,18 @@ hledger -f- roi --inv investment --pnl pnl -b 2017-06 -e 2018
+---++------------+------------++---------------+----------+-------------+-----++-------+--------+
>>>=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