How to get the tree of all account balances/positions in python?

96 views
Skip to first unread message

cla...@numericable.fr

unread,
Jul 29, 2025, 8:57:59 PMJul 29
to bean...@googlegroups.com
Hello, sorry if this a trivial question.

In Python, I am struggling to get the tree of all accounts and their final balance or position for the main currency, including the open account without any attached posting, such as the intermediate nodes whose balances are expected to be the sum of their subaccount balances.
 
Realization only provides it for accounts with postings, Claude nor ChaGPT don’t help to solve this (probably not enough source code available for beancount v3), and looking at fava code, it seems they are re-calculating the positions.

Is it possible to get it from the core data structure or do we have to use bean-query?

Claude

Chary Chary

unread,
Jul 30, 2025, 3:50:08 AMJul 30
to bean...@googlegroups.com
Hi,

I think if can post here an example of a beancount ledger as well as en example of the desired output for this ledger, it will be easier to help you.

--
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/99181069-9bbc-4edd-b359-655f580a5342%40Spark.

Red S

unread,
Jul 30, 2025, 12:16:45 PMJul 30
to Beancount

It’s been a while since I looked at this, but IIRC, you’re right in that you’ll have to write a bit of python code. There isn’t a built in function, and what Fava does is the closest reference.

But it should be minor: for each account in the realization, you’ll want to sum up inventories of all its children, and then call convert to convert them to the main currency. Which IIRC is exactly what Fava does.

Red S

unread,
Jul 30, 2025, 7:17:36 PMJul 30
to Beancount

This prints the balance for the five top level accounts. Easily modified to print it for all the accounts:

#!/usr/bin/env python3 from beancount import loader from beancount.core import realization from beancount.core.realization import compute_balance from beancount.core.convert import convert_position from beancount.core.number import ZERO from beancount.core.prices import build_price_map # 1. Load your file entries, errors, options_map = loader.load_file("main.bc") main_currency = options_map['operating_currency'][0] # 2. Build the realization tree & price map tree = realization.realize(entries) price_map = build_price_map(entries) # 3. For each top‑level account, compute its total (including all sub‑accounts) for acct_name, acct in tree.items(): inv = compute_balance(acct) # Aggregate leaf and branch balances :contentReference[oaicite:0]{index=0} print(f"→ {acct_name!r} has {len(inv)} positions: ") total = ZERO for pos in inv: converted = convert_position(pos, main_currency, price_map) if converted and converted.currency == main_currency: total += converted.number print(f"{acct_name}: {total:.2f} {main_currency}")

Mts Clt

unread,
Jul 31, 2025, 8:32:14 AMJul 31
to Beancount
It works well, thank you very much!

Here is an example of code for exploring the full account tree with each associated balance:

#!/usr/bin/env python3

from beancount import loader
from beancount.core import realization
from beancount.core.realization import compute_balance
from beancount.core.convert import convert_position
from beancount.core.number import ZERO
from beancount.core.prices import build_price_map

# 1. Load your file
file_path = '/opt/hpf/20250726/compta.bean'
entries, errors, options_map = loader.load_file(file_path)


main_currency = options_map['operating_currency'][0]

# 2. Build the realization tree & price map
tree = realization.realize(entries)
price_map = build_price_map(entries)

# 3. Compute the balance for a RealAccount instance
def compute_realacct_balance(racct):
    global main_currency
    global price_map

    inv = compute_balance(racct)  # Aggregate leaf and branch balances :contentReference[oaicite:0]{index=0}


    total = ZERO

    for pos in inv:
        converted = convert_position(pos, main_currency, price_map)
        if converted and converted.currency == main_currency:
            total += converted.number

    print(f"{racct.account}: {total:.2f} {main_currency}")    

# Main loop on the full account tree (without recursivity)
for acct in realization.iter_children(tree):
    compute_realacct_balance(acct)

Reply all
Reply to author
Forward
0 new messages