# Strategy Library

## Pairs Trading with Stocks

### Introduction

The pairs trading algorithm aims to find two stocks which have prices that moved historically together. If price series diverges, long and short positions are opened in the opposite direction. With the assumption of mean reversion, the algorithm expects to make profits from the abnormal fluctuation of prices. The crucial part of pairs trading is determining which stocks are correlated and how to define a price divergence.

### Method

#### Pairs Formation

The first step of this algorithm is to select stock pairs from a universe of stocks. We use the history request to get the history closing price for the last one year. This is called the formation period. The matching partner for each stock is found by looking for the security that minimizes the sum of squared deviations between two normalized price series. Assume there are two stocks A and B with the price series X and Y. For price normalization, the starting price during formation period is set to $1. The formula of distance measure is \[\sum_{i=1}^n{(\frac{x_i}{x_1}-\frac{y_i}{y_1}})^2\] Top 4 pairs with the smallest historical distance measure are then traded. The trading pairs are selected every half year. We use the schedule event method to fire the rebalance function.

#### Trading Pairs

As prices in a pair of stocks were closely cointegrated in past, there is high probability that those two securities share common sources of fundamental return correlations. A temporary shock could move one stock out of the common price band which presents statistical arbitrage opportunity. Given the trading pairs, the trading period is the next six months. We calculate the price spread series of the last one year. When pair prices have diverged by two standard deviations, which means the spread is 2 times standard deviation away from its long-term mean, the algorithm will go short the stock which price is diverging up and go long the stock which price is diverging down. The position is closed when prices revert back.

You can also see our Documentation and Videos. You can also get in touch with us via Discord.

Did you find this page helpful?