ローリング回帰:PerformanceAnalytics

Data Source http://www.oanda.com/

Sys.time()
## [1] "2014-09-22 16:30:15 JST"

Package

#install.packages("knitr", dependencies=T)
#install.packages("quantmod", dependencies=T)
#install.packages("PerformanceAnalytics", dependencies=T)
library(knitr)
library(quantmod) # http://cran.r-project.org/web/packages/quantmod/quantmod.pdf
library(PerformanceAnalytics)

Import Data & Plot

username<-Sys.info()['user']
path01<-paste("C:/Users/",username,"/Desktop",sep="")
path02<-"R_output"
path<-file.path(path01,path02)
if(file.exists(path)==F){dir.create(path)}
FirstDate<-as.Date("2014/1/1")
LastDate<-as.Date("2014/12/31")
period<-1 #1:days 2:months
if(period==1){
period<-"days"
DateStyle<-"%Y/%b/%d"
}else
if(period==2){
period<-"months"
DateStyle<-"%Y/%b"  
}
location<-"oanda"
currency<-c("AUD","JPY","EUR","GBP","NZD","ZAR","CHF","CAD","XAU")
fx<-list()
allFx<-xts()
for(iii in 1:length(currency)){
pair=paste(currency[iii],"/USD",sep="") 
fx[[iii]]<-getSymbols(pair,src=location,auto.assign=FALSE,from=FirstDate) # ,to=LastDate
allFx<-merge(allFx,fx[[iii]])
}
allFx<-cbind(allFx,INDEX=c(1:length(allFx[,1])))
head(allFx,1)
##                     AUD.USD  JPY.USD EUR.USD GBP.USD NZD.USD ZAR.USD
## 2014-01-01 09:00:00  0.8927 0.009521   1.379   1.652  0.8223 0.09553
##                     CHF.USD CAD.USD XAU.USD INDEX
## 2014-01-01 09:00:00   1.125  0.9399    1201     1
tail(allFx,1)
##                     AUD.USD  JPY.USD EUR.USD GBP.USD NZD.USD ZAR.USD
## 2014-09-22 09:00:00  0.8929 0.009174   1.283   1.629  0.8137 0.09045
##                     CHF.USD CAD.USD XAU.USD INDEX
## 2014-09-22 09:00:00   1.063  0.9125    1216   265

plot

lag<-30
for(iii in 1:length(currency)){
chart.RollingCorrelation(allFx[,ncol(allFx)],allFx[,iii],legend.loc="bottomleft",width=lag,main=paste("Rolling Correlation:Trend","width=",lag),type="h",col=grey(0.5))
par(new=T)
chart.TimeSeries(allFx[,iii],main="",yaxis.right=T)
}

plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4