Contents

# Strategy Library

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