The pair trading is a market neutral trading strategy and gives traders a chance to profit regardless of market conditions. The idea of this strategy is quite simple. 1 : Select two stocks(or any assets) moving similarly 2 : Short out-performing stock, buy under-performing one 3 : If “spread”(price difference between two stocks) converge, close your position.
So, Let’s start to explain how to use this package. (This example is in PDF manual of this package)
0: Install & load package You can install and load “PairTrading” package via CRAN in the same way as other packages.
At the moment, we have only normal linear regression method to estimate parameters, but we will develop more sophisticated method in the future.The estimation result contains the following contents.
>str(reg) List of 3 $ spread :An ‘xts’ object from 2008-03-31 to 2011-08-05 containing: Data: num [1:821,1]0.260.2710.2940.2750.255 ... -attr(*,"dimnames")=List of 2 ..$:NULL ..$: chr "7203" Indexed byobjects of class:[Date]TZ: xts Attributes: NULL $ hedge.ratio: num 0.285 $ premium : num 6.34
The most important thing in this estimation is “spread”, then we try to plot it.
( augmented Dickey–Fuller test (ADF) and Phillips-Perron test)
3: Estimate parameters for back-test To run back-test, you have to estimate parameters historically by using “EstimateParametersHistorically“function. This function do something like “rolling regression” to estimate parameters. This point is different from “EstimateParameter” function.
>#estimate parameters for back test > params <- EstimateParametersHistorically(price.pair, period = 180) >#create & plot trading signals >str(params) An ‘xts’ object from 2008-12-18 to 2011-08-05 containing: Data: num [1:642,1:3]0.0650.05770.03960.01360.021 ... -attr(*,"dimnames")=List of 2 ..$:NULL ..$: chr [1:3]"spread""hedge.ratio""premium" Indexed byobjects of class:[Date]TZ: xts Attributes: NULL
4: Create trading signal
Next, you create trading singal using estimated spread. “Simple” function give a very simple trading strategy(If The spread is more(less) than specified value, you will buy(sell))