Active currency holdings and average cost booking

122 views
Skip to first unread message

Red S

unread,
Nov 25, 2019, 5:18:05 AM11/25/19
to Beancount
I'm trying to get a list of currencies active as of a given date in order to download prices. However, I get way more currencies than are truly active because I have accounts that need average cost booking, which I've so far booked this way until that feature is available:

2016-01-06 * "buy"
  Assets:Inv  3 XYZ {9 USD}
  Assets:Bank

2016-01-06 * "buy"
  Assets:Inv  3 XYZ {10 USD}
  Assets:Bank

2016-01-06 * "buy"
  Assets:Inv  4 XYZ {11 USD}
  Assets:Bank

2016-01-07 * "sell"
  Assets:Inv  -10 XYZ @ 8 USD
  Assets:Bank 80 USD
  Income:CapGains

So I really have zero units of XYZ. However, because of the way these are booked, bean-price includes XYZ in its output. Given I have several such cases, what is the best way to handle this? I can think of these:
- wait until average cost booking is available (will it solve this problem?)
- book the purchases above as prices instead of commodities held at cost (will I lose the average cost of the commodity? How will I know the cost basis to book profit and loss?)
- hack a way to get a list of commodities whose lots aggregate to zero, and remove those from what bean-price lists?
- something else?

Thank you.

Martin Michlmayr

unread,
Nov 26, 2019, 1:14:34 PM11/26/19
to bean...@googlegroups.com
* Red S <redst...@gmail.com> [2019-11-25 02:18]:
> 2016-01-07 * "sell"
> Assets:Inv -10 XYZ @ 8 USD
> Assets:Bank 80 USD
> Income:CapGains

do it manually:

2016-01-07 * "sell"
Assets:Inv -3 XYZ { 9 USD} @ 8 USD
Assets:Inv -3 XYZ {10 USD} @ 8 USD
Assets:Inv -4 XYZ {11 USD} @ 8 USD
Assets:Bank 80 USD
Income:CapGains

or calculate average cost first manually:

2016-01-07 * "Calculate average cost"
Assets:Inv 10 XYZ {10.1 USD}
Assets:Inv -3 XYZ { 9 USD}
Assets:Inv -3 XYZ {10 USD}
Assets:Inv -4 XYZ {11 USD}

2016-01-07 * "sell"
Assets:Inv -10 XYZ {10.1 USD} @ 8 USD
Assets:Bank 80 USD
Income:CapGains



> So I really have zero units of XYZ. However, because of the way these are
> booked, bean-price includes XYZ in its output. Given I have several such
> cases, what is the best way to handle this? I can think of these:
> - wait until average cost booking is available (will it solve this problem?)
> - book the purchases above as prices instead of commodities held at cost
> (will I lose the average cost of the commodity? How will I know the cost
> basis to book profit and loss?)
> - hack a way to get a list of commodities whose lots aggregate to zero, and
> remove those from what bean-price lists?
> - something else?
>
> Thank 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 on the web visit https://groups.google.com/d/msgid/beancount/5e4d37c1-016d-499b-a988-a2cf0e7138eb%40googlegroups.com.


--
Martin Michlmayr
https://www.cyrius.com/

Red S

unread,
Nov 29, 2019, 3:37:55 AM11/29/19
to Beancount
Thanks, but that was the scenario I'm hoping to avoid. It involves way too many manual entries (for my case), or a bunch of scripting or plugins that will become obsolete once average cost booking is implemented.

I guess I could use FIFO instead. My Income:Capital-Gains numbers may be off, but that shouldn't matter much.

Do others have this issue at all?

Thanks!
> To unsubscribe from this group and stop receiving emails from it, send an email to bean...@googlegroups.com.

Martin Blais

unread,
Nov 29, 2019, 9:27:45 AM11/29/19
to Beancount
Here's how I get around this myself now:
1. DIsable booking by using "NONE" on the account:


2016-01-01 open Assets:Inv "NONE"
2016-01-01 open Assets:Bank
2016-01-01 open Income:CapGains

2016-01-06 * "buy"
Assets:Inv 3 XYZ {9 USD}
Assets:Bank

2016-01-06 * "buy"
Assets:Inv 3 XYZ {10 USD}
Assets:Bank

2016-01-06 * "buy"
Assets:Inv 4 XYZ {11 USD}
Assets:Bank

;; 2016-01-07 * "sell"
;; Assets:Inv -10 XYZ {USD} @ 8 USD
;; Assets:Bank 80 USD
;; Income:CapGains


Don't include your reducing transaction.
Compute average cost manually:"

bean-query avgcost.beancount "select account, sum(number),
sum(cost(position)) where account = 'Assets:Inv' group by 1"
account su sum_cos
---------- -- -------
Assets:Inv 10 101 USD

So I've got 10 units of XYZ at 101 USD total cost

Insert your transaction:

2016-01-07 * "sell"
Assets:Inv -10 XYZ {10.1 USD} @ 8 USD
Assets:Bank 80 USD
Income:CapGains

which expands to


bean-doctor linked avgcost.beancount 20
/home/blais/r/q/beancount-data/user/redstreet0/avgcost.beancount:17:

2016-01-07 * "sell"
Assets:Inv -10 XYZ {10.1 USD, 2016-01-07} @ 8 USD
Assets:Bank 80 USD
Income:CapGains 21.0 USD


Alternatively if you know you capital gains somehow, in your specific
case because you're selling the entire position (all of the 10 units)
Beancount is able to match it against the full set of positions and
reduce that, so this would run without errors:

2016-01-07 * "sell"
Assets:Inv -10 XYZ {} @ 8 USD
Assets:Bank 80 USD
Income:CapGains 21 USD

Unfortunately it's currently not able to do that if you don't supply the gains:
> 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/9e714aa2-b9a3-4925-afaf-555646fb734f%40googlegroups.com.

Red S

unread,
Dec 7, 2019, 7:25:07 PM12/7/19
to Beancount
Thanks for sharing your "recipe," Martin. That's a nifty query to get the average cost.

The first solution doesn't quite work for me given it ends up with a mix of positive and negative lot units, which bean-price doesn't aggregate, and therefore incorrectly tries to look up prices for commodities that I have zero of. Perhaps extending bean-prices to consider aggregates is a solution. But I also do like the cleanliness of true lot reductions. Knowing {} means all helps. In cases where it didn't, I moved a subset of transactions to a FIFO account to help with automatic lot reduction.

Thanks again!
Reply all
Reply to author
Forward
0 new messages