A fast and efficient approach for evaluating trading strategies

Aha! You found a pattern in the market that can potentially make you the next Warren Buffett… Now what? After designing a new trading strategy that can systematically profit from that pattern, it is now the time to ask yourself: “What if I had this strategy five years ago?”.

Today, I will discuss how vectorized backtesting can help you “answer” that question, how to implement it using Pandas, and what subtleties should you be aware of. For those who have some experience in this area, feel free to jump to section 5 directly.

(You can find the full code and additional resources here)

1. What Is Backtesting?

Backtesting is used to simulate the past performance of a trading strategy.

The core concept of backtesting is to simulate a given trading strategy by going back in time and execute the trades as if you were in the past. The generated profits are often compared against a benchmark performance through some metrics (e.g. max drawdown, Sharpe ratio, annualized returns, etc.). Depending on the nature of the strategy, different benchmarks and metrics should be used, but that is a whole topic on its own so I won’t be going into details here.

2. What Backtesting Is Not

Backtesting is not an exact indicator of past performance and should not be used as a research tool, especially in inexperienced hands.

Funnily enough, backtesting is not a good indicator of how rich you would be today if you could travel back in time. Time-travelling aside, it is virtually impossible to replicate past performance exactly, since there are a number of factors that are just way too complicated to be modelled exactly (slippage, market impact of trades, etc.). In addition, in inexperienced hands, a backtest can be full of biases (e.g. look-ahead bias, survivor bias, etc.) which can make a strategy look much better than reality (see section 5.3).

Finally, backtesting is not a research tool. Do not randomly change your strategy parameters just because it looks better in the backtest. You should consider the historical performance of the market as one of many possible realizations of a random variable. Fitting your parameters to the backtest without sound economic logic will inevitably lead to over-fitting.

3. Event-Driven vs Vectorized Backtesting

Vectorized backtesting is drastically faster than event-driven backtesting and should be used for the explorative stage of strategy research.

There are two main methods for backtesting a strategy: (i) event-driven and (ii) vectorized. An event-driven backtest often involves the use of a loop that iterates over time, simulating an agent that sends orders depending on the market signals. This loop based approach can be extremely flexible, allowing you to simulate potential delays in orders execution, slippage costs, etc.

A vectorized backtest instead gathers strategy related data and organizes them into vectors and matrices, which are then processed through linear algebra methods (the thing your computer is really good with) to simulate past performance.

Due to the expressive power of linear algebra, vectorized backtests are DRASTICALLY FASTER than event-driven backtests, although it lacks in terms of flexibility. Usually, vectorized backtests are better suited for an initial explorative strategy analysis, whereas event-driven backtests are suited for a more in-depth analysis.

#money #python #trading #finance #data-science

Backtest Trading Strategies with Pandas — Vectorized Backtesting
7.00 GEEK