### 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.

Where is the simple return of the -th ETF and 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

```
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 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 , 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 , 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.CalculateOrderQuantity(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.CalculateOrderQuantity(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)
```