How to Calculate Capital Gains Tax on Stocks — With Examples

#capital gains tax#stock taxes#cost basis#investment taxes#selling stocks

You sold some stocks and made a profit. Now you need to figure out what you owe the IRS. The calculation seems straightforward — sale price minus purchase price — but cost basis methods, reinvested dividends, and wash sale rules create complications that catch investors off guard every tax season.

Run your stock sale through the Capital Gains Tax Calculator to see your exact tax

The Basic Formula

Capital Gain = Sale Proceeds – Cost Basis – Selling Costs

  • Sale proceeds: The total amount you received from the sale
  • Cost basis: What you originally paid, including commissions
  • Selling costs: Trading fees (though most brokers now charge $0)

Example: You bought 100 shares of XYZ at $50/share ($5,000 total) and sold them at $80/share ($8,000). Your capital gain is $8,000 – $5,000 = $3,000.

If you held the shares for over one year, that $3,000 is taxed at long-term capital gains rates (0%, 15%, or 20%). Under one year, it's taxed as ordinary income.


Cost Basis Methods — They Change Your Tax

When you buy the same stock at different times and prices, which shares are you "selling"? The method you choose changes your taxable gain.

MethodHow It WorksBest For
FIFO (First In, First Out)Oldest shares sold firstDefault if you don't specify
Specific IdentificationYou choose exactly which shares to sellMaximizing tax efficiency
Average CostTotal cost ÷ total sharesMutual funds (simplified)

Why This Matters — Example

You bought shares of ABC stock over three years:

  • Lot 1: 50 shares at $40 (Jan 2023)
  • Lot 2: 50 shares at $60 (Jun 2024)
  • Lot 3: 50 shares at $80 (Mar 2025)

Current price: $90. You want to sell 50 shares.

MethodShares SoldCost BasisGainTax at 15%
FIFOLot 1 ($40)$2,000$2,500$375
Specific ID (Lot 3)Lot 3 ($80)$4,000$500$75
Average CostAverage ($60)$3,000$1,500$225

Choosing Lot 3 (highest cost basis) saves $300 in taxes. But Lot 3 was bought less than a year ago — so it would be taxed at short-term rates instead. You need to weigh the cost basis advantage against the rate difference.

Pro tip: Tell your broker to use specific identification before you sell. Most online brokerages let you select specific lots at the time of sale.


Reinvested Dividends — The Forgotten Cost Basis

If you reinvest dividends through a DRIP (Dividend Reinvestment Plan), each reinvestment is a separate purchase with its own cost basis. Many investors forget this and end up paying tax twice on the same money.

Example: You own 100 shares of a stock that pays $2/share in dividends annually. Over 5 years, you reinvested $1,000 in dividends, buying additional shares at various prices. When you sell everything, your cost basis includes both your original purchase and the $1,000 in reinvested dividends.

If you forget to add the reinvested dividends to your cost basis, you'll report $1,000 more in gains than you actually earned — and you already paid income tax on those dividends when you received them.

Your brokerage's 1099-B should track this, but verify the numbers. Transfers between brokers sometimes cause cost basis data to get lost.


The Wash Sale Rule

You can't sell a stock at a loss and buy it back within 30 days (before or after the sale) to claim the tax deduction. The IRS disallows the loss and adds it to the cost basis of the replacement shares.

ScenarioResult
Sell XYZ at a $2,000 loss, buy XYZ back 15 days laterLoss disallowed — added to new shares' cost basis
Sell XYZ at a $2,000 loss, buy XYZ back 35 days laterLoss allowed — $2,000 deductible
Sell XYZ at a $2,000 loss, buy similar ETF same dayLoss allowed (different security)
Sell XYZ at a $2,000 loss, buy XYZ in your IRALoss disallowed — wash sale applies across accounts

The wash sale rule applies across all your accounts — including IRAs. Selling a stock at a loss in your taxable account and buying it in your Roth IRA within 30 days triggers a wash sale, and the loss is permanently disallowed.

Read more about wash sale rule details.


Stock Losses — How to Use Them

Capital losses offset capital gains dollar-for-dollar. If your total losses exceed your gains, you can deduct up to $3,000 per year ($1,500 if married filing separately) against ordinary income. Unused losses carry forward indefinitely.

Your SituationTax Treatment
$10,000 gains, $6,000 lossesPay tax on $4,000 net gain
$5,000 gains, $12,000 losses$5,000 offsets gains + $3,000 deducts from income. $4,000 carries to next year
$0 gains, $8,000 losses$3,000 deducts from income. $5,000 carries forward

Short-term losses offset short-term gains first, then long-term gains. Long-term losses offset long-term gains first, then short-term gains.


How Stock Sales Are Reported to the IRS

Your brokerage sends you Form 1099-B by mid-February, showing every sale you made during the year. You report these on Schedule D and Form 8949.

FormPurpose
1099-BBroker reports your sales (proceeds and cost basis)
Form 8949You list each transaction with gain/loss calculations
Schedule DSummary of all capital gains and losses

Most tax software imports 1099-B data directly. But if you transferred shares between brokers, check that cost basis is correct — missing basis data is the most common source of overpaying capital gains tax.


Tax on Stock Dividends

Stock dividends are taxed separately from capital gains:

Dividend TypeTax Rate
Qualified dividends0%, 15%, or 20% (same as long-term capital gains)
Ordinary (non-qualified) dividendsYour marginal income tax rate

Most dividends from US companies held 60+ days are qualified. Dividends reported on your 1099-DIV show the breakdown between qualified and ordinary.

See how dividends and gains affect your overall tax with the Federal Tax Calculator.


Bottom Line

Calculating stock gains tax comes down to: sale price minus cost basis, with the rate depending on holding period. Use specific identification to sell your highest-cost-basis lots first, don't forget reinvested dividends in your cost basis, and avoid triggering wash sales when harvesting losses. Before selling, plug your numbers into the Capital Gains Tax Calculator — especially if you're close to a rate threshold or the one-year holding mark.

Share this article