simonmichael / hledger

Robust, fast, intuitive plain text accounting tool with CLI, TUI and web interfaces.
https://hledger.org
GNU General Public License v3.0
3.02k stars 321 forks source link

roi "Ratio has zero denominator" when --pnl account balances to 0 #2281

Open iartarisi opened 1 day ago

iartarisi commented 1 day ago

I am trying to use hledger-roi and keep track of average cost using something similar to the method described in this multicurrency accounting tutorial. That means that both the cash and stock amounts should always balance to zero. In order to do that, all buy and sell commodity transactions go through an Income:Trading account (simplified below to just Income).

This crashes roi, I think, because it can't handle the Income account evaluating to 0

$ cat roi-example.journal 
P 2020/1/1 A GBP 100
2020/1/1 foo
    Assets                                  GBP -100
    Assets                                         1 A
    Income                                        -1 A
    Income                                   GBP 100

2020/2/1 doesn't matter what's in this transaction
    Equity                                  GBP -100
    Assets                                   GBP 100

$ hledger roi -f roi-example.journal --inv Assets --pnl Income -V
hledger: Ratio has zero denominator
$ hledger roi -f roi-example.journal --inv Assets --pnl Income -V --cashflow

IRR cash flow for 2020-01-01 - 2020-02-01
+------------++----------+
|            ||   Amount |
+============++==========+
| 2020-02-01 || GBP -100 |
| 2020-02-02 ||  GBP 100 |
+------------++----------+

TWR cash flow for 2020-01-01 - 2020-02-01
hledger: Ratio has zero denominator

$ hledger bal -f roi-example.journal -EV
             GBP 100  Assets
            GBP -100  Equity
                   0  Income
--------------------
                   0  
adept commented 1 day ago

Quoting from roi manual:

Query supplied to --inv has to match all transactions that are related to your investment. Transactions not matching --inv will be ignored.

In these transactions, ROI will conside postings that match --inv to be "investment postings" and other postings (not matching --inv) will be sorted into two categories: "cash flow" and "profit and loss", as ROI needs to know which part of the investment value is your contributions and which is due to the return on investment.

I think that the property "other postgres are either cash flows or PnL" does not hold for your initial transaction.

Try and separate your Income postings out:

P 2020/1/1 A GBP 100
2020/1/1 foo
    Assets                                  GBP -100
    Assets                                         1 A

2020/1/1 foo
    Income                                        -1 A
    Income                                   GBP 100

2020/2/1 doesn't matter what's in this transaction
    Equity                                  GBP -100
    Assets                                   GBP 100
iartarisi commented 1 day ago

Thanks for the reply @adept .

That does work for roi. But those first two transactions don't balance to zero individually.

hledger -f roi-example.journal check -s
hledger: Error: /.../roi-example.journal:2-4:
2 | 2020-01-01 foo
  |     Assets        GBP -100
  |     Assets             1 A

This multi-commodity transaction is unbalanced.
Automatic commodity conversion is not enabled.
The real postings' sum should be 0 but is: 1 A, GBP -100
Consider adjusting this entry's amounts, adding missing postings,
or recording conversion price(s) with @, @@ or equity postings.
iartarisi commented 1 day ago

I think that the property "other postgres are either cash flows or PnL" does not hold for your initial transaction.

Not sure about this. The way I read it, for that initial transaction, the Assets postings are in --inv and the Income postings are in --pnl. There are no other postings?