public
Last active

spain stocks and bonds

  • Download Gist
spainallocation.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#analyze asset allocation experience in Spain
 
require(lattice)
require(latticeExtra)
require(reshape2)
require(directlabels)
require(quantmod)
require(PerformanceAnalytics)
require(RQuantLib)
 
data <- read.csv("spain stocks and bond from bloomberg.csv",stringsAsFactors=FALSE)
 
spainstock <- na.omit(as.xts(as.numeric(data[2:NROW(data),2]),order.by=as.Date(data[2:NROW(data),1],"%m/%d/%Y")))
colnames(spainstock) <- "SpainStocks.IBEX"
spainbond <- na.omit(as.xts(as.numeric(data[2:NROW(data),5]),order.by=as.Date(data[2:NROW(data),4],"%m/%d/%Y")))
colnames(spainbond) <- "SpainBonds.10y"
 
spainbondpricereturn<-spainbond
 
spainbondpricereturn[1,1]<-0
colnames(spainbondpricereturn)<-"SpainBond.10y.Price"
#use quantlib to price the Spanish bonds from yields
#these are 10 year bonds so will advance date by 10 years
#we can just use US/GovtBond calendar
for (i in 1:(NROW(spainbond)-1)) {
spainbondpricereturn[i+1,1]<-FixedRateBondPriceByYield(yield=spainbond[i+1,1]/100,issueDate=Sys.Date(),
maturityDate= advance("UnitedStates/GovernmentBond", Sys.Date(), 10, 3),
rates=spainbond[i,1]/100,period=2)[1]/100-1
}
#merge returns
spain.return <- na.omit(merge(spainbondpricereturn,ROC(spainstock,type="discrete",n=1)))
#get drawdowns
spain.drawdown <- Drawdowns(spain.return)
#get in melted data.frame for lattice
spain.drawdown.df <- as.data.frame(cbind(index(spain.drawdown),coredata(spain.drawdown)))
spain.drawdown.melt <- melt(spain.drawdown.df,id.vars=1)
colnames(spain.drawdown.melt) <- c("date","series","drawdown")
spain.drawdown.melt[,"date"] <- as.Date(spain.drawdown.melt[,"date"])
#plot drawdowns
direct.label(asTheEconomist(
xyplot(drawdown~date,groups=series,data=spain.drawdown.melt,
main="Spain - Drawdown of Stocks and Bonds (source: Bloomberg)")),
list("last.points",hjust=1,vjust=0,cex=1.2))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.