Contents

# Strategy Library

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

### 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 (θ, µ, σ) of the 252 portfolio values using the Maximum Likelihood Estimation method, maximizing the average log-likelihood defined by this function:
$-\frac{1}{2}ln(2\pi)-ln(\widetilde{\sigma})-\frac{1}{2n{\widetilde{\sigma}}^2}\sum_{i=1}^{n}[x_i-x_{i-1}e^{-\mu\Delta t}-\theta(1-e^{-\mu\Delta t})]^2\quad(2.2)$

where

xj is the value at the jth index of the portfolio values

and

$\widetilde{\sigma}^2 = \sigma ^2\frac{1-e^{-2\mu\Delta t}}{2\mu}$

and n = the number of portfolio values

and Δt = 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 θ*, µ*, σ* values. The implementation of finding θ*, µ*, and σ* can be found in ou_mle.py in the Algorithm section.

### Results

Our algorithm yielded a Sharpe ratio of 0.898 over a five year period, while holding SPY over the same period yielded a Sharpe ratio of 0.667. 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.

### References

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.

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