主要金融指標

Sys.time()

[1] “2014-09-29 21:31:16 JST”

Package

library(knitr)
library(quantmod)
library(xts)
library(gridExtra)
library(ggplot2)

Read Data

cnt<-1
sub.data<-list()
o.sub.data<-list()
ts.data<-list()
list.s<-list()
list.s[[1]]<-c("FRED","DCOILWTICO","DGS10")
list.s[[2]]<-c("yahoo","^GSPC","^FTSE","^N225")
list.s[[3]]<-c("oanda","JPY","EUR","AUD","NZD","ZAR","CAD","GBP","CHF","XAU","XAG","XPT","BTC")
for(lll in 1:length(list.s)){
for(iii in 2:length(list.s[[lll]])){
if(list.s[[lll]][1]=="oanda"){
item<-paste("USD/",list.s[[lll]][iii],sep="") 
}
else{
item<- list.s[[lll]][iii]
} 
ts.data[[cnt]]<-getSymbols(item,src=list.s[[lll]][1],auto.assign=FALSE)
if(lll==1 & iii==2){sub.data[[1]]<-ts.data[[cnt]]}else{sub.data[[1]]<-merge(sub.data[[1]],ts.data[[cnt]])}
cnt<-cnt+1
}
}
#Japanese Government Bonds
url<-c(
"http://www.mof.go.jp/english/jgbs/reference/interest_rate/historical/jgbcme_all.csv",
"http://www.mof.go.jp/english/jgbs/reference/interest_rate/jgbcme.csv"
)
for(ddd in 1:length(url)){
ts.data[[cnt]]<-read.table(url[ddd],header=T,sep=",",stringsAsFactor=F)
ts.data[[cnt]][,1]<-as.Date(ts.data[[cnt]][,1])
colnames(ts.data[[cnt]])[1]<-"date"
ts.data[[cnt]]<-as.xts(ts.data[[cnt]][,-1],order.by=as.Date(ts.data[[cnt]]$date),stringsAsFactor=F)
ts.data[[cnt]]<-apply(ts.data[[cnt]],c(1,2),function(x) as.numeric(x))
ts.data[[cnt]]<-as.xts(ts.data[[cnt]])
if(ddd==1){sub.data[[2]]<-ts.data[[cnt]]}else{sub.data[[2]]<-rbind.xts(sub.data[[2]],ts.data[[cnt]])}
cnt<-cnt+1
}

Plot

par(mfrow=c(5,4),mar=c(5,5,5,5))
for(ppp in 1:(cnt-1)){
buf<-ts.data[[ppp]]
buf<-data.frame(date=index(buf),buf,row.names=NULL)
plot(x=buf[,1],y=buf[,2],type="l",main=colnames(buf)[2],xlab="",ylab=colnames(buf)[2],xaxt="n")
axis.Date(side=1,at=seq(buf[,1][1],buf[,1][length(buf[,1])],"days"),format="%Y/%m")
}

plot of chunk unnamed-chunk-4