Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created October 16, 2012 02:28
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/3896940 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3896940 to your computer and use it in GitHub Desktop.
what if 10 year goes to 0
require(quantmod)
require(PerformanceAnalytics)
require(RQuantLib)
require(latticeExtra)
getSymbols("DGS10",src="FRED")
GS10 <- to.monthly(DGS10)[,4]
getSymbols("GS10",src="FRED")
#Fed monthly series of yields is the monthly average of daily yields
#set index to yyyy-mm-dd format rather than to.monthly mmm yyy for better merging later
index(GS10)<-as.Date(index(GS10))
#add next month as 0%
GS10 <- rbind(GS10,as.xts(0,order.by=as.Date("2012-10-01")))
GS10pricereturn<-GS10
GS10pricereturn[1,1]<-0
colnames(GS10pricereturn)<-"PriceReturn-monthly avg GS10"
#use quantlib to price the GS10 and BAA bonds from monthly yields
#GS10 and BAA series are 20-30 year bonds so will advance date by 25 years
for (i in 1:(NROW(GS10)-1)) {
GS10pricereturn[i+1,1]<-FixedRateBondPriceByYield(yield=GS10[i+1,1]/100,issueDate=Sys.Date(),
maturityDate= advance("UnitedStates/GovernmentBond", Sys.Date(), 10, 3),
rates=GS10[i,1]/100,period=2)[1]/100-1
}
#total return will be the price return + yield/12 for one month
GS10totalreturn<-GS10pricereturn+lag(GS10,k=1)/12/100
colnames(GS10totalreturn)<-"TotalReturn-monthly avg GS10"
GS10totalreturn[1,1] <- 0
GS10cumul <- cumprod(GS10totalreturn+1)
asTheEconomist(xyplot(GS10cumul,scales=list(y=list(rot=0)),
main="US 10 Year Cumulative Growth since 1954"))
asTheEconomist(xyplot(log(GS10cumul),
scales=list(y=list(rot=0)),
main="US 10 Year Cumulative Growth (log scale) since 1954"))
asTheEconomist(xyplot(ROC(GS10cumul,12, type = "discrete"),
scales=list(y=list(rot=0)),
main="US 10 Year 12 Month Total Return since 1954"))
roc <- ROC(GS10,12,type="discrete")
asTheEconomist(xyplot(roc["::2012-09-01"],
scales=list(y=list(rot=0)),
main="US 10 Year 12 Month % Change in Yield since 1954"))
roc["2012-10-01"] <- -1
asTheEconomist(xyplot(roc,
scales=list(y=list(rot=0)),
main="US 10 Year 12 Month % Change in Yield (with 0% for October 2012) since 1954"))
plot.zoo(GS10)
asTheEconomist(xyplot(diff(GS10,6),
scales=list(y=list(rot=0)),
main="US 10 Year 6 Month Change in Yield since 1954"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment