**My Life as a Mock Quant in English**, and kindly contributed to R-bloggers)

**“PairTrading” package**, and uploaded it on CRAN.

This article shows you how you can use it.

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.

> install.packages("PairTrading")

> library(PairTrading)

**1: Load sample data**

We prepared sample stock price data in our package. You can load it by using “data” command.

> #load sample stock price data

> data(stock.price)

**2: Estimate parameters**

Next, We extract two stock prices(from 31 Mar, 2008) and estimate parameters.

> #select 2 stocks

> price.pair <- stock.price[,1:2]["2008-03-31::"]

> #Estimate parameters & plot spread

> reg <- EstimateParameters(price.pair, method = lm)

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.26 0.271 0.294 0.275 0.255 ...

- attr(*, "dimnames")=List of 2

..$ : NULL

..$ : chr "7203"

Indexed by objects 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.

> plot(reg$spread)

And, you can check the stationarity of that by using “IsStationary” function.

This function return the result of two types unit root test.

> #check stationarity

> IsStationary(reg$spread, 0.1)

PP.test adf.test

TRUE TRUE

( 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.065 0.0577 0.0396 0.0136 0.021 ...

- attr(*, "dimnames")=List of 2

..$ : NULL

..$ : chr [1:3] "spread" "hedge.ratio" "premium"

Indexed by objects of class: [Date] TZ:

xts Attributes:

NULL

**4: Create trading signal**

In this case, The trading signal is drawn as below.

In this case, our strategy seems to work correctly

**6: Conclusion and remarks**

It may be useful for you to understand the basic concept of pair trading if you are interested in it.

**Introduction to pairtrading**

Enjoy!

**leave a comment**for the author, please follow the link and comment on his blog:

**My Life as a Mock Quant in English**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...