| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Hledger.Data.Valuation
Description
Convert amounts to some related value in various ways. This involves looking up historical market prices (exchange rates) between commodities.
Synopsis
- data ConversionOp
- data ValuationType
- = AtThen (Maybe CommoditySymbol)
- | AtEnd (Maybe CommoditySymbol)
- | AtNow (Maybe CommoditySymbol)
- | AtDate Day (Maybe CommoditySymbol)
- type PriceOracle = (Day, CommoditySymbol, Maybe CommoditySymbol) -> Maybe (CommoditySymbol, Quantity)
- journalPriceOracle :: Bool -> Journal -> PriceOracle
- journalAliasPriceDirectives :: Journal -> [PriceDirective]
- mixedAmountToCost :: Map CommoditySymbol AmountStyle -> ConversionOp -> MixedAmount -> MixedAmount
- mixedAmountApplyValuation :: PriceOracle -> Map CommoditySymbol AmountStyle -> Day -> Day -> Day -> ValuationType -> MixedAmount -> MixedAmount
- mixedAmountValueAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> MixedAmount -> MixedAmount
- amountValueAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> Amount -> Amount
- mixedAmountApplyGain :: PriceOracle -> Map CommoditySymbol AmountStyle -> Day -> Day -> Day -> ValuationType -> MixedAmount -> MixedAmount
- mixedAmountGainAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> MixedAmount -> MixedAmount
- marketPriceReverse :: MarketPrice -> MarketPrice
- priceDirectiveToMarketPrice :: PriceDirective -> MarketPrice
- amountPriceDirectiveFromCost :: Day -> Amount -> Maybe PriceDirective
- valuationTypeValuationCommodity :: ValuationType -> Maybe CommoditySymbol
- tests_Valuation :: TestTree
Documentation
data ConversionOp Source #
Which cost conversion to perform on amounts, if any: to cost basis where known, otherwise transacted cost (-B/--value=cost); or to transacted cost always (--value=transacted). (There was also an "infer equity postings" operation, but that is now done earlier, in journal finalisation.)
Constructors
| NoConversionOp | |
| ToCost | |
| ToTransactedCost |
Instances
| Eq ConversionOp Source # | |
Defined in Hledger.Data.Valuation | |
| Show ConversionOp Source # | |
Defined in Hledger.Data.Valuation Methods showsPrec :: Int -> ConversionOp -> ShowS show :: ConversionOp -> String showList :: [ConversionOp] -> ShowS | |
data ValuationType Source #
What kind of value conversion should be done on amounts ? CLI: --value=then|end|now|DATE[,COMM]
Constructors
| AtThen (Maybe CommoditySymbol) | convert to default or given valuation commodity, using market prices at each posting's date |
| AtEnd (Maybe CommoditySymbol) | convert to default or given valuation commodity, using market prices at period end(s) |
| AtNow (Maybe CommoditySymbol) | convert to default or given valuation commodity, using current market prices |
| AtDate Day (Maybe CommoditySymbol) | convert to default or given valuation commodity, using market prices on some date |
Instances
| Eq ValuationType Source # | |
Defined in Hledger.Data.Valuation | |
| Show ValuationType Source # | |
Defined in Hledger.Data.Valuation Methods showsPrec :: Int -> ValuationType -> ShowS show :: ValuationType -> String showList :: [ValuationType] -> ShowS | |
type PriceOracle = (Day, CommoditySymbol, Maybe CommoditySymbol) -> Maybe (CommoditySymbol, Quantity) Source #
A price oracle is a magic memoising function that efficiently looks up market prices (exchange rates) from one commodity to another (or if unspecified, to a default valuation commodity) on a given date.
journalPriceOracle :: Bool -> Journal -> PriceOracle Source #
Generate a price oracle (memoising price lookup function) from a journal's directive-declared and transaction-inferred market prices. For best performance, generate this only once per journal, reusing it across reports if there are more than one, as compoundBalanceCommand does. The boolean argument is whether to infer market prices from transactions or not.
Commodity aliases are also used, as fixed exchange rates (see buildPriceIndexes),
but only when no conversion can be found with market prices alone;
so they can't change a valuation which market prices provide (eg one declared in another file tree).
So an alias can be the valuation commodity,
and amounts left in an alias (by a file which didn't declare it) can be valued.
journalAliasPriceDirectives :: Journal -> [PriceDirective] Source #
A journal's commodity aliases, as fixed exchange rates: price directives dated nulldate,
saying that one unit of a commodity is worth the alias's quantity of the alias (as written), at all dates.
Each has the source position of the commodity directive which declared the alias.
mixedAmountToCost :: Map CommoditySymbol AmountStyle -> ConversionOp -> MixedAmount -> MixedAmount Source #
Convert all component amounts to cost/selling price if requested, and style them.
mixedAmountApplyValuation :: PriceOracle -> Map CommoditySymbol AmountStyle -> Day -> Day -> Day -> ValuationType -> MixedAmount -> MixedAmount Source #
Apply a specified valuation to this mixed amount, using the provided price oracle, commodity styles, and reference dates. See amountApplyValuation.
mixedAmountValueAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> MixedAmount -> MixedAmount Source #
Find the market value of each component amount in the given commodity, or its default valuation commodity, at the given valuation date, using the given market price oracle. When market prices available on that date are not sufficient to calculate the value, amounts are left unchanged.
amountValueAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> Amount -> Amount Source #
Find the market value of this amount in the given valuation commodity if any, otherwise the default valuation commodity, at the given valuation date. (The default valuation commodity is the commodity of the latest applicable market price before the valuation date.)
The returned amount will have its commodity's canonical style applied, (with soft display rounding).
If the market prices available on that date are not sufficient to calculate this value, the amount is left unchanged.
mixedAmountApplyGain :: PriceOracle -> Map CommoditySymbol AmountStyle -> Day -> Day -> Day -> ValuationType -> MixedAmount -> MixedAmount Source #
Calculate the gain of each component amount, that is the difference between the valued amount and the value of the cost basis (see mixedAmountApplyValuation).
Note: the cost subtracted here is the sum of every contributing posting's
cost basis where known (lot postings), else transacted cost, regardless of
direction. (Summing postings merges amounts and can drop their bases, so
for --gain reports, lot postings' transacted costs are first replaced by
their basis costs; see Hledger.Cli.Utils.journalPrepareLotDetail.)
For a non-lot disposal posting recording its sale price with ,
the negative quantity flips its cost contribution from acquisition cost to
negated sale proceeds, so the result accumulates realised gain alongside
any unrealised gain on remaining units. In other words, with bare this
reports total gain since inception; with lot tracking, the unrealised gain
on the remaining units.
If the commodity we are valuing in is not the same as the commodity of the cost, this will value the cost at the same date as the primary amount. This may not be what you want; for example you may want the cost valued at the posting date. If so, let us know and we can change this behaviour.
mixedAmountGainAtDate :: PriceOracle -> Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> MixedAmount -> MixedAmount Source #
Calculate the gain of each component amount, that is the difference between the valued amount and the value of the cost basis.
If the commodity we are valuing in is not the same as the commodity of the cost, this will value the cost at the same date as the primary amount. This may not be what you want; for example you may want the cost valued at the posting date. If so, let us know and we can change this behaviour.
amountPriceDirectiveFromCost :: Day -> Amount -> Maybe PriceDirective Source #
Infer a market price from the given amount and its cost (if any), and make a corresponding price directive on the given date. The price's display precision will be set to show all significant decimal digits; or if they seem to be infinite, defaultPrecisionLimit.