Sub topic 8
Turning weights into buy and sell amounts
This chapter assumes you can compute portfolio weights and drift from the earlier chapters, and that you can read short Python functions. A rule such as "rebalance when stocks drift 5 points" tells you when to act. It does not tell you what to trade. This chapter covers that second step: converting a set of weights into dollar amounts, then into whole shares you can order.
The core formula
Rebalancing back to the target means making each asset class's new value equal to its target weight times the portfolio total. The trade is the difference between that target value and the current value.
target value = target weight × total portfolio value
trade = target value − current valueA positive trade is a buy. A negative trade is a sell. Trades are in currency units, not percentages, because that is what you place in an order.
Take the portfolio from earlier in the tutorial. The target is 60% stocks and 40% bonds. Stocks are worth 72,000 and bonds are worth 38,000.
- Total value is 72,000 + 38,000 = 110,000.
- Target stock value is 0.60 × 110,000 = 66,000.
- Target bond value is 0.40 × 110,000 = 44,000.
- The stock trade is 66,000 − 72,000 = −6,000, a sale.
- The bond trade is 44,000 − 38,000 = +6,000, a purchase.
The current weights are 65.5% stocks and 34.5% bonds. The sale of 6,000 stocks pays for the purchase of 6,000 bonds, and the portfolio total does not change.
The calculation in code
The function below applies the formula to every asset class at once. It rounds to cents so that floating-point noise does not show up in the output.
def rebalance_trades(values, targets):
"""Positive means buy, negative means sell. Trades sum to zero."""
total = sum(values.values())
return {a: round(targets[a] * total - values[a], 2) for a in targets}
targets = {"stocks": 0.60, "bonds": 0.40}
values = {"stocks": 72000.0, "bonds": 38000.0}
print(rebalance_trades(values, targets))The output is {'stocks': -6000.0, 'bonds': 6000.0}.
Two properties of this function are worth knowing, because you can use them as checks.
- The trades sum to zero. With no money added or removed, every sale funds a purchase. If your trades do not sum to about zero, you have a bug or a missing holding.
- The weights add up to one. The formula assumes the target weights sum to 1.0. If they sum to 0.95, the trades will not sum to zero, and 5% of the portfolio is left unassigned.
More than two asset classes
The formula does not change as you add asset classes. Each class gets its own target value, and the signs of the trades show where money moves. Here is a three-class portfolio.
targets = {"us_stocks": 0.45, "intl_stocks": 0.15, "bonds": 0.40}
values = {"us_stocks": 58000.0, "intl_stocks": 16000.0, "bonds": 36000.0}
trades = rebalance_trades(values, targets)
print(trades)
print("sum of trades:", round(sum(trades.values()), 2))The total is 110,000. The target values are 49,500, 16,500 and 44,000. The trades are therefore −8,500 for US stocks, +500 for international stocks and +8,000 for bonds. They sum to zero.
Notice that international stocks drifted only a little (14.5% against a 15% target), yet they still receive a trade of 500. A full rebalance trades every class, even ones that are close to target. Whether such a small trade is worth placing is a cost question, covered below.
From dollars to whole shares
A dollar trade is not an order. Funds and stocks trade at a price per share, and many brokers only accept whole shares. Some brokers allow fractional shares or dollar-based orders for certain funds. Check what yours supports.
For whole shares, divide the dollar amount by the price and round down. Rounding down means you never spend more than you planned on a purchase.
def trades_to_shares(trades, prices, min_trade=0.0):
"""Convert dollar trades to whole-share orders.
Trades smaller than min_trade (in currency) are skipped.
Positive means buy shares, negative means sell shares.
"""
shares = {}
for a, amount in trades.items():
if abs(amount) < min_trade:
shares[a] = 0
continue
n = int(abs(amount) // prices[a])
shares[a] = n if amount > 0 else -n
return shares
prices = {"us_stocks": 250.0, "intl_stocks": 60.0, "bonds": 80.0}
orders = trades_to_shares(trades, prices, min_trade=100.0)
print(orders)
net_cash = -sum(orders[a] * prices[a] for a in orders)
print("cash left over:", net_cash)The output is {'us_stocks': -34, 'intl_stocks': 8, 'bonds': 100} and a leftover of 20.0.
- Selling 34 shares at 250 raises 8,500.
- Buying 8 shares at 60 costs 480, which is 20 short of the 500 target. Buying 100 shares at 80 costs 8,000.
- The 20 that remains is cash. It is a rounding residue, and it is small enough to leave in the account or fold into the next rebalance.
Why a minimum trade size helps
The min_trade argument skips trades that are too small to matter. A 500 trade on a 110,000 portfolio moves the weight by under half a percentage point. If your broker charges a fee per order, or the fund has a minimum purchase, that trade can cost more than it is worth. Pick a floor that is a small multiple of your per-trade cost, and treat skipped trades as accepted drift.
Shortcuts that give the same answer
You do not always need to compute every class. Two simplifications are common.
- Compute n−1 trades and derive the last. Since the trades sum to zero, the last trade is the negative of the others added together. This is handy by hand, but the full function is a better check on your arithmetic.
- Trade in weight points first. Drift in weight points multiplied by the portfolio total gives the same dollar amount. For the first example, stocks are 5.45 points over target, so 0.0545 × 110,000 ≈ 6,000. This shortcut works because the target value minus the current value equals the weight gap times the total.
Common errors
| Error | What goes wrong | Fix |
|---|---|---|
| Using target value minus current value with a stale total | Trades are sized for yesterday's portfolio | Recompute the total from current prices just before you trade |
| Computing the total from only the classes you plan to trade | Trades are scaled wrongly | Include every holding the targets cover |
| Treating the trade as a percentage | You order "6%" of something | Convert to currency, then to shares |
| Ignoring settlement and price moves | Sell proceeds arrive after the buy is due, or prices change between order and fill | Check whether your broker lets you buy against unsettled proceeds, and allow a small cash buffer |
| Rounding every trade up | Purchases exceed the available cash | Round down and check net cash |
Where the formula is extended
The basic formula assumes you rebalance exactly to the target with no cash flowing in or out. The following chapters change one input at a time.
- Destination. Instead of the target, you can trade only to the nearest band edge. The formula stays the same, but the target weight you plug in changes.
- Contributions and withdrawals. New money raises the total before you compute target values. The trades then sum to the contribution instead of zero, and you can often avoid selling anything.
- Costs and taxes. In a taxable account, a sale can realize a gain. The formula tells you the trade that restores the target, not whether the tax bill makes it worth doing.
The formula is a starting point you adjust, not a command you must follow.
Exercise
A portfolio has a 50/30/20 target for stocks, bonds and real estate funds. Current values are stocks 64,000, bonds 26,000 and real estate 10,000. Compute the dollar trade for each class that returns the portfolio exactly to target. Then say how the three trades relate to each other.
Show answer
The total is 64,000 + 26,000 + 10,000 = 100,000. Target values are 50,000 for stocks, 30,000 for bonds and 20,000 for real estate. Trades are stocks 50,000 − 64,000 = −14,000 (sell), bonds 30,000 − 26,000 = +4,000 (buy) and real estate 20,000 − 10,000 = +10,000 (buy). The trades sum to zero: −14,000 + 4,000 + 10,000 = 0. The sale of stocks funds both purchases.
Exercise
You need to buy 1,000 of a fund priced at 42.50 per share, and your broker accepts only whole shares. How many shares do you order, and how much of the 1,000 stays as cash? What do you do if the sale that funds this purchase can only be made in whole shares and raises 20 less than planned?
Show answer
1,000 / 42.50 = 23.53, so you round down and order 23 shares. They cost 23 × 42.50 = 977.50, which leaves 22.50 in cash. If the sale raises 20 less than planned, the available proceeds are 980, which still covers 977.50. If the shortfall were larger than 22.50, you would reduce the order to 22 shares, because the net cash after rounding must not be negative.