Quant500 Academy

1. Optimizing without fooling yourself: what a backtest really is

Level 12: Backtesting and Real Risk Metrics

A backtest is, put plainly, the process of testing a trading hypothesis over past periods of time. Instead of applying the strategy going forward and waiting years to judge its behavior, the trader simulates the rules on relevant historical data and gets an answer in seconds. It is the most powerful tool you have, and also the easiest one to misuse.

Alongside the backtest there is always its dangerous sibling: optimization. Optimizing lets you improve a strategy's results by tuning the parameters and formulas that govern it. You can tune a single parameter or a whole set of them to reach a specific objective. One optimization objective might be to maximize total profit. Another might be to minimize drawdowns, that is, the falls from peak equity.

Having stated the purpose of optimization, it is worth being aware that it is a double-edged sword. If you pile enough rules onto the training data until you get the equity curve you wanted, the result is overfitting: the model will most likely lose its predictive power on the test data and on the future. Put another way, if you torture the data long enough, it will confess whatever you want to hear. And the market is under no obligation to repeat that confession tomorrow.

There is an intuition that helps you stay grounded: every parameter you add and every combination you test consumes degrees of freedom. If you evaluate thirty thousand parameter combinations over the same stretch of history, the best of them will show a spectacular result even if your signal is worthless, simply because you went looking for the maximum of the noise. That maximum does not repeat out of sample.

Types of backtesting systems

Broadly speaking, there are two kinds of backtesting system for putting a hypothesis to the test.

Backtrader, the library you will use in this level, is an event-driven engine: it processes bars one at a time and only lets you see what has already happened.

To go deeper into this lesson

The rest of Quant500

What you are learning, applied to the index every day:

The model portfolio, weekly in your inbox

Every Saturday, its week in one minute: how it went, the best and the worst, and which insiders bought with their own money. Plus, the day the model is rebuilt, which stocks come in and which go out. A simulation, not advice. At most one email a week, no ads, no account; one-click unsubscribe. Made by one person: Pol Gracia.

A model portfolio with simulated results, not a personalized recommendation. Your email is used for this only: privacy.

Behind the Academy and the rest of Quant500 there is one person: Pol Gracia Serrahima.