Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iartarisi opened this issue Nov 4, 2024 · 6 comments · Fixed by #2283
Closed

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

iartarisi opened this issue Nov 4, 2024 · 6 comments · Fixed by #2283
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. roi

Comments

@iartarisi
Copy link

iartarisi commented Nov 4, 2024

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
Copy link
Collaborator

adept commented Nov 4, 2024

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
Copy link
Author

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
Copy link
Author

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?

@adept
Copy link
Collaborator

adept commented Nov 6, 2024

I can't reproduce your unbalanced transaction with hledger from tip. But lets backtrack. Do we at least agree that both TWR and IRR for your journal would be zero?

Regardless of what I think about the semantics of your journal, it is clear that division by zero should not be there, so lets at least fix that (via #2283 )

@iartarisi
Copy link
Author

Do we at least agree that both TWR and IRR for your journal would be zero?

Regardless of what I think about the semantics of your journal, it is clear that division by zero should not be there, so lets at least fix that (via #2283 )

Yup, agreed. That's the issue I was raising here.

@simonmichael simonmichael added A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. roi labels Nov 7, 2024
@simonmichael
Copy link
Owner

Thanks for the report @iartarisi and fix @adept. We've had many of these in the tricky roi calculations unfortunately, hopefully this is the last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. roi
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants