Tax Loss Harvesting with Beancount

This article is written for users in the US.


Tax loss harvesting (TLH) is a technique to improve the after-tax return of your taxable investments.” as mentioned in the link. Many automated investment services (aka Robo-advisors) provide automated tax-loss harvesting as a part of their services that you pay for. However, dear reader, you are a savvy user with a plain text accounting ledger, so why would you pay for these (a cut of your assets, every year in most cases!) when you can get it for free?

Actually, what you get for free from your ledger can be even better, as your ledger has an aggregate view of all your investments across all brokerages in a single interface, which is a requirement for doing TLH.

All right, enough with the smugness, let us jump into the details!


  • You are comfortable with TLH. You have done it already, or you have read about it, and are wanting to try it

  • You use Specific Identification of shares in your taxable accounts

  • Your Beancount ledger (or other supporting plain text ledger) is up to date with your transactions from all your brokerages, and with current prices (Setting up your importing to be lightning fast helps. See The Five-Minute Ledger Update)

  • This article is going to be centered around the fava_investor tlh module for Beancount and Fava, though I imagine this can be replicated in any plain text accounting ledger that supports cost tracking and querying.

    • There is a command line version too! Most fava_investor modules, including TLH, are built to work with plain Beancount on the command line without fava

What your TLH dashboard needs to tell you

At its core, your accounting software needs to show you a list of lots in taxable accounts (not tax-advantaged accounts) where the current price is lower than the cost. In addition, it is nice to know:

  • the brokerage
  • whether it would be a long term or short term loss
  • reminder of your favorite non-substantially identical alternates
  • whether a wash sale would be involved (see next section for more)

This is exactly what the main table of fava_investor shows. All screenshots in this article use this example file.

TLH main table

There are summary tables as well:

TLH summary tables

Wash sales

The Bogleheads wiki has a good informal description of a wash sale:

If you sell a mutual fund or stock for less than the purchase price, you have a capital loss, and you can usually report this as a loss and subtract it from your income. However, if you sold the shares and then bought them right back, or bought new shares and then immediately sold the old shares, the IRS will rule that you did not really sell them, and will not allow you to deduct the loss at that time.

Our ledger can warn us of wash sales.

What are your IRA accounts up to? First, note that you could have purchased a certain stock in a tax-advantaged account, which can still generate a wash sale in a taxable account. Even worse, the lowered basis in the tax-advantaged account is useless. This is where a personal ledger shines because unlike your brokerage, your ledger’s picture of your finances is complete. fava_investor provides the wash_pattern configuration option to handle this requirement.

When will a sale result in a wash? Next, the first day that a stock can be sold and TLH’ed without being considered a wash is the 31st day after the last purchase of the same ticker. fava_investor marks possible wash sales in the main table, and shows a separate table of purchases within the last 30 days:

TLH What not to TLH

When will a purchase result in a wash? Finally, once you sell a stock in order to TLH, buying it back within 30 days will make it a wash sale. To remind you of this, fava_investor shows you a table of what not to purchase, and until when:

TLH What not to buy

Comparison to a Robo-Advisor

An interesting question is to ask how our system compares against a robo-advisor, so we know if we are missing out on anything. A quick comparison of Betterment’s TLH advantage summary yields this:

Depends on your brokerage:

  • No extra trading costs to harvest losses

Depends on your strategy, independent of your plain text ledger:

  • Every harvested dollar reinvested
  • No short-term capital gains tax, ever
  • Harvest rebalance

Your plan text ledger actually has an advantage over your robo-advisor here:

  • IRA harvest protection
  • Customer-realized losses protected

That leaves us with “Automated algorithm that checks regularly”. This is certainly something an automated system can do better. One can easily setup a cron job to go do this and alert you if a loss opportunity is detected. It could also be argued that frequent TLH’ing – better accomplished with automation – would lead to better yields. However, I personally doubt that this will improve your after-tax returns with any significance, especially if your portfolio is built around broad market index funds where volatility is generally low. But how can we evaluate this to know for sure?

One way to evaluate is to compare what you harvested manually against what could have been harvested in a given time period. This is tricky: the right time to harvest is always only known in retrospect. Therefore, all one can evaluate is one’s heuristics or strategy (eg: I will TLH as soon as a stock slides over 1%). All the data needed for this is available in your ledger. A few scripts ought to do it. If you do so, please so share in the comments below.

One other part that automation handles is knowing when to pause regular purchases. We need to consult our ledger and note when it is safe to purchase a TLH’ed stock.


  1. Betterment TLH Methodology

Tax and financial laws vary geographically. This article is written for users in the US. Of course, I bear absolutely no liability if you read this article and make errors in your taxes or finances. This is not advice of any sort, legal, financial, or otherwise, yada yada.

Notes mentioning this note