The MegaWash: How to compute capital gain tax in the UK

Life in UK be like

Let’s say you are UK resident and at least one of these is true

  • part of your income is paid as equity (~you’re in FAANG/any other public company)
  • you ever sold and bought same type of shares back with <30 day gap in transactions
  • you traded the same stock in >2 transactions (i.e. anything except just BUY->SELL (all). e.g. BUY(price1)+BUY(price2)->SELL or BUY->SELL (not all), etc)
  • you exist
  • [random other conditions]

When…

  • Congratulations! Your tax calculation is really complicated until the end of days

Since I have also fallen into some of the traps earlier I wanted to figure it out once and for all.

In this article you’ll understand

  • rules and algorithm for UK capital gain* tax computation (Section 104 pool, same day, bed and breakfast rules, and… lots of nuances you can miss)
  • how to compute it in practice
  • how to save on your taxes** when you understand the rules.

*limited to stock/shares (capital gain in general can include many other assets, e.g. real estate which are outside the scope of this article)

**be careful with that and be sure to read the disclaimer in relevant section

Definitions

Asset
Anything you own that can have financial value. For the purposes of this article, we’ll limit the definition of an asset to shares/stocks but generally could also be property, art, etc.

Capital Gain
The profit you make when you sell an asset for more than what you paid for it. Computed as the total proceeds (“sell price”) minus allowable costs (“buy price” but more on that later).
Example: You bought 100 shares of XYZ company for £10 each (£1,000 total). Later, you sold all 100 shares for £15 each (£1,500 total).
Capital Gain = £1,500 - £1,000 = £500 profit. (If you sold for less, it would be a capital loss.)

Note: in this article terms buy/acquire and sell/dispose are used interchangeably for simplicity (generally speaking acquire can be more not just buy, e.g. your employer gives you shares), it does not affect tax computation.

Capital Gains Tax (CGT)
The capital gain is taxable, meaning you pay tax on your total gains if they exceed the “tax-free allowance” (Annual Exempt Amount) set by HMRC. This allowance lets you have relatively small gains without paying tax.

2022-23 Tax-Free Allowance: £12,300
2023-24 Tax-Free Allowance: £6,000
2024-25 Tax-Free Allowance: £3,000

If your total gains (across all assets for the entire tax year) exceed the allowance, you’ll pay CGT on the amount above it.

Sounds simple, right? Now there is something called a wash sale.

Wash sale

Imagine it’s April 5th, the last day of the tax year, and your total gain is way over the tax-free allowance. However, you also have some shares of XYZ company in your portfolio that, if sold now, would result in a significant loss. So, you sell them and… immediately buy them back.

Effectively, nothing changes in your portfolio - you still hold the same shares, and you haven’t spent any money (aside from maybe small transaction fees). But you’ve now added a new loss to offset your total yearly gain.

You pay less tax!

Or do you?

Same day rule

HMRC has special rules on how to determine the cost basis for the sell transaction.

Let’s say you did the following

  • [June 1 2023] bought 100 shares of XYZ by £20/share
  • [Apr 5 2024, 1pm] sell 100 shares of XYZ by £10/share
  • [Apr 5 2024, 3pm] buy 100 shares of XYZ by £9.95/share

You’d expect to have loss of 100*(£10-£20)=-£1000
But with Same Day Rule applied (matches disposal of share with acquisition of same type of share on the same day)
You would gain 100*(£10-£9.95)=£5 as result

Bed and breakfast rule (B&B)

Hmm, but I really need that loss, what if I buy back 1 day later? i.e.

  • [June 1 2023] buy 100 shares of XYZ by £20/share
  • [Apr 5 2024] sell 100 shares of XYZ by £10/share
  • [Apr 6 2024] buy 100 shares of XYZ by £9.95/share

Now the Bed and Breakfast rule applies instead, and you have £5 gain as before (matches disposal of share with acquisition of same type of share up to 30 days upfront).

In order to “just sell it normally” matching with the past acquisitions you need to buy back (if at all) no earlier 31 days after the disposal.

These rules exist exactly to prevent such sort of tax evasive behaviour, when artificial transactions lead to no capital gains.

Section 104 pool

Now imagine you work for XYZ company and they award you stock compensation every month (and it’s public company so shares are listed and you can trade them openly)

  • [June 13 2023] awarded 100 shares of XYZ by £20/share
  • [July 13 2023] awarded 100 shares of XYZ by £23/share
  • [Aug 13 2023] awarded 100 shares of XYZ by £47/share
  • [Aug 20 2023] sold 200 shares of XYZ by £40/share

What’s the cost basis for the sale?

  • In most countries (e.g. in the US) you should track individual shares, i.e. when you sell 200 shares it matters which shares* you sold, and that would define the cost basis.

  • In the UK it doesn’t matter which shares you sold. For tax purposes they are all considered identical and part of the same pool (Section 104 rule). The pool is computed historically by averaging all acquisitions which go to the pool and deducting disposals which are matched to the pool chronologically.

In this particular example cost basis is (100*£20 + 100*£23 + 100*£47) / 300 * 200 = £30*200=£6000. And when we deduct the disposal the pool will still be £30/share, only the total amount would be reduced to 100 shares.

*usually when you do part sale you can select strategy, e.g. FIFO, lowest price first, etc or select shares to sell manually

Full algorithm

In practice to compute the capital gain you’ll need:

  • All your broker transactions since the time you become a UK resident. For all brokers you have during this period, including, if applicable, international accounts.
  • If you relocated to the UK and had any shares at the moment of becoming resident
    • the date of first day you were considered resident
    • the shares you owned at that date

The simplified algorithm:

  • compute initial prices for all stocks you owned (on day you become resident) to initialize Section 104 pools

  • aggregate transactions from all your brokers into 1 list, sorted by transaction day (not limited to just current tax year - include both all previous transactions since becoming resident and at least +30 days after current tax year so B&B rule will be applied correctly)

  • if there are multiple BUY or SELL (acquire/dispose) transactions for the same stock on the same day - merge them into 1 transaction (use weighted average for the price), i.e. at most 1 BUY and 1 SELL per stock&day will be present after this step.

  • go through transactions chronologically and apply (in this order of priority) - same day, b&b, section 104 rules to update section 104 pool price and compute gains/losses per transaction

  • total gain is sum of all gains you had over the given tax year

The full algorithm for capital gain tax
  • Determine initial cost in Section 104 pool
    • If you didn’t have shares at relocation time / you’re originally from UK - 0, empty pool
    • If you relocated and had >0 shares at the date of becoming resident
      • For each stock price per share is determined as price at the market closing on the day you became the UK resident is your initial price for Section 104 pool.
  • For each calendar day
    • If there are multiple acquisitions of same stock - match them into single transaction with average price (total cost / number of shares)
    • If there are multiple disposals of same stock - match them into single transaction with average price (total cost / number of shares)
  • Match same day same stock acquire/disposal transactions when applicable
    • Number of shares matched =min(shares acquired, shares disposed off)
    • [if there’s any remainder - it should be matched in the next steps]
  • Match B&B 30-day rule in a similar way for the remainder. Matching goes chronologically since the date of your residency (note: it’s insufficient to match just in the current tax year because transactions from it can batch both into previous tax year and into the next one. For the previous tax year the matching can be continued on and on theoretically if you had many complex transactions until the day of you becoming resident. For the next tax year you need to have at least +30 days of that tax year for proper matching)
    • Chronologically, since the day of you becoming resident
      • If there’s sale unmatched on that day - try +1, +2, … +30 days one by one
      • If there’s unmatched acquisition - match min(shares acquired, shares disposed off)
      • [if there’s any remainder - should be matched on next steps]
  • Calculate Section 104 pool over time and compute remaining gains chronologically
    • Unmatched sale amount should be matched with the pool, decreasing amount of shares available but not changing cost per share
    • Unmatched acquisition amount is integrated into the pool by averaging the cost
      • new cost per share = (current cost per share in pool * num shares in pool + acquisition cost per share * num shares acquired) / (num shares in pool + num shares acquired)

Caveat: fees, currency translation, splits, mergers and spin offs (company can be split into multiple companies and shares cost basis changes accordingly to the proportion of the split) are omitted for simplicity

Notes: It is not sufficient to only have information about the current tax year. (B&B rule can apply both from current to past and from current to next tax years. And on these years it can continue to other years… and so on)

The “fun” part

Now finally, can the system be exploited somehow? Like, you should be able to save some of your taxes after learning all that, right?

The MegaWash sale (=never pay tax?)

Let’s say you have some shares of XYZ and if you’d sell them right now you’d have £100k gain. Can you prevent the gain from happening instead?

If you sell all of the shares, buy them all back - there’s no gain by applying the same day rule. And when the next day you sell all the shares again. Assuming the price is the same as the day before there’s no gain at this time! Because the base cost is set at the time you bought them back.

Remember, we started with wash sale - and the B&B rules were mainly aimed to prevent it. But now they created an even bigger issue (I call it the megawash sale). The original wash sale was limited by the amount of loss you had at the moment in your portfolio individual stocks. But the megawash sale has unlimited offset - any gain can be completely avoided!

You can even enhance this strategy - every time the stock you own reaches a new maximum you do same day sell & buy back - which resets the price to a new higher base cost.

GAAR

When you think you are too smart

The thing is that HMRC didn’t issue rules for you to exploit. The rules are to make their life easier, not yours. For those feeling too smart and willing to exploit the rules there’s GAAR (General Anti-Abuse Rule), which prevents you from using any other rules for the sole purpose of avoiding/reducing the tax (e.g. if you introduce artificial transactions, use loopholes, etc). The HMRC provides you several ways you can not pay tax completely legally (yearly tax-free allowance, trading under ISA, pensions, etc). But there is a capital gain tax beyond that, and it’s clearly intended for everyone, not just for dumb people who don’t read the rules carefully.

You might be curious if there is a way to… abuse the anti-abuse rule?

Probably. No rule is perfect, so ~trading strategy where “bad actors” (who want to only/primarily save tax) and reasonable people are indistinguishable might be the way (it will still be a gray area and depend e.g. on the amount you trade/other evidence/etc). BUT even if there’s a loophole you shouldn’t go for it. The tax rate for capital gain tax is 18-24% (not quite as bad as 45% for income tax highest bracket). Or do you want to be notorious like rich people who pay 0% tax? I’d say it’s not worth it.

Acceptable strategies

*to the best of the author’s knowledge; not financial/legal advice.

Claim loss + maybe wait >30 days to buy back

If you have gains in the year > tax-free amount you can offset them by selling shares which are at loss. And you can theoretically buy them back in 31+days. 30 days for B&B is included to prevent immediate manipulation - after that if you decide to buy back it is far not a risk-free strategy because the price may change significantly, so generally it’s allowed. Although if you consistently use this rule you might end up under some scrutiny anyway.

Buying back is not necessarily wise, do so only if you believe this stock will actually increase in price, you can always immediately buy other shares or do some alternative investment. Do not forget - your goal is not to minimize tax but to maximize profit :)

Relocation

If you intend to eventually relocate from the UK there are a few things you can do to lower your initial share price the moment you become resident of another country and stop being a UK resident.

1 - Sell cheapest shares first At any moment if you sell part (but not all) of the stock you own - select the method to pick the shares obtained by lowest price as priority method in your broker settings. It doesn't have any effect on the UK tax but may have effect at the time you become resident of the other country.

Example: you had 100 shares of XYZ obtained by £1 and 100 shares of XYZ obtained by £1000. You sold 100 shares. You relocated out (to, say, the US, which doesn’t automatically adjust the price). On the first day of your residency in a new country you sold the remaining 100 shares for £100. In case you selected “cheapest first” priority as suggested in this advice - you will rightfully declare loss of 100*(£100-£1000)=-£90000. In case you sold “most expensive first” - you’d declare gain of 100*(£100-£1)=£9900.

2 - Last residency day sales Let's say you almost relocated - a few days only left for when you'd stop being a UK resident. And you do have huge gains on some of the shares you own (you haven't realised the gains as you still own the shares). Most countries (like the US) do not automatically adjust the price of your shares on the day of relocation like the UK do.

So in order to adjust it - you need to sell & buy back all the shares where you have gains (you can keep shares where you have loss - so that loss is still carried to your new place or if you choose so; or claim the loss w/o buying back to offset your capital gains in the UK in that year - depends on your situation)

Note: lowest price first sell strategy can give extra benefit to this strategy since you can check for the shares individually if they are at gain/loss wrt their buy time (unlike section 104 pool), and sell only ones which are at gain)

Final notes

I hope it was helpful and now you understand the B&B rules better. In practice:

  • If you plan to relocate to the UK soon - know that it will readjust the shares you’d own at the moment of relocation to current market price - so you may want to do smth to improve tax in your current country.

  • Keep all transactions from all brokers, since the start of your UK residency

  • For computing the tax

    • if you can do some programming there are some good repositories - 1, 2 (usually you’d need to manually fix the .csv files or the source code since the export format from brokers changes frequently - most likely will work out within a in a few minutes but can be up to couple hours).
    • one of these repos^ is wrapped into a service. As of the date of this publication, it provides report in similar format and you don’t need to install or pay anything. Computation is entirely in browser and your data is not sent anywere, but there might be issues with parsing your documents similar to the ones open-source repositories have.