Update! I finally passed all the CFA exams and wrote an eBook about the program. If you're interested, click here.

A Pairs Trading Example

by Lumilog on July 26, 2008

Note: Please read the disclaimer. The author is not providing professional investing advice.

While waiting for results from the CFA exam to come in, I thought I’d cover an example of a trading technique I’ve recently begun test driving, pairs trading.

Pairs trading was something I thought I’d invented! Alas like many of my so-called brilliant flashes of insight, a little googling revealed that not only had others come up with it decades earlier, but their techniques were more elegant and robust than my first stab.

2. If the price movements are indeed highly correlated, then on most days the price per share of Stock A divided by the price per share of Stock B should come out to be about the same number, within a small range.

3. But occasionally, you might notice a significant divergence from this “average” ratio. If you do, enter a pairs trade. This will involve simultaneously shorting one of the stocks while taking a long position in the other.

4. At some time in the future when the price ratio (hopefully) reverts to the mean, exit the pair trade, wire that (almost) riskless profit to your checking account, and take your wife out to a nice dinner.

Now anyone familiar with the basic workings of the stock market knows that you can make money in either an up or down market, by using both long and short positions. But the catch is, you have to know which way the market is heading in order to know which to use. Easier said that done, even for the pros.

But with pairs trading, you are performing trades that are (theoretically, at least) market neutral. As long as the pair ratio reverts to the mean, you make money – regardless of whether you re-arrive at the mean by the short decreasing in price, the long increasing in price, or both. You don’t have to know or even care which way the market is heading.

A second benefit is that pair trade positions can be viewed as a nice additional asset class for one’s portfolio, since they should be relatively uncorrelated with your traditional asset classes consisting of long positions in various domestic and international indexes.

So let’s illustrate pair trading by an example.

It’s January 1, 2007 and I’m thinking of trying out some pair trading in the new year. Now I could code up a script to perform correlations between the historical price movements of hundreds of stocks in order to find those that move together, but I have a simpler idea.

I have an inkling that Lowe’s (LOW) and Home Depot (HD) might be highly correlated. To me at least, they are roughly equivalent businesses, and which one I go to depends solely on which one is closer when I need something. Let’s examine their closing price-per-share data from the previous year…

They definitely do seem to move together. And I could compute a correlation on the price movements to confirm what I see visually. But perhaps a more important question is not is the relationship between LOW and HD highly correlated, but rather is it mean-reverting. Because my making a profit depends upon this.

So I now plot the price of LOW divided by the price of HD, what we’ll call the LOW/HD pair ratio…

…and this indeed seems to be what I was looking for – a price ratio (blue curve) that appears to oscillate around a mean (purple line). And I’ve also added the 1-, 2-, and 3-standard deviation lines just to get a feel for how far away from the mean the oscillations might go.

So, we appear to know the habits and patterns of this animal. Time to set a trap and lie in wait…

But this creature is dangerous and could eat us alive, so we have to be careful. We see that in 2006 the pair ratio often makes excursions 1 or 2 standard deviations above or below the mean. We could spring the trap then but we might end up making lots of trades for tiny profits, and the commissions could eat us alive too.

Examining the figure above, we do see one time when the ratio appeared to go almost 3 standard deviations from the mean. Therefore that will be our criteria for 2007. If we see an excursion +/- 3 standard deviations from the mean, we’ll enter a pair trade. And whenever the ratio returns to the mean, we’ll exit both positions.

And here’s what 2007 ended up looking like.

On July 10, the LOW/HD ratio fell 3 standard deviations below the mean, so we buy LOW long at $29.46 and sell HD short at $38.98.

And a little over a month later, on August 16, 2007, the ratio reverted to the mean so we exited both positions. This turned out to mean selling LOW at $26.42 and covering our HD short at $31.79.

So our long LOW position was a 10.3% loss but the short HD position was a 22.6% gain. Therefore our equal dollar positions averaged out to a 6.1% gain.

We’re now back in cash and ready to spring the trap again should a new opportunity arise.

And indeed it shortly does. On September 24, 2007 the ratio has now gone 3 standard deviations above the mean. So this time we end up doing the reverse as the previous time. We take a long position in HD at $33.02 and sell short LOW at $30.10.

And a little less than 2 months later, the ratio has once again reverted to the mean. On November 20, 2007 we sell our long HD position at $27.78 and cover our short LOW position at $22.25.

So we take a 15.9% loss on HD but get a 35.3% gain on LOW. This averages to a 9.7% gain.

And this is the last time we get to pounce in 2007. Thus our two pair trades compounded to a +16.5% annual return! Not bad, considering buy-and-hold would have given you a 27% loss in LOW and 31% loss in HD. (I told you their prices move together…)

And further, we were only committed to a pair trade about 3 months of the whole year. While we were waiting we could have kept our money in a low-volatility bond index fund (e.g. VFISX or VBMFX) to generate fixed income in the mean time. I haven’t run the exact dates but these returned 8% and 7% respectively if you’d held them for all of 2007. So maybe it isn’t unrealistic to assume that this would have pushed our +16.5% past the 20%+ return mark.

In balance, though this example used real historical data, it’s probably making pair trading look easier than it is. Some companies diverge and don’t mean revert. And should your mean and standard deviations be based on last year or multiple years? A fixed start and stop date or a moving average? Should you jump in at 2, 2.5, or 3 standard deviation excursions – and when do you exit (for both profit and for stop loss).

This is good stuff. Can I ask what resource you use to create your charts. I seem to have gone as far as I can go with Excell, unless there is a way to add the lines noting the standard deviations on the slide with the prices ratios. Any thoughts? Thanks.

ah, that is one of the big questions. do you use a simple moving average or exponential? how many days should it be estimated over? should you enter a pairs trade when you see 2, 3, 4, standard deviations from the mean? should you exit when it returns to the mean or just drops down 1 or 2 standard deviations?

you understood correctly that the mean I used for all of 2007 was just one number, computed over 2006. of course that isn’t necessarily the best way – there are just so many different parameters you can turn the dials on and what is optimal varies from pair to pair. ideally i like to look for a pair that is so similar that the mean ratio doesn’t vary much over short periods of time.

I use Matlab for my own analysis. This works great if you’re an engineer or engineering student who already has a license. Otherwise you might want to investigate using Excel or OpenOffice Calc. Good luck!

Therefore I said I’d “invested” $1000 but made $613.09 + $448.40 = $1061.49 or 6.1%.

But there’s another way to think about this (perhaps the correct way) and it goes like this. On the day I enter the pairs trade I begin by shorting $500 worth of HD. This puts $500 into my account. I can then use that $500 to go long with LOW.

In the end I wind up having $61.49 left over after I sell the long and cover the short, but I never invested a penny of my own money. In that case perhaps a “return on investment” is meaningless.

Hi Lumi,
I dont have much knowledge about pair trading and statistical arbitrage,but i would love to know about it.Currently i am working as a research associate for equity class.Please let me known about the stuffs and books or the cd”s from which i can start learning.

Not wanting to spam you but this might be helpful for anyone who has really struggled to find affordable retail software that truly does the job of testing and assessing the tradeability of cointegrated pairs.

My partner and I used to model our cointegration pair trades in excel but hit limits. So we decided to build a more flexible and faster tool for release in Q4 this year (and will be commercialising it).

Obviously we are bullish about the product. But anyone joining the beta (starting next month on the current schedule) will be able to judge how comprehensive it is for themselves.