roi: check valuation on dates we have price directives for

This commit is contained in:
Dmitry Astapov 2021-02-09 23:06:41 +00:00 committed by Simon Michael
parent 8013d650f3
commit 0fa51482c6

View File

@ -60,8 +60,8 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{rsOpts=ReportOpts{..}
d <- getCurrentDay d <- getCurrentDay
-- We may be converting posting amounts to value, per hledger_options.m4.md "Effect of --value on reports". -- We may be converting posting amounts to value, per hledger_options.m4.md "Effect of --value on reports".
let let
priceOracle = (journalPriceOracle infer_value_ j) priceOracle = journalPriceOracle infer_value_ j
styles = (journalCommodityStyles j) styles = journalCommodityStyles j
today = rsToday rspec today = rsToday rspec
mixedAmountValue periodlast date = mixedAmountApplyCostValuation priceOracle styles periodlast today date cost_ value_ mixedAmountValue periodlast date = mixedAmountApplyCostValuation priceOracle styles periodlast today date cost_ value_
let let
@ -85,7 +85,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{rsOpts=ReportOpts{..}
requestedSpan = periodAsDateSpan period_ requestedSpan = periodAsDateSpan period_
requestedInterval = interval_ requestedInterval = interval_
wholeSpan = spanDefaultsFrom requestedSpan journalSpan wholeSpan = dbg3 "wholeSpan" $ spanDefaultsFrom requestedSpan journalSpan
when (null trans) $ do when (null trans) $ do
putStrLn "No relevant transactions found. Check your investments query" putStrLn "No relevant transactions found. Check your investments query"
@ -96,7 +96,9 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{rsOpts=ReportOpts{..}
interval -> interval ->
splitSpan interval wholeSpan splitSpan interval wholeSpan
tableBody <- forM spans $ \(DateSpan (Just spanBegin) (Just spanEnd)) -> do let priceDirectiveDates = dbg3 "priceDirectiveDates" $ map pddate $ jpricedirectives j
tableBody <- forM spans $ \span@(DateSpan (Just spanBegin) (Just spanEnd)) -> do
-- Spans are [spanBegin,spanEnd), and spanEnd is 1 day after then actual end date we are interested in -- Spans are [spanBegin,spanEnd), and spanEnd is 1 day after then actual end date we are interested in
let let
cashFlowApplyCostValue = map (\(d,amt) -> (d,mixedAmountValue spanEnd d amt)) cashFlowApplyCostValue = map (\(d,amt) -> (d,mixedAmountValue spanEnd d amt))
@ -111,17 +113,20 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{rsOpts=ReportOpts{..}
total trans (And [investmentsQuery total trans (And [investmentsQuery
, Date (DateSpan Nothing (Just spanEnd))]) , Date (DateSpan Nothing (Just spanEnd))])
priceDates = dbg3 "priceDates" $ nub $ filter (spanContainsDate span) priceDirectiveDates
cashFlow = cashFlow =
((map (\d -> (d,0)) priceDates)++) $
cashFlowApplyCostValue $ cashFlowApplyCostValue $
calculateCashFlow trans (And [ Not investmentsQuery calculateCashFlow trans (And [ Not investmentsQuery
, Not pnlQuery , Not pnlQuery
, Date (DateSpan (Just spanBegin) (Just spanEnd)) ] ) , Date span ] )
pnl = pnl =
cashFlowApplyCostValue $ cashFlowApplyCostValue $
calculateCashFlow trans (And [ Not investmentsQuery calculateCashFlow trans (And [ Not investmentsQuery
, pnlQuery , pnlQuery
, Date (DateSpan (Just spanBegin) (Just spanEnd)) ] ) , Date span ] )
thisSpan = dbg3 "processing span" $ thisSpan = dbg3 "processing span" $
OneSpan spanBegin spanEnd valueBefore valueAfter cashFlow pnl OneSpan spanBegin spanEnd valueBefore valueAfter cashFlow pnl