Tracking a stock portfolio against a live market chart
PORTFOLIOTRACKR
Analysis

Why Your Spreadsheet Gets Average Cost Wrong on Partial Sells

By Daniel Hartley · August 14, 2026 · 9 min read

Your spreadsheet probably shows the wrong average cost after you sell part of a position, and the error compounds every time you trade. This post breaks down exactly how entry-price-weighted averaging works with partial buys and sells, why the most common spreadsheet formula silently corrupts your cost basis, and how real-time P&L engines get it right.

What is weighted average cost basis?

Weighted average cost basis is the total amount you paid for a position divided by the total number of shares or units you still hold. It answers one question: what did each share currently in your account actually cost you on average?

The word weighted matters. If you buy 10 shares of AAPL at $180 and later buy 30 shares at $200, your average is not $190. It is weighted toward the larger lot.

Every real-time P&L calculation depends on this number being correct. Get the average cost wrong and your unrealized gain, your percentage return, and your allocation view all inherit the error.

How do partial buys change your average entry price?

A partial buy always pulls your average toward the price of the new lot, weighted by size. The formula adds the new cost to your existing total cost, then divides by the new total quantity.

The correct formula for adding to a position

The correct formula is new average = (existing total cost + new lot cost) / (existing shares + new shares). You never average the two prices directly, because that ignores how many shares each price applies to.

Continuing the AAPL example, suppose you add 20 more shares at $210:

This scales to crypto too. If you dollar-cost-average into BTC-USD with weekly buys, each purchase reweights the average by its dollar size. PortfolioTrackr recalculates this on every fill so your entry price is never stale.

Why does average cost NOT change when you sell?

Your average cost per share stays exactly the same after a partial sell, because selling removes shares at your average cost, not at the market price. This is the single most misunderstood rule in cost basis math.

When you sell, two things happen and only one of them touches your average:

Say you hold 60 shares of AAPL at a $200 average and sell 20 shares at $230. You remove 20 x $200 = $4,000 of cost, leaving $8,000 on 40 shares. The average is still $200. You booked $600 of realized gain on the sale.

Understanding whether your gains are realized or unrealized also matters when you check whether your portfolio is actually beating the S&P 500, since paper gains and booked gains behave very differently.

Why do spreadsheet users get average cost wrong?

Spreadsheet users get average cost wrong because they let sells contaminate the average, usually by averaging the sale price into the cost formula or by re-deriving cost from a running net-quantity total. Both are silent errors that never throw a warning.

Mistake 1: averaging the sell price in

The most common error treats a sell as a negative buy and feeds its price into the weighted average. That distorts the average because the sale price has nothing to do with what your remaining shares cost. A sell at $230 should never lower or raise a $200 average.

Mistake 2: rebuilding cost from net shares

The second trap is computing average = running total spent / net shares held, where total spent mixes buy outflows and sell inflows. Subtracting sale proceeds from your cost total understates your basis and inflates your apparent gains. Your spreadsheet then reports a return that never happened.

These bugs hide well because the numbers still look plausible. This is a core reason we cover in our breakdown of portfolio tracker versus spreadsheet tradeoffs: manual formulas fail exactly when your trade history gets complex.

The correct spreadsheet approach

To do it correctly in a spreadsheet you must track two separate running totals that sells and buys touch differently:

  1. A total cost column that only buys increase, and sells decrease at the current average
  2. A share count column that both buys and sells adjust
  3. Average recomputed as total cost / share count after every row

Most people never build the sell logic this way, which is why the average drifts after the first partial exit.

Spreadsheet formula versus real-time engine: what breaks?

The difference between a naive spreadsheet and a real-time engine shows up the moment you have partial sells, multiple lots, or trades across brokers. Here is how the two approaches handle the same events.

EventNaive spreadsheetWeighted engine
Partial buyOften correctCorrect
Partial sellAverage driftsAverage unchanged
Realized P&LFrequently missingBooked per sale
Multi-broker same tickerManual mergeAuto-consolidated

The multi-broker row is where retail investors lose the most accuracy. If you hold EMAAR.AE on one broker and add to it on another, your true average spans both. PortfolioTrackr consolidates identical tickers across accounts automatically, which is far harder to maintain by hand.

How does real-time P&L get calculated under the hood?

Real-time P&L is calculated by holding a live average cost per position and continuously comparing it against the latest market price for every open lot. The engine only recalculates the average when a buy or sell fill actually changes your holdings.

The sequence for each new trade looks like this:

  1. Classify the fill as a buy or a sell
  2. On a buy, add lot cost to total cost and shares to quantity, then recompute the average
  3. On a sell, remove shares at the current average, book the difference as realized P&L, and leave the average untouched
  4. On every price tick, compute unrealized P&L = (market price minus average cost) x shares held

Because US equities now settle T+1 since May 2024 and major crypto venues list well over 350 spot pairs trading around the clock, prices move constantly while your average stays fixed between trades. Separating the two is what makes the P&L both fast and correct.

Why entry-price weighting beats FIFO for a live view

Entry-price-weighted averaging gives you one clean cost number for a position, which is ideal for a live dashboard, while FIFO tracks individual lots for tax reporting. Many trackers show weighted average for your at-a-glance return and switch to lot-level accounting only when you need capital gains figures.

How to keep your cost basis accurate across brokers

The reliable way to keep cost basis accurate across brokers is to connect each account to one tracker so every buy and sell flows through the same weighted-average logic. Manual entry across platforms is where drift and double-counting creep in.

Practical steps that prevent the classic spreadsheet errors:

If you are wiring up multiple accounts, our guide on connecting a brokerage account to a portfolio tracker walks through the setup, and the 2026 comparison of six portfolio tools shows which ones handle partial sells correctly.

The bottom line

Weighted average cost only changes on buys, and a partial sell should leave your average completely untouched while booking realized P&L. The spreadsheets that get this wrong do so by feeding sale prices into the average or by rebuilding cost from net shares, and the error quietly inflates your reported returns.

A real-time engine keeps a live average, recomputes it only on fills, and separates realized from unrealized gains on every price tick. PortfolioTrackr does this automatically across stocks, crypto, and UAE markets, so your entry price stays correct no matter how many partial buys and sells you make.

Track your portfolio in real time: free for 3 days

Live P&L across stocks, crypto, and global markets. WhatsApp and Telegram price alerts. AI trade import. Unified dividend tracking. No brokerage connection required.

Start Free Trial
Download on the App Store Get it on Google Play
See the live demo first →

Frequently asked questions

Does average cost change when you sell part of a position?

No, your average cost per share stays the same after a partial sell. Selling removes shares at your existing average cost, which lowers your total cost and share count proportionally. The difference between the sale price and your average becomes realized profit or loss, but the average itself is unchanged.

How do you calculate weighted average entry price with multiple buys?

Add the total cost of every buy together, then divide by the total number of shares bought. For example, 10 shares at $180 plus 30 shares at $200 equals $7,800 across 40 shares, giving a weighted average of $195. You never average the two prices directly.

Why does my spreadsheet show the wrong average cost after a sell?

Most spreadsheets get it wrong by treating a sell as a negative buy and averaging the sale price into your cost, or by rebuilding cost from net shares. Both let sale proceeds contaminate your basis. Sells should only reduce share count and total cost at the current average, never affect the average price.

What is the difference between realized and unrealized P&L?

Realized P&L is profit or loss locked in when you actually sell, calculated as sale price minus average cost times shares sold. Unrealized P&L is paper gain or loss on shares you still hold, calculated as current market price minus average cost. Only realized gains are typically taxable.

Can PortfolioTrackr calculate cost basis across multiple brokers automatically?

Yes, PortfolioTrackr consolidates the same ticker held on different brokers into one position and applies weighted-average logic to every buy and sell. This prevents the drift and double-counting that happen when you track cost basis manually across separate broker apps or spreadsheets.

Daniel Hartley
Daniel Hartley writes about the fundamentals of portfolio tracking at PortfolioTrackr: profit and loss, position sizing, and turning a messy multi-broker setup into one clear picture for everyday investors.