check_average_cost plugin on parent and subaccounts

80 views
Skip to first unread message

Chris Frey

unread,
Mar 25, 2026, 5:42:34 AMMar 25
to Beancount
I'm trying to use beancount to help manage Canadian style average costing.

The rules say that interchangeable units all contribute to the same
average cost, regardless of where they are bought or sold.

For example, let's say you buy BTC from 2 exchanges and wish to
record this in beancount in such a way as to make it easy to track
balances at each exchange, and easy to calculate average cost for all
your BTC when the time comes.

I imagine the open statements would be something like this:

... Assets:Crypto:BTC BTC "NONE"
... Assets:Crypto:BTC:Kraken BTC "NONE"
... Assets:Crypto:BTC:Coinbase BTC "NONE"

Using tools like fava and query, it's easy to get a report for each
subaccount, or the parent total account.

Unfortunately, the check_average_cost plugin only looks at the accounts
individually. Anything I sell in the Coinbase subaccount will not
pick up the costs from the BTC or BTC:Kraken accounts. Indeed,
anything I sell in the parent BTC account won't see costs in the
subaccounts either.

The python code looks simple enough, but I'm stuck at how best to handle
the different accounts.

Any coding and/or bookkeeping tips are welcome!

Thanks,
- Chris

Chris Frey

unread,
Apr 14, 2026, 8:03:54 PM (9 days ago) Apr 14
to bean...@googlegroups.com
If I wish to mark a set of accounts as a group, such that they all
work together for calculating average cost, what syntax would you
recommend?

For example, say I buy some gold and store it at vault vendor "Solid"
and buy and store some more at vault vendor "Guard".
I would record my purchases like:

2026-01-20 * "Solid" "buy 1.5 oz"
Assets:Metals:Vaulted:Solid 1.5 AU {5000 USD}
Assets:Bank -7500.00 USD

2026-02-15 * "Guard" "buy 2oz"
Assets:Metals:Vaulted:Guard 2 AU {4300 USD}
Assets:Bank -8600.00 USD


When it comes time to sell, say I sell 3oz at average cost. I wish
to use the cost records in both Vaulted:Solid and Vaulted:Guard
to calculate a true average cost for gold.

How would I do that now? And if not now, how would beancount prefer
the syntax to be for such a grouping?

; mark accounts as a costing group
2026-01-01 open Assets:Metals:Vaulted AU "NONE" "GoldCost"
2026-01-01 open Assets:Metals:Vaulted:Guard AU "NONE" "GoldCost"
2026-01-01 open Assets:Metals:Vaulted:Solid AU "NONE" "GoldCost"

; use the group
2026-04-01 * "Guard" "sell 2 oz"
Assets:Bank 9750.00 USD
; auto calculated cost of 4600.00 USD:
Assets:Metals:Vaulted:Guard -2 AU @ 4875.00 USD
; auto calc PnL: -550.00 USD
Income:Metals:PnL

2026-04-01 * "Solid" "sell 1 oz"
Assets:Bank 4850.00 USD
; auto calculated cost of 4600.00 USD:
Assets:Metals:Vaulted:Solid -1 AU @ 4850.00 USD
; auto calc PnL: -250.00 USD
Income:Metals:PnL

Thanks,
- Chris
> --
> 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 visit https://groups.google.com/d/msgid/beancount/acOThKs7QhI2G28l%40foursquare.net.

Martin Blais

unread,
Apr 14, 2026, 10:05:41 PM (9 days ago) Apr 14
to bean...@googlegroups.com
There's no real way right now to do this (love that you're holding gold BTW).
I think what you could do, however, today, is compute the average cost with a script and manually enter the cost basis of the average cost.



Damien R.

unread,
Apr 20, 2026, 12:00:32 PM (4 days ago) Apr 20
to Beancount
I decided to keep it simple and just record the transaction as they are presented by the brokerage firms. Then I run a script on the ledger to compute the capital gains I should report to the CRA.

The script is there: https://github.com/d-k-c/bean-acb
I've checked that in my case, it matches with values computed by this website: https://www.adjustedcostbase.ca/

It's pretty limited for now as I don't really need more, and implemented with my best understandings of the tax rules, so take the output with a (coarse) grain of salt.

Martin Blais

unread,
Apr 22, 2026, 8:53:58 AM (2 days ago) Apr 22
to bean...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages