Introduction
Pairs trading is a non-directional, market-neutral trading strategy which seeks to identify the price divergence from two highly correlated assets. Pairs trading assumes the co-movement and the mean reversion of two asset prices. It indicates that the price of selected assets tend to move together, and when they diverge, we can exploit the investment opportunity by taking a market neutral position as we assume their price will tend to move back to the average price over time. In this algorithm, we use a large family of international exchange-traded funds (ETFs) to examine the performance of the pairs trading strategy.
Method
The selection of pairs
The investment universe consists of 25 international ETFs. The pair selection is based on a rolling time period called the formation period.
During each formation period, we save the history closing price of each ETF. Here we use a Python list-like container called deque
to keep a fixed-length price series. From these prices, we compute normalized cumulative price indices which are comparable across the ETFs in the group.
According to the paper Pairs Trading on International ETFs from Panagiotis, Dimitrios and Tao,
here the formation period is 120 days.
\[R_t^n=\prod_{1}^{t}(1+r_t^n) \ \ \ for \ \ \ t=1,2,...,120 \] \[r_t^n=\frac{P_t}{P_{t-1}}-1\]
Where \(r_t^n\) is the simple return of the \(n\)-th ETF and \(R_t^n\) is the index value.
For each formation period, we compute the average absolute distance among all pairs in the group and then rank the distances from the smallest to largest to identify the trading opportunities. The top 5 pairs with the smallest distance are used in the subsequent 20 day trading period. The definition of distance is
\[D_t^{a,b}=\frac{1}{120}\sum_{t+1}^{t+120}\mid P_t^a-P_t^b\mid \ for \ all\ pairs \ a,b \]
class Pair:
def __init__(self, symbol_a, symbol_b, price_a, price_b):
self.symbol_a = symbol_a
self.symbol_b = symbol_b
self.price_a = np.array(price_a)
self.price_b = np.array(price_b)
# compute normalized cumulative price indices
self.index_a = np.cumprod(self.price_a[1:]/self.price_a[:-1])
self.index_b = np.cumprod(self.price_b[1:]/self.price_b[:-1])
def distance(self):
return 1/120*sum(abs(self.index_a -self.index_b))
The Definition of Divergence
Now we consider the top 5 pairs. For each of the one month trading period, we compute the 120-day normalized cumulative price indices and compare them to a fraction of the distance value \(D_t^{a,b}\) for each pair (a,b). Assume the threshold is 0.5, if the index value of asset A is greater than asset B by 0.5 times the current distance \(D_t^{a,b}\), then we sell asset A and buy asset B. The quantity of A and B is based on the dollar neutral. We invest the same amount of money in the long leg and the short leg, vice versa if the index value of asset B is greater than asset A by 0.5 times the distance. When the difference between index A and index B falls into the range \(\left(-0.5D_t^{a,b},0.5D_t^{a,b}\right)\), we liquidate the pair. The above procedure is repeated for all top 5 pairs. At the start of next month, the new pairs are selected and trade for next month.
for i in self.sorted_pairs:
pair = Pair(i[0], i[1], self.history_price[i[0].value], self.history_price[i[1].value])
index_a = pair.index_a[-1]
index_b = pair.index_b[-1]
delta = pair.distance()
if index_a - index_b > self.threshold*delta:
if not self.portfolio[pair.symbol_a].invested and not self.portfolio[pair.symbol_b].invested:
ratio = self.portfolio[pair.symbol_a].price / self.portfolio[pair.symbol_b].price
quantity = int(self.calculate_order_quantity(pair.symbol_a, 0.2))
self.sell(pair.symbol_a, quantity)
self.buy(pair.symbol_b, floor(ratio*quantity))
elif index_a - index_b < -self.threshold*delta:
if not self.portfolio[pair.symbol_a].invested and not self.portfolio[pair.symbol_b].invested:
ratio = self.portfolio[pair.symbol_b].price / self.portfolio[pair.symbol_a].price
quantity = int(self.calculate_order_quantity(pair.symbol_b, 0.2))
self.sell(pair.symbol_b, quantity)
self.buy(pair.symbol_a, floor(ratio*quantity))
# the position is closed when prices revert back
elif self.portfolio[i[0]].invested and self.portfolio[i[1]].invested:
self.liquidate(pair.symbol_a)
self.liquidate(pair.symbol_b)
Derek Melchin
See the attached backtest for an updated version of the algorithm with the following changes:
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jing Wu
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!