New plugin: capital gains long/short classifier

81 views
Skip to first unread message

Red S

unread,
Apr 26, 2022, 3:42:49 PM4/26/22
to Beancount
Thought this deserved a thread of its own. Here is a new plugin that classifies sales into short term or long term capital gains based on how long they have been held, like so: Converts:
```
plugin "long_short" "{
   'generic_account_pat':   ':Capital-Gains',
   'short_account_rep': ':Capital-Gains:Short',
   'long_account_rep':  ':Capital-Gains:Long',
   }"
       
2014-01-01 open Assets:Brokerage
2014-01-01 open Assets:Bank
2014-01-01 open Income:Capital-Gains

2014-02-01 * "Buy"
  Assets:Brokerage    100 ORNG {1 USD}
  Assets:Bank        -100 USD

2016-02-01 * "Buy"
  Assets:Brokerage    100 ORNG {2 USD}
  Assets:Bank        -200 USD

2016-03-01 * "Sell"
  Assets:Brokerage   -100 ORNG {1 USD} @ 2.50 USD
  Assets:Brokerage   -100 ORNG {2 USD} @ 2.50 USD
  Assets:Bank         500 USD
  Income:Capital-Gains
```

to:
```
2014-01-01 open Assets:Brokerage
2014-01-01 open Assets:Bank
2014-01-01 open Income:Capital-Gains:Short
2014-01-01 open Income:Capital-Gains:Long

2014-02-01 * "Buy"
  Assets:Brokerage    100 ORNG {1 USD}
  Assets:Bank        -100 USD

2016-02-01 * "Buy"
  Assets:Brokerage    100 ORNG {2 USD}
  Assets:Bank        -200 USD

2016-03-01 * "Sell"
  Assets:Brokerage   -100 ORNG {1 USD} @ 2.50 USD
  Assets:Brokerage   -100 ORNG {2 USD} @ 2.50 USD
  Assets:Bank         500 USD
  Income:Capital-Gains:Short -50 USD
  Income:Capital-Gains:Long -150 USD

```

WARNINGS:
- still under development
- doesn't work for leap years yet
- doesn't distinguish between reductions and short purchases (doesn't understand the
  latter)
- there are probably cases outside the current unit tests that this fails for

See beancount-reds-plugins for how to install and configure:

Added to Martin's contribs doc.

Red S

unread,
Apr 27, 2022, 5:16:17 AM4/27/22
to Beancount
Okay, this plugin is now usable. All problems above have been fixed, tests updated.

If you use it, I'd be interested in feedback, especially on how the configuration specification works (or not) for your account hierarchy.

Red S

unread,
Apr 29, 2022, 2:44:42 PM4/29/22
to Beancount
In addition to classifying capital gains as long-term or short-term (in the US), it is useful to classify them into gains and losses, useful for debugging taxes, or tax planning, and such.

Here is a second plugin, gain_loss, that trivially rewrites capital gains postings into losses and gains. For example, it converts:

2016-03-02 * "Sell"
 Assets:Brokerage   -100 ORNG {1 USD} @ 0.50 USD
 Assets:Bank          50 USD
 Income:Capital-Gains

to

2016-03-02 * "Sell"
 Assets:Brokerage   -100 ORNG {1 USD} @ 0.50 USD
 Assets:Bank          50 USD
 Income:Capital-Gains:Losses

An example config for using both plugins (long_short and gain_loss) is in the README.
Reply all
Reply to author
Forward
0 new messages