r - Testing cointegration of two stocks using Yahoo Finance data -


i trying perform co-integration test on 2 stocks, using data yahoo finance. have been reading there less complicated ways retrieve yahoo data. need retrieve 2 securities , define them stk1 , stk2 able adjust time frame of data retrieved. here have far.

library(zoo)             library(tseries)          # read csv files data frames stk1 <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=cat&a=8&b=1&c=2009&d=12&e=31&f=2010&g=d&ignore=.csv", stringsasfactors=f) stk2 <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=dd&a=8&b=1&c=2009&d=12&e=31&f=2010&g=d&ignore=.csv", stringsasfactors=f)  # first column contains dates. as.date converts strings date objects stk1_dates <- as.date(stk1[,1]) stk2_dates <- as.date(stk2[,1])  # seventh column contains adjusted close. use zoo function # create zoo objects data. function takes 2 arguments: # vector of data , vector of dates. stk1 <- zoo(stk1[,7], stk1_dates) stk2 <- zoo(stk2[,7], stk2_dates)  # merge function combines 2 (or more) zoo objects, # computing either intersection (all=false) or union (all=true). t.zoo <- merge(stk1, stk2, all=false)  # @ point, t.zoo zoo object 2 columns: stk1 , stk2. # statistical functions expect data frame input, convert. t <- as.data.frame(t.zoo)  # tell user dates spanned data. cat("date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")  m <- lm(stk1 ~ stk2 + 0, data=t) beta <- coef(m)[1] cat("assumed hedge ratio is", beta, "\n")  sprd <- t$stk1 - beta*t$stk2 ht <- adf.test(sprd, alternative="stationary", k=0) cat("adf p-value is", ht$p.value, "\n")  if (ht$p.value < 0.05) {     cat("the spread mean-reverting\n") } else {     cat("the spread not mean-reverting.\n") } 

what tools exist make easier and/or more robust?

quantmod provides nice interface yahoo (and other providers) data:

library(quantmod) library(tseries)  stk1 <- getsymbols("dd",  from="2009-01-01", auto.assign=false) stk2 <- getsymbols("cat", from="2009-01-01", auto.assign=false)  # update: here's how approach rest of op's example # ad() helper function in quantmod pair <- merge(ad(stk1), ad(stk2), all=false)  cat("date range is", format(start(pair)), "to", format(end(pair)), "\n")  # build formula instrument names eqn <- as.formula(paste(colnames(pair), collapse=" ~ 0 + ")) # note can use zoo/xts objects lm(); # don't *need* data.frame, can't mix types # zoo/xts because use matrix internally m <- lm(eqn, data=pair) beta <- coef(m)[1]  cat("assumed hedge ratio is", beta, "\n")  # index number, since won't know colnames sprd <- pair[,1] - beta*pair[,2] ht <- adf.test(sprd, alternative="stationary", k=0)  cat("adf p-value is", ht$p.value, "\n")  if (ht$p.value < 0.05) {     cat("the spread mean-reverting\n") } else {     cat("the spread not mean-reverting.\n") } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -