### Abstract

In this tutorial, we apply Ornstein-Uhlenbeck model to a Pairs Trading process and derive the Optimal Entry and Liquidation levels.

### Introduction

Pairs trading is holding one stock while simultaneously shorting another stock, typically in an attempt to profit
from the convergence of the spread between these two stocks. One method of execution is to apply a Kalman Filter,
which we have implemented in this post.
However, today, we will model the portfolio values of holding positions in
a pair of stocks as an Ornstein-Uhlenbeck (OU) process in order to derive the optimal values to enter and liquidate
the position in the pair of stocks. Let (`number`

) refer to the corresponding equation in the given paper, which can
be found under the **References** section. For example, (2.1) refers to equation 2.1 in the paper.

### Method

#### Computing the OU Coefficients

Say there are two arbitrary stocks A and B. For each value in .01, .02, .03, …, 1.00:

- We compute the portfolio values over 252 days of the pairs trade by computing the value of holding $1 of A minus the value of holding $ B (we short B) on each of the 252 days
- Then we compute the OU coefficients (, , ) of the 252 portfolio values using the Maximum Likelihood Estimation method, maximizing the average log-likelihood defined by (2.2):

where

- = the value at the -th index of the portfolio values
- = the number of portfolio values
- = 1 ÷ (days between the start and end dates of the portfolio values)

We then select the , which we differentiate as , that maximizes the average log-likelihood defined in the equation in step 2, while keeping the corresponding , , and values. The implementation of finding , , and can be found in **ou_mle.py** in the **Algorithm** section.

#### Deriving the Optimal Entry and Liquidation Levels

With the OU coefficients in hand, we can now calculate the optimal entry level, the portfolio value at which we buy, and liquidation level, the portfolio value at which we sell. We don’t buy the portfolio of $1 of A and -$ of B immediately (note: to scale up a position, for each additional dollar we invest in A, we short an additional -$ of B). Instead, we wait until our portfolio reaches the computed entry level, before we buy the portfolio of the pair of stocks. Once we bought this portfolio, we wait until the portfolio value reaches the computed liquidation level before we liquidate our portfolio. By waiting for our optimal entry and liquidation values to be reached, we can mathematically maximize our expected gain. To compute the optimal entry and liquidation levels, we first need to define a few equations:

Where constants = the cost of transaction and = investor’s subject discount rate. We choose to set as these were the values given in the paper.

With these equations, we can now solve for the optimal liquidation level, which we denote as , as well as the optimal entry level, which we denote at . Note: the reason we derive the optimal liquidation level first is because we use in the computation of .

As deriving and require the derivative of the functions given above, given an arbitrary function input and an arbitrary function , we approximate with the following equation equation:

where is some arbitrarily small value (we set in our implementation).

To find , we solve for in the following equation:

We solve this equation by getting all terms to one side:

Finding the root of the above equation results in .

Now to derive , we solve for in the following equation:

Again, we will move all terms to one side:

And finding the root of this equation results in .

The full method of finding and can be found in the **OptimalStopping.py**
file in the **Algorithm** section.

#### Trading

Our trading logic is very simple. First, we feed in 252 points of the most recent data for the daily closing prices for stocks A and B. We then train our model on these data points, and we get the , , and values. We then keep track of a hypothetical portfolio of holding $1 of A and -$ of B. Once the value of our hypothetical portfolio is less than or equal to , we allocate 1.0 of our capital to long of A and short B using ( x capital used for A) worth of stock. Once the hypothetical portfolio value we tracking reaches , we liquidate our positions. We repeat these trading rules when possible. On the first day of every quarter, we retrain our model with the most recent 252 points of data to update our , , and values.

### Video Walkthrough

### Results

Our algorithm yielded a Sharpe ratio of 0.815 over a five year period, while holding SPY over the same period yielded a Sharpe ratio of 0.612. However it should be noted, due to the fact the algorithm had to wait periods of time before our optimal entry and liquidation levels were reached, our algorithm only made twelve trades over the entire backtest duration. To increase the number of trades, we can add additional pairs, such as GLD-GDX. We encourage our users to clone this algorithm and and to experiment with different ideas for pairs and to play with the parameters of the algorithm.

### Reference

- Leung, Tim and Li, Xin, Optimal Mean Reversion Trading with Transaction Costs and Stop-Loss Exit (April 26, 2015). International Journal of Theoretical and Applied Finance, Vol. 18, No. 3, 2015. Online Copy.