How to Calculate Capital Gains Tax on Stocks — With Examples
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.
| Method | How It Works | Best For |
|---|---|---|
| FIFO (First In, First Out) | Oldest shares sold first | Default if you don't specify |
| Specific Identification | You choose exactly which shares to sell | Maximizing tax efficiency |
| Average Cost | Total cost ÷ total shares | Mutual 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.
| Method | Shares Sold | Cost Basis | Gain | Tax at 15% |
|---|---|---|---|---|
| FIFO | Lot 1 ($40) | $2,000 | $2,500 | $375 |
| Specific ID (Lot 3) | Lot 3 ($80) | $4,000 | $500 | $75 |
| Average Cost | Average ($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.
| Scenario | Result |
|---|---|
| Sell XYZ at a $2,000 loss, buy XYZ back 15 days later | Loss disallowed — added to new shares' cost basis |
| Sell XYZ at a $2,000 loss, buy XYZ back 35 days later | Loss allowed — $2,000 deductible |
| Sell XYZ at a $2,000 loss, buy similar ETF same day | Loss allowed (different security) |
| Sell XYZ at a $2,000 loss, buy XYZ in your IRA | Loss 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 Situation | Tax Treatment |
|---|---|
| $10,000 gains, $6,000 losses | Pay 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.
| Form | Purpose |
|---|---|
| 1099-B | Broker reports your sales (proceeds and cost basis) |
| Form 8949 | You list each transaction with gain/loss calculations |
| Schedule D | Summary 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 Type | Tax Rate |
|---|---|
| Qualified dividends | 0%, 15%, or 20% (same as long-term capital gains) |
| Ordinary (non-qualified) dividends | Your 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