Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created February 4, 2013 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/4704779 to your computer and use it in GitHub Desktop.
Save timelyportfolio/4704779 to your computer and use it in GitHub Desktop.
#get Japan yield data from the Ministry of Finance Japan
#data goes back to 1974
require(RQuantLib)
require(PerformanceAnalytics)
#get data from the Japanese Ministry of Finance
url <- "http://www.mof.go.jp/english/jgbs/reference/interest_rate/"
filenames <- paste("jgbcme",c("","_2010","_2000-2009","_1990-1999","_1980-1989","_1974-1979"),".csv",sep="")
#load all data and combine into one jgb data.frame
jgb <- read.csv(paste(url,filenames[1],sep=""),stringsAsFactors=FALSE)
for (i in 2:length(filenames)) {
jgb <- rbind(jgb,read.csv(paste(url,"/historical/",filenames[i],sep=""),stringsAsFactors=FALSE))
}
#now clean up the jgb data.frame to make a jgb yield xts series
jgb.xts <- as.xts(data.matrix(jgb[,2:NCOL(jgb)]),order.by=as.Date(jgb[,1]))
#initialize the price return object
JGB9pricereturn<-jgb.xts[,"X9"]
JGB9pricereturn[1,1]<-0
colnames(JGB9pricereturn)<-"PriceReturn-JGB9"
#use quantlib to price the JGB 9 year
#9 year has a longer history than the 10 year so we'll use 9 year
for (i in 1:(NROW(jgb.xts[,"X9"])-1)) {
JGB9pricereturn[i+1,1]<-FixedRateBondPriceByYield(yield=jgb.xts[i+1,"X9"]/100,issueDate=Sys.Date(),
maturityDate= advance("Japan", Sys.Date(), 9, 3),
rates=jgb.xts[i,"X9"]/100,period=2)[1]/100-1
}
#total return will be the price return + yield/12 for one month
JGB9totalreturn<-JGB9pricereturn+lag(jgb.xts[,"X9"],k=1)/250/100
colnames(JGB9totalreturn)<-"TotalReturn-JGB9"
JGB9totalreturn[1,1] <- 0
JGB9cumul <- cumprod(JGB9totalreturn+1)
charts.PerformanceSummary(JGB9totalreturn,
main=NA,
xlab=NA)
title(main="Japanese Government Bond (JGB) 9 Year Total Return",adj=0.04,outer=TRUE,line=-1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment