redstreet / beancount_reds_plugins

Miscellaneous plugins for Beancount double entry accounting
GNU General Public License v3.0
95 stars 18 forks source link

feat(rename_accounts): accept regexes for renames #20

Closed ankurdave closed 2 years ago

ankurdave commented 2 years ago

This PR adds support for regex account renames with capturing groups and backreferences.

One use case is calculating the net inflows into an account, excluding internal flows such as dividends, realized capital gains, and fees. Regexes can be used to move these internal flows into Assets:, preventing them from affecting the account balance.

Unfortunately, this does affect performance even for non-regex renames. On my ledger with 10,000 transactions, with a rename that affects 165 postings, it increases the runtime from 26 ms to 35 ms. If we want to avoid this regression, we could try to detect non-regex renames and use a fast path for those, or we could add a separate user-facing configuration for regex renames.

redstreet commented 2 years ago

Nice, and thank you! I think 35ms is a very acceptable performance level for the benefit.

redstreet commented 2 years ago

And thanks for the compliant commit message!

ankurdave commented 2 years ago

Thanks for the quick reviews!

redstreet commented 2 years ago

Welcome!

I'd be very interested in hearing your experience with using renames to solve finding net inflows. Sounds like that would make an IRR computation simpler and lightweight as you'd mentioned, why we didn't the ability to "zoom in" all the way down to specific funds. It's been on my wish list to put together for when I'm less time constrained.

ankurdave commented 2 years ago

Renames do seem to work well for finding net inflows, and the rename_accounts plugin makes it easy to plot the inflows over time in Fava.

For the IRR computation we also need the beginning and ending market values, so I don't think it can be done purely with renames - we'd at least need to toggle the renames on and off. But the rename-based mapping from each income/expenses account to the corresponding asset account makes it easy to write code to extract the cashflows in between.

I prototyped an IRR computation based on a rename map that can analyze all accounts in one pass and show individual accounts' cashflows for debugging. Ideally it would display in Fava, but currently here's what it looks like to use:

$ python3 ../portfolio-returns/irr.py --asset-account-map '{"Assets(:.+)?": r"\g<0>", "Expenses(:.+)?:Fees(:.+)?$": r"Assets\1\2", "Income(:.+)?:(CapitalGains|Dividends|Interest|LongTermCapitalGainsDistributions|ShortTermCapitalGainsDistributions)(:.+)?$": r"Assets\1\3",}' main.beancount

Asset Account                                       Net Inflows   Market Value               IRR
------------------------------------------------  -------------  -------------  ----------------
Assets                                              $XXX,XXX.XX    $XXX,XXX.XX             X.XX%
Assets:Ally                                          $XX,XXX.XX     $XX,XXX.XX             1.09%
Assets:Ally:Savings                                  $XX,XXX.XX     $XX,XXX.XX             1.09%
Assets:BankOfAmerica                                 $XX,XXX.XX     $XX,XXX.XX             0.01%
Assets:BankOfAmerica:Checking                        $XX,XXX.XX     $XX,XXX.XX            -0.00%
Assets:BankOfAmerica:Savings                            $XXX.XX        $XXX.XX             0.00%
Assets:Vanguard                                     $XXX,XXX.XX    $XXX,XXX.XX             7.30%
Assets:Vanguard:Taxable                             $XXX,XXX.XX    $XXX,XXX.XX             6.32%
Assets:Vanguard:Taxable:BND                          $XX,XXX.XX     $XX,XXX.XX            -2.07%
Assets:Vanguard:Taxable:BNDX                         $XX,XXX.XX     $XX,XXX.XX            -3.89%
Assets:Vanguard:Taxable:Cash                              $X.XX          $X.XX            -0.00%
Assets:Vanguard:Taxable:VMFXX                         $X,XXX.XX      $X,XXX.XX             0.61%
Assets:Vanguard:Taxable:VTI                           $X,XXX.XX     $XX,XXX.XX            11.31%
Assets:Vanguard:Taxable:VXUS                         $XX,XXX.XX     $XX,XXX.XX           -21.00%