What is QSTrader?
QSTrader is a freely available backtesting simulation and live trading engine written in Python.
It has been developed for both retail quant traders and institutional quant hedge funds to aid strategy development and deployment.
It is currently under heavy development from a team of volunteer software developers. Bugs are fixed and features are added each week, leading to a robust, optimised institutional-grade algorithmic trading infrastructure.
Why use QSTrader?
Building a custom trading infrastructure or using vendor software is a complex decision. Here's why you should consider QSTrader.
Free, Open Source Licensing - QSTrader has been released under an extremely permissive open-source MIT License, which allows full usage in both research and commercial applications, without restriction. You can use it at home to carry out retail trading or within a quant fund as a basis for your research and/or order management system, with no download cost or usage license whatsoever.
Maintainability - Many developers improve the software for their own usage and collaborate with the community by sharing their code. New features are added frequently (often daily, but nearly always weekly). Any bugs are quickly determined and fixed.
Software Development - QSTrader is written in the Python programming language for straightforward cross-platform support. The codebase contains a suite of unit tests for the majority of its calculation engine and tests are constantly added for new features.
Event-Driven Architecture - QSTrader is completely event-driven both for backtesting and live trading, which leads to straightforward transitioning of strategies from a research and testing phase to a live trading implementation. Core strategy/portfolio code is often identical across both deployments.
Backtesting - QSTrader supports both intraday tick-resolution (top of order book bid/ask) datasets as well as OHLCV "bar" resolution data on various time scales.
Performance Metrics - QSTrader contains an entire Statistics module for performance measurement of portfolios over a backtest. This currently includes equity curve, period returns, drawdowns (through the Matplotlib/Seaborn libraries) and the usual risk/reward measures.
You can see the full source code and documentation for the project at the Github page.
Installation and Usage
QSTrader is in an early alpha state at the moment. It should only be used for exploratory backtesting research. The installation procedure is a little more involved than a standard Python package as it has not yet been added to the Python package repository.
Ubuntu Linux is the recommended platform on which to install QSTrader, but it will also work on Windows or Mac OSX under the Anaconda distribution (https://www.continuum.io/downloads).
For those that wish to create their own Python virtual environment the following steps are necessary to run both a basic Buy And Hold strategy as well as a slightly more complex Moving Average Crossover trend-following strategy.
An example virtual environment directory
~/venv/qstraderp3 has been used here. If you wish to change this directory then rename it in the following steps.
The following steps will create a virtual environment directory with Python 3 and then activate the environment:
$ mkdir -p ~/venv/qstraderp3 $ cd ~/venv/qstraderp3 $ virtualenv --no-site-packages -p python3 . $ source ~/venv/qstraderp3/bin/activate
At this point it is necessary to use pip to install QSTrader as a library and then manually install the requirements. The following steps will take some time (5-10 minutes as QSTrader relies on NumPy, SciPy, Pandas, Matplotlib as well as many other libraries and hence they will all need to compile:
(qstraderp3)$ pip install git+https://github.com/mhallsmoore/qstrader.git (qstraderp3)$ pip install -r https://raw.githubusercontent.com/mhallsmoore/qstrader/master/requirements.txt
Now that the library itself and requirements have been installed it is necessary to create the default directories for the data and output. In addition it is possible to download the necessary data and example code to run a simple backtest of a Buy And Hold strategy on the S&P500 index:
(qstraderp3)$ mkdir -p ~/qstrader/examples ~/data ~/out (qstraderp3)$ cd ~/data (qstraderp3)$ wget https://raw.githubusercontent.com/mhallsmoore/qstrader/master/data/SPY.csv (qstraderp3)$ cd ~/qstrader/examples (qstraderp3)$ wget https://raw.githubusercontent.com/mhallsmoore/qstrader/master/examples/(qstraderp3)$ buy_and_hold_backtest.py
Finally, we can run the backtest itself:
(qstraderp3)$ python buy_and_hold_backtest.py
Once complete you will see a full "tearsheet" of results including:
- Equity curve
- Drawdown curve
- Monthly returns heatmap
- Yearly returns distribution
- Portfolio-level statistics
- Trade-level statistics
The tearsheet will look similar to:
You can explore the
buy_and_hold_backtest.py file to examine the API of QSTrader. You will see that it is relatively straightforward to set up a simple strategy and execute it.
For slightly more complex buy and sell rules it is possible to consider a Moving Average Crossover strategy.
The following strategy creates two Simple Moving Averages with respective lookback periods of 100 and 300 days. When the 100-period SMA exceeds the 300-period SMA 100 shares of AAPL are longed. When the 300-period SMA exceeds the 100-period SMA the position is closed out. To obtain the data for this strategy and execute it run the following code:
(qstraderp3)$ cd ~/data (qstraderp3)$ wget https://raw.githubusercontent.com/mhallsmoore/qstrader/master/data/AAPL.csv (qstraderp3)$ cd ~/qstrader/examples (qstraderp3)$ wget https://raw.githubusercontent.com/mhallsmoore/qstrader/master/examples/moving_average_cross_backtest.py
The backtest can be executed with the following command:
(qstraderp3)$ python moving_average_cross_backtest.py
Once complete a full tearsheet will be presented, this time with a benchmark:
Other example strategies can be found in the
examples directory. Each example is self-contained in a
****_backtest.py file, which can be used as templates for your own strategies.
The project is constantly being developed, so unfortunately it is likely that the current API will experience backwards incompatibility until a mature beta version has been produced.
If you have any questions about the installation then please feel free to email email@example.com.
If you notice any bugs or other issues that you think may be due to the codebase specifically, feel free to open a Github issue here: https://github.com/mhallsmoore/qstrader/issues
License and Disclaimer
Copyright © 2015-2018 QuarkGluon Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Trading equities on margin carries a high level of risk, and may not be suitable for all investors. Past performance is not indicative of future results. The high degree of leverage can work against you as well as for you. Before deciding to invest in equities you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with equities trading, and seek advice from an independent financial advisor if you have any doubts.