CalcMaven
Tax & Investing

How to Calculate Capital Gains Tax on Stock Sales

By Alex B.|Updated April 9, 2026|8 min read

For informational purposes only, not financial advice. Full disclaimer

Calculating capital gains tax on a stock sale comes down to four moving parts: what you paid, what you sold it for, how long you held it, and how that gain stacks on top of the rest of your income. The tax formula itself is simple, but cost-basis rules, lot selection, and wash-sale traps make the real-world calculation less obvious than many investors expect.

The IRS reports most stock sales on Form 1099-B, and taxpayers generally reconcile the sale on Form 8949 and Schedule D. IRS Topic No. 409 explains the broad rules for capital gains and losses.

Run the Stock Sale Math

Enter purchase price, sale price, holding period, income, and filing status to estimate the federal tax on your gain.

Try the Capital Gains Tax Calculator

The Core Formula

Capital gain or loss = Net sale proceeds - Adjusted cost basis
  1. Start with your adjusted cost basis for the shares sold.
  2. Subtract commissions or selling costs from the gross proceeds to get net sale proceeds.
  3. Subtract cost basis from net proceeds to find the gain or loss.
  4. Check the holding period: one year or less is short-term, more than one year is long-term.
  5. Apply ordinary-income treatment for short-term gains or long-term capital gains treatment for long-term gains.

Step 1: Confirm Your Cost Basis

For a single-lot stock purchase, cost basis is usually just the purchase price plus any purchase commission. But many investors buy shares over time through recurring contributions, dividend reinvestment, or multiple entry points. In those cases, lot selection matters.

If you do nothing, a brokerage may default to FIFO, which usually sells the oldest shares first. Specific identification lets you choose which lot to sell. That can reduce the gain, control whether the sale is long-term or short-term, and lower the tax bill.

Example Calculation

You bought 100 shares at $40, another 100 shares at $58, and then sold 100 shares at $72.

  1. If FIFO applies, the sold lot may be the $40 shares, creating a $32 per-share gain.
  2. If you specifically identify the $58 shares, the gain drops to $14 per share.
  3. On 100 shares, FIFO creates a $3,200 gain while specific identification creates a $1,400 gain.

Lot selection can change the taxable gain dramatically even when the sale price is the same.

Step 2: Check the Holding Period

A stock held for one year or less creates a short-term capital gain, which is generally taxed like ordinary income. A stock held for more than one year creates a long-term gain, which generally falls into the 0%, 15%, or 20% federal long-term capital gains structure under current law.

For planning, that one-day difference can matter a lot. Investors who are close to the one-year mark often compare the tax cost of selling today with the after-tax outcome of waiting until the position qualifies for long-term treatment.

Step 3: Estimate the Tax Layer

Example Calculation

You sell stock for a $12,000 gain after holding it for 18 months. Your other taxable income already places you in the 15% long-term capital gains bucket.

  1. Long-term gain: $12,000
  2. Applicable long-term rate in this example: 15%
  3. Federal capital gains tax estimate: $12,000 x 15% = $1,800

The estimated federal tax is $1,800 before considering any offsetting capital losses or the possible 3.8% NIIT layer for high-income taxpayers.

Watch the NIIT Layer

High-income investors may owe an extra 3.8% Net Investment Income Tax on some or all of a stock gain when total income crosses the IRS threshold.

Read the NIIT Guide

Capital Losses and the Wash-Sale Trap

Losses on stock sales can offset capital gains, and net losses can reduce ordinary income up to the annual IRS limit. But the wash-sale rule can disallow the loss if you buy the same or a substantially identical security within the 30-day window before or after the sale.

That rule matters most when investors sell a losing position for tax reasons and immediately buy it back. The loss is not gone forever, but it usually gets rolled into the basis of the replacement shares instead of helping on the current return.

Do Not Harvest and Rebuy Too Fast

If you are realizing a stock loss for tax purposes, make sure you understand the 30-day wash-sale window before buying back the same position.

What You Need at Tax Time

  • Your broker Form 1099-B
  • Trade confirmations or lot history if basis is incomplete
  • Any records of reinvested dividends that increased basis
  • Form 8949 and Schedule D support for gains and losses

The cleanest process is to track the sale at the lot level when you execute it, not months later during tax prep. Knowing the basis method and holding period before you click sell often matters more than trying to optimize after the fact.

Frequently Asked Questions

How do I calculate capital gains tax on stocks?+
Subtract adjusted cost basis from net sale proceeds, determine whether the shares were short-term or long-term, and then apply the relevant federal tax treatment. Short-term gains are usually taxed like ordinary income, while long-term gains usually use the 0%, 15%, or 20% federal structure.
What cost basis should I use when I bought shares at different prices?+
That depends on your brokerage basis method. FIFO is common by default, but specific identification may let you choose the lot with the best tax result. Review the lot method before selling.
Do I owe tax if I reinvest the stock sale proceeds right away?+
Yes. Reinvesting the cash does not cancel the tax event. Once you sell at a gain, the taxable sale has already happened and the new purchase simply creates a new basis for the replacement position.
What is the wash-sale rule for stocks?+
The wash-sale rule generally disallows a capital loss if you buy the same or a substantially identical security within 30 days before or after the sale. The disallowed loss is usually added to the basis of the replacement shares.
Can stock losses offset stock gains?+
Yes. Capital losses offset capital gains, and if losses exceed gains you can usually deduct up to the annual IRS limit against ordinary income, with the remaining loss carried forward.

Related Calculators

Related Guides

Continue through the same topic cluster with connected examples and formulas.

Disclaimer: This article is for informational purposes only and does not constitute financial advice. Consult a qualified financial advisor for decisions about your specific situation.

How to Calculate Capital Gains Tax on Stock Sales | CalcMaven