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.