Price fetching, imputation, and PnL

87 views
Skip to first unread message

Eric Altendorf

unread,
Aug 1, 2023, 8:55:05 PM8/1/23
to bean...@googlegroups.com
I'm having trouble understanding how automatic price fetching works, and I think it's being additionally complicated by questions of price imputation and maybe PnL accounts.  Let's start with the simplest case.  Consider (commodity declarations omitted for brevity):

2020-01-01 * "Buy BTC (from USD)"
  Assets:Coinbase:BTC       0.50000000 BTC
  Assets:Coinbase:USD  -4750.00000000 USD

I would have expected bean-price to fetch a price for BTC for 2020-01-01, but instead it fails, saying the transaction doesn't balance.  Now, if I supply a price 

2020-01-01 * "Buy BTC (from USD)"
  Assets:Coinbase:BTC       0.50000000 BTC @ 9500.0 USD
  Assets:Coinbase:USD  -4750.00000000 USD

or supply a price spec with no number:

2020-01-01 * "Buy BTC (from USD)"
  Assets:Coinbase:BTC       0.50000000 BTC @ USD
  Assets:Coinbase:USDT  -4750.00000000 USD

then there is no error, but bean-price fetches today's price.

Now let's consider a more complex example:

2020-02-01 * "Buy BTC (from USDT)"
  Assets:Coinbase:BTC       0.50000000 BTC
  Assets:Coinbase:USDT  -4750.00000000 USDT
  Income:PnL

Here, I'd like to fetch the USD prices of both BTC and USDT, which would allow PnL to be computed, but bean-price fetches nothing.  Now, if I put "@ USD" at the end of the BTC posting (as above), instead of getting the price on 2020-01-01 (as I desire), or the price today (as in the earlier example), I get "Too many missing numbers for currency group 'USD'".

I can't quite figure out what bean-price is doing, or square it against the documentation.  Any tips to get me unblocked?

thanks,
eric

Eric Altendorf

unread,
Aug 7, 2023, 7:39:31 PM8/7/23
to bean...@googlegroups.com
Let me ask a more basic question.  What is bean-price actually supposed to do?

Re-reading
I'm piecing together that perhaps the only thing bean-price does is value your assets as of a certain date.  Is that true?  TBH it's not clear from the docs.

There are other uses for prices, e.g., establishing cost basis for a purchase, or establishing the value of the transfer of an asset at a particular point in time be that for income or expenses.  Are there existing plugins that can look up historical prices and annotate transactions with them where they're missing?

William Bean

unread,
Aug 8, 2023, 7:04:05 PM8/8/23
to Beancount
Hey Eric,

Check out the repo for this that is located here: https://github.com/beancount/beanprice

There is a little documentation there that you can take a look at, but basically you'll want to use:

bean-price --update ledger.beancount

That will fetch all historical prices (as needed by your ledger) up to the current day.

Eric Altendorf

unread,
Aug 9, 2023, 12:07:16 AM8/9/23
to bean...@googlegroups.com
Thanks, William.

On Tue, Aug 8, 2023 at 4:04 PM William Bean <wbe...@gmail.com> wrote:
Hey Eric,

Check out the repo for this that is located here: https://github.com/beancount/beanprice

There is a little documentation there that you can take a look at, but basically you'll want to use:

I have read that too.
 
bean-price --update ledger.beancount

That will fetch all historical prices (as needed by your ledger) up to the current day.

Thanks for confirming that it is supposed to fetch historical prices.

Do you know what beancount considers "needed by the ledger"?  I can't seem to make sense of it; as I reported in my original post, it's not fetching the prices that it seems to *me* the ledger would need.

I'm assuming bean-price is designed for a different use case than mine and offers different behavior, but I can't quite figure out what that use case or behavior is, and without understanding that I can't figure out whether I can adapt bean-price for my case...


 
--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/ede58116-e234-4ffb-8d69-4188304af919n%40googlegroups.com.

Eric Altendorf

unread,
Aug 9, 2023, 12:11:47 AM8/9/23
to bean...@googlegroups.com
Here's another example:

2020-06-02 * "Mining reward of 0.05 BTC"
  Assets:Ledger:BTC  0.05 BTC
  Income:Mining:USD

$ bean-price -vn price-fetch-test.beancount
INFO    : Using price cache at "/tmp/bean-price.cache" (with indefinite expiration)
INFO    : Processing at date: 2023-08-08
INFO    : Loading "price-fetch-test.beancount"
BTC /USD                         @ latest     [ beanprice.sources.yahoo(BTC-USD) ]

None of the transactions I enter seem to cause bean-price to fetch anything but @latest.

Is there some annotation I need to be putting on my transactions to say "hey, price is needed here" ?


Martin Blais

unread,
Aug 9, 2023, 8:48:48 AM8/9/23
to Beancount
The original goal of bean-price is

- load the ledger and figure out which price entries are missing, say, at transaction times and at regular intervals while positions are held
- fetch/scrape rates from some external sites
- print out the missing Price directives in Beancount format so you can insert them into your file.
That's all.

In v3/master, moved it to its own repo so I could focus on just the core. I admit it needs a bit more love.



Eric Altendorf

unread,
Aug 9, 2023, 11:50:27 AM8/9/23
to bean...@googlegroups.com
On Wed, Aug 9, 2023 at 5:48 AM Martin Blais <bl...@furius.ca> wrote:
The original goal of bean-price is

- load the ledger and figure out which price entries are missing, say, at transaction times and at regular intervals while positions are held
- fetch/scrape rates from some external sites
- print out the missing Price directives in Beancount format so you can insert them into your file.
That's all.

OK, that's what I thought.  It doesn't seem to be doing this for me, not sure if there's a bug or if my test cases (e.g. earlier this thread) don't match the patterns it looks for.  Although I'm strongly biased against forking/duplicating code, this may be a case where I'm better off managing price fetching myself...I'll dig inside the library to see if I can at least reuse the fetching infra, even if I drive it with my own triggers.

In v3/master, moved it to its own repo so I could focus on just the core. I admit it needs a bit more love.

No problem.  Don't construe any of my questions as complaints.  :)  I'm just trying to understand the state of the world so I can plan in ways that meet my own goals and hopefully also advance the Beancount platform at the same time.
 

Martin Blais

unread,
Aug 13, 2023, 11:14:58 PM8/13/23
to bean...@googlegroups.com
On Thu, Aug 10, 2023 at 12:50 AM Eric Altendorf <erical...@gmail.com> wrote:
On Wed, Aug 9, 2023 at 5:48 AM Martin Blais <bl...@furius.ca> wrote:
The original goal of bean-price is

- load the ledger and figure out which price entries are missing, say, at transaction times and at regular intervals while positions are held
- fetch/scrape rates from some external sites
- print out the missing Price directives in Beancount format so you can insert them into your file.
That's all.

OK, that's what I thought.  It doesn't seem to be doing this for me, not sure if there's a bug or if my test cases (e.g. earlier this thread) don't match the patterns it looks for.  Although I'm strongly biased against forking/duplicating code, this may be a case where I'm better off managing price fetching myself...I'll dig inside the library to see if I can at least reuse the fetching infra, even if I drive it with my own triggers.

Code rots. I haven't been using this regularly myself. It's possibly broken.
There is not much to reuse here; roll out your own is a fine alternative.

 

In v3/master, moved it to its own repo so I could focus on just the core. I admit it needs a bit more love.

No problem.  Don't construe any of my questions as complaints.  :)  I'm just trying to understand the state of the world so I can plan in ways that meet my own goals and hopefully also advance the Beancount platform at the same time.

No worries.
I'm sorry I haven't been able to follow / respond to the flurry of emails.


Reply all
Reply to author
Forward
0 new messages