Where did the change in portfolio wealth come from?

Track the cash, shares and wealth of a portfolio in a long pandas panel of closes and trades, and explain every day’s change in wealth by price moves and fees.

After this lesson, you can track a portfolio’s cash, shares and wealth, and explain each day’s change in wealth.

How do I track cash, shares and wealth?

Wealth is cash plus the value of the shares held at each close. A purchase moves money from cash into shares, so only its fee changes wealth. On 2 March, the portfolio buys shares for 9,000 dollars and pays a fee of 4.50 dollars, so its wealth falls from 10,000 to 9,995.50 dollars.

\[ W_t = \text{cash}_t + \sum_i n_{i,t} P_{i,t}, \qquad \Delta\text{cash}_t = -\sum_i \big(\Delta n_{i,t} P_{i,t} + c\,|\Delta n_{i,t} P_{i,t}|\big) \]

Here \(n_{i,t}\) is the number of shares of stock \(i\) held after the trades of day \(t\), \(P_{i,t}\) the close, \(\Delta n_{i,t}\) the shares traded and \(c\) the fee rate.

The example is a long panel, as in the lesson on long and wide panels, of two made-up stocks over three trading days. shares_traded holds each day’s trade at the close, positive for a purchase and negative for a sale. The portfolio starts with 10,000 dollars, buys 100 AAA and 50 BBB shares on 2 March, and sells 50 AAA shares on 3 March. The fee is 0.05% of the dollars traded.

.cumsum() within each stock adds up the trades, so shares_held is the number of shares after each day’s trade. .groupby("date").sum() adds the two stocks of each day, and .cumsum() on the daily cash flows gives the cash.

The following box builds the panel and the daily ledger.

import numpy as np
import pandas as pd

dates = pd.bdate_range("2026-03-02", periods=3)

panel = pd.DataFrame({
    "date": np.tile(dates, 2),
    "ticker": np.repeat(["AAA", "BBB"], 3),
    "close": [40.0, 42.0, 41.0, 100.0, 98.0, 101.0],
    "shares_traded": [100, -50, 0, 50, 0, 0],
})
panel["shares_held"] = panel.groupby("ticker")["shares_traded"].cumsum()
panel["trade_value"] = panel["shares_traded"] * panel["close"]
panel["fee"] = 0.0005 * panel["trade_value"].abs()
panel["position"] = panel["shares_held"] * panel["close"]

daily = panel.groupby("date")[["trade_value", "fee", "position"]].sum()
daily["cash"] = 10_000 - (daily["trade_value"] + daily["fee"]).cumsum()
daily["wealth"] = daily["cash"] + daily["position"]
print(daily)
            trade_value   fee  position     cash    wealth
date                                                      
2026-03-02       9000.0  4.50    9000.0   995.50   9995.50
2026-03-03      -2100.0  1.05    7000.0  3094.45  10094.45
2026-03-04          0.0  0.00    7100.0  3094.45  10194.45

On 3 March, the sale adds 2,100 dollars less a fee of 1.05 dollars to cash, which rises to 3,094.45 dollars.

Where did each day’s change in wealth come from?

The shares held at the previous close earn the day’s price changes, and the day’s fees leave the portfolio. On 3 March, the 100 AAA shares gain 2 dollars each and the 50 BBB shares lose 2 dollars each, a price move of 100 dollars, and the fee of 1.05 dollars leaves a change of 98.95 dollars.

\[ \Delta W_t = \sum_i n_{i,t-1}\,\Delta P_{i,t} - F_t \]

Here \(\Delta P_{i,t}\) is the change in stock \(i\)’s close and \(F_t\) the day’s fees.

As in the lesson on shifting rows, .shift(1, fill_value=0) within each stock gives the shares held at the previous close, with zero before the first day. .diff() gives the change in each stock’s close. .shift(1, fill_value=10_000) on the wealth gives the wealth before each day, starting from the 10,000 dollars invested.

The following box explains each day’s change in wealth.

panel["shares_before"] = panel.groupby("ticker")["shares_held"].shift(1, fill_value=0)
panel["price_change"] = panel.groupby("ticker")["close"].diff()

daily["price_move"] = (panel["shares_before"] * panel["price_change"]).groupby(panel["date"]).sum()
daily["explained"] = daily["price_move"] - daily["fee"]
daily["change"] = daily["wealth"] - daily["wealth"].shift(1, fill_value=10_000)
print(daily[["wealth", "change", "price_move", "fee", "explained"]])
              wealth  change  price_move   fee  explained
date                                                     
2026-03-02   9995.50   -4.50         0.0  4.50      -4.50
2026-03-03  10094.45   98.95       100.0  1.05      98.95
2026-03-04  10194.45  100.00       100.0  0.00     100.00

The explained change equals the change in wealth on every day.

Your turn

AAA pays a dividend of 0.50 dollars per share on 4 March. Add it to the wealth of that day. You should see 25 dollars for the 50 shares held and a wealth of 10,219.45 dollars.

The dividend goes to the shares held after the day’s trades, and it adds cash without a price move.

held = panel.loc[(panel["date"] == "2026-03-04") & (panel["ticker"] == "AAA"), "shares_held"].sum()
dividend = 0.50 * held
print(dividend, daily["wealth"].iloc[-1] + dividend)
25.0 10219.45