Python Backtesting Frameworks: Six Options to Consider
February 27, 2023
5 min read
Explore 6 powerful Python backtesting framework options to find what's best for your trading needs, put your theories to the test, and improve your trading strategies.
Backtesting is an important step in creating a trading strategy. While testing a model against historical data isn't a guarantee of success, and overfitting is always a risk, backtesting is still the best indicator you have regarding how your model will do in live trading. But do you know how to get started?
It's no surprise that Python is a popular option for testing trading models. The language is easy to learn and has robust tools for processing data and statistics. There are at least a dozen open-source frameworks for backtesting trading models. While many of them share common properties, they are not created equal.
Let's look at six popular options for writing backtests with Python. But before we get started, let's talk about backtesting and Python.
What is Backtesting?
Backtesting is a process for testing how well a trading strategy would have fared in the past. It uses historical market data to see how well your algorithm would have played out during a time interval in the past. If your test is successful, you have more confidence — but no guarantee — that your strategy will work in the future. Backtesting acts as a "stake in the ground," an indicator of how your strategy will fare in the future.
Backtesting has been available to large institutions for a long time, but because of the high cost of market data it wasn't feasible for smaller traders. But with data prices falling and open-source tools becoming more widely available, anyone can test a trading strategy.
Is Python Good for Backtesting?
Python is an excellent tool for backtesting, and here are some reasons why:
- Tools like Jupyter Notebook and iPython make it easy to write and test code
- NumPy and Pandas simplify processing large numerical datasets
- There are many backtesting libraries available in Python
- R, another popular language for processing numbers, interacts well with Python
- It's easy to call C and C++ from Python if you need to call faster code or an external algorithm
Even if your trading code will run in another language, it's easy to either call your trading code from Python or express your algorithm using Python mathematical modeling tools. As you'll see, since most Python backtesting frameworks work well with these tools, it's easy to swap out Python frameworks or test your strategies with more than one tool.
Python Backtesting Frameworks
Let's look at six of the more popular options for backtesting in Python. We'll start with an overview of frameworks, then dive in.
When evaluating a backtesting library, there are a few key features to keep in mind:
- Documentation — Are the docs up-to-date and easy to follow? Sample code isn't enough, especially if you run into problems down the road.
- Supported data sources and formats — A good framework won't tie you to one or two sources for historical pricing. Ideally, it accepts market data as a Pandas array so you can adapt any data you wish.
- "Pythonic" frameworks — While Python is an easy and forgiving language, most developers prefer to write code that takes advantage of the language's unique features and supports commonly used tools, like Pandas and NumPy. One of the many advantages of this style is it avoids locking you into a specific framework.
- Licensing — You want flexible licensing that won't create any liability issues.
Backtesting for Python (bt)
BT is a flexible framework for testing quantitative trading strategies. It makes it easy to build models that mix and match different algorithms. Its developer designed it so you can create building blocks of trading logic that are easy to test and reuse.
BT has built-in code for retrieving prices from Yahoo, but you can plug in your own prices with a few lines of code. The framework has an excellent getting started guide and is covered by MIT's permissive license.
Finmarketpy has a simple API for analyzing market data and testing trading strategies. You can define your backtests using a simple templating system. Finmarketpy's object-oriented model, which isn't common in Python frameworks, makes it very easy to extend and reuse trading strategies. It relies on sample code for documentation though, so you may need some extra time and effort to get up to speed, and it might prove difficult for novices to use.
This framework uses findatapy to retrieve market data. This script supports several market data sources, including Yahoo, Quandl, and Bloomberg. It's also easy for experienced developers to modify this script for new sources. Finmarketpy is covered by the Apache 2.0 license.
One of the first things you'll see on Backtrader's homepage is a link to extensive documentation. If you're looking for a framework to learn with, this one might be your best bet. Backtrader works with Pandas DataFrames, CSV, and real-time data feeds from Interactive Brokers, Oanda, and Visual Chart.
Backtrader calls itself a platform, and its features list supports that assertion. It supports multiple simultaneous data sources, as well as varying timeframes — it's a full-featured trading and testing platform.
Backtrader uses the GPL v3.0 license.
Like the previous libraries, Zipline integrates well with Pandas DataFrames, but it also comes with an extensive library of working algorithms and building blocks for creating your own. It also works well with machine learning toolkits like scikit-learn.
Zipline is licensed under Apache 2.0.
PyAlgoTrade is a well-established backtesting library with complete documentation and thorough integration with NumPy, SciPy, and Pandas. It boasts integrated support for Yahoo, Google, and NinjaTrader for market data. It also supports events from Twitter.
PyAlgoTrade is licensed under Apache 2.0.
Fastquant is a simple library that makes it easy to pull data into Jupyter notebooks. It has built-in access to data from Yahoo, the Philippine Stock Exchange, and Binance. It also has several integrated trading strategies, including Relative Strength Index, simple moving average crossover, buy and hold, and more.
Fastquant is offered under the MIT license.
Like Zipline, QSTrader offers live trading capabilities. It's another full-featured backtesting framework built and supported by the team at QuantStart. QSTrader has Alpha Models tools for building trading models, a simulated broker engine, and sophisticated graphing tools.
One key benefit of QSTrader is its design. It's designed with an eye toward clean separation of concerns and modularity, which makes it highly extensible and easy to integrate with your code.
QSTrader is also offered under the MIT license.
Backtesting is critical to trading success, and selecting the right framework to use for that backtesting is key. We've looked at six popular Python frameworks and have seen how you can get started with them for testing your trading efforts. Each of them has its benefits and specific offerings, and now you have an idea of how to evaluate them and see how they fit into your trading systems.
Are you looking for tools to simplify setting up backtesting pipelines and running multiple strategies and tests? Pipekit's Managed Argo Workflows are the platform you're looking for. Sign up for a demo now.
Subscribe for Pipekit updates.
Get the latest articles on all things Pipekit & data orchestration delivered straight to your inbox.