Quant trading python code

Python to Extend Backtesting Dominance with 5x Faster Performance

8 August 2022
by Stuart Broadfoot, VP Engineering & Peter Huber, Investment Writer

Python is the optimal solution for quantitative backtesting. This optimality has been further compounded by a commitment from its developers to achieve a 5x increase in the runtime performance of the language.

Backtesting with Python


Backtesting is a form of data analysis which seeks to discern the historical profitability of a trading or investment strategy for a specified time period. It allows researchers, analysts and traders to identify indicators (e.g. simple moving average), trade signals (e.g. moving average crossover), and key performance metrics (e.g. Sharpe ratio, Sortino ratio) of an investment strategy prior to taking on exposure to the financial markets. Whilst strong backtesting results are no guarantee of the future profitability of a strategy, by subjecting historical data to rigorous analysis, an investor can shortlist potentially profitable strategies for further investigation.

Python is an open-source, general-purpose programming language which prioritises readability and facilitates structured, object-oriented, and functional programming paradigms. Originally conceived in the 1980s by Guido Van Rossum, Python was released to the public in 1991. Since then, its remarkable global popularity has propelled its ascent within both the computer programming and finance industries. Today, it is the most commonly used programming language within the fintech industry and the world’s leading programming language for backtesting. Due to its rising popularity, it is expected to surpass C++ shortly as the second most widely used programming language in finance.

The most popular programming languages for finance and fintechs
3. The most popular programming languages for finance and fintechs

The Benefits of Using Python for Backtests

Simple and intuitive, Python is the natural choice for backtesting. Users benefit from a comprehensive range of quality, open source packages (see examples in figure (2)) and a broad selection of machine learning, data science, and AI applications. A rich ecosystem of open source packages and programmer commentary makes Python the ideal language for building innovative, data-centric financial models. Furthermore, Python’s prevalence within academia provides students and scholars access to the latest research methods. Increasingly, these methods are being used to automate research processes.

Python Library

Use

Pandas

Data Analysis

NumPy

Numerical Computing

CVXPY

Convex Optimisation

Scikit-learn

Predictive Analysis

Scipy

Scientific Computing

Matplotlib

Visualisation

TensorFlow

Training Neural Networks

PyTorch

Training Neural Networks

Plotly

Visualisation

Seaborn

Visualisation


Figure 2: Popular Python libraries

Subscribe

Receive our latest blogs, trading strategy development ideas and insights

Python vs other Programming Languages

Python’s primary weakness compared with other programming languages is speed. When formulating the language the developer community had initially prioritised the ease of writing code, consequently sacrificing efforts to improve runtime performance. This has been a common site for criticism, with experienced developers familiar with other compiled programming languages (e.g. C, C++, Java) criticising Python for its sluggishness. However, the gap between Python’s performance and the performance of other compiled languages is narrowing. Indeed, at SigTech we feel that existing issues surrounding speed can be overcome with careful optimisation.


The Future of Python Backtests

Moving forward, speed and performance will be the central focus of Python programmers. This is a consequence of the widening applications of the language. Mark Shannon, one of the leading CPython developers1, has declared an intention ‘to speed up CPython by a factor of five over the next four releases’. Van Rossum suggested further efforts to speed up Python could include the introduction of frame stack optimisation, an adaptive bytecode interpreter, a stable application binary interface and machine code generation. Whilst Shannon envisages the eventual arrival of a JIT compiler, this is not expected until the release of Python 3.12. With Python 3.11 scheduled for release in October this year, 3.12 is expected in October 2023.

This commitment to further improve performance, coupled with the existing advantages of the language, makes us confident at SigTech that Python will remain the first choice for backtesting amongst quants and traders. We expect the language to continue to inform the analysis of financial markets data.


SigTech’s Python Backtesting Engine

SigTech began constructing its backtesting framework in 2013. Python was chosen as the programming language. Users of the platform often build and backtest trading and investment strategies drawing upon vast, multidecade datasets across asset classes and incorporating multiple data points per day. Conscious of the concerns over speed, we continuously monitor strategy runtime performance benchmarks for our codebase to ensure the optimal runtime of our platform. These benchmarks are critical in maintaining the current performance status quo and to protect the framework against sub-optimal code changes.

Due to the improvements made to Python in recent years, SigTech users have already enjoyed a pronounced increase in speed and performance. This increase in performance has been further compounded by the rise of cloud computing. Parts of our framework previously coded in Python have been moved into the cloud or have been optimised using Cython2. These changes precipitated a 2x general increase in the performance of SigTech’s backtesting engine. In some instances, these gains were substantially greater. The chart below describes how the runtime for a 10-year backtest of a systematic strategy using minute bar data has decreased markedly since 2015. Over the next two years we expect the backtest to be completed in under two minutes, representing a cumulative improvement of >95%.

Minutes taken to run backtest in python
Minutes taken to run backtest in python

With the upcoming changes announced by Python’s developers, we expect this trend to continue. SigTech is confident that Python will remain the first choice for backtesting for the foreseeable future.

Discover how SigTech can help you integrate Python-based backtesting into your trading and investment processes.

Get in Touch

References


1. CPython is the default reference implementation of Python.

2. Cython is a programming language which allows code predominantly written in Python to be converted to C and compiled for improved performance.

3. Source: https://blog.hackerrank.com/em...

Disclaimer

This document is not, and should not be construed as financial advice or an invitation to purchase financial products. It is provided for information purposes only and is subject to the terms and conditions of our disclaimer which can be accessed at: https://www.sigtech.com/legal/general-disclaimer