Skip to content

@timelyportfolio /jgb total return.r
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#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
Something went wrong with that request. Please try again.