Beanprice on Import

93 views
Skip to first unread message

Paul Walker

unread,
May 4, 2025, 1:23:20 PM5/4/25
to Beancount
Is there an easy way to leverage beanprice with its cache and commodity-meta awareness in a beangulp import workflow? I want to fetch prices for crypto airdrops and staking rewards on import so I can attach cost/lots; Kraken doesn't offer this in its ledger api response. I was thinking to persist find_currencies_declared and maybe process "DatedPrice" jobs inline, but wondered if it's been done already.

Thanks,
Paul

Paul Walker

unread,
May 13, 2025, 11:55:25 PM5/13/25
to Beancount
For anyone watching, I think I've got it. I tried the AI route, CoPilot w/ gpt 4o failed miserably, completely making up functions.

But it wasn't terrible to hack out. Note: python is not my native language.

# "price" is too generic
from beanprice import price as beanprice
...
    def extract(self, filepath, existing):
        # Initialize cache (string path) and a dict of commodity:sources_list for easy reference later
        sometuples = beanprice.find_currencies_declared(existing)
        commodity_sources = { 
sometuple[0]:sometuple[2] for sometuple in sometuples }
        # I made price_cache path an importer option for flexibility
        if self.price_cache:
            beanprice.setup_cache(self.price_cache, False)

 ... Later pulling a price:
            # Get that "sources_list" for, e.g. "BTC"
            srcs = commodity_sources[asset]
            # Make the thing we need to pass to fetch_price, using date of a given transaction
            dated_price = beanprice.DatedPrice(self.base_currency, None, date, srcs)
            price = beanprice.fetch_price(dated_price)
            cost = CostSpec(price.amount.number, None, price.amount.currency, None, None, None)
            # ... add cost to Posting

In context:

Paul
Reply all
Reply to author
Forward
0 new messages