Measure how much of SPY's rally your portfolio actually kept, with pandas.
By PyQuant News
Published: 2026-10-17
URL: https://pyquantnews.substack.com/p/use-capture-ratios-to-improve-trading
Each week, I send out one Python tutorial to help you get started with algorithmic trading, market data analysis, and quant finance. Upgrade to a paid plan to access the Python code notebooks, Discord community, and code support.
Students in my course can tell me what their account returned last year, but not whether it kept up with SPY.
The assumption I hear most in office hours is simple. If the account made money, the strategy must be working. So you fund it again and move on to the next idea.
That assumption cost me most of a year, because my test only made money in the months when the market itself rose.
The number nobody checks
When I started out, a moving average crossover I tested in pandas on daily closes returned 38% in 2020, before commissions. SPY, the fund that tracks the S&P 500 index, returned about 18% that year, and I never checked how much of my 38% came from that.
The same pattern shows up in my course community most weeks. Someone posts a backtest with tuned moving average lengths and three indicators, but SPY never appears on the chart.
The teams that vet managers at pension funds and endowments split performance into rising and falling months before they judge anything at all.
What you’ll build
In today’s post, you’ll measure how much of a rising market your portfolio actually captured, using Python.
Let’s go.
