Which trades bring a portfolio back to its target weights?

Turn target weights into target shares, subtract the current shares to get the trades, and compute the turnover and the trading cost of a rebalance in pandas.

After this lesson, you can compute the trades that bring a portfolio back to its target weights, and their turnover and cost.

Which shares do I buy or sell?

Each stock’s target shares are its target weight times the portfolio’s value, divided by its price, and the trade is the target shares minus the current shares. In the example, AAA has drifted to 600 of the portfolio’s 1,100 dollars, so the rebalance sells 50 dollars of AAA and buys 50 dollars of BBB.

\[ n_i^* = \frac{w_i V}{P_i}, \qquad \Delta n_i = n_i^* - n_i \]

Here \(w_i\) is stock \(i\)’s target weight, \(V\) the portfolio’s value, \(P_i\) the stock’s price, \(n_i\) its current shares and \(\Delta n_i\) the trade, positive for a purchase and negative for a sale.

The example is the portfolio from the lesson on drifting weights on 3 March. It holds 50 AAA shares at 12 dollars and 25 BBB shares at 20 dollars, with targets of one half each. One row holds one stock.

rebalance_trades adds up the portfolio’s value, computes the target shares and returns the trades. Multiplying each trade by the price gives its value in dollars.

The following box computes the trades.

import pandas as pd

holdings = pd.DataFrame({
    "ticker": ["AAA", "BBB"],
    "shares": [50.0, 25.0],
    "price": [12.0, 20.0],
    "target": [0.5, 0.5],
})

def rebalance_trades(holdings):
    portfolio_value = (holdings["shares"] * holdings["price"]).sum()
    target_shares = holdings["target"] * portfolio_value / holdings["price"]
    return target_shares - holdings["shares"]

holdings["trade"] = rebalance_trades(holdings)
holdings["trade_value"] = holdings["trade"] * holdings["price"]
print(holdings.round(3))
  ticker  shares  price  target  trade  trade_value
0    AAA    50.0   12.0     0.5 -4.167        -50.0
1    BBB    25.0   20.0     0.5  2.500         50.0

The trade values add up to zero, because the targets add up to 1, so the sale of AAA pays for the purchase of BBB.

How much do I trade, and what does it cost?

Turnover is the dollars traded on both sides, divided by the portfolio’s value, and the fee is a rate on the dollars traded. Here the rebalance trades 100 dollars, a turnover of 9.1%, and a fee of 5 basis points (0.05%) costs 5 cents.

\[ \text{turnover} = \frac{\sum_i |\Delta n_i P_i|}{V} \]

.abs() turns the sale’s negative value positive, and .sum() adds the two sides. Some reports halve this turnover, so that each dollar moved counts once.

The following box computes the turnover and the fee.

portfolio_value = (holdings["shares"] * holdings["price"]).sum()
traded = holdings["trade_value"].abs().sum()
print(round(traded / portfolio_value, 4), round(0.0005 * traded, 2))
0.0909 0.05

Your turn

Compute the trades for the targets 0.75 for AAA and 0.25 for BBB. You should see a purchase of 18.75 AAA shares and a sale of 11.25 BBB shares, 225 dollars on each side.

.assign(target=...) returns a copy of holdings with new targets, and rebalance_trades runs on that copy.

new_trade = rebalance_trades(holdings.assign(target=[0.75, 0.25]))
print(pd.DataFrame({"ticker": holdings["ticker"], "trade": new_trade, "trade_value": new_trade * holdings["price"]}))
  ticker  trade  trade_value
0    AAA  18.75        225.0
1    BBB -11.25       -225.0