Pages

Saturday, January 14, 2017

Pair Trading using Quantmod - HUL & ITC


In my earlier post, I have explained that very first step for stat Arb Strategies is to find out the relation between two stocks. To analyze the whole data and run the relations could be very time consuming if done using downloading data and running calculations separately.

In this blog we will discuss Quantmod which is a package in R language and can help us to get it done easily. It helps to download the data from Yahoo finance and run quick calculations since the data gets downloaded in the form of vectors.

This blog will be using example of Indian Market's stock prices to understand basic use of quantmod.

TO INSTALL PACKAGE

>install.packages("quantmod")
>library(quantmod)
>require(quantmod)

TO RETRIEVE DATA

> symbolsHUL<-c("HINDUNILVR.NS")
> getSymbols(symbolsHUL)
[1] "HINDUNILVR.NS"
> startT <- "2016-01-01"
> endT <- "2017-01-01"
> rangeT <- paste(startT,"::",endT,sep="")
> HUL <- HINDUNILVR.NS[,6][rangeT]
> symbolsITC<-c("ITC.NS")
> getSymbols(symbolsITC)
[1] "ITC.NS"
> HUL <- HINDUNILVR.NS[,6][rangeT]
> ITC <- ITC.NS[,6][rangeT]

ANALYZE DATA & RUNNING REGRESSION

> pdtHUL <- diff(HUL)[-1]
> pdtITC <- diff(ITC)[-1]
> model <- lm(pdtHUL ~ pdtITC -1)
> hr <- as.numeric(model$coefficients)
 [1] 0.1332138

PLOTTING DATA

> plot(ITC)



No comments:

Post a Comment