Pairs Trading Strategy - Nifty & Bank Nifty

Pairs Trading Strategy - Nifty & Bank Nifty

Himanshu Agrawal | March 10, 2021


What is Pairs Trading?


Pairs trading strategy is a simple non-directional strategy. As the name suggests, you take offsetting long and short positions in a pair of highly correlated stocks. This strategy is a non-directional strategy; it works not only when there are big up-moves in the market but also any market conditions.


The idea is to find a pair of stocks that have a high correlation. We trade when the correlation between the two securities breaks. When one stock moves up while the other moves down, one would sell the outperforming stock and buy the underperforming one. The bet is that the "spread" between the two would converge.


This blog will try to build a pair trading strategy between the Indian indices, Nifty & Bank Nifty. We first understand the maths behind pairs trading and then show how to implement the strategy live using Quantinsti's Blueshift platform.


Mathematics behind Pairs Trading


While the concept behind pairs trading is simple, the mathematics behind the pairs trading strategy is based on certain advanced statistical concepts like mean reversion and stationary stochastic processes.


We start by looking at the correlation & co-integration between the two asset classes and then try to get a stationary relation that we can trade. We will brush up on these concepts in the blog, but there are many useful resources to study the pairs trading strategy.


Dr. Ernest Chan, a leading expert in AI and Quantitative Finance, explains pairs trading strategy simply and excitingly in "Mean Reversion Strategies in Python" on an interactive learning platform, Quantra. In this course, he describes various statistical methods to identify pairs in different instruments: equities, forex, commodities, among others. 


Correlation & Co-integration

 

We start off by correlation between the price data of the two indices. 


As the charts show, the two series are almost 80% correlated. But you not just want to look at the correlation but also at a statistic called co-integration before you take a pairs trade. A Co-integration test is used to establish a correlation between the time series in the long term. 


A famous test for co-integration, the Engle-Granger method starts by creating residuals based on the static regression and then testing the residuals for unit-roots presence. It uses the Augmented Dickey-Fuller Test (ADF) or other tests to test stationarity in time series.


Stationarity & Mean Reversion


We used a standard co-integration using statmodels in python and saw that the two series are co-integrated. The next step is to find the stationary relation based on which we can take the pair trade.


A time series is stationary if its mean and variance are constant over time. Finding a stationary time series is critical to model mean reversion. Only with a stationary process can you confidently say that the values will return to their mean, and fluctuations around the mean will have roughly equal amplitudes.


If you look at the spread between the two time-series, just looking at it, you can confidently say that it is not mean-reverting or stationary.


We, therefore, now need to work on our two time-series to a stationary relation based on which we can trade. We find that while the spread or the ratio of values is not stationary, the regression residuals between the two time-series are stationary. 


The plot below the regression residuals between the two time-series can be easily recognized as a stationary relation. The Augmented Dickey-Fuller test for stationarity also detects stationarity in the residuals.

Having detected this stationary relation, we can confidently say that if the residual goes above or below a certain value, we can expect it to revert back to the mean.


Implementing the pairs trading strategy


We used the Quantra Blueshift platform to implement the pairs trading strategy.


In the excellent course by Dr. Ernest Chan on Mean Reversion Strategies in Python, we learn about mean reversion using demos, examples & quizzes. We can build mean reversion strategies while learning it from experts with live models on the platform itself!


The course enables one to learn to create these strategies and start live trading them on the Quantra Blueshift platform.





The implementation is done using the zipline API on the Blueshift platform. The first step is creating the initialization function, which will define all the global variables. The set_commision and set_slippage functions are used to set transaction cost to zero.




Then we define a function that will handle the incoming data every minute, as we are running and looking for a pairs trading opportunity as often as possible.



Now, we finally apply the model and look at trading signal and how we will place orders. For trading signal, we will buy the undervalued index and sell the overvalued index when the spread calculated is above 1.5 (positive or negative). We will empty our positions when the spread is below 0.5 (positive or negative). We will place orders on the weights depending on trading signal and a constant. Looking at trading_signal and place_order python functions everything in the paragraph becomes clear.


Strategy Performance


Now is the time for the results. Our implementation gave a fairly decent performance on the Quantra platform. We see that the returns using a leverage of 3.5 are 59% in the 3 year period and the Sharpe Ratio is 2.28. The drawdown is also quite low at -3%



We can further improve our model by playing with the variables defined during initialization. As can be seen our results are promising to go live with. Pairs trading has always been seen as a lucrative statistical arbitrage strategy.

Further Reading


Readers can watch this video on pairs trading by Quantra to know more about the pair trading strategy. If you are interested in the Mean Reversion Strategies in Python course, you can email us at info@wrightresearch.in to avail a special 15% discount specially for Wright Research subscribers 


Investing in our pair trading strategy


Wright Research has recently launched a simple Nifty-Banknifty pair trading strategy on Tradetron that users of tradetron can subscribe and trade here: Pairs Trading by Wright Research


Drop in your email to continue reading

Welcome to Wright!

We are dedicated to help you invest better using our superior research backed portfolios.

Drop in your email to stay in touch!