Skip to content
TraderCalcs.io

Cost Basis Calculator

Work out cost basis and realised gain across multiple purchases and sales, by average cost or FIFO.

Cost basis is the figure a gain is measured against, and it stops being obvious the moment a holding is built in more than one purchase. Two holders can own identical shares bought on identical dates, sell the same sixty of them on the same afternoon at the same price, and report gains that differ by a wide margin — because the shares that left were identified by different rules, and the rule is what decides which cost went with them. Choosing that rule — pooling everything into an average cost basis, or matching sales against dated parcels in turn — is the first thing this cost basis calculator asks you to do, because every figure downstream of it changes with the answer.

Formula: average cost = total spent / total shares; FIFO matches sales to the earliest lots

Your figures

Average cost pools every purchase into one basis per share. FIFO keeps the purchases as dated parcels and matches each sale against the oldest first. Which one applies to a particular holding is a matter of local law and of the account it sits in.

Transactions

In the order they happened, oldest first. A positive quantity buys shares and a negative quantity sells them, so there is one number to get right and it cannot disagree with itself. Commission is always a positive charge: it is added to the cost of a purchase and taken off the proceeds of a sale. The commissions in the rows below are a round illustrative figure, not any broker's rate — replace them with what your account was billed, or clear them for a commission-free account. Blank rows are ignored.

Transaction 1
Transaction 2
Transaction 3

Results

Cost basis of the holding

US$4,098.00

90 shares at US$45.5333 each

Shares still heldEverything bought less everything sold, in the order entered.
90
Cost basis per share heldThe pooled cost divided by the pooled shares. Selling does not move it: a sale takes cost and shares out in the same proportion.
US$45.5333
Total spent on purchasesQuantity times price plus commission, across every buy.
US$6,830.00
Proceeds from salesQuantity times price less commission, across every sell.
US$4,070.00
Cost basis disposed ofThe part of the basis the sales took out of the holding. This is the figure the two methods disagree about.
US$2,732.00
Realised gainProceeds less the basis disposed of. Nothing here is realised by the shares still held.
US$1,338.00
Realised gain against the basis disposed ofThe realised gain as a percentage of the cost it was measured against.
+48.98%
Commission across every transactionCounted twice over a completed round trip: once into the cost and once out of the proceeds.
US$30.00
Transactions countedBlank rows are not counted.
3 · 2 bought, 1 sold
Matching methodBoth methods give the same total gain once every share has been sold. What the choice changes is when a gain is recognised and how it is divided between the sales.
Average cost
Sale on transaction 360 shares at US$68.00 produced US$4,070.00 net of commission, against US$2,732.00 of basis at US$45.5333 a share. 90 shares were left afterwards at US$45.5333 a share, from US$45.5333 immediately before it.
US$1,338.00 · +48.98%

What this means

3 transactions — 2 purchases costing US$6,830.00 and 1 sale returning US$4,070.00 net of commission — leave 90 shares carrying US$4,098.00 of cost basis, and have realised US$1,338.00 against the US$2,732.00 of basis those sales disposed of. Average cost pools every purchase, so all 90 remaining shares carry the same US$45.5333 of basis, and a sale removes cost in exactly the proportion of the pool it takes — which is why the pooled cost per share is unchanged by selling. The two methods differ only in which basis a sale disposes of: they part company on the gain recognised at each individual sale and on the basis of what is still held, and they agree exactly on the total gain once the last share has gone, because that total is proceeds less cost and no allocation rule changes a sum.

  • Every figure above is yours to enter. No price, share count, earnings figure or commission is fetched from anywhere, and no broker's commission is preset — where a field opens with one it is a round illustration. That is what lets these work on a private company, a delisted one, a statement you would rather check than trust, and a date no live feed still serves.
  • No tax is computed. Which matching method a holder may use, whether a disposal is taxable at all, and how a payment for a fraction is characterised are questions of jurisdiction and of the individual holder's circumstances rather than of arithmetic.
  • Commission is treated as part of the cost on a purchase and as a deduction from the proceeds on a sale, so a round trip is charged at both ends and reduces the reported gain twice.
  • Order matters. The rows are read top to bottom as the sequence of events, and a sale that comes before the shares it disposes of were bought is rejected rather than netted off later purchases.
  • Corporate actions are not applied here. A split, a consolidation, a spin-off or a return of capital changes the share count or the basis without appearing as a transaction, and has to be worked through before these rows will describe the holding.
  • Assumes every row is in the currency selected above. Purchases settled in another currency have to be converted first — no exchange rate is applied here.

Cost basis of the holding: US$4,098.00

The link reopens this calculator with the same numbers. Nothing you enter is sent to a server.

About the Cost Basis calculation

It reads a transaction history in the order it happened, working out either an average cost basis across the whole pool or a FIFO queue of dated parcels from the same rows. A positive quantity buys shares and a negative quantity sells them, so there is one number to get right per row and it cannot contradict itself; commission is always entered as a positive charge and is added to the cost of a purchase or taken off the proceeds of a sale. It returns the shares still held and the basis they carry, the basis each sale disposed of, the gain that disposal realised, and — under FIFO — the dated parcels still open. A basis tracker and a realised gain calculator are the same tool once the matching rule is settled, which is why both sets of figures come out of one history.

The matching method is a choice on screen and often not a choice in life. Some jurisdictions mandate pooling, some permit an election between methods, some apply different rules to different account types, and some add rules about shares bought back shortly after a sale. This tool computes whichever method you select and reports no tax at all, because which one a particular holding is entitled to use is a question of law rather than of arithmetic.

Cost Basis formula

purchase cost   = quantity × price + commission
sale proceeds   = |quantity| × price − commission

average cost
  basis / share  = pooled basis ÷ shares held
  basis disposed = pooled basis × (shares sold ÷ shares held)

FIFO
  lot basis/share = lot cost ÷ lot shares
  basis disposed  = Σ (shares taken × that lot's basis/share)
                    taking the oldest open lot first

realised gain   = proceeds − basis disposed
gain %          = realised gain ÷ basis disposed × 100
basis retained  = Σ purchase cost − Σ basis disposed
quantity
Signed. Positive buys shares, negative sells them, and the sign is the whole of the distinction between the two.
commission
The charge your account was actually billed on that transaction, entered as a positive number. Nothing is preset for any broker.
pooled basis
Under average cost, everything paid for the shares still held, treated as one undifferentiated sum with no purchase dates attached to it.
lot
A parcel of shares from one purchase, carrying that purchase's price and its share of that purchase's commission. FIFO holds the position as a queue of these.
basis disposed
The part of the cost that leaves the holding with a sale. This single figure is the whole of the disagreement between the two methods.

Commission enters the arithmetic twice and in opposite directions, which is why a round trip is charged at both ends. On a purchase it is added to what the shares cost, so it raises the basis and therefore lowers the eventual gain; on a sale it is taken off what was received, so it lowers the proceeds and lowers the gain again. Treating it as a separate expense outside the basis produces the same net figure only when the whole position is closed at once, and a different one on every partial sale.

Average cost has one property worth stating as algebra, because it is the reason people choose it and the reason the calculator reports it on every sale. A sale removes cost in exactly the proportion of the pool it takes: numerator and denominator are scaled by the same factor, so the pooled basis per share afterwards is identical to what it was immediately before. Selling does not make the shares you keep cheaper or dearer. Nothing about the remaining holding changes except how many shares are in it.

FIFO behaves in the opposite way, and it has to. The queue is consumed from the front, so the shares that left were the oldest, and the basis left behind is the cost of the parcels still open. In a position built through a rising market the earliest parcels are the cheapest ones, so each sale strips out cheap basis, recognises a larger gain now, and raises the cost per share of everything still held. That is not a quirk of the implementation; it is what first-in-first-out means.

The two methods agree on exactly one number, and it is worth being precise about which. Once every share has been sold, total realised gain is identical under both, because the total is then simply every pound received minus every pound paid and no allocation rule can change a sum. What the choice changes is when a gain is recognised and how it is divided between the individual sales — which is precisely why jurisdictions legislate about it rather than leaving it to the holder's convenience.

Order is read as sequence rather than as data entry. The rows are processed top to bottom as the history of the position, so a sale is matched against the shares held at that point in the story and not against purchases that came later. A history that sells shares before they were bought is refused rather than netted off at the end, because a holding that went short in the middle is a different position from the one these formulas describe.

Cost Basis: a worked example

One holding built in two purchases and reduced by one sale, with a round 10.00 of commission on each: 100 shares at 42.50, then 50 more at 51.20, then 60 shares sold at 68.00. These are the figures the page loads with, so the panel beside this reproduces every line below.

  1. First purchase100 × 42.50 + 10.00 = 4,260.00, or 42.60 a share
  2. Second purchase50 × 51.20 + 10.00 = 2,570.00, or 51.40 a share
  3. Pooled holding150 shares carrying 6,830.00, or 45.5333 a share
  4. Sale proceeds60 × 68.00 − 10.00 = 4,070.00
  5. Average cost — basis disposed6,830.00 × 60 ÷ 150 = 2,732.00
  6. Average cost — realised gain4,070.00 − 2,732.00 = 1,338.00, or +48.98%
  7. Average cost — basis retained6,830.00 − 2,732.00 = 4,098.00 on 90 shares, still 45.5333 each
  8. FIFO — basis disposed60 × 42.60 = 2,556.00, taken entirely from the first parcel
  9. FIFO — realised gain4,070.00 − 2,556.00 = 1,514.00, or +59.23%
  10. FIFO — basis retained40 × 42.60 + 2,570.00 = 4,274.00 on 90 shares, now 47.4889 each

The same three transactions produce a realised gain of 1,338.00 under average cost and 1,514.00 under FIFO — 176.00 apart. The retained basis is 176.00 apart in the other direction, 4,274.00 against 4,098.00, and that symmetry is not a coincidence: FIFO recognised a larger gain now by disposing of cheaper basis, and left the expensive basis behind for a later sale to use. Sell the remaining 90 shares and both methods arrive at the identical total.

How to use the Cost Basis calculator

  1. Choose the matching method first. It changes the basis disposed of on every partial sale and therefore every figure downstream of it, so it is not a display setting.
  2. Enter the transactions oldest first, in the order they actually happened. A positive quantity buys and a negative quantity sells; there is no separate buy or sell control to disagree with the number.
  3. Put the commission your account was billed on each row, not a rate this site decided on. Zero is a perfectly ordinary entry for a commission-free account, and the spread you paid is not commission — it is already inside the price you dealt at.
  4. Leave a row blank while you are still filling the form. Blank rows are skipped rather than treated as errors, so the result stays on screen while the history is being built up.
  5. Read the per-sale rows, not only the totals. They show the basis each individual sale disposed of and what the remaining holding cost per share immediately before and after it, which is where the two methods visibly part company.
  6. Deal with corporate actions before you get here. A split, a consolidation, a spin-off or a return of capital changes the share count or the basis without being a transaction, so this is not an adjusted cost basis calculator — work the adjustment through first and enter the restated figures.

Limitations of the Cost Basis calculator

Every calculation makes assumptions. These are the ones that matter here.

  • Computes no tax of any kind. Whether a disposal is taxable, which matching method the holder is entitled to use, and what allowances or reliefs apply are questions of jurisdiction and of individual circumstances.
  • Applies none of the anti-avoidance rules several jurisdictions attach to repurchases. Rules that match a sale against shares bought back within a set number of days, or on the same day, change which basis a disposal uses and are not modelled here.
  • Specific identification is not offered. Where a holder is permitted to nominate exactly which parcel a sale draws on, the answer can differ from both methods shown, and the choice of parcel is a decision this calculator cannot make for you.
  • Corporate actions are not applied. Splits, consolidations, spin-offs, rights issues, scrip dividends and returns of capital all change the share count or the basis without appearing in a transaction list.
  • Assumes every row is in one currency. A purchase settled in another has to be converted before it is entered, because no exchange rate is applied anywhere in this calculator.
  • Dividends are not part of cost basis and are not counted here. Reinvested dividends are, but only as the purchases they became — enter each reinvestment as an ordinary buy at the price it was reinvested at.

Frequently asked questions about Cost Basis

Which matching method applies to my holding?

That is set by the tax rules of your jurisdiction and by the type of account the shares sit in, not by preference. Some regimes pool everything into a single average basis and give the holder no election at all; others permit FIFO, average cost or specific identification, sometimes with different rules for funds than for individual shares. The calculator computes whichever you select and advises on neither.

Does a FIFO cost basis calculator ever agree with an average cost basis?

Always, on the total gain, once every share has been sold — at that point the answer is simply total proceeds minus total cost and no allocation rule can alter a sum. They also agree whenever every purchase was made at the same price, because then the oldest parcel and the pooled average cost the same. Everywhere else they differ on the gain at each sale and on the basis of what is still held.

Why is my cost basis per share higher than any price I paid?

Because commission is part of what the shares cost. A hundred shares bought at 42.50 with 10.00 of commission cost 4,260.00, which is 42.60 a share — slightly above the dealing price, and correctly so, since that is what leaving the account bought them. The same charge on the way out reduces the proceeds, which is why a round trip is charged twice.

Does selling some shares change the cost basis of the ones I keep?

Under average cost, no: the sale removes cost and shares in the same proportion, so the pooled basis per share afterwards is exactly what it was before. Under FIFO, yes: the shares that left were the oldest, so the parcels still open are the later and usually the dearer ones, and the basis per share of the remaining holding moves after every sale.

How do I enter a stock split in the transaction list?

You do not — a split is not a transaction, because nothing was bought or sold and nothing was paid. Work it through on the stock split calculator first, which leaves the total basis untouched and divides the basis per share by the ratio, then restate the affected rows here as the adjusted share counts and prices.

Why was my sale rejected when I hold more than enough shares in total?

Because the rows are read as a sequence of events rather than as a set. A sale is matched against the shares held at that point in the history, so selling 200 shares on row two when only 100 had been bought by then is refused even if a later row buys another 500. Reorder the rows into the sequence they actually happened in and the history will resolve.

Last reviewed . See our methodology and editorial policy.

More stocks calculators