Yahoo Finance의 데이터를 사용하여 두 주식에 대해 공동 통합 테스트를 수행하려고합니다. 내가 읽은 바에 따르면 야후 데이터를 검색하는 데 덜 복잡한 방법이 있습니다. 두 증권을 검색하고 이들을 stk1
및 stk2
으로 정의하고 검색된 데이터의 시간 프레임을 조정할 수 있어야합니다. 여기까지 내가 지금까지 가지고있는 것이있다.Yahoo Finance 데이터를 사용하여 두 주식의 공적분 통합 테스트
library(zoo)
library(tseries)
# Read the CSV files into 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)
# The first column contains dates. as.Date converts strings into Date objects
stk1_dates <- as.Date(stk1[,1])
stk2_dates <- as.Date(stk2[,1])
# The seventh column contains the adjusted close. We use the zoo function to
# create zoo objects from that data. The function takes two arguments: a
# vector of data and a vector of dates.
stk1 <- zoo(stk1[,7], stk1_dates)
stk2 <- zoo(stk2[,7], stk2_dates)
# The merge function combines two (or more) zoo objects,
# computing either their intersection (all=FALSE) or union (all=TRUE).
t.zoo <- merge(stk1, stk2, all=FALSE)
# At this point, t.zoo is a zoo object with two columns: stk1 and stk2.
# Most statistical functions expect a data frame for input, so we convert.
t <- as.data.frame(t.zoo)
# Tell the user what dates are spanned by the 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 is likely mean-reverting\n")
} else {
cat("The spread is not mean-reverting.\n")
}
더 쉽고 강력하게 만들 수있는 도구는 무엇입니까?
가있는 경우 귀하의 질문은 무엇입니까? –
멋지게 주석 처리 된 코드 예! – Stedy
OP 코드의 원본 출처 : http://quanttrader.info/public/testForCoint.html – GSee