
In his article on QuantStart, expert in the development of financial applications Frank Smitana (Frank Smietana) spoke about the existing frameworks for creating software for backtesting trading strategies and gave some tips on choosing such tools. We adapted this useful material.
What is backtesting
Backtesting is called the “run” of the programmed trading strategy for real trading data that occurred in the past (“historical data”). This is an extremely important process that has the same meaning as the actual development of a strategy and its launch during real-time trading in real time. It is believed that high-quality 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 making deals with a financial program — in this case, real-time mode is used.
')
Most of the frameworks described in this article contain not only the functionality of direct backtesting, but also provide certain opportunities for running simulations.
In the Python sphere, the situation with specialized software is quite good - the community has six open frameworks available for creating backtesting tools.
Before choosing a framework
Before proceeding to the selection of the framework and the development of a backtester, you should determine the requirements for the trading system as a whole. Among other things, there are the following important questions that require answers:
- Operations with which asset class the system will perform . Most frameworks support working with data on trading in US stocks (via Yahoo Finance), but if you need to trade in derivatives, ETFs, or other tools, you can import this information in some way. In addition, it is not enough to find it, it is also necessary for the framework to work with the necessary type of assets - whether it can handle operations with futures and options, correctly make large transactions in not the most liquid markets, and so on.
- How high-frequency and data-demanding the strategy will be . A trading system that requires information on each tick or a change in the bid / ask spread is very different from a trading robot operating on five-minute or one-hour trading data intervals. It is important to understand that in order to create systems of the first type, 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. There are also platforms that provide datasets for various asset classes, such as the S & P index shares at the minute scale.
- What types of orders should the system support ? 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 in the early stages of their development and can not boast of extensive documentation and support, far removed from the discussions on the forums.
Components of the backtesting and optimization framework
Framework for creating backtesters, as a rule, include several components:
- The data receiving module - it is responsible for reading the file with the strategy code. If the framework requires recoding the strategy before launching the backtest, then it is important that it supports library functions for the most popular technical indicators - this will speed up testing.
- Performance testing module - it analyzes a wide range of risk metrics and strategy performance when working on a dedicated historical data set (including maximum deposit drawdown during trading), Sharpe and Sortino coefficients.
- Optimization takes the lion's share of resources in the strategy development process. 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 become clear 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 suggest that in this case it will be necessary to calculate 40 combinations of various parameters of possible intersections.
In the context of working with a portfolio, optimization implies finding the optimal weights for each of the assets, including tools that can be sold short or leverage is used to work with them. Periodically there should be a rebalancing of the portfolio, which is expressed in the conduct of additional transactions in order to bring it to the optimal form.
Another important element of optimization is the control of the size of open positions. This approach allows developers to simulate and analyze the impact of the use of leverage and dynamic scaling of positions on the overall effectiveness of the strategy.
Six Backtesting Framework for Python
Standard open backtesting platforms for Python usually have several common characteristics:
- focus on events;
- flexible licensing without special restrictions;
- extensive set of integrated technical indicators;
- standard functionality for calculating performance metrics, visualization and report generation.
PyAlgoTrade
PyAlgoTrade is an already established framework that includes the ability to test both historical data and simulations in real-time. Supports data from Yahoo! Finance, Google Finance, NinjaTrade and any sources providing information in CSV (for example, Quandl). Supports orders types of market, limit, stop and stop limit.
PyAlgoTrade supports Bitcoin trading via Bitstamp, as well as real-time processing of information from Twitter.
bt - Backtesting for Python
The creators of the bt framework strive to facilitate the development of easily testable, flexible and reusable logic blocks of trading strategies, which should open up the possibility to create complex automated financial applications.
The framework is suitable for testing so-called portfolio-based strategies, including algorithms for portfolio weighting and rebalancing. Modifying strategies for launching at different time intervals and using different weights of tools in the portfolio requires minimal effort to change the code. In addition, bt is built into ffn - this is the popular financial library Python.
Backtrader
This platform is superbly documented, the developers maintain a blog and develop an active online community, whose members are happy to help find the answer to the interesting 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
Rob Carver, pysystemtrade developer, published an excellent article on why he decided to create another Python backtesting framework, 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 an algorithmic trading simulator. You can work with it through the browser interface IPython Notebook. The system is an alternative to command line interface tools. The service is developed and maintained by the Quantopian project team, and it can be used both as a separate backtesting 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 1-minute resolution, several options for importing information are also available.
QSTrader
Another framework with real trading functionality, launched by QuantStart, the founder of the resource for finance experts, Michael Halls-Moore. He wanted to create a tool that would be suitable for use by large hedge funds and private investors at the same time. Currently, the QSTrader supports “bar” data resolution (
OHLCV ) at different time intervals, however, the use of tick data is not yet available.
Both modes of operation (backtesting and real trading) are completely event-based, which allows you to quickly move from developing strategies to testing them and then launching them in a “combat” mode. One of the main advantages of the system is its modularity, which leaves ample opportunities for customizing the code.
Other materials on finance and stock market from ITI Capital :