Thanks for all the suggestions. Using
https://hub.darcs.net/falsifian/misc-pub/browse/beancount_plugins/falsifian/parallel_average_cost.py as a starting point, I'm trying to write a plugin that will duplicate every transaction in a second currency. So for example:
2020-01-01 * "Buy"
Assets:Chequing -400.00 USD
Assets:Brokerage 4 ACME {100.00 USD}
Would turn into
2020-01-01 * "Buy"
Assets:Chequing -400.00 USD
Assets:Brokerage 4 ACME {100.00 USD}
2020-01-01 * "Buy"
Assets:Chequing_CAD -500.00 CAD
Assets:Brokerage_CAD 4 ACME_CAD {500.00 CAD}
(1.25 CAD = USD) This works fine for basic stuff, but the problem I'm having is I want to use the beancount engine to calculate the values of certain lines. In the above example, if the exchange rate changes to 1.2 CAD = USD, and I sell 2 units of ACME, I would add
2020-03-01 * "Sell"
Assets:Chequing 220.00 USD
Income:Capital-gains
Assets:Brokerage -2 ACME {} @ 110.00 USD
(Beancount calculates cap gains as $20 since previous buy was at $100)
I want my plugin to generate symmetric lines in CAD using the 1.2 exchange rate:
2020-03-01 * "Sell"
Assets:Chequing_CAD 264.00 CAD
Income:Capital-gains_CAD
Assets:Brokerage_CAD -2 ACME_CAD {} @ 132.00 CAD
And then beancount calculate $14 CAD as cap gains since the 2 ACME_CAD were previously purchased at $125 CAD each.
The problem I'm having is in my plugin, when I loop through the entries (for entry in entries in python) then the entry figure has the USD value for the Capital-gains, so it just converts the USD to CAD with the relevant exchange rate instead of calculating it using the cost of the ACME_CAD asset. So in this example, instead of $14 CAD cap gains, it is $16.67 CAD ($20 / 1.2).
Is there a way to loop over the entries before beancount does these calculations? Or is there a better approach?
Thanks,
Ben