How to calculate the average cost of stocks (without getting splits and bonus shares wrong)
Average cost looks like back-of-the-envelope math: you buy, divide the total by the number of shares, done. And it is — until the second purchase, the partial sale, the stock split and the bonus issue come along. Then the "simple math" turns into a spreadsheet nobody trusts anymore, and the wrong number contaminates two expensive things: your sense of profit and the cost basis that goes into your income tax return.
This guide shows the rule, a complete example, and the spots where almost everyone slips.
The tax authority's rule: weighted average cost
In Brazil, the acquisition cost of stocks follows the weighted average cost method, as required by the Receita Federal (Brazil's tax authority):
- Purchases enter the average — at the amount paid, including costs (brokerage and exchange fees).
- Sales don't change the average cost — they only reduce the quantity. Whatever leaves the position leaves at the current average cost.
- The average is per asset, counting all your brokerages together.
A complete example, from zero
Follow the fictional stock XPTO3:
| Event | Quantity | Total cost | Average cost |
|---|---|---|---|
| Buy: 100 at R$ 10.00 + R$ 5.00 in fees | 100 | R$ 1,005.00 | R$ 10.05 |
| Buy: 50 at R$ 12.00 + R$ 5.00 in fees | 150 | R$ 1,610.00 | R$ 10.73 |
| Sell: 50 at R$ 13.00 | 100 | R$ 1,073.33 | R$ 10.73 (unchanged) |
| 2-for-1 split (each share becomes two) | 200 | R$ 1,073.33 | R$ 5.37 |
| Bonus shares: 10 shares at R$ 8.00 (cost assigned by the company) | 210 | R$ 1,153.33 | R$ 5.49 |
Two details worth noting:
- On the sale, the taxable profit was
50 × (13.00 − 10.7333) = R$ 113.33— calculated with the average cost, not the price of the latest purchase (internally the average is kept unrounded; the table shows 10.73 for display only). Worth noting: in Brazil, gains on regular stock sales are exempt from income tax when total sales in the month are at or below R$ 20,000 (Lei 11.033/2004) — this example's gain would be exempt; the math still matters for the return and for months above the threshold, covered in DARF on stock sales: the R$ 20,000 exemption. - On the split, the total cost doesn't change. You didn't get any richer: the same slices, cut smaller. The average cost drops in the exact proportion.
Where the math goes off the rails
- The forgotten split. The brokerage hands you 100 "new" shares and the spreadsheet never records it. Result: the position looks sold (or the average cost sits at double the real value) and the profit on a future sale comes out wrong. International brokerages often don't even export the event in their statements.
- Bonus shares at the wrong cost. Bonus shares carry an assigned cost reported by the company (it can be zero). Booking them at market price inflates your cost — and understates the tax due.
- One spreadsheet per brokerage. Average cost is per asset, not per brokerage. If you buy XPTO3 at two houses and compute each one separately, you file it wrong.
- Ignored fees. Brokerage and exchange fees are part of the acquisition cost — leaving them out pushes your average cost down and inflates your tax for nothing.
How CaixaUnica does this math for you
You import the reports you already have — B3 trading and movement reports (B3 is the Brazilian stock exchange), Avenue and Binance statements — and the portfolio recalculates the weighted average cost of every asset, with corporate actions in the right place:
- Splits and bonus shares from the B3 reports come in as quantity events, without inflating cost.
- When the brokerage doesn't export the split (common with US stocks), the system detects the historical event and fixes the position on its own — without that, a stock that split 10-for-1 would show up as "sold".
- No phantom profit: if an asset arrived via transfer, with no recorded purchase price, the portfolio doesn't invent a gain — it flags it for you to review and lets you set a reference price per year.
- Re-importing the same file duplicates nothing.

At tax time, that same average cost becomes the Assets and Rights section of the IRPF report (Brazil's annual income tax return) — exportable as PDF and CSV.
Important: this article is educational and does not replace an accountant or the official tax rules. For less common operations (day trading, stock lending, complex corporate actions), confirm the treatment with a professional.
Sources
- Receita Federal — Ganho Líquido (net gain) — acquisition cost on the spot market is computed as the weighted average of unit costs.
- Receita Federal — Isenções (exemptions) — the limit is measured on the total sold in the month, and brokerage and exchange fees form part of the acquisition cost.
- Lei 11.033/2004 — art. 3, I: stock gains are exempt when the month's sales total R$ 20,000 or less.
- Lei 9.249/1995 — art. 10, § 1: the cost of bonus shares is the share of capitalized profit or reserve.