hledger icon indicating copy to clipboard operation
hledger copied to clipboard

Cost, exchange, and equity postings

Open angelo-peronio opened this issue 2 years ago • 6 comments

Cost, exchange, and equity postings

I am using hledger 1.28-g6e130a4b6-20221201, windows-x86_64. Given the following euro-last.journal

2022-12-01 Buy bananas
    assets:fruit          5 bananas
    equity:conversion    -5 bananas
    equity:conversion     5 €
    assets:cash          -5 €

2022-12-02 Buy avocados
    assets:fruit          5 avocados
    equity:conversion    -5 avocados
    equity:conversion    10 €
    assets:cash         -10 €

2022-12-02 Buy more expensive avocados
    assets:fruit          5 avocados
    equity:conversion    -5 avocados
    equity:conversion    50 €
    assets:cash         -50 €

I am able to see how much I paid for my fruit each day with

hledger balance `
    assets:fruit `
    --file=euro-last.journal `
    --value="cost" `
    --infer-costs `
    --infer-market-prices `
    --daily

obtaining

Balance changes in 2022-12-01..2022-12-02, converted to cost:

              || 2022-12-01  2022-12-02
==============++========================
 assets:fruit ||        5 €        60 €
--------------++------------------------
              ||        5 €        60 €

However, I am not able to do so if the postings within each transaction are in a different order, with the fruit coming last, as in the following fruit-last.journal

2022-12-01 Buy bananas
    assets:cash          -5 €
    equity:conversion     5 €
    equity:conversion    -5 bananas
    assets:fruit          5 bananas

2022-12-02 Buy avocados
    assets:cash         -10 €
    equity:conversion    10 €
    equity:conversion    -5 avocados
    assets:fruit          5 avocados

2022-12-02 Buy more expensive avocados
    assets:cash         -50 €
    equity:conversion    50 €
    equity:conversion    -5 avocados
    assets:fruit          5 avocados

In particular, I need to specify I want results in €

hledger balance `
    assets:fruit `
    --file=fruit-last.journal `
    --value="cost,€" `
    --infer-costs `
    --infer-market-prices `
    --daily

but this triggers the "valuation at period end", giving

Balance changes in 2022-12-01..2022-12-02, converted to cost, valued at period ends:

              || 2022-12-01  2022-12-02
==============++========================
 assets:fruit ||        5 €       100 €
--------------++------------------------
              ||        5 €       100 €

Is there a way to obtain the sum of the costs per period with the latter journal?

Moreover, the same command gives a different result on the former euro-last.journal

hledger balance `
    assets:fruit `
    --file=euro-last.journal `
    --value="cost,€" `
    --infer-costs `
    --infer-market-prices `
    --daily
Balance changes in 2022-12-01..2022-12-02, converted to cost, valued at period ends:

              || 2022-12-01  2022-12-02
==============++========================
 assets:fruit ||        5 €        60 €
--------------++------------------------
              ||        5 €        60 €

Is this a bug?

angelo-peronio avatar Dec 09 '22 16:12 angelo-peronio

Thanks for the report.

I am able to see how much I paid for my fruit each day with

hledger balance `
    assets:fruit `
    --file=euro-last.journal `
    --value="cost" `
    --infer-costs `
    --infer-market-prices `
    --daily

--value="cost" is supported for backward compatiblity, but deprecated. I would avoid valuation here, and request the cost directly:

hledger -f euro-last.journal balance assets:fruit --infer-costs --cost --daily  # or -BD

However, I am not able to do so if the postings within each transaction are in a different order, with the fruit coming last, as in the following fruit-last.journal

That's right; when inferring a cost, the first posting is priced in the commodity of the last posting, so the order of postings matters. You can see the difference with print --infer-costs. https://hledger.org/hledger.html#-b-convert-to-cost mentions it a bit.

In particular, I need to specify I want results in €

hledger balance `
    assets:fruit `
    --file=fruit-last.journal `
    --value="cost,€" `
    --infer-costs `
    --infer-market-prices `
    --daily

but this triggers the "valuation at period end", giving

Yes.. you are trying to emulate --cost with --value here, but I think this example is showing that it's not always possible. I tried --value=then and the deprecated --value=cost, but either they or the final conversion to euros, picks just one price per day, so the first price on 12/2 gets lost.

Is there a way to obtain the sum of the costs per period with the latter journal?

It seems tricky! I'm still looking...

Moreover, the same command gives a different result on the former euro-last.journal

I think it's not a bug, but explainable by --infer-costs' sensitivity to posting order, if you walk through each of the infer/convert steps.

simonmichael avatar Dec 09 '22 18:12 simonmichael

Well, there might be a sane way to report cost in euros from that euro-postings-first journal, but I'm not seeing it. Instead I would say if inferring cost, you must write the cost posting(s) last. (I realise this might be a bit annoying if you like keeping a strict FROM, TO posting order..)

simonmichael avatar Dec 09 '22 18:12 simonmichael

Thank you! I have now a way forward. Feel free to close this issue.

Some thoughts/suggestions -- but please keep in mind I am a newbie.

  • Now the order of the postings determines which commodity the costs will be reported in.
    • I found this semantics quite surprising.
    • In most use cases I cant think of, one trades cash for a commodity, so it is OK to report costs in one single "cash" commodity. I imagine however that when multiple currencies are involved, one might want to choose the currency of the report.
  • hledger could output a newbie-friendly deprecation warning upon a deprecated option such as --value=cost.

angelo-peronio avatar Dec 10 '22 14:12 angelo-peronio

On Dec 10, 2022, at 04:25, Angelo Peronio @.***> wrote: Some thoughts/suggestions -- but please keep in mind I am a newbie.

Great, thanks -

Now the order of the postings determines which commodity the costs will be reported in. I found this semantics quite surprising. I hear that. I'll make it more prominent in the docs.

Can you think of a less surprising, still general, way to choose the cost commodity ?

In most use cases I cant think of, one trades cash for a commodity, so it is OK to report costs in one single "cash" commodity. I imagine however that when multiple currencies are involved, one might want to choose the currency of the report. I think that's possible, if you either put cost postings second in each pair, or use @ notation. Counterexamples welcome.

hledger could output a newbie-friendly deprecation warning upon a deprecated option such as --value=cost. True. On the other hand it would go against our current no-e^Hwarnings policy. I think I'll make it an error now instead.

simonmichael avatar Dec 10 '22 14:12 simonmichael

Ouch! I meant to say no-warnings. :-)

simonmichael avatar Dec 10 '22 15:12 simonmichael

I have added more mentions of the effect of posting order when inferring costs, at https://hledger.org/dev/hledger.html#costs and https://hledger.org/dev/hledger.html#inferring-cost-from-equity-postings.

I don't want to work on it just now, but here are some notes:

  • here is where --value=cost is accepted
  • IIRC I kept it around specifically for the use case above - showing costs converted to some other commodity
  • thinking about it, I would expect the combination of --cost [--infer-costs] --value=then,COMM to also work for this
  • more investigation needed to understand why both of these seem not to work.

simonmichael avatar Dec 13 '22 15:12 simonmichael