Greetings,
Recently I have encountered some behaviour I do not understand when
using transactions with elided amounts and the `--infer-costs` flag.
Since I am not really sure of what is going on, I have written my
findings here in hope for help, as searching around online did not help
me answer make sense of the situation.
The `--infer-costs` flag works as expected with the following
multi-currency transaction
```
2023-01-01 Good
expenses:food 111.00 NOK
equity:conversion -111.00 NOK
equity:conversion 11.41 EUR
assets:bank -11.41 EUR
```
This changes when encountering an elided amount, for example
```
2023-01-02 Bad
expenses:food 111.00 NOK
equity:conversion -111.00 NOK
equity:conversion 11.41 EUR
assets:bank
```
Now running `hledger print --infer-costs` we output the following error
```
hledger: Error: -:1-5:
1 | 2023-01-02 Bad
| expenses:food 111.00 NOK @@ 11.41 EUR
| equity:conversion -111.00 NOK
| equity:conversion 11.41 EUR
| assets:bank -22.82 EUR
| assets:bank 111.00 NOK
This multi-commodity transaction is unbalanced.
The real postings' sum should be 0 but is: -11.41 EUR, 111.00 NOK
Consider adjusting this entry's amounts, adding missing postings,
or recording conversion price(s) with @, @@ or equity postings.
```
It seems `hledger` is adding both amounts found in the conversion
postings to the elided amount, which seems like a weird behaviour at
first, as here it unbalances a valid entry.
From the manual [1] we find
> Postings will be recognised as equity conversion postings if they are
> 1. to account(s) declared with type V (Conversion; or if no such
> accounts are declared, accounts named equity:conversion, equity:trade,
> equity:trading, or subaccounts of these)
> 2. adjacent
> 3. and exactly matching the amounts of two non-conversion postings.
It seems that the condition that is failing in the second entry is the
third one: the equity postings identified involve the amounts 111.00 NOK
and 11.41 EUR, and although the `expenses:food` posting matches the NOK
amount, there is no explicit match for the EUR one.
While the 11.41 EUR amount is implicitly stated, it seems believable
that in general this approach could not use the implicit amount, as when
running `hledger print` with the `--explicit` flag, as for a more
complicated entry the implicit amount might add up to the required
matching amount in an unexpected manner, or something similar.
What confused me is that if we add a commission cost to the examples, we
find similar results:
```
2023-01-01 Good
expenses:food 111.00 NOK
equity:conversion -111.00 NOK
equity:conversion 11.41 EUR
expenses:commission 0.57 EUR
assets:bank -11.98 EUR
2023-01-02 Bad
expenses:food 111.00 NOK
equity:conversion -111.00 NOK
equity:conversion 11.41 EUR
expenses:commission 0.57 EUR
assets:bank
```
Entries of this style are what prompted this message.
Running `hledger print --infer-costs` outputs the following error
```
hledger: Error: -:8-13:
8 | 2023-01-02 Bad
| expenses:food 111.00 NOK @@ 11.41 EUR
| equity:conversion -111.00 NOK
| equity:conversion 11.41 EUR
| expenses:commission 0.57 EUR
| assets:bank -23.39 EUR
| assets:bank 111.00 NOK
This multi-commodity transaction is unbalanced.
The real postings' sum should be 0 but is: -11.41 EUR, 111.00 NOK
Consider adjusting this entry's amounts, adding missing postings,
or recording conversion price(s) with @, @@ or equity postings.
```
In this case, it seems like the first entry should not have satisfied
the third condition from the manual, as there is no posting with the
explicit amount of 11.41 EUR, but is handled correctly regardless.
Meanwhile, the second entry fails, and again we see that `hledger` added
the conversion amounts to the elided amount, again unbalancing it.
Altogether, this leads me to believe that the transaction with the
elided amount could have been handled exactly like the explicit one,
using the amount that can be calculated with `hledger print --explicit`.
In fact, the explicit EUR amount, 11.98 = 11.41 + 0.57, is available to
at some point, as it appears implicitly in the `assets:bank` posting,
since the stated amount in the error is 23.39 = 11.98 + 11.41.
Is there a deeper reason why a transaction with an elided amount is
handled differently? Perhaps I am misunderstanding something, but given
how entries with no elided amounts are handled, it appears that the rest
could be handled in an equivalent manner.
Now, given that the behaviour involving explicit entries does not match
the description from the manual, it might be that the misbehaviour
occurs there, and not in the entries with elided amounts.
Anyway, some help here would be appreciated, as I have been logging my
finances with elided amounts using only one currency since 2019, and
only recently encountered this multi-currency scenario, and do not
really look forward rewriting them to be explicit.
Best,
Claudi
```
$ hledger --version
hledger 1.28, linux-x86_64
```
[1]
https://hledger.org/1.28/hledger.html#inferring-cost-from-equity-postings