beancount_portfolio_allocation

137 views
Skip to first unread message

Ghislain Bourgeois

unread,
Jun 16, 2018, 11:12:47 PM6/16/18
to Beancount
Hi,

I recently started using beancount to manage my finances, and I needed a quick way to figure out the asset allocations in different portfolios. I wrote a quick reporting for that using metadata and custom directives.

Example usage would look like this:

$ bean-portfolio-allocation-report ledger.beancount --portfolio pension
cash
===========================================================================
subclass 	 amount 	 percent 	 target 	 difference
---------------------------------------------------------------------------
cash       	 380.00 	 19.00 		 0.00 		 -380.00

equity
===========================================================================
subclass 	 amount 	 percent 	 target 	 difference
---------------------------------------------------------------------------
ca-stock   	 700.00 	 35.00 		 30.00 		 -100.00
us-stock   	 600.00 	 30.00 		 30.00 		 0.00

fixed
===========================================================================
subclass 	 amount 	 percent 	 target 	 difference
---------------------------------------------------------------------------
ca-bond    	 320.00 	 16.00 		 40.00 		 480.00



You can find more information on github: https://github.com/ghislainbourgeois/beancount_portfolio_allocation

Comments and suggestions are welcome!

Ghislain

Justus Pendleton

unread,
Jun 17, 2018, 9:34:20 AM6/17/18
to Beancount
Nifty idea.

I tried it and it told me:

Traceback (most recent call last):

  File "/Users/justus/anaconda/bin/bean-portfolio-allocation-report", line 11, in <module>

    sys.exit(main())

  File "/Users/justus/anaconda/lib/python3.5/site-packages/beancount_portfolio_allocation/allocation_report.py", line 58, in main

    print(report(args.bean, args.portfolio))

  File "/Users/justus/anaconda/lib/python3.5/site-packages/beancount_portfolio_allocation/allocation_report.py", line 10, in report

    targets, allocations, total = loader.load(bean, portfolio)

  File "/Users/justus/anaconda/lib/python3.5/site-packages/beancount_portfolio_allocation/loader.py", line 18, in load

    total = allocations.total_invested_for_portfolio()

  File "/Users/justus/anaconda/lib/python3.5/site-packages/beancount_portfolio_allocation/allocation.py", line 38, in total_invested_for_portfolio

    return sum([p.value for p in self])

TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'NoneType'


If I have time tomorrow, I'll try to figure out why :)

Ghislain Bourgeois

unread,
Jun 17, 2018, 10:13:01 AM6/17/18
to bean...@googlegroups.com
You might be missing price directives for some of your commodities. I still need to add error handling and input validation.

Ghislain

--
You received this message because you are subscribed to a topic in the Google Groups "Beancount" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beancount/O34K2lBYSGg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beancount+...@googlegroups.com.
To post to this group, send email to bean...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/a4640773-3d3c-4dd3-b61f-6d79b8ea388c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justus Pendleton

unread,
Jun 17, 2018, 10:46:42 AM6/17/18
to Beancount
I have price directives for everything.

From a quick look, the allocation_query in get_allocations is the culprit. In particular the sum(value(position)). It gets a list of all positions, even ones that are now 0. (e.g. I bought some MSFT and then sold it, so I now have 0 MSFT in that account.) The sum of the position is None. Here's the same query run through bean-query so you can see how (part of) my results look. (I've replaced some actual numbers with <redacted> but you should get the general idea.)

  currency      value_sum_position      c         s                  account            

------------- ----------------------- ----- ------------- ------------------------------

AUD                 2.00     AUD                          Assets:AUS:CMC                

AUD                40.47     AUD                          Assets:US:Interactive-Brokers 

EMGF           <redacted>    USD      stock em            Assets:US:WellsFargo:Brokerage

FSS_INTL       <redacted>    FSS_INTL stock international Assets:AUS:First-State-Super  

IEMG                                  stock em            Assets:US:WellsFargo:Brokerage

IEMG           <redacted>    USD      stock em            Assets:US:Interactive-Brokers 

RZV                                   stock small cap     Assets:US:WellsFargo:Brokerage

RZV            <redacted>    USD      stock small cap     Assets:US:Interactive-Brokers 

VWIUX                                 bond  us-bond       Assets:US:Vanguard:Taxable    


Now that I look at it, that sum(value(position)) probably needs to convert everything to a single, uniform currency to be meaningful. If I have some things in Euros and something in USD, the results won't make much sense otherwise.

Ghislain Bourgeois

unread,
Jun 18, 2018, 3:04:26 PM6/18/18
to Beancount
I have just released version 0.2.0 that now supports multiple currencies by converting everything to the first operating_currency. I will also add an optional parameter in the near future to select the currency to use for reporting.

I can also confirm the issue with reducing positions. The sum function in bean-query returns None in error cases (like missing price entries) and also when the sum adds up to 0. I think this is a bug in beancount and will open an issue for it, but was able to put a workaround in place. You should be able to test the new version and get more useful data now.

Thanks for the feedback!

Justus Pendleton

unread,
Jun 22, 2018, 10:25:21 PM6/22/18
to Beancount
The update works for me. Thanks!
Reply all
Reply to author
Forward
0 new messages