Turn a column of returns into the growth of one unit of money with (1 + r).cumprod().
An equity curve is what one unit of money is worth after each return in turn. Add 1 to every return to get a growth factor, then multiply the growth factors together as you go: (1 + r).cumprod().
In this lesson I build the curve on three returns I can check by hand, then on a column of daily returns read from a CSV, and I show that the last value of the curve is the last close divided by the first close.
Step 1. Three returns
cumprod() is the running product. Row 0 is the first growth factor, row 1 is the first times the second, row 2 is the first times the second times the third.
A gain of 10% followed by a loss of 10% leaves 0.99, not 1.0. The 10% loss is taken off 1.10, so it removes 0.11 and puts back less than the 0.10 that was gained.
One unit of money became 1.0395, so the three returns compounded to a gain of 3.95%.
print(f"{equity.iloc[-1] -1:.4%}") # -> 3.9500%
3.9500%
Step 2. A curve from a file of closing prices
prices.csv sits next to this lesson and holds simulated data: 40 business days of closes for three tickers. I keep AAPL, reset the index so the rows count from 0, and turn the closes into returns with pct_change() from Lesson 17.
The first day of the curve is just 1 + ret, since there is nothing yet to multiply by. Every day after that multiplies yesterday’s level by today’s growth factor.
Step 3. Check the end against the prices
Multiplying every growth factor together is the same as dividing the last close by the first, so the final value of the curve has to match.