/ Python

Current State of Open-Source Backtesting Frameworks in Python

Python has become quite popular in the quant finance community. No wonder – it has dozens of useful open source libraries for data analysis, optimization, machine learning, visualization and reporting among others. Its code is simple and readable and allows for quick prototyping. And yet it's speed is suitable for most real-world applications.

Thanks to that there's also large number of backtesting frameworks to choose from – from some basic testing tools to very mature production-ready algorithmic trading libraries. It can be difficult and time-consuming to find the right one for your purpose. And while everyone's goals can be quite different there are some basic attributes that can help to filter out quickly those frameworks that are unsuitable for particular needs. These are for example:

  • (Lack of) proper documentation
  • Development activity and support
  • Supported data feeds and formats
  • Availability of live trading and various order types
  • Supported timeframes (and/or support for real-time / tick data)
  • Included indicators
  • Capability for optimization
  • Type of license

The following is a list of backtesting frameworks I stumbled upon while searching for the right one for my needs. I haven't tried or explored in-depth all of them. Sometimes because of the lack of documentation and other times because it was obvious that the particular library lacks a feature I need. So it's just a basic overview which can point you in the right direction.

Backtrader

  • Repository: https://github.com/backtrader/backtrader
  • License: GPL v3.0
  • Well documented and mature framework with active development and community. Allows to backtest using multiple timeframes. Its CSV data feed is very flexible and allows for custom formats. A feed containing tick data can be also used but trade simulation with only ask and bid prices would probably require some tweaking. Allows to simulate different order types and also slippage. According to the documentation it's possible to trade live with Interactive Brokers, Oanda and using Visual Chart. Has its own indicators but allows also for TA-Lib integration.

Bt

  • Repository: https://github.com/pmorissette/bt
  • License: MIT
  • Also seems to be actively developed although the documentation is not as rich as in Backtrader's case. It uses a "tree structure" which allows to form a hierarchy of strategies and dynamically allocate funds to "sub-strategies" based on their equity curve. This allows to build complex systems from relatively simple blocks and makes Bt suitable for portfolio strategies based on daily data and asset weighting and rebalancing.

Pinkfish

  • Repository: https://github.com/fja05680/pinkfish
  • License: MIT
  • Is supposed to allow testing intraday strategies with daily data (not suitable for HFT). Uses Pandas for spreadsheet-like features, Matplotlib for charting and TA-Lib for indicators. The repository contains code examples written in Jupyter notebooks. The development doesn't seem to be very active.

Prophet

  • Repository: https://github.com/Emsu/prophet
  • License: BSD-3
  • A simple framework which seems to be in early stages of development. Allows to simulate slippage and commission. Ability to handle data with high frequency is planned for future release. Leverages Pandas and NumPy for data handling and analysis.

PyAlgoTrade

  • Repository: https://github.com/gbeced/pyalgotrade
  • License: Apache 2.0
  • Well-documented framework with great features, support for many different data feeds and live trading capabilities. Unfortunately it seems that the development has stopped. The last version was released two years ago. The library doesn't include tick data feed but it shouldn't be hard to write your own. Also few changes need to be done in order to use it with Python3. Contains some indicators but allows also for TA-Lib integration. Moreover the Twitter package adds support for receiving Twitter events and incorporating them in your strategies.
  • EDIT: A new version (0.20) has been released on 2018-08-20

PyBacktest

PySystemtrade

  • Repository: https://github.com/robcarver17/pysystemtrade
  • License: GNU v3.0
  • Open source version of Rob Carver's backtesting engine which has been released to accompany his book (Systematic Trading) and blog articles. As many other Python backtesting libraries it uses Pandas, NumPy, Matplotlib and SciPy. The original intent was to release a really well-documented code with minimum support. As far as I can tell the first part went well. The user guide is extensive with many examples. This probably makes it a good choice for learning how backtesting environments work and maybe even as a starting point for building your own framework. In the future the project should include live trading for Interactive Brokers futures.

QsForex, QsTrader

  • Repository: https://github.com/mhallsmoore
  • License: MIT
  • Both tools were made by well-known QuantStart founder Michael Halls-Moore who chose a modular approach in order to be able to use the same core code for development and trading. QsTrader is now being completely redeveloped. It supports OHLC data as well as tick data. In the future it should allow for live trading. Development of QsForex has stopped.

QTPyLib

  • Repository: https://github.com/ranaroussi/qtpylib
  • License: GNU LGPL v3.0
  • Well-documented, feature-rich and actively developed event-driven algorithmic trading system written in Python which supports both backtesting and live trading (with Interactive Brokers). Data events use asynchronous, non-blocking architecture. Allows to save data into MySQL database for further processing. Comes with a basic reporting web app where you can monitor your trade history and open positions. Supports both automatic trade notifications via SMS and custom messages. Integrates TA-Lib for technical indicators. The strategies can react to every price tick.

Rqalpha

Ultra-finance

Visualize-wealth

Zipline

  • Repository: https://github.com/quantopian/zipline
  • License: Apache 2.0
  • One of the most advanced and feature-rich trading libraries. It is used as a backtesting and live-trading engine powering Quantopian – a free, community-centered, hosted platform for building and executing trading strategies. Has a good tutorial. Provides an easy way to run the algorithm inside Jupyter Notebook without using command line. Comes with several data bundles included and allows to register new ones. OHLC data can be also read from CSV files but the library is not suited for working with high-frequency / tick data. A trading calendar feature allows to account for market hours in backtests.