I am building a stock portfolio tracking application, and I am having some difficulties wrapping my head around something very basic (maybe I'm just tired)

How does selling a stock affect the cost basis / average price of the remaining stock?


I buy 10 shares at an average price of $1

averagePrice: 1,
costBasis: 10,
quantity: 10,

I then sell 5 of these shares at market price of $5

averagePrice: ???
costBasis: ???
quantity: 5

The only value that I know for certain is the quantity, I have heard different brokers use different methods for calculating this cost basis.

Can someone help me fill in the values? I apologise if this is really dumb.

  • 1
    Are you interested in cost basis potentially for tax purposes? If so, a country tag is needed. Tax rules for capital gains/losses do vary. Aug 13, 2022 at 0:03
  • I had a disclaimer noting that is is not for tax, but i removed it. To be clear. I am building an application for external users, so its not for tax. Cost basis will just assume the US method Aug 15, 2022 at 17:14

2 Answers 2


If you use the average cost basis method, then your cost basis for the 5 shares that were sold and the remaining 5 shares is still $1. If you buy more shares, those are combined with the 5 shares with an average cost of $1 and a new average is computed to be used for future sales.

In absolute terms, if your cast basis is $1 per share and you have 10 shares, your cost basis is $10. If you sell 5 shares and have 5 shares left, then your overall cost basis is $5.

If you want to use another method like FIFO, LIFO, or specific lost, then you need to know the cost basis of individual lots to calculate a new cost basis.

  • Thanks for this, maybe my terminology is wrong, but here I am referring to cost basis, not per share but for the entire purchase. Would this remain the same? I would imagine it would change after the quantity decreases. But the average price remains the same? Aug 12, 2022 at 18:27

How does selling a stock affect the cost basis / average price of the remaining stock?

It doesn't. The value of the shares at the time of acquisition* determines the cost basis for each share in that transaction. When you sell, your capital gain (or loss) is the difference between CB & the sell price.

Notionally, each share has its own CB but realistically each one acquired in a given transaction will be the same and stay the same for as long as you own it **. If you sell shares acquired over multiple transactions you might have multiple CBs to wrangle at tax time.

*The flowery and vague language is due to the fact that buying is not the only way to acquire shares. Reinvestment plans, rights issues, takeovers & other corporate actions are also ways to gain shares that's not always the market price. Additionally, the cost to buy incorporates any brokerage fees, not just the price itself.

**Splits & consolidations may change the per-share CB, but not the CB of the transaction.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .