How to Calculate Cost Basis When You Bought a Stock at Different Prices
Table of Contents
Most retail investors do not buy a stock once — they buy it many times over months or years, sometimes through automatic recurring investments, sometimes opportunistically when the price drops. The result is a position made up of many "lots" purchased at many different prices. Calculating your true profit when you sell becomes more complicated than the simple "buy at A, sell at B" math.
This guide shows how to calculate the weighted average cost basis for any multi-lot position and how to plug it into free stock profit calculator to get your real profit. Once you know the trick, it becomes one calculation.
What "Cost Basis" Means
Cost basis is your total investment in a stock, including the price per share AND any commissions or fees you paid. When you sell, you compare the proceeds against the cost basis to determine your gain or loss.
For a single purchase, cost basis is simple: 100 shares at $50 = $5,000 cost basis. Sell at $60 and your gain is $1,000.
For multiple purchases, you have to calculate a weighted average. If you bought 100 shares at $50 and then another 100 shares at $80, your average cost is NOT $65. It is the total dollars invested divided by the total shares owned: ($5,000 + $8,000) / 200 = $65 per share. In this case it happens to equal the simple average — but only because both purchases had the same number of shares. Try unequal share counts and the math gets more interesting.
The Weighted Average Formula
For any multi-lot position:
Average Cost Basis = Total Dollars Invested ÷ Total Shares Owned
Worked example. You bought NVDA in three batches:
- March 2024: 50 shares at $80 = $4,000
- July 2024: 30 shares at $115 = $3,450
- December 2024: 20 shares at $135 = $2,700
Total invested: $4,000 + $3,450 + $2,700 = $10,150
Total shares: 50 + 30 + 20 = 100
Average cost basis: $10,150 / 100 = $101.50 per share
Now you sell all 100 shares in 2025 at $180. Plug into our stock profit calculator: Buy Price $101.50, Sell Price $180, Shares 100, commissions $0.
- Total cost: $10,150
- Total revenue: $18,000
- Net profit: $7,850
- Return: +77.34%
Including Commissions in Cost Basis
If you paid commissions on any of the buys, those should be added to the cost basis. Same NVDA example, but each buy had a $5 commission:
- March: $4,000 + $5 = $4,005
- July: $3,450 + $5 = $3,455
- December: $2,700 + $5 = $2,705
Total cost basis: $10,165. Average cost per share: $101.65.
The difference is small ($15 across 100 shares = $0.15 per share) but it is the technically correct way to calculate it. For modern zero-commission brokers, you can usually skip this step entirely. For older trades with $5-10 commissions, include them.
Brokers usually display the cost basis already adjusted for commissions in your account history. Use that number directly to save the math.
Selling Some But Not All Shares
What if you do not sell all 100 NVDA shares? Just 30 of them. Now you have to choose which "lot" you are selling — and the choice affects your tax bill.
Three accounting methods:
- FIFO (First In, First Out): Sell the oldest shares first. Default at most US brokers. Selling 30 shares means selling 30 from the March batch (cost $80/share). Profit per share: $180 - $80 = $100. Total profit on 30 shares: $3,000.
- LIFO (Last In, First Out): Sell the newest shares first. Selling 30 shares means selling 20 from December ($135) plus 10 from July ($115). Average cost on those 30 shares: ($2,700 + $1,150) / 30 = $128.33. Profit per share: $51.67. Total profit on 30 shares: $1,550.
- Specific Lot Identification: You pick which specific lots to sell. Most flexibility. Used for tax-loss harvesting and to control which holding period (short vs long term) applies.
For tax purposes, FIFO usually means selling longer-held shares (often LTCG-eligible) but with potentially larger gains. LIFO means selling more recently held shares (often STCG, with potentially smaller gains). Specific lot ID lets you optimize.
Most brokers default to FIFO. To use specific lot ID, you usually need to tell your broker before the sale executes.
Dividend Reinvestment Complicates Things
If you have dividend reinvestment turned on (DRIP), every dividend payment buys additional shares automatically. Each reinvested dividend creates a new lot at whatever the share price was that day. After a few years, a single position can have 30-50 lots — making manual cost basis tracking impossible.
The good news: brokers track this for you. Your broker statement will show the average cost basis (or per-lot details if you ask) for any position with DRIP enabled. Use the average cost basis as your "buy price" in free stock profit calculator and the math works out.
For tax reporting, the broker also categorizes each reinvested lot by holding period — so the IRS gets accurate STCG vs LTCG classification on every sale, even when the position is fragmented across many small lots.
The lesson: always rely on your broker's cost basis reporting for DRIP positions. Manual calculation is impossible after about a year of dividend reinvestment.
Calculate Your Trade Profit Free
Get net profit, percentage return, and ROI in seconds. No signup, no ads, runs in your browser.
Open Stock Profit CalculatorFrequently Asked Questions
Where do I find my cost basis on my broker?
Look in your account positions or holdings screen. Most brokers display "Average Cost" or "Cost Basis" for each holding. For per-lot detail, look in account history or annual statements (1099-B for US accounts).
What happens to cost basis after a stock split?
A 2-for-1 split doubles your share count and halves the per-share cost basis, leaving the total cost basis unchanged. A 3-for-1 split triples shares and divides cost basis by 3. Total cost basis is always preserved across splits.
Does cost basis matter inside a Roth IRA?
No, because Roth IRA gains are not taxed. The IRS does not require cost basis tracking for tax-advantaged retirement accounts. Brokers may still show it for your reference, but it has no tax implications.
What if I forget to track cost basis on old shares?
For shares purchased after 2011, brokers are required to track cost basis. For older shares, you may need to find old broker statements or estimate from historical prices. The IRS will accept reasonable estimates supported by documentation.

