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

1. 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
2. Then we compute the OU coefficients ($\theta$, $\mu$, $\sigma$) of the 252 portfolio values using the Maximum Likelihood Estimation method, maximizing the average log-likelihood defined by (2.2):

where

• $x_j$ = the value at the $j$-th index of the portfolio values
• $\widetilde{\sigma}^2 = \sigma ^2\frac{1-e^{-2\mu\Delta t}}{2\mu}$
• $n$ = the number of portfolio values
• $Δt$ = 1 ÷ (days between the start and end dates of the portfolio values)

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

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

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