Pairs Trading - Slides - Pairs - Trading
Pairs Trading - Slides - Pairs - Trading
Everybody is amiliar with the concept o correlation between two random variables:
correlation is high when they co-move
correlation is zero when they move independently
So what is cointegration?
cointegration is high when two quantities move together or remain close to each other
cointegration is inexistent i the two quantities do not stay together
Clear? You can see why this concept may be diffcult to grasp at rst, but the truth is
that it’s easy.1
In the nancial context:
Cointegration o (log-)prices yt reers to long-term co-movements.
Correlation o (log-)returns ∆yt = yt − yt−1 characterizes short-term co-movements in
(log-)prices yt .
1
Y. Feng and D. P. Palomar, A Signal Processing Perspective on Financial Engineering. Foundations and
Trends in Signal Processing, Now Publishers, 2016.
D. Palomar (HKUST) Pairs Trading 5 / 63
Correlation vs. cointegration
Example o high correlation with no cointegration:
5
ỹ1t
y2t
ỹ1t − y2t
−1
0 20 40 60 80 100 120 140 160 180 200
0.8
0.6
0.4
Log−returns of stock 2
0.2
−0.2
−0.4
−0.6
−0.8
−1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
Log−returns of stock 1
0.5
−0.5
−1
−1.5
−2
−2.5
−3
y1t
y2t
y1t − y2t
−3.5
0 20 40 60 80 100 120 140 160 180 200
0.8
0.6
0.4
Log−returns of stock 2
0.2
−0.2
−0.4
−0.6
−0.8
−1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
Log−returns of stock 1
A time series is called integrated o order p, denoted as I(p), i the time series obtained
by dierencing the time series p times is weakly stationary,
while by dierencing the time series p − 1 times is not weakly stationary.
Example: stock log-prices yt are integrated o order I(1) because
log-prices are not stationary
but log-returns yt − yt−1 are stationary (at least or some period o time).
A multivariate time series is said to be cointegrated i it has at least one linear
combination being integrated o a lower order, e.g., yt is not stationary but wT yt is
stationary or some weights w.
0.5
−0.5
−1
−1.5
−2
−2.5
−3
y1t
y2t
y1t − y2t
−3.5
0 20 40 60 80 100 120 140 160 180 200
Recall that i two time series are cointegrated, then in the long term they remain close to
each other.
In other words, the spread zt = y1t − γy2t is mean reverting.
This mean-reverting property o the spread can be exploited or trading and it is
commonly reerred to as “pairs trading” or “statistical arbitrage”.
The idea behind pairs trading is to
short-sell the relatively overvalued stocks and buy the relatively undervalued stocks,
unwind the position when they are relatively airly valued.
zt
Sell Sell
s0
Sell to unwind
−s0
Buy
2
G. Vidyamurthy, Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons, 2004.
D. Palomar (HKUST) Pairs Trading 16 / 63
Pairs trading or statistical arbitrage
Statistical arbitrage can be used in practice with prots:3
0.5
Spread 0
−0.5
0 20 40 60 80 100 120 140 160 180 200
(a)
1
0.5
Position
−0.5
−1
0 20 40 60 80 100 120 140 160 180 200
(b)
40
30
P&L
20
10
0
0 20 40 60 80 100 120 140 160 180 200
(c)
3
M. Avellaneda and J.-H. Lee, “Statistical arbitrage in the US equities market,” Quantitative Finance,
vol. 10, no. 7, pp. 761–782, 2010.
D. Palomar (HKUST) Pairs Trading 17 / 63
But how to discover cointegrated pairs and γ?
One interesting approach is based on a VECM modeling o the universe o stocks: From
the parameter β contained in the low-rank matrix Π = αβ T one can extract a
cointegration subspace. Ater that, one can design some portolio within that
cointegration subspace.4
A simpler approach to discover pairs is by brute orce, i.e., try exhaustively dierent
combinations o pairs o stocks and see i they are cointegrated.
But, given a potential pair, how do we obtain the “secret” γ?
Easy! Just a simple LS regression!
Recall that
γ is needed to orm the spread to be traded (i.e., portolio)
the spread mean µ is needed to determine the thresholds or entering a trade and unwind
later the position.
4
Z. Zhao and D. P. Palomar, “Mean-reverting portolio with budget constraint,” IEEE Trans. Signal
Process., vol. 66, no. 9, pp. 2342–2357, 2018.
D. Palomar (HKUST) Pairs Trading 18 / 63
Outline
1 Cointegration
2 Basic Idea o Pairs Trading
3 Design o Pairs Trading
Pairs selection
Cointegration test
Optimum threshold
4 LS Regression and Kalman or Pairs Trading
5 From Pairs Trading to Statistical Arbitrage (StatArb)∗
VECM
Optimization o mean-reverting portolio (MRP)
6 Summary
Design o a pairs trading strategy
We rst ocus on pairs trading (i.e., statistical arbitrage between two stocks) as the
example to introduce the main steps o statistical arbitrage.
In practice, pairs trading contains three main steps5 :
Pairs selection: identiy stock pairs that could potentially be cointegrated.
Cointegration test: test whether the identied stock pairs are indeed cointegrated or not.
Trading strategy design: study the spread dynamics and design proper trading rules.
5
G. Vidyamurthy, Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons, 2004.
D. Palomar (HKUST) Pairs Trading 20 / 63
Outline
1 Cointegration
2 Basic Idea o Pairs Trading
3 Design o Pairs Trading
Pairs selection
Cointegration test
Optimum threshold
4 LS Regression and Kalman or Pairs Trading
5 From Pairs Trading to Statistical Arbitrage (StatArb)∗
VECM
Optimization o mean-reverting portolio (MRP)
6 Summary
Pairs selection: normalized price distance
where the normalized price p̃1t o stock 1 is given by p̃1t = p1t /p10 . The normalized prices
o stock 2 dened similarly.
One can easily (i.e., cheaply) compute the NPD or all the possible combination o pairs
and select some pairs with smallest NPD as the potentially cointegrated pairs.
Later one can use a more rened measure o cointegration (more computationally
demanding).
6
E. Gatev, W. N. Goetzmann, and K. G. Rouwenhorst, “Pairs trading: Perormance o a relative-value
arbitrage rule,” Review of Financial Studies, vol. 19, no. 3, pp. 797–827, 2006.
D. Palomar (HKUST) Pairs Trading 22 / 63
Outline
1 Cointegration
2 Basic Idea o Pairs Trading
3 Design o Pairs Trading
Pairs selection
Cointegration test
Optimum threshold
4 LS Regression and Kalman or Pairs Trading
5 From Pairs Trading to Statistical Arbitrage (StatArb)∗
VECM
Optimization o mean-reverting portolio (MRP)
6 Summary
Least Squares (LS) regression
I the spread zt is stationary, it can be written as7
zt = y1t − γy2t = µ + ϵt
where
µ represents the equilibrium value and
ϵt is a zero-mean residual.
Equivalently, it can be written as
y1t = µ + γy2t + ϵt
LS regression is used to estimate the parameters µ and γ, obtaining the estimates µ̂ and
γ̂.
I y1t and y2t are I(1) and are cointegrated, then the estimates converge to the true values
as the number o observations goes to innity8 .
Using the estimated parameters µ̂ and γ̂, we can compute the residuals
Then, one has to decide whether the spread is stationary, i.e., ϵt is stationary. In practice,
the estimated residuals are used ϵ̂t
There are many well-dened mathematical tests or the stationarity o ϵ̂t , e.g., augmented
Dicky-Fuller (ADF) test, Johansen test, etc.
8
R. F. Engle and C. W. J. Granger, “Co-integration and error correction: Representation, estimation, and
testing,” Econometrica: Journal of the Econometric Society, pp. 251–276, 1987.
D. Palomar (HKUST) Pairs Trading 25 / 63
Outline
1 Cointegration
2 Basic Idea o Pairs Trading
3 Design o Pairs Trading
Pairs selection
Cointegration test
Optimum threshold
4 LS Regression and Kalman or Pairs Trading
5 From Pairs Trading to Statistical Arbitrage (StatArb)∗
VECM
Optimization o mean-reverting portolio (MRP)
6 Summary
Optimum threshold
Once some identied pairs have passed the cointegration test, one still needs to decide
the entry and exit thresholds to open and unwind the positions, respectively.
For the sake o concreteness, we ocus on studying the entry threshold:
open positions when the spread diverges rom its long-term mean by s0
unwind the position when it reverts to its mean
Thus, the key problem now is how to design the value o s0 such that the total prot is
maximized.
Total prot:
prot o each trade × number o trades
prot o each trade is s0
number o trades is related to the zero crossings, which can be analized theoretically as well
as empirically.
We ocus now on estimating the number o trades.
1 − Φ(s0 )
For each trade, the prot is s0 and then the total prot is s0 T(1 − Φ(s0 )).
Then the optimal threshold is s⋆0 = arg maxs0 {s0 T(1 − Φ(s0 ))}.
In practice, one cannot know the true distribution but can estimate the distribution
parameters.
Then one can compute the total prot based on estimated distribution.
D. Palomar (HKUST) Pairs Trading 28 / 63
Optimum threshold s0 : Parametric approach∗
Optimal threshold s⋆0 maximizes the total prot:
0.7 3
Theoretical
0.6 Parametric 2.5
Probability of trades
0.1 0.5
0 0
0 1 2 3 0 1 2 3
s0 s0
(a) (b)
0.25
Theoretical
0.2 Parametric
Total profit
0.15
0.1
0.05
0
0 0.5 1 1.5 2 2.5 3
s0
(c)
The empirical values f̄j may not be a smoothed enough and the resulted prot unction
may not be accurate enough.
Smooth the trading requency unction by regularization:
J
∑ J−1
∑
minimize (f̄j − fj )2 + λ (fj − fj+1 )2
j=1 j=1
where
1 −1
1 −1
D=
∈ R(J−1)×J .
.. ..
. .
1 −1
Setting the derivative o the objective w.r.t. to zero yields the optimal solution
⋆ = (I + λDT D)−1 ̄.
The optimal threshold is the one maximizes the total prot:
0 0
0 1 2 3 0 1 2 3
s0 s0
(a) (b)
0.2
Theoretical
NonParam: empirical
0.15 NonParam: regularized
Total profit
0.1
0.05
0
0 0.5 1 1.5 2 2.5 3
s0
(c)
Using the estimated parameters µ̂ and γ̂, we can compute the residuals
ϵ̂t = y1t − µ̂ − γ̂y2t .
Then, one has to decide whether the cointegration is acceptable or not so move to the
trading part.
There are many well-dened mathematical tests or the stationarity o ϵ̂t , e.g., augmented
Dicky-Fuller (ADF) test, Johansen test, etc.
Total prot:
prot o each trade × number o trades
prot o each trade is s0
number o trades is related to the zero crossings, which can be analized theoretically as well
as empirically.
Ideally, we want residuals with large amplitude (variance) as well as a strong mean
reversion because they directly aect the prot.
1 1
0 0
−1 −1
−2 −2
Z−score
−3 signal −3
Aug 01 Sep 01 Oct 02 Nov 01 Dec 01 Jan 02 Feb 01 Apr 02 May 01 Jun 01 Jul 02 Aug 01 Sep 04 Nov 01 Dec 03 Jan 02 Jan 31
2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2002 2002
2.5 2.5
2.0 2.0
1.5 1.5
1.0 1.0
Aug 01 Sep 01 Oct 02 Nov 01 Dec 01 Jan 02 Feb 01 Apr 02 May 01 Jun 01 Jul 02 Aug 01 Sep 04 Nov 01 Dec 03 Jan 02 Jan 31
2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2002 2002
8 Z−score 8
signal
6 6
4 4
2 2
0 0
−2 −2
Jul 03 Aug 01 Sep 01 Oct 02 Nov 01 Dec 01 Jan 02 Feb 01 Apr 02 May 01 Jun 01 Jul 02 Aug 01 Sep 04 Nov 01 Dec 03
2000 2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
1.8 1.8
1.6 1.6
1.4 1.4
1.2 1.2
1.0 1.0
Jul 03 Aug 01 Sep 01 Oct 02 Nov 01 Dec 01 Jan 02 Feb 01 Apr 02 May 01 Jun 01 Jul 02 Aug 01 Sep 04 Nov 01 Dec 03
2000 2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
The problem with the LS regression is that it assumes that µ and γ are constant.
In practice, they can change with time, resulting in a spread that drits rom equilibrim
never to revert back with huge potential losses.
Thus, in practice, µ and γ are time-varying and have to be tracked.
How to track time-varying parameters?
O course… Kalman!!!
Well, you can also try a rolling regression or exponential smoothing, but Kalman works
better.
Recall the previous static relationship or cointegrated series y1t and y2t :
y1t = µ + γy2t + ϵt
µt+1 = µt + η1t
γt+1 = γt + η2t
Kalman lter consist o two equations that model the time-varying hidden state xt and
the observations yt :
xt+1 = Tt xt + η t
y t = Z t x t + ϵt
The observation equation yt = Zt xt + ϵt relates the observation yt to the hidden state xt
as a linear relationship, where Zt is the time-varying observation matrix and ϵt is a
zero-mean Gaussian error ϵt ∼ N (0, R) with covariance matrix R.
The state transition equation xt+1 = Tt xt + η t expresses the transition o the hidden
state rom xt to xt+1 as a linear relationship, where Tt is the time-varying transition
matrix and η t is a zero-mean Gaussian error η t ∼ N (0, Q) with covariance matrix Q.
The Kalman lter is extremely versatile in modeling a variety o real-lie processes.9
9
J. Durbin and S. J. Koopman, Time Series Analysis by State Space Methods, 2nd Ed. Oxord University
Press, 2012.
D. Palomar (HKUST) Pairs Trading 40 / 63
Kalman or pairs trading
Kalman lter (state transition equation and observation equation):
xt+1 = Txt + η t
y1t = Zt xt + ϵt
where µt
xt ≜ is the hidden state
γ
t
1 0
T≜ is the state transition matrix
0 1
σ12 0
η t ∼ N (0, Q) is the i.i.d. state transition noise with Q =
0 σ22
Zt ≜ 1 y2t is the observation coeffcient matrix
ϵt ∼ N 0, σϵ2 is the i.i.d. observation noise
Note that this is a time-varying Kalman lter since Zt is time-varying.
Parameters σ12 , σ22 , σϵ2 can be estimated using the EM algorithm using historical data or
calibration.
The hidden state path xt gives the sought time-varying coeffcients.
D. Palomar (HKUST) Pairs Trading 41 / 63
Kalman or pairs trading
Log-prices o ETFs EWH and EWZ:
Log−prices 2000−08−01 / 2003−12−31
2.4 2.4
2.2 2.2
2.0 2.0
1.8 1.8
1.6 1.6
Aug 01 Nov 01 Feb 01 Jun 01 Sep 04 Jan 02 Apr 01 Jul 01 Oct 01 Jan 02 Apr 01 Jul 01 Oct 01 Dec 31
2000 2000 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003 2003
mu.LS
1.0 mu.rolling.LS 1.0
mu.Kalman
0.8 0.8
0.6 0.6
0.4 0.4
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
gamma.LS
0.6 gamma.rolling.LS 0.6
gamma.Kalman
0.5 0.5
0.4 0.4
0.3 0.3
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
LS
0.15 rolling.LS 0.15
Kalman
0.10 0.10
0.05 0.05
0.00 0.00
−0.05 −0.05
−0.10 −0.10
−0.15 −0.15
Aug 01 Nov 01 Feb 01 Jun 01 Sep 04 Jan 02 Apr 01 Jul 01 Oct 01 Jan 02 Apr 01 Jul 01 Oct 01 Dec 31
2000 2000 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003 2003
2 Z−score 2
signal
1 1
0 0
−1 −1
−2 −2
−3 −3
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
2.5 2.5
2.0 2.0
1.5 1.5
1.0 1.0
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
2 Z−score 2
signal
1 1
0 0
−1 −1
−2 −2
−3 −3
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
3.0 3.0
2.5 2.5
2.0 2.0
1.5 1.5
1.0 1.0
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03 May 01 Aug 01 Nov 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003 2003 2003 2003
Z−score
2 signal 2
1 1
0 0
−1 −1
−2 −2
−3 −3
Aug 01 Oct 02 Dec 01 Feb 01 Apr 02 Jun 01 Aug 01 Oct 01 Dec 03 Feb 01 Apr 01 Jun 03 Aug 01 Oct 01 Dec 02 Feb 03 Mar 31
2000 2000 2000 2001 2001 2001 2001 2001 2001 2002 2002 2002 2002 2002 2002 2003 2003
4.0 4.0
3.5 3.5
3.0 3.0
2.5 2.5
2.0 2.0
1.5 1.5
Aug 01 Oct 02 Dec 01 Feb 01 Apr 02 Jun 01 Aug 01 Oct 01 Dec 03 Feb 01 Apr 01 Jun 03 Aug 01 Oct 01 Dec 02 Feb 03 Mar 31
2000 2000 2000 2001 2001 2001 2001 2001 2001 2002 2002 2002 2002 2002 2002 2003 2003
LS
4.0 4.0
rolling.LS
Kalman
3.5 3.5
3.0 3.0
2.5 2.5
2.0 2.0
1.5 1.5
1.0 1.0
Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 01 May 01 Aug 01 Nov 01 Feb 03
2000 2000 2001 2001 2001 2001 2002 2002 2002 2002 2003
The Kalman lter can and has been used in many aspects o nancial time-series
modeling as one could expect.10
Examples o univariate time series: rate o infation, national income, level o
unemployment, etc.
Typical models include: local model, trend-cycle decompositions, seasonality, etc.
Examples o multivariate time series: infation and national income.
Multiple time series allows or more sophisticated models including common actors,
cointegration, etc.
Also data irregularities can be easily handled, e.g., missing observations, outliers, mixed
requencies.
Plenty o applications or nonlinear and non-Gaussian models as well, e.g., GARCH
modeling and stochastic volatility modeling.
10
A. Harvey and S. J. Koopman, “Unobserved components models in economics and fnance: The role o the
Kalman flter in time series econometrics,” IEEE Control Systems Magazine, vol. 29, no. 6, pp. 71–81, 2009.
D. Palomar (HKUST) Pairs Trading 49 / 63
Outline
1 Cointegration
2 Basic Idea o Pairs Trading
3 Design o Pairs Trading
Pairs selection
Cointegration test
Optimum threshold
4 LS Regression and Kalman or Pairs Trading
5 From Pairs Trading to Statistical Arbitrage (StatArb)∗
VECM
Optimization o mean-reverting portolio (MRP)
6 Summary
From pairs trading to statistical arbitrage
Denote the log-prices o multiple stocks as yt and the log-returns as rt = ∆yt = yt − yt−1 .
Most o the multivariate time-series models attempt to model the log-returns rt (because
the log-prices are nonstationary whereas the log-returns are weakly stationary, at least
over some time horizon).
However, it turns out that dierencing the log-prices may destroy part o the structure.
The VECM11 tries to x that issue by including an additional term in the model:
p−1
∑
rt = ϕ0 + Πyt−1 + Φ̃i rt−i + wt ,
i=1
11
R. F. Engle and C. W. J. Granger, “Co-integration and error correction: Representation, estimation, and
testing,” Econometrica: Journal of the Econometric Society, pp. 251–276, 1987.
D. Palomar (HKUST) Pairs Trading 53 / 63
VECM - Matrix Π
12
Z. Zhao and D. P. Palomar, “Mean-reverting portolio with budget constraint,” IEEE Trans. Signal
Process., vol. 66, no. 9, pp. 2342–2357, 2018.
13
Z. Zhao, R. Zhou, and D. P. Palomar, “Optimal mean-reverting portolio with leverage constraint or
statistical arbitrage in fnance,” IEEE Trans. Signal Process., vol. 67, no. 7, pp. 1681–1695, 2019.
D. Palomar (HKUST) Pairs Trading 56 / 63
Mean-reverting portolio (MRP)
For example, i we use the Portmanteau statistics as a proxy or the mean reversion, the
problem ormulation becomes:
p 2
wT M i w
minimize i=1 wT M 0 w
w
subject to wT M0 w = ν
w ∈ W.
Using other proxies, the ormulation can be expressed more generally as14
p 2
minimize wT Hw + λ i=1 wT Mi w
w
subject to wT M0 w = ν
w ∈ W.
14
Z. Zhao and D. P. Palomar, “Mean-reverting portolio with budget constraint,” IEEE Trans. Signal
Process., vol. 66, no. 9, pp. 2342–2357, 2018.
D. Palomar (HKUST) Pairs Trading 58 / 63
MRP in practice
Observe several stock log-prices and the spreads obtained rom β:
5.5
APA
AXP
CAT
5.0 COF
FCX
IBM
MMM
Log-prices
4.5
4.0
3.5
3.0
6.0
s1
5.5
5.4
5.2
s2
5.0
5.6
s3
5.4
5.2
2012 2013 2014
0.4
Spreads
0.2
0 MRP-cro (prop.)
-0.2 Spread s 1
-0.4
0.02
MRP-cro (prop.) - SR=3.2471
ROI
0
-0.02
0.01
Spread s 1 - SR=2.8579
ROI
0
-0.01
8
MRP-cro (prop.)
Spread s 1
6
Cum. P&L
0
2012 2013 2014
https://www.danielppalomar.com