tax calculators
How to Calculate Capital Gains Tax on Stock Sales
Capital gains tax on stock sales depends on three variables most investors underestimate: which shares you designate for sale when you own multiple lots, whether reinvested dividends are included in your cost basis, and whether the Net Investment Income Tax applies to your income level. This guide covers the full calculation with verified examples.

Calculating capital gains tax on stock sales is not just gain times rate. The tax bill depends on which lot of shares you sell, whether you have tracked reinvested dividends in your cost basis, and whether NIIT applies to your income. Miss any of these and you either overpay tax or file incorrectly. This guide walks through the exact five-step calculation with three real-dollar examples covering lot selection, forgotten basis additions, and the NIIT threshold.
The core formula is simple: capital gain = sale proceeds minus cost basis. But two variables make stock sales more complex than they appear. First, the holding period at the time of sale determines the applicable tax rate. Second, when you own multiple lots of the same stock, choosing which shares you sell can change the taxable gain by thousands of dollars — independent of the stock's price.
Every stock sale runs through the same five-step sequence:
- Identify the shares being sold and establish cost basis — purchase price plus eligible commissions per share. Each purchase lot carries its own basis and holding period.
- Calculate the capital gain: sale proceeds minus cost basis. If proceeds are lower than basis, you have a capital loss, which may offset other gains.
- Determine the holding period. Shares held more than one year are long-term; one year or less is short-term. Count from the day after purchase through the day of sale.
- Apply the federal tax rate. Long-term gains are taxed at 0%, 15%, or 20% based on taxable income and filing status. Short-term gains are taxed at ordinary income rates — the same rate as wages.
- Check for the Net Investment Income Tax. If MAGI exceeds $200,000 (single) or $250,000 (married filing jointly), an additional 3.8% NIIT applies to the lesser of net investment income or excess MAGI.
- Lot selection: when you hold multiple purchase lots, designating which shares to sell via specific identification can reduce the taxable gain dramatically compared to the FIFO default.
- Holding period at sale: crossing the one-year mark changes the applicable rate from ordinary income to long-term rates — the difference can exceed 10 percentage points.
- Reinvested dividends: each dividend reinvestment adds to your cost basis. Ignoring these additions leads to an overstated gain and overpaid tax.
- Capital loss offsets: losses from other sales in the same year reduce net capital gains dollar for dollar before any tax is applied.
- MAGI and filing status: determines which LTCG bracket applies (0%, 15%, 20%) and whether NIIT is triggered.
These three examples show how lot selection, forgotten basis additions, and NIIT each produce tax outcomes that differ sharply from a straightforward gain-times-rate estimate.
- Jordan, 34 — 22% ordinary income bracket, 15% LTCG rate: Jordan holds 300 shares of the same stock in three lots. Lot A: 100 shares at $80, purchased 4 years ago (long-term). Lot B: 100 shares at $140, purchased 2 years ago (long-term). Lot C: 100 shares at $175, purchased 7 months ago (short-term). Current price: $200. Jordan needs to sell 100 shares. Under FIFO (Lot A): gain = 100 × ($200 − $80) = $12,000 long-term → 15% = $1,800 tax. Using specific identification to sell Lot C (highest basis, short-term): gain = 100 × ($200 − $175) = $2,500 → 22% = $550 tax. Specific identification saves $1,250 — despite the short-term rate being higher, the dramatically smaller gain wins.
- Marcus, 47 — dividend reinvestment and forgotten basis: Marcus invested $10,000 in an index fund and held for 6 years, reinvesting all dividends. Over that period, $3,200 in dividends were reinvested, each adding to his cost basis. He sells the full position for $18,500. Without tracking reinvested dividends: gain = $18,500 − $10,000 = $8,500 → 15% = $1,275 tax. With correct basis ($10,000 + $3,200 = $13,200): gain = $18,500 − $13,200 = $5,300 → 15% = $795 tax. Marcus overpays $480 in capital gains tax if he uses only the original purchase price.
- Rebecca, 52 — NIIT at higher income: Rebecca is a single filer with $280,000 in MAGI. She sells stock for an $18,000 long-term gain. Her income is below the 20% LTCG threshold, so the base rate is 15% — $2,700. But her MAGI exceeds the NIIT threshold by $80,000. The NIIT base is the lesser of net investment income ($18,000) or the excess MAGI ($80,000), so NIIT applies to the full gain: 3.8% × $18,000 = $684. Total tax: $3,384, an effective rate of 18.8%. If she also harvests $5,000 in losses from another position, net investment income falls to $13,000 — saving $750 in LTCG tax and $190 in NIIT, a combined reduction of $940.
In each case, the variable that changed the tax bill had nothing to do with the stock's performance — it was the mechanics of which shares were sold, what basis was tracked, and which income threshold applied.
- Accepting FIFO by default without checking whether selling a higher-basis lot would produce a lower taxable gain.
- Forgetting to include reinvested dividends in cost basis — a persistent oversight that causes investors to overstate gains and overpay tax year after year.
- Assuming 15% is the ceiling for long-term capital gains and not accounting for the additional 3.8% NIIT that applies above the $200,000 or $250,000 MAGI threshold.
- Missing the one-year long-term threshold by days — holding 364 days versus 366 days can mean a 7 to 15 percentage point difference in federal tax rate.
- Triggering the wash sale rule by selling a stock for a loss and buying it back within 30 days before or after the sale, which disallows the loss for the current tax year.
Testing multiple lot scenarios, adjusting for reinvested dividends, and checking NIIT eligibility by hand across a portfolio takes time and is prone to error. A capital gains tax calculator lets you compare scenarios in seconds before placing a sell order.
- Compare the after-tax result of selling different lots before the trade settles.
- Model how a loss harvest changes net gain and NIIT exposure.
- Estimate whether waiting a few more weeks for long-term treatment is worth the price risk.
- See the effective rate — including NIIT — based on your income level.
Here are the questions investors most often ask when calculating capital gains tax on stock sales.
Capital gains tax on stock sales is more controllable than most investors realize. Lot selection, accurate basis tracking, and awareness of NIIT thresholds can each shift the tax bill meaningfully without changing the underlying investment position. Use the capital gains tax calculator above to model your specific situation before you sell.
Frequently asked questions
What is the capital gains tax rate on stock sales?
Long-term gains — shares held more than one year — are taxed at 0%, 15%, or 20% depending on taxable income and filing status. Short-term gains are taxed as ordinary income at your marginal rate. Higher-income filers may also owe an additional 3.8% Net Investment Income Tax, bringing the effective maximum long-term rate to 23.8%.
How do I calculate cost basis when I bought shares in multiple lots?
Each purchase lot has its own basis equal to the price paid plus eligible commissions per share. Under FIFO, the oldest shares are sold first. With specific identification, you designate exactly which lot you are selling at the time of the transaction. Tracking each lot separately gives you the most flexibility to minimize taxes.
Do reinvested dividends change my cost basis?
Yes. When dividends are reinvested to buy additional shares, those purchases increase your total cost basis. Using only your original purchase price as basis overstates your gain and leads to overpaid tax. Your brokerage year-end statements or Form 1099-B often include adjusted basis figures that account for reinvestments.
What is specific identification and how do I use it?
Specific identification lets you choose exactly which lot of shares you are selling rather than using the FIFO default. To use it, you must instruct your brokerage — typically through the platform at the time of placing the sell order — which lot you are designating. Most online brokerages offer this option before the trade settles.
What is the Net Investment Income Tax and when does it apply?
The NIIT is a 3.8% surtax on investment income — including capital gains — that applies when MAGI exceeds $200,000 for single filers or $250,000 for married filing jointly. It applies to the lesser of net investment income or MAGI above the threshold, so a large capital gain can trigger it even if most of your income falls below the threshold.
Can capital losses offset stock gains?
Yes. Capital losses from selling stocks or other assets at a loss offset capital gains dollar for dollar in the same tax year. If net losses exceed gains, up to $3,000 of the remaining loss can offset ordinary income per year, and the remainder carries forward to future years.
What happens if I sell shares I have held for just under one year?
Shares held one year or less are short-term and taxed at ordinary income rates. If your marginal rate is 22% or 24%, selling a few days before the one-year mark can cost significantly more than waiting. The long-term holding period begins the day after purchase and includes the day of sale.
How does the wash sale rule affect loss harvesting?
The wash sale rule disallows a capital loss if you buy the same or substantially identical security within 30 days before or after the sale that generated the loss. The disallowed loss is added to the basis of the replacement shares rather than being permanently lost, but you cannot claim it in the current tax year.
Do states tax capital gains separately from federal taxes?
Yes, in most states. Many states tax capital gains as ordinary income at the state rate with no distinction between long-term and short-term treatment. States like California apply the full ordinary income rate to capital gains. States with no income tax — including Florida, Texas, and Washington — mean federal rates represent the entire liability.
Should I sell before or after year-end to manage capital gains?
It depends on your gain and loss picture for the year. Selling before year-end lets you offset gains with any remaining losses in the same tax year. If you have a large gain and no offsets, delaying into the next year can spread the liability across two tax years. A capital gains tax calculator can model both scenarios before you decide.