ソロスチャート 1971年2月~2014年8月

Data Source:FRED,Bank of Japan

Sys.time()
## [1] "2014-09-25 17:55:19 JST"

Package

library(knitr)
library(quantmod) # http://cran.r-project.org/web/packages/quantmod/quantmod.pdf
library(tseries)
library(PerformanceAnalytics)

Import Data

FirstDate<-as.Date("1970/1/1")
LastDate<-as.Date("2014/9/1")
period<-2 #1:days 2:months
if(period==1){
period<-"days"
DateStyle<-"%Y/%b/%d"
}else
if(period==2){
period<-"months"
DateStyle<-"%Y/%b"  
}
#local data
username<-Sys.info()['user']
folder.name<-c("R_Data_Read","R_image","R_Data_Write")
path<-paste("C:/Users/",username,"/Desktop",sep="")
for(fff in 1:length(folder.name)){
folder.path<-file.path(path,folder.name[fff])
if(file.exists(folder.path)==F){dir.create(folder.path)}
assign(paste("path",fff,sep=""),folder.path)
}
setwd(path1) #read
file.list<-list()
for(iii in 1:length(dir(path1))){
file.list[[iii]]<-read.table(file=dir(path1)[iii],header=TRUE,sep=",",as.is=T) # fill=T,na.string=""
file.list[[iii]][,1]<-as.Date(file.list[[iii]][,1])
print(head(file.list[[iii]],1))
cat("\n")
print(tail(file.list[[iii]],1))
cat("\n")
str(file.list[[iii]])
}
##         date mb.japan
## 1 1970-01-01    45652
## 
##           date mb.japan
## 536 2014-08-01  2423138
## 
## 'data.frame':    536 obs. of  2 variables:
##  $ date    : Date, format: "1970-01-01" "1970-02-01" ...
##  $ mb.japan: int  45652 42889 44223 44354 43996 45532 47710 47586 45499 46467 ...
dir(path1)
## [1] "nme_R031.28078.20140925153410.02.csv"
summary(file.list)
##      Length Class      Mode
## [1,] 2      data.frame list
#file.select<-menu(dir(path1),title="File(0:Exit)")
file.select<-1
dataset.l<-file.list[[file.select]]
#web data
all.data<-data.frame(date=seq(FirstDate,LastDate,by=period))
symbol<-c("AMBNS","mb.usa","FRED","DEXJPUS","usd.jpy","FRED")
for(iii in seq(1,length(symbol),by=3)){
dataset.xts<-getSymbols(symbol[iii],src=symbol[iii+2],auto.assign=FALSE,from=FirstDate,to=LastDate)
dataset<-data.frame(date=index(dataset.xts),dataset.xts,row.names=NULL,stringsAsFactors=F)
dataset<-subset(dataset,dataset[,2]!="." & is.na(dataset[,2])==F) # &,|
names(dataset)[2]<-symbol[iii+1]
all.data<-merge(all.data,dataset,by="date",sort=T) #,all=T 
}
#merge
all.data<-merge(all.data,dataset.l,by="date",sort=T) #,all=T 
head(all.data,1)
##         date mb.usa usd.jpy mb.japan
## 1 1971-02-01  66.71   357.7    50060
tail(all.data,1)
##           date mb.usa usd.jpy mb.japan
## 337 2014-08-01   4095   102.5  2423138
period.date<-paste(format(all.data[,1][1],DateStyle),"-",format(all.data[,1][length(all.data[,1])],DateStyle))
all.data[,2]<-all.data[,2]*10
all.data$mb.ratio<-all.data[,4]/all.data[,2]
all.data<-as.xts(read.zoo(all.data))
all.data<-all.data[,c(4,2,1,3)]
all.data.diff<-diff(all.data[,c(1,2)])[-1]
tail(all.data,1)
##            mb.ratio usd.jpy mb.usa mb.japan
## 2014-08-01    59.17   102.5  40953  2423138
tail(all.data.diff,1)
##            mb.ratio usd.jpy
## 2014-08-01   -1.402    0.94

Check Unit Root

adf.test(all.data$mb.ratio)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  all.data$mb.ratio
## Dickey-Fuller = -2.611, Lag order = 6, p-value = 0.3188
## alternative hypothesis: stationary
adf.test(all.data$usd.jpy)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  all.data$usd.jpy
## Dickey-Fuller = -2.408, Lag order = 6, p-value = 0.4047
## alternative hypothesis: stationary
adf.test(all.data.diff$mb.ratio)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  all.data.diff$mb.ratio
## Dickey-Fuller = -7.077, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
adf.test(all.data.diff$usd.jpy)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  all.data.diff$usd.jpy
## Dickey-Fuller = -6.484, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary

Check Cointegrated

#H0:Not cointegrated
po.test(all.data[,c(1,2)])
## 
##  Phillips-Ouliaris Cointegration Test
## 
## data:  all.data[, c(1, 2)]
## Phillips-Ouliaris demeaned = -6.911, Truncation lag parameter = 3,
## p-value = 0.15
po.test(all.data.diff[,c(1,2)])
## 
##  Phillips-Ouliaris Cointegration Test
## 
## data:  all.data.diff[, c(1, 2)]
## Phillips-Ouliaris demeaned = -437, Truncation lag parameter = 3,
## p-value = 0.01

Plot

chart.TimeSeries(all.data[,1],main=paste("Level",period.date),yaxis.right=F,col=2,legend.loc="bottomleft")
par(new=T)
chart.TimeSeries(all.data[,2],main="",yaxis.right=T,legend.loc="topright")

plot of chunk unnamed-chunk-6

chart.TimeSeries(all.data.diff[,1],main=paste("1stDifference",period.date),yaxis.right=F,col=2,legend.loc="bottomleft")
par(new=T)
chart.TimeSeries(all.data.diff[,2],main="",yaxis.right=T,legend.loc="bottomleft")

plot of chunk unnamed-chunk-6

lag=c(12,24,36,60,120)
for(lll in 1:length(lag)){
chart.RollingCorrelation(all.data[,1],all.data[,2],legend.loc="bottomleft",width=lag[lll],main=paste("Rolling Correlation Level","width=",lag[lll]),type="h",col=grey(0.5))
chart.RollingCorrelation(all.data.diff[,1],all.data.diff[,2],legend.loc="bottomleft",width=lag[lll],main=paste("Rolling Correlation 1stDifference","width=",lag[lll]),type="h",col=grey(0.5))
}

plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6plot of chunk unnamed-chunk-6