Maybe a defect related to computing a commodity price?

8 views
Skip to first unread message

J. B. Rainsberger

unread,
Jun 18, 2024, 5:19:35 PMJun 18
to hle...@googlegroups.com
Hi, folks. I'm not sure that this is a defect, so I'll ask here first.

Summary: I have some USD/CAD currency price entries, some in the direction USD -> CAD and some in the direction CAD -> USD. When hledger tries to find the appropriate price to use in a "--value=then,CAD" report, it seems to fail. This seems to work when I have literally 1 USD -> CAD price and 1 CAD -> USD, but not when I have a realistic number of them.


There are three commands: wrong_exchange_rate_1, wrong_exchange_rate_2, and ok.

The "wrong" commands show hledger choosing the forward exchange rate (USD -> CAD) on the wrong day over the reverse exchange rate on the right day. The "ok" command shows hledger choosing the correct reverse exchange rates. I'm not sure why and it looks like a defect to me.

I tried a simpler example and it works:

P 2024-01-01  CAD  0.5 USD
P 2024-01-02  USD  1.2 CAD

2024-01-01  Worth 200 CAD
    Assets:USD    100 USD
    Revenue

2024-01-02  Worth 120 CAD
    Assets:USD    100 USD
    Revenue

$ hledger --debug=2 --value=then,CAD register "Assets"

This shows the expected balance of 150 + 120 = 270 CAD and the debug output shows hledger applying the expected exchange rate for each day.

I have no intuition for when this starts to break down, so I haven't tried yet. I was hoping that someone might have a clue what might be the problem before I try to bisect my way to a minimal failing example. Or maybe I have it all wrong.

Thanks.
--
J. B. (Joe) Rainsberger :: tdd.training :: jbrains.ca ::
blog.thecodewhisperer.com

Replies from this account routinely take a few days, which allows me to reply thoughtfully. I reply more quickly to messages that clearly require answers urgently. If you need something from me and are on a deadline, then let me know how soon you need a reply so that I can better help you to get what you need. Thank you for your consideration.

J. B. Rainsberger

unread,
Jun 19, 2024, 12:32:17 PMJun 19
to hle...@googlegroups.com
On Tue, Jun 18, 2024 at 5:18 PM J. B. Rainsberger <m...@jbrains.ca> wrote:
 
Hi, folks. I'm not sure that this is a defect, so I'll ask here first.

I believe I have found the defect.

The chronological order of the forward exchange rate (USD -> CAD) and the reverse exchange rate (CAD -> USD) changes the behavior.

The four latest commits to https://github.com/jbrains/hledger-reverse-price-curiosity/commits/simpler/ demonstrate the difference as well as I can do.

To summarize:

- $ hledger --file simpler.journal --value=then,CAD register "Assets" --debug=2
- In simpler.journal: when the CAD->USD (reverse) rate comes before the USD->CAD (forward) rate chronologically, then hledger applies the correct rates
- When the USD->CAD (forward) rate comes before the CAD->USD (reverse) rate chronologically, then hledger incorrectly appies the forward rate to transactions instead of the reverse rate on that day

I feel confident now that this is a defect, so I intend to open an issue unless someone objects.

Thanks,

Simon Michael

unread,
Jun 19, 2024, 5:42:39 PMJun 19
to J. B. Rainsberger, hle...@googlegroups.com
Thanks J.B. I haven't had time to dig into this, and you have probably already checked the valuation docs, but if not, they might have something about the intended priority of market prices. Or if not the docs, then the code:

--
You received this message because you are subscribed to the Google Groups "hledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hledger+u...@googlegroups.com.

J. B. Rainsberger

unread,
Jun 20, 2024, 9:00:33 AMJun 20
to Simon Michael, hle...@googlegroups.com
On Wed, Jun 19, 2024 at 5:42 PM Simon Michael <si...@joyful.com> wrote:
 
Thanks J.B. I haven't had time to dig into this, and you have probably already checked the valuation docs, but if not, they might have something about the intended priority of market prices. Or if not the docs, then the code:


Indeed, the docs reflect what I intuitively expected:

1. forward price on the day, and if not, then
2. reverse price on the day, and if not, then
3. other things

In this case, hledger is choosing (Forward price on earlier Day) over (Reverse price on more-recent Day) when the P directives are given as (Forward prices..., Reverse prices....). This seems to directly contradict the claims in the docs.

When the P directives are (Reverse prices...., Forward prices....), then hledger seems to choose the correct price.

I could do more testing, but I presume this ends up behaving as "if there are only forward/direct market prices before today, but a reverse price today, I'll choose the most-recent direct market price (even though I should choose today's reverse price)". That's a guess at this point. I don't read Haskell well enough to confidently grok what's there.

I have a few more ideas for tests, but I'm not sure when I'll be able to try them. For now, for my purposes, it seems like I need to avoid mixing forward prices and reverse prices.
Reply all
Reply to author
Forward
0 new messages