Description Details Author(s) References Examples
This package gives classical trading strategy called "Pair trading". You can easily specify pairs for trading and do back-testing. Analysis are based on the idea of Cointegration that is a statistical feature of time series proposed by Engle and Granger.
Package: | PairTrading |
Type: | Package |
Version: | 1.1 |
Date: | 2012-03-24 |
License: | BSD |
LazyLoad: | yes |
Shinichi Takayanagi, Kohta Ishikawa Maintainer: Shinichi Takayanagi<shinichi.takayanagi@gmail.com>, Kohta Ishikawa<quantumcorgi@gmail.com>
Engle, Robert F. and C.W.J. Granger, 1987, Co-integration and error correction: Representation, estimation, and testing, Econometrica 55, 251-276. Granger, C.W.J., 1986, Developments in the study of cointegrated economic variables, Oxford Bulletin of Economics and Statistics 48, 213-228.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #load library
library(PairTrading)
#load sample stock price data
data(stock.price)
#select 2 stocks
price.pair <- stock.price[,1:2]["2008-12-31::"]
#Estimate parameters & plot spread
reg <- EstimateParameters(price.pair, method = lm)
str(reg)
plot(reg$spread)
#check stationarity
IsStationary(reg$spread, 0.1)
#estimate parameters for back test
params <- EstimateParametersHistorically(price.pair, period = 180)
#create & plot trading signals
signal <- Simple(params$spread, 0.05)
barplot(signal,col="blue",space = 0, border = "blue",xaxt="n",yaxt="n",xlab="",ylab="")
par(new=TRUE)
plot(params$spread)
#performance
return.pairtrading <- Return(price.pair, lag(signal), lag(params$hedge.ratio))
if(!all(is.na(return.pairtrading))){
plot(100 * cumprod(1 + return.pairtrading))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.