Selection: 6 open frameworks for creating backtests of trading strategies in Python

Original author: Frank Smietana


In his article on QuantStart, financial application development expert Frank Smietana talked about existing frameworks for creating software for backtesting trading strategies and gave some tips on choosing these tools. We have adapted this useful material.

What is backtesting


Backtesting is called the “run” of a programmed trading strategy based on real trading data that has occurred in the past (“historical data”). This is an extremely important process, which is as important as the development of a strategy and its launch during real-time trading in real time. It is believed that well-conducted testing on historical data helps to identify some of the shortcomings and bottlenecks of the trading system before it leads to massive losses.

In addition to backtesting, there is the concept of simulating a trading strategy. Simulation programs simulate the occurrence of conditions that should serve as triggers for transactions by a financial program - in this case, real-time mode is used.

Most of the frameworks described in this article contain not only direct backtesting functionality, but also provide certain capabilities for running simulations.

In Python, the situation with specialized software is pretty good - there are six open frameworks available for community members to create backtesting tools.

Before choosing a framework


Before proceeding to the choice of a framework and the development of a backtester, you should decide on the requirements for the developed trading system as a whole. Among other things, there are the following important questions that need answers:

  • Operations with which class of assets the system will perform . Most frameworks support working with data on trading in US stocks (through Yahoo Finance), but if you need to trade derivatives, ETFs or other instruments, it is possible to somehow import this information. In addition, it is not enough to find it, you also need the framework to work with the necessary type of assets - can it handle transactions with futures and options, correctly make large transactions in not-so-liquid markets, and so on.
  • How high and demanding the data strategy will be . A trading system that requires information on each tick or change in the bid / ask spread is very different from a trading robot working on five-minute or hourly intervals of trading data. It is important to understand that to create the first type of system, hedge funds and HFT companies have to invest huge amounts of money in development - this is the only way they can create software that can cope with the required loads. At the same time, there are platforms that provide data sets for various asset classes, such as shares of the S&P index on a minute scale.
  • What types of orders should the system support . A gentleman's set of any self-respecting trading system includes market, limit, stop and stop-limit orders.
  • The level of support and documentation required. Some of the available frameworks are at the first stages of their development and cannot boast of extensive documentation and support, which has gone far from discussions on the forums.

Backtesting and Optimization Framework Components


Backtest frameworks typically include several components:

  • Data receiving module - it is responsible for reading the file with the strategy code. If the framework requires recoding the strategy before running the backtest, it is important that it supports library functions for the most popular technical indicators - this will speed up testing.
  • Performance testing module - it is used to analyze a wide range of risk metrics and strategy performance when working on a dedicated set of historical data (including the maximum “drawdown” of a deposit when trading), Sharp and Sortino ratios .
  • Optimization takes the lion's share of resources in the process of developing a strategy. If you can not do without it, then you should choose frameworks that support distributed and parallel processing.

When developing strategies using technical indicators, developers try to choose the optimal set of parameters for each of them. For example, during testing, it may turn out that the use of intersections of 6 and 10-day moving averages leads to an increase in the profitability of the strategy when it is run on historical data in comparison with any other periods from 1 to 20 days. Simple calculations indicate that in this case, you need to calculate 40 combinations of various parameters of possible intersections.

In the context of working with a portfolio, optimization involves finding the optimal weights for each of the assets, including instruments that can be sold shortly or used to work with leverage. The portfolio should be rebalanced periodically, which is expressed in additional transactions in order to bring it to its optimal form.

Another important element of optimization is the control of the sizes of opened positions. This approach allows developers to simulate and analyze the impact of leverage and dynamic scaling of positions on the overall performance of the strategy.

Six backtesting frameworks for Python


The standard open backtesting platforms for Python usually have a number of common characteristics:

  • event orientation;
  • Flexible licensing without special restrictions;
  • an extensive set of built-in technical indicators;
  • Standard functionality for counting performance metrics, visualization and reporting.

PyAlgoTrade


PyAlgoTrade is an already established framework that includes the ability to both test on historical data and conduct simulations in real-time. Supports data from Yahoo! Finance, Google Finance, NinjaTrade and any sources that provide information in CSV (for example, Quandl). Supports orders of types market, limit, stop and stop limit.

PyAlgoTrade supports trading bitcoins through Bitstamp, as well as processing information from Twitter in real time.


bt - Backtesting for Python


The creators of the bt framework seek to facilitate the development of easily testable, flexible and reusable logical blocks of trading strategies, which should open up the possibility of creating complex automated financial applications.

The framework is suitable for testing the so-called portfolio-based strategies, including algorithms for weighing and rebalancing the portfolio. Modifying strategies for launching at different time intervals and using different instrument weights in the portfolio requires minimal effort to change the code. In addition, bt is built into ffn, a popular Python financial library.


Backtrader


This platform is well documented, developers blog and develop an active online community, whose members are happy to help find the answer to your question. Backtrader supports various data formats, including CSV, Pandas DataFrames, realtime data feeds from several foreign brokers and various iterators. Data processing from different sources can be carried out simultaneously and even at different time intervals.


pysystemtrade


Pysystemtrade developer Rob Carver published an excellent article about why he decided to create another framework for backtesting in Python, which listed the pros and cons of developing a new framework. pysystemtrade includes a number of important functions, such as optimization and calibration modules, and also allows you to implement fully automated futures trading.


Zipline


Zipline is a algorithmic trading simulator. You can work with it through the IPython Notebook browser interface. The system is an alternative to command line interface tools. The service is developed and supported by the Quantopian project team, and it can be used both as a separate backtester development tool and in conjunction with the Quantopian development and testing environment. The Zipline platform provides access to ten years of historical data on US stocks in a 1-minute resolution, and several options for importing information are also available.


QSTrader


Another framework with real trading functionality launched by QuantStart founder Michael Halls-Moore. He wanted to create a tool that was suitable for both large hedge funds and private investors. At the moment, QSTrader supports “bar” data resolution ( OHLCV ) at various time intervals, however, the use of tick data is not yet available.

Both operating modes (backtesting and real trading) are completely event-driven, which allows you to quickly switch from developing strategies to testing them and, then, launching them in “combat” mode. One of the main advantages of the system is its modularity, which leaves ample opportunities for customizing the code.


Other financial and stock market related materials from ITI Capital :



Also popular now: