diff --git a/hledger/Hledger/Cli/Commands/Roi.hs b/hledger/Hledger/Cli/Commands/Roi.hs index 52b3d0599be..6145d2e7915 100644 --- a/hledger/Hledger/Cli/Commands/Roi.hs +++ b/hledger/Hledger/Cli/Commands/Roi.hs @@ -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 @@ -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) -> @@ -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' @@ -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"