Chapter 6. Building Classes for Event-Based Backtesting
The actual tragedies of life bear no relation to one’s preconceived ideas. In the event, one is always bewildered by their simplicity, their grandeur of design, and by that element of the bizarre which seems inherent in them.
Jean Cocteau
On the one hand, vectorized backtesting with NumPy
and pandas
is generally convenient and efficient to implement due to the concise code, and it is fast to execute due to these packages being optimized for such operations. However, the approach cannot cope with all types of trading strategies nor with all phenomena that the trading reality presents an algorithmic trader with. When it comes to vectorized backtesting, potential shortcomings of the approach are the following:
- Look-ahead bias
-
Vectorized backtesting is based on the complete data set available and does not take into account that new data arrives incrementally.
- Simplification
-
For example, fixed transaction costs cannot be modeled by vectorization, which is mainly based on relative returns. Also, fixed amounts per trade or the non-divisibility of single financial instruments (for example, a share of a stock) cannot be modeled properly.
- Non-recursiveness
-
Algorithms, embodying trading strategies, might take recurse to state variables over time, like profit and loss up to a certain point in time or similar path-dependent statistics. Vectorization cannot cope with such features.
On the other hand, event-based backtesting
Get Python for Algorithmic Trading now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.