This document was written with the intention of fusing trading theory with application by using analytical tools manipulating a time-series database. This guide offers step-by-step instructions on how to turn popular trading ideas into computer-driven models. The author assumes that the reader has a general financial knowledge base as well as a fundamental understanding of the XMIM software.
“Pairs Trading” is a type of arbitrage that first became popular about 20 years ago. Its methodology focuses around finding pairs of instruments that are similar in all aspects, both qualitative and quantitative. Rather than spending days sifting through 10K’s trying to locate matches, most Pairs Traders have developed computer models which measure quantitative relationships to find pairs. Once a pair is determined, and the relationship quantified, the trader uses a hedge trade by going long one instrument and short the other as they deviate from their expected course. The trades will offset each other for the most part and the trader will decide which to weigh more heavily to realize the gains (some companies have made a lot of money off of the small differences by investing immense amounts of money.) The further they deviate, the higher the probability that they will snap back, the greater the propensity for gain, assuming that the pair was chosen accurately. Each system for picking pairs differentiate from one another on some level, wherein lies the creativity and mathematical knowledge of the analyst. Where to start?
Correlations are one of the basis measurements for locating pairs. Without diving too deep into the mathematics, a correlation measures the degree to which prices move together based on a specified time range. The calculation returns a value between -1 and 1 ( 1 being perfectly positively correlated, -1 being perfectly negatively correlated, and 0 having no price correlation.) For example, if XMIM returns the value .75, then the two instruments move in the same direction about 75% of the time. If XMIM returns -.70, then the two instrument prices move in opposite directions 70% of the time.
SHOW 1: 180 day correlation of YHOO and SUNW WHEN Date is within 5 years
The above query will return the moving 180 day[1] correlation of Yahoo! and Sun Microsystems. In other words, XMIM will calculate the 180 correlation every day for the last 5 years for the two data series.
Once the analyst has discerned a pair, he can then use the ORDER screen in XMIM to back-test a particular trading scenario. It might look something like this:
1: ORDER
1.1: Buy 1000 contracts of YHOO
WHEN
Date is after 1990
AND
180 day correlation of YHOO and SUNW crosses below 0.3
EXIT
Date is 1 week after entry_date In plain terms, the above query uses the historical (moving) 180 day correlation of YHOO and SUNW as the sole buy and sell signal. It is a simple example and makes the assumption that SUNW and YHOO have been identified as a pair. The query simultaneously goes long 1000 shares of YHOO and short 1000 shares of SUNW when the correlation moves below 30%[2]. The pairs-trade allows for one week of corrective price action and then exits both trades.
XMIM can also be used to locate a highly correlated pair. For example, you can pick a single equity and then ask XMIM to search all the S&P Groups for the top 5 highest correlated groups. The way you would do this is to first define a variable equal to the entire subset of the S&P Groups and then put that variable in the correlation function.
LET SPgroups = TopRelation:Indices:S&P500Groups TOP 5 180 day correlation of SPgroups and SUNW SHOW 1: 180 day correlation of SPgroups and SUNW WHEN Date is SUNW last_data_day
We defined our variable “Spgroups” in the LET statement as all the S&P500Groups in the database. Then we told it to only return the TOP 5 highest correlations. In the SHOW statement, we want to see the correlations, and only on the most recent measurement which is the WHEN statement. XMIM will then, in a matter of seconds, rip through the correlation calculations for Sun Microsystems and 118 S&P Groups! The first answer we get (the highest correlated match) as you might have guessed is TECH.COMP that is the High-Tech Composite in the technology sector at a current 92% correlation:
Let variable values:
SPgroups=TECH.COMP
TOP 1 Using 0.921
Date Day 1
01/10/2002 Thu 0.9212
Avg 0.9212
AvgPos 0.9212
AvgNeg NaN
PctPos 100.0000
PctNeg 0.0000
Maximum 0.9212
Minimum 0.9212
StdDev NaN
ZStat NaN
Variance NaN
1 OccurrenceAn important note to make here is that you are looking at the most current 180 day correlation which is only the most current measurement. It has no regard for the variance, standard deviation, or average of the historical moving correlation. If you wanted to see what the correlation looks like for the last 2 years instead of just the most current instance, you can run the query below:
SHOW
1: 180 day correlation of TECH.COMP and SUNW
WHEN
Date is within 2 years
Avg 0.7905
AvgPos 0.8219
AvgNeg -0.0433
PctPos 96.3671
PctNeg 3.6329
Maximum 0.9706
Minimum -0.0735
StdDev 0.2892
ZStat 2.7333
Variance 0.0836
523 OccurrencesYou will notice that the average (Avg) of the moving 180 day correlation is significantly lower due to the fact that the user is taking into account 523 data points as opposed to just 1. It turns out that the two series are still at an 80% correlation with a small standard deviation therefore leaving the match still valid.
[1] “180 days” is traditionally taught as a standard time frame for measuring a correlation. Again, it is important to note that measurement time frames will vary dependant upon the goals of the trader or his/her beliefs about market movements.
[2] The theory behind this signal is based on the belief that YHOO and SUNW have, or should have, a high historical correlation. If that correlation were to drop below a certain level, that could preclude the need for corrective price action in these two stocks.