Sub topic 5
Calculating Average Cost per Share
This chapter assumes you know what a share is, how fractional shares let you buy by dollar amount, and what dollar-cost averaging (DCA) is. You should also be comfortable with weighted averages. The goal is to compute your average cost per share correctly, including edge cases, and to avoid the mistakes people commonly make with it.
The working model: total spent divided by total shares
Average cost per share is the total money you put in, divided by the total number of shares you own.
average cost = total invested / total sharesIt is not the average of the prices you paid. Each purchase counts in proportion to how many shares it bought, so you need the share count for every purchase.
Suppose you invest $100 on each of five dates, and the price is $50, $40, $25, $40 and $50.
| Purchase | Price | Invested | Shares bought |
|---|---|---|---|
| 1 | $50 | $100 | 2.0 |
| 2 | $40 | $100 | 2.5 |
| 3 | $25 | $100 | 4.0 |
| 4 | $40 | $100 | 2.5 |
| 5 | $50 | $100 | 2.0 |
| Total | $500 | 13.0 |
The average cost is $500 / 13 shares, which is about $38.46. The code below does the same calculation.
prices = [50, 40, 25, 40, 50]
contribution = 100
shares = [contribution / p for p in prices]
total_shares = sum(shares)
invested = contribution * len(prices)
avg_cost = invested / total_shares
print(round(total_shares, 4), round(avg_cost, 2))
# 13.0 38.46Why the result is below the average price
The simple average of the five prices is (50 + 40 + 25 + 40 + 50) / 5 = $41. Your average cost, $38.46, is lower. The reason is that equal dollars buy more shares when the price is low. At $25 you got 4 shares, and at $50 you got only 2. The cheap purchase therefore carries more weight in the share count.
Mathematically, when you invest equal dollars each time, your average cost is the harmonic mean of the prices. The harmonic mean is the number of purchases divided by the sum of the reciprocals of the prices:
5 / (1/50 + 1/40 + 1/25 + 1/40 + 1/50) = 5 / 0.13 = 38.46If instead you bought one whole share each time, your average cost would equal the arithmetic mean, $41. That is the difference between investing a fixed dollar amount and buying a fixed share count.
The harmonic mean is never larger than the arithmetic mean. The two are equal only when every price is the same. The more the price swings, the bigger the gap.
Unequal contributions and an existing position
Real plans rarely have identical purchases. You may already own shares, change your contribution, or make a one-off extra purchase. The formula stays the same: add up all dollars and all shares across every lot (a lot is a single purchase).
Here is a position of 10 shares bought earlier for $450, followed by three purchases:
- $100 at $40, which buys 2.5 shares
- $100 at $25, which buys 4 shares
- $50 at $50, which buys 1 share
from decimal import Decimal
def average_cost(lots):
"""lots is a list of (dollars_invested, shares) pairs."""
total_cost = sum(cost for cost, _ in lots)
total_shares = sum(shares for _, shares in lots)
return total_cost / total_shares
lots = [
(Decimal("450.00"), Decimal("10")),
(Decimal("100.00"), Decimal("100.00") / Decimal("40")),
(Decimal("100.00"), Decimal("100.00") / Decimal("25")),
(Decimal("50.00"), Decimal("50.00") / Decimal("50")),
]
print(sum(shares for _, shares in lots))
print(average_cost(lots).quantize(Decimal("0.01")))
# 17.5
# 40.00Total cost is $700 and total shares are 17.5, so the average cost is $40.00. Decimal is used instead of float so that cents do not drift from binary rounding. Float is fine for quick estimates, but use Decimal if you reconcile against a broker statement.
Updating the average one purchase at a time
You can also keep a running average without storing every lot. You only need your current share count and current average cost:
new average = (old average * old shares + new dollars) / (old shares + new shares)For example, you hold 10 shares at an average cost of $45. You then invest $100 at $40, which buys 2.5 shares. The new average is (45 * 10 + 100) / 12.5 = 550 / 12.5 = $44.00.
The running formula works because "old average * old shares" recovers the old total cost.
Common errors
- Averaging the prices. Using $41 instead of $38.46 in the first example overstates your cost. This is the most common mistake.
- Averaging averages. In the update example above, taking (45 + 40) / 2 gives $42.50. The correct answer is $44.00, because the old position has four times as many shares as the new purchase. Always weight by shares.
- Ignoring fees. If a purchase carries a fee, add it to the dollars invested, not to the price. With a $1 fee on each of the five $100 purchases, your total cost is $505 and the average cost is 505 / 13, or about $38.85, if the fee is charged on top of the $100. If the fee is taken out of the $100 instead, you buy fewer shares, and the average cost rises for that reason. Check which way your broker does it.
- Mixing up average cost and profit. A low average cost does not guarantee a gain. If the price ends at $50, your 13 shares are worth $650, a gain of $150 on $500. If the price ends at $25, they are worth $325, a loss of $175. Compare the average cost with the current price, not with the other prices you saw.
- Forgetting dividends and splits. Reinvested dividends buy extra shares, which are new lots with their own costs. A stock split multiplies your shares and divides your per-share cost, but leaves your total cost unchanged. Both change the share count in the formula.
Rounding and fractional precision
Brokers often truncate fractional quantities at a fixed number of decimal places, and the exact precision varies. A $50 order at $300 per share gives 0.166666 shares, not 0.166667 or 1/6.
from decimal import Decimal, ROUND_DOWN
amount = Decimal("50.00")
price = Decimal("300.00")
shares = (amount / price).quantize(Decimal("0.000001"), rounding=ROUND_DOWN)
print(shares)
# 0.166666Multiplying back gives 0.166666 * $300 = $49.9998, so about $0.0002 of your $50 is not turned into shares. Brokers handle this in different ways. Some report your cost as $50.00 and some as the share count times the fill price. After many purchases the difference is a few cents at most.
This matters in two situations:
- Reconciling. Your own calculation may differ from the broker's by a cent or two. If it differs by much more, look for fees, a missed dividend, or a wrong price.
- Fill price. A dollar-based order fills at the execution price, which can differ slightly from the quote you saw. For your average cost, use the actual fill price and share count from the confirmation, not the quote.
Average cost and tax records
The average cost is a good summary for tracking performance. It is not always the number used for taxes. Many brokers track each lot separately, and the rules for choosing which lots a sale uses depend on your country and account type. The later chapter on tax lots and cost-basis records covers this. For now, keep every confirmation, because the lot-level data lets you rebuild any average.
Summary of the method
- Convert each purchase to shares: dollars divided by fill price.
- Add up all dollars, including fees, and all shares, including reinvested dividends.
- Divide total dollars by total shares.
- Weight by shares whenever you combine positions.
- Compare the result with the current price to see where you stand.
Exercise
You invest $100 on each of three dates at prices of $20, $25 and $50. What is your average cost per share, and how does it compare with the simple average of the three prices?
Show answer
Shares bought are 5, 4 and 2, so you own 11 shares for $300 in total. The average cost is 300 / 11, about $27.27. The simple average of the prices is (20 + 25 + 50) / 3, about $31.67. Your average cost is lower because the $20 purchase bought the most shares.
Exercise
You own 20 shares with an average cost of $30. You then invest $200 when the price is $40. What is your new average cost per share, and why is it not $35?
Show answer
The $200 buys 5 shares. Your old total cost is 20 * 30 = $600, so the new total is $800 for 25 shares, and the average cost is $32.00. It is not $35 because that would average $30 and $40 equally. Your old position has four times as many shares as the new purchase, so it carries four times the weight.