Skip to content

Commit

Permalink
roi: better debug, fix calculation on dates that have just P update
Browse files Browse the repository at this point in the history
  • Loading branch information
adept committed Dec 11, 2024
1 parent 745b48e commit b067e4d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions hledger/Hledger/Cli/Commands/Roi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
, Date (DateSpan Nothing (Just end))])

priceDates = dbg3 "priceDates" $ nub $ filter (spanContainsDate spn) priceDirectiveDates
cashFlow =
cashFlow = dbg3 "cashFlow" $
((map (,nullmixedamt) priceDates)++) $
cashFlowApplyCostValue $
calculateCashFlow wd trans (And [ Not investmentsQuery
Expand Down Expand Up @@ -226,6 +226,7 @@ timeWeightedReturn styles showCashFlow prettyTables investmentsQuery trans mixed
$ datedAmounts

let units =
dbg3 "units" $
tailDef (error' "Roi.hs units was null, please report a bug") $
scanl
(\(_, _, unitCost, unitBalance) (date, amt) ->
Expand All @@ -235,7 +236,12 @@ timeWeightedReturn styles showCashFlow prettyTables investmentsQuery trans mixed
Right amt' ->
-- we are buying or selling
let unitsBoughtOrSold = unMix amt' / unitCost
in (valueOnDate, unitsBoughtOrSold, unitCost, unitBalance + unitsBoughtOrSold)
unitBalance' = unitBalance + unitsBoughtOrSold
unitCost' =
if unitsBoughtOrSold == 0 && valueOnDate/=0
then valueOnDate/unitBalance' -- just a price update without units bought or sold
else unitCost
in (valueOnDate, unitsBoughtOrSold, unitCost', unitBalance')
Left pnl' ->
-- PnL change
let valueAfterDate = valueOnDate + unMix pnl'
Expand Down Expand Up @@ -273,16 +279,18 @@ timeWeightedReturn styles showCashFlow prettyTables investmentsQuery trans mixed
TL.putStr $ Tab.render prettyTables id id T.pack
(Table
(Tab.Group NoLine (map (Header . showDate) dates))
(Tab.Group DoubleLine [ Tab.Group Tab.SingleLine [Tab.Header "Portfolio value", Tab.Header "Unit balance"]
, Tab.Group Tab.SingleLine [Tab.Header "Pnl", Tab.Header "Cashflow", Tab.Header "Unit price", Tab.Header "Units"]
, Tab.Group Tab.SingleLine [Tab.Header "New Unit Balance"]])
[ [val, oldBalance, pnl', cashflow, prc, udelta, balance]
| val <- map showDecimal valuesOnDate
(Tab.Group DoubleLine [ Tab.Group Tab.SingleLine [Tab.Header "Portfolio value", Tab.Header "Unit balance", Tab.Header "Unit price"]
, Tab.Group Tab.SingleLine [Tab.Header "Pnl", Tab.Header "Cashflow"]
, Tab.Group Tab.SingleLine [Tab.Header "Units +/-"]
, Tab.Group Tab.SingleLine [Tab.Header "New Unit Balance", Tab.Header "New Unit Price"]])
[ [val, oldBalance, oldPrice, pnl', cashflow, udelta, balance, newPrice]
| val <- map showDecimal (valueBefore:valuesOnDate)
| oldBalance <- map showDecimal (0:unitBalances)
| oldPrice <- map showDecimal (initialUnitCost:unitPrices)
| balance <- map showDecimal unitBalances
| pnl' <- map (showMixedAmountOneLineWithoutCost False . styleAmounts styles) pnls
| cashflow <- map (showMixedAmountOneLineWithoutCost False . styleAmounts styles) cashflows
| prc <- map showDecimal unitPrices
| newPrice <- map showDecimal (unitPrices)
| udelta <- map showDecimal unitsBoughtOrSold ])

printf "Final unit price: %s/%s units = %s\nTotal TWR: %s%%.\nPeriod: %.2f years.\nAnnualized TWR: %.2f%%\n\n"
Expand Down

0 comments on commit b067e4d

Please sign in to comment.