The same stock, held in four different accounts, produces four different tax bills. None of them depends on the stock. All of them depend on what kind of return it produces and which account it sits in.

A four-by-three grid: for each of taxable, traditional IRA or 401(k), Roth, and HSA, whether money is taxed going in, while it grows, and coming out. The taxable account is taxed while it grows at a rate that depends on the return type; the traditional account is taxed only on the way out, at the ordinary rate; the Roth and the federal HSA are not taxed coming out.
When each account taxes a dollar. Only the taxable account cares what kind of return the dollar was. California taxes the HSA every year regardless.

Three treatments, not four

Read across the rows and the four accounts collapse into three treatments.

  • Taxed as you go, at a rate that depends on the kind of return. The taxable account. Long-term gains and qualified dividends get the lower rate; short-term gains and interest get the higher one.
  • Taxed once at the end, at the ordinary rate, whatever the return was. The traditional IRA and 401(k). A long-term gain earned inside comes out taxed like salary. So does a short-term one. The account does not care.
  • Never taxed. The Roth, and (federally) the HSA. Return type does not matter here either.

The rule that falls out of the table

In two of the three treatments the kind of return is irrelevant. In the taxable account it is everything. So the taxable account is where return that already gets the low rate belongs, and the sheltered accounts are where return that would be taxed at the high rate belongs.

Long-term gains and qualified dividends: taxable account or Roth. Short-term gains, interest, anything with high turnover: traditional IRA or 401(k), where turnover is free and the exit tax is the same whether the money came from patient holding or frantic trading.

That is the whole idea of asset location. The next read applies it to the arrangement most households actually have, which is the reverse.