scripting: Is there a function to get value of an account on a given date?

106 views
Skip to first unread message

vijayvithal jahagirdar

unread,
Nov 26, 2021, 2:15:13 AM11/26/21
to Beancount
I am writing a bunch of python scripts to generate various compliance reports required by the India Govt.
As a part of this I am interested in getting
1. The value of all postings in an account on a give date,
2. The value of all postings in an account in a given date range,

Currently I am
1. Iterating through all entries ->Transactions ->posting
2. checking whether it matches account name and date range
3. Adding up the value.

Since It looks like a fairly common task for accounting, I am wondering whether I am reinventing the wheel? Does a similar function exist within beancount?

Patrick Ruckstuhl

unread,
Nov 26, 2021, 2:31:01 AM11/26/21
to bean...@googlegroups.com

Hi,

I would recommend to use BQL, that should allow you to easily do it.


https://beancount.github.io/docs/beancount_query_language.html


You can use bql also from your scripts


Regards,

Patrick

--
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/71818f11-e867-424e-9695-2c4d55e94d67n%40googlegroups.com.

Martin Blais

unread,
Nov 26, 2021, 9:08:32 AM11/26/21
to Beancount

Stefano Zacchiroli

unread,
Nov 27, 2021, 5:10:57 AM11/27/21
to bean...@googlegroups.com
On Fri, Nov 26, 2021 at 09:08:18AM -0500, Martin Blais wrote:
> https://github.com/beancount/beancount/blob/master/beancount/ops/summarize.py#L589

Thanks a lot Martin, I didn't know about
beancount.ops.summarize.balance_by_accounts(), and now that I know it
helped me simplify some code I had around.

While I'm at it, the code in question compute the average daily balance
in a given account for a given year and currency (which matters for
fiscal reasons in Italy, don't ask...). The core of it looks like this:

# XXX naive approach: we re-calculate realizations from the beginning of the ledger
# to each day in the target year. It is acceptably fast, so we didn't bother making
# it more efficient than this (for now).
for day in tqdm(list(year_days(year))):
inventory = balance_by_account(entries, date=day)[0][account]
amount = inventory.get_currency_units(currency)
balances.append(amount.number)

As per comment, it is good enough (takes ~20 seconds on my main ledger),
but it's not great. What would be the best way/internal API to implement
this "right", i.e., with a running balance that is updated with daily
batches of entries?

TIA,
Cheers
--
Stefano Zacchiroli . za...@upsilon.cc . upsilon.cc/zack _. ^ ._
Full professor of Computer Science o o o \/|V|\/
Télécom Paris, Polytechnic Institute of Paris o o o </> <\>
Co-founder & CTO Software Heritage o o o o /\|^|/\
Former Debian Project Leader & OSI Board Director '" V "'

Martin Blais

unread,
Nov 27, 2021, 10:35:56 AM11/27/21
to Beancount
Check out what I do in this script:
https://github.com/beancount/beanlabs/blob/master/beanlabs/compensation/net-worth-over-time.py#L223

It could be made nicer, an iterator could be created that yields the state at a given list of timestamps, so all the aggregation would be abstracted away.



--
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.

Martin Blais

unread,
Nov 27, 2021, 11:06:30 AM11/27/21
to Martin Blais, Beancount

Oon-Ee Ng

unread,
Aug 16, 2022, 9:04:12 PM8/16/22
to bean...@googlegroups.com, Martin Blais
These scripts - do they rely on beancount v3? I'm still on 2.3.5 here

Andrew Laurence

unread,
Aug 16, 2022, 10:13:41 PM8/16/22
to bean...@googlegroups.com


Sent from Proton Mail for iOS
Reply all
Reply to author
Forward
0 new messages