top of page

Backtesting with Python and Quantopian

Writer's picture: Manogane SydwellManogane Sydwell

Updated: Oct 14, 2020

Backtesting is the process where an individual analyses how well a devised trading strategy performs when applied to historical data. I first became acquainted with the idea of backtesting when studying derivatives in my undergraduate degree. The topic was covered in the book Options, Futures and Derivatives by John C. Hull.


In this particular post, I will describe the implementation of a mean-reversion strategy using the pipeline API provided by Quantopian, a company established with the intention of becoming a crowd-based hedge fund. More interesting than the idea of a crowd-based hedge fund would be that this particular company is in competition with various other crow-based hedge funds such as WorldQuant and QuantConnect. 






As an example, lets assume that you devise a strategy or a model or an algorithm that you presume will consistently predict the value of the JSE All Share Index. By making use of historical price data, the model can be backtested to verify weather the predicted results would be the same as the actual results. Backtesting is therefore, used to validate the assumptions behind a model.


The strategy to be backtested in this post is a mean reversion strategy. According to Wikipedia, mean reversion is a term that is used to describe that a stock's price will generally move towards its average price. According to the rationale behind this idea, when a stock experiences a shock due to a quarterly earnings release or because of macroeconomic phenomena or some other reason, it will eventually revert back towards the mean price.



Details regarding the Pipeline API

The mean reversion strategy will be tested by making use of Quantopian's Pipeine API.

The pipeline API is used because it makes the deployment of algorithmic strategies much more efficient. The typical algorithmic trading strategy development process has the following structure:

  1. Define a universe of tradable assets.


2.Select a subset within this group according to some specified ranking metric.

3.Calculate the preferred portfolio weights for the subset of tradable assets.


4.Place orders to move the current portfolio allocations to the desired portfolio allocations.


Without the Pipeline API, the process described above would be extremely more difficult to implement. Just like how the pandas python library makes data analysis more efficient for individuals conducting data analysis, the Quantopian API provides the same benefit, but for people conducting quantitative analysis. If analysts do not make use of Pipeline, then all the efficiencies it provides would have to be constructed manually, and that would take a long time!




Pipeline Filter

A functionality provided by the Pipeline API, would be Filter, which is used to reduce the number of securities that are eventually traded. One reason to narrow down the traded securities would be to remove securities that do not adhere to trading criteria. Maybe the proposed strategy only trades stocks with a certain liquidity, and completely does not trade exchange traded funds. This filter would help us adhere to this criteria.



Trading Universe

The stocks that can be used in this strategy are defined in the base universe variable, as shown above. This variable is in turn equal to the QTradableStock filter, which consists of in between 1600-2100 stocks. The diversity in stocks helps reduce the risk associated with the individual stocks in the portfolio. Portfolio diversification is a key component of portfolio management. Check out what the Chicago Mercantile Exchange has to say about diversification.


QTradableStocksUs is a pipeline filter which selects a daily universe of stocks. It complies with a predetermined criteria in order to yield the most liquid universe possible

An important point to mention is that QtradableStocksUS is not a dataset, which is fixed, but rather a filter, which is variable and changes on a daily basis.


On the day that the backtest was conducted, some of the stocks traded in the mean-reversion strategy were Microsoft,which most individuals are familiar with,and Array BioPharma, Inc. which engages in the research, development and commercialization of targeted small molecule drugs for the treatment of cancer and other high-burden diseases. The benefits of diversification were mentioned above.



Implementing the Algorithm

Quantopian encourages the development of algorithmic trading strategies in Jupyter Notebook, an open source notebook the allows for the creation and sharing of documents with live code. This simplifies the trial and error phase. Having gone through this a few times myself, I stand by the advice given. 


After one obtains satisfactory performance from the algorithm, in order to implement it, one has to make use of an Integrated Development Environment, or IDE for short. The visual performance of the proposed algorithm is presented below.



Results from the Backtest

The backtest was performed on the filtered securities from January 2011 to July 2012.

The key metrics from the backtest are as follows:

Returns:

Mean-Reversion Strategy: 19.09% Benchmark(SnP 500): 10.42%


Volatility:

Mean-Reversion Strategy: 21%


Sharpe:

Mean-Reversion Strategy: 1.28


Maximum Drawdown:

Mean-Reversion Strategy: 0.15


A month to month look at the metrics provided above can be found here. More information on the above metrics can be found here


Concluding Remarks

The Pipeline API improves the development and implementation of algorithmic trading strategies. Another benefit would be the community support that Quantopian provides. For the sake of brevity, other parts of the algorithmic trading strategy development process were not included so as to better focus on the backtesting component.The business model followed by Quantopian is also something to marvel at. I look forward to a similar initiative in Africa in future, for our financial markets.The full algorithm can be found here


Resources

Working on our Best Universe yet, Quantopian.com

Array BioPharama, BusinessInsider.com

Mean Reversion, Wikipedia.com

TSP Model Performance Metrics, modelInvesting.com

91 views0 comments

Recent Posts

See All

Comments


©2020 by creativeAfricanProjects.

bottom of page