Latest News (May 2020)
We are pleased to announce that QSTrader has now been added to the Python Package Index as version 0.1.0. This means QSTrader can now be installed via
pip install qstrader
- What is QSTrader?
- Why use QSTrader?
- How does QSTrader work?
- Frequently Asked Questions
- Software Engineering Principles
- Trading Disclaimer
What is QSTrader?
QSTrader is a free Python-based open-source modular schedule-driven backtesting framework for long-only equities and ETF based systematic trading strategies.
QSTrader can be best described as a loosely-coupled collection of modules for carrying out end-to-end backtests with realistic trading mechanics.
The default modules provide useful functionality for certain types of systematic trading strategies and can be utilised without modification.
However the intent of QSTrader is for the users to extend, inherit or fully replace each module in order to provide custom functionality for their own use case.
The software is currently under active development (semantic version 0.x.x) and is provided under a permissive "MIT" license.
Why use QSTrader?
QSTrader comes with an extensive, well-tested suite of interchangeable systematic trading modules for simulation and backtesting of long-only equity/ETF strategies.
This means you can concentrate on strategy development and not infrastructure development. Here is a list of the top features that are currently supported:
- Alpha Models - A collection of single or multi-asset forecast templates that can be extended to produce complex forecasting models. Holding period return (HPR)-based "momentum" and simple moving average "trend" templates are provided as baseline examples. It is straightforward to use the built-in Alpha Models as a template for your own.
- Simulation Engine - Signal generation, portfolio construction and execution are decoupled from the time iteration of the simulation engine to allow independent control of cashflows, risk reporting, rebalancing and trading. This allows portfolios to be rebalanced at a frequency independent of both signal generation and the frequency used to poll for performance/risk.
- Portfolio Construction - Portfolio weight optimisation and order sizing is included 'out-of-the-box'. A collection of baseline optimisers is provided including equal weight (1/N) and user-supplied static proportional asset weights.
- Simulated Broker with Portfolio Accounting - The default simulated brokerage engine in QSTrader handles all asset position tracking and management, with support for multiple separate portfolios. The intent is to provide a framework for periodic and one-off cashflows such as subscriptions and withdrawals, allowing realistic simulation of investment income over long-term investment horizons. Specific brokers have associated fee models, which allow realistic simulation of commissions/fees for trades.
- Data Handling - Price/volume data is currently supported from flat-file CSV.
- Performance Statistics - An extensive collection of performance and risk statistics to enable post-simulation analytics is included. By default a 'tearsheet' visualisation is generated to determine performance 'at a glance' upon completion of a backtest.
- Rapid Prototyping - QSTrader has an extensive suite of end-to-end modules with sensible defaults. This minimises the need to customise the software if you just want to carry out a quick prototype test of some signal logic. Equally, if you wish to run a sophisticated portfolio optimisation mechanism with multiple alpha and risk models, it is easy to override the defaults with your custom logic.
QSTrader is also the backtesting system used within many of the QuantStart strategy tutorials. Hence if you wish to see some examples of what it is capable of you can browser through our articles list.
How does QSTrader work?
QSTrader is a periodically-scheduled and rebalanced backtesting engine, built around the concept of alpha, risk and transaction cost models that feed into a portfolio construction model and execution mechanism.
This paradigm of systematic trading engines is inspired by the design outlined within Inside The Black Box by Rishi K. Narang.
QSTrader works by constructing a desired target portfolio of assets on every algorithm rebalance. This target portfolio is compared against the current portfolio at the simulated broker. The comparison generates rebalancing trades that push the current portfolio towards the desired portfolio. This process is then run iteratively for every desired rebalance period.
In order to generate such a target portfolio a Portfolio Construction Model is used, which takes in a set of quantitative 'opinions' from Alpha Models, Risk Models and Transaction Cost Models.
Alpha Models provide forecasts, or strength of convictions, on one or more tradeable assets. These forecasts may utilise features of the asset price/volume data or external data. The Alpha Models are designed to encapsulate logic for typical quant trading signals such as momentum, value or mean-reversion.
Risk Models provide guidance on exposures of assets to certain potentially undesirable risks such as market risk or sector exposure. These exposures can then be utilised to neutralise potential weightings of assets that might allocate too much 'risk' to certain unwanted factors. Note: Risk Models are not present in the current version of QSTrader.
Transaction Cost Models provide estimates for the profitability of individual trades and outline whether they may be too expensive to carry out. For instance a rebalance process of a portfolio may wish to buy only a single share of a stock in order to move the current portfolio closer to the target portfolio. However if trading commissions are fixed then the expected return for carrying out the rebalance may be less than the hurdle of overcoming the transaction cost. In this case the trade might simply be cancelled until the current portfolio has drifted far enough from the target portfolio to make a rebalancing trade worthwhile over its cost. Note: Transaction Cost Models are not present in the current version of QSTrader.
The Portfolio Construction Model utilises all three of the above models, along with additionally provided quantitative optimisation and order sizing processes, to generate a target portfolio to be executed by the Execution Model.
Crucially, the schedule of the simulation as a whole can be carried out independently of the portfolio construction rebalance schedule, which provides the ability, say, to determine daily or intraday performance even on a strategy that only rebalances once a month. Similarly this functionality can provide higher-frequency intraday performance and exposure updates on a strategy that may only trade once per day. This mechanism also allows for cashflows and corporate actions to be applied when they actually occurred historically.
It is the task of the Broker engine to keep track of the cash, asset positions and portfolio state at all times in the simulation process. This allows a clean separation between target portfolio generation logic and portfolio cashflows and accounting.
QSTrader contains sensible, conservative default implementations for all of the above behaviour. This allows useful backtests to be run with minimal customisation beyond specification of Alpha and Risk models. It also allows straightforward customisation for those who wish to develop bespoke behaviour for any module in the system.
We recommend utilising the Python 3.7 Anaconda distribution as it contains nearly all of the external libraries needed by QSTrader. Python 2.7 is not supported for QSTrader. Alternatively a virtual environment can be utilised.
To install QSTrader simply use pip:
pip install qstrader
If you have any trouble installing the software please contact us at firstname.lastname@example.org with any appropriate error logs and we will attempt to resolve your installation issues.
Frequently Asked Questions
Q) Does QSTrader support live trading?
A) At this stage QSTrader does not have direct linkage to any brokerage API. However it is one of the most important features on our development roadmap.
Despite this however it is designed to provide a straightforward list of rebalance trades per rebalance run. This means it is possible to execute the QSTrader systematic trading portfolio construction process as desired and then carry out the order execution of all the trades generated by this process.
Q) Is QSTrader suitable for technical analysis style 'indicator' trading?
A) QSTrader supports TA-style indicators through the concept of Alpha Models. Classical indicators such as Moving Averages, Stochastic Oscillators, Bollinger Bands etc can be incorporated into Alpha Models.
Be aware however that QSTrader is built around a slightly different paradigm as it makes use of a Portfolio Construction process prior to order generation. This means that TA indicator signals are not converted directly into buy/sell orders of a certain specified size.
Instead a form of optimisation is carried out over the signals to produce target weights, which are then sized appropriately into orders depending upon specified risk and transaction cost models.
Converting directly from TA-style indicator signals into buy/sell orders effectively circumvents this process, which is also supported by simply not supplying any risk or transaction cost models.
Q) Can I help out with QSTrader development?
A) Yes - we gladly welcome contributions to the QSTrader codebase. The first step is to request access to our dedicated Slack channel by contacting email@example.com. Once you have gained access we recommend proposing a change and discussing it with the team and the community. We will then review a submitted Pull Request against the Github QSTrader codebase. One of the most important aspects of QSTrader development is that any submitted code must have a significant unit and/or integration suite present, otherwise we will not be able to review code.
Software Engineering Principles
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 team's systematic strategy research, 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. Any bugs are quickly determined and fixed.
Testing - The codebase contains an extensive suite of unit and integration tests for all of the provided modules. The aim is to keep code coverage high.
Cross-Platform - QSTrader is writte in the Python programming language for straightforward cross-platform support. Utilising the Python Anaconda distribution it is supported on Windows, macOS and Linux.
Copyright © 2015-2020 QuantStart.com, 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.