I think the answer falls out of the IRS's Publication 550 section on More or less stock bought than sold.:
If the number of shares of substantially identical stock or securities you buy within 30 days before or after the sale is either more or less than the number of shares you sold, you must determine the particular shares to which the wash sale rules apply. You do this by matching the shares bought with an equal number of the shares sold. Match the shares bought in the same order that you bought them, beginning with the first shares bought. The shares or securities so matched are subject to the wash sale rules.
So to reiterate my example and put some dates on it as well:
- Day 0 — buy 20 shares of stock at $100/share
- Day 60 — sell 10 shares at $90/share
- Day 65 — buy 5 shares at $95/share
- Day 85 — sell 1 share at $101/share
- Day 90 — sell 10 shares at $95/share [changed]
- Day 96 — buy N shares at $X/share
The key part I was missing is that any wash sale loss needs to be considered per-share rather than as a whole. So in step #2, I need to track the loss specifically as $10/share on 10 shares rather than a general "$100 loss". Then as of step #3, I could still book a $10/share loss on 5 of those shares. The rest of the loss gets applied to the cost basis of some of the original (step #1) shares, as if I had bought the first 5 of the remaining ten shares at $110/share instead of $100.
Now in step #4, I am actually selling at a $9/share loss as well since the basis for this $101 share has been adjusted to $110. Because it is less than 30 days from the buy of step #3 this is already a wash. This gets applied to the first of the remaining shares of the step #1 block.
At this point, as we come to step #5 (which I changed from my original example to cover more territory…) the cost basis of the shares I hold from earliest to latest would be:
- 1 share with basis $100+10+9 = $119/share
- 8 shares with basis $100+10 = $110/share
- 5 shares with basis $95/share
If I sell ten of these shares at $95, I am actually selling…
- 1 share at a $24/share loss
- 8 shares at a $15/share loss
- 1 share at a $0/share break-even
But the first five of these are again wash sales, due to step #3 still being within the ±30 day window! I am now holding:
- 1 share with basis $95+24 = $119/share
- 3 shares with basis $95+15 = $110/share
…and I have 1 outstanding $15/share "wash sale" loss, and a possible capital net of 4 shares times $15/share loss (plus 1 share at $0/share break-even).
Finally bringing us to step #6. If I haven't completely muddled this already […again], here's my understanding:
- if N is 0, i.e. I stop trading and leave off for more than thirty days, I cannot book the 1 pending $15/share wash loss. I do book a net capital loss of $60 from the 5 non-wash shares.
- if N is 1 or more, the cost basis of the first share I buy gets the $15/share outstanding wash loss added to its price. The next four shares after that also create a wash of the earlier $15/share losses. (The sixth share in a sense also gets adjusted if you consider $0/share a "loss"…) The cost basis for the seventh and following shares is simply the price, assuming no further activity for the next 30 days.
There's at least one other rule that may come into play here:
Loss from a wash sale of one block of stock or securities cannot be used to reduce any gains on identical blocks sold the same day.
…but I think this answer is complicated (and potentially wrong!) enough as it is already…