Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rename_accounts): accept regexes for renames #20

Merged
merged 1 commit into from
Aug 26, 2022

Conversation

ankurdave
Copy link
Contributor

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 redstreet merged commit 7f873fb into redstreet:main Aug 26, 2022
@redstreet
Copy link
Owner

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

@redstreet
Copy link
Owner

And thanks for the compliant commit message!

@ankurdave
Copy link
Contributor Author

Thanks for the quick reviews!

@redstreet
Copy link
Owner

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
Copy link
Contributor Author

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%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants