aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/backtest.R50
1 files changed, 37 insertions, 13 deletions
diff --git a/R/backtest.R b/R/backtest.R
index 2723be4d..cef602cf 100644
--- a/R/backtest.R
+++ b/R/backtest.R
@@ -22,7 +22,8 @@ get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr")){
}
get.indexmaturity <- function(index, series){
- sqlstr <- paste("select maturity, coupon/10000 as running from index_maturity where index='%s'",
+ sqlstr <- paste("select maturity, coupon/cast(10000 as float)as running, tenor",
+ "from index_maturity where index='%s'",
"and series=%s order by maturity")
stmt <- sprintf(sqlstr, index, series)
df <- dbGetQuery(serenitasdb, stmt)
@@ -45,17 +46,37 @@ fastduration <- function(sc, cs, tradedate, maturities){
return( r )
}
+fasttheta <- function(sc, cs, recov, tradedate, maturities, quotes){
+ startdate <- tradedate+1
+ acc <- cdsAccrued(tradedate, 1)
+ r <- numeric(length(maturities))
+ newmaturities <- maturities+years(-1)
+ for(i in seq_along(newmaturities)){
+ if(startdate>newmaturities[i]){
+ r[i] <- NA
+ }else{
+ newcs <- cs[cs$unadj.dates<=newmaturities[i],]
+ upfront <- (couponleg(newcs, sc, startdate, accruedondefault=TRUE)-acc)*0.05-
+ defaultleg(newcs, sc, recov, startdate)
+ r[i] <- upfront+quotes[i]+0.05
+ }
+ }
+ return( r )
+}
+
index <- 'HY'
tenors <- c("3yr", "5yr", "7yr")
recov <- 0.3
colnames(df) <- c("date", tenors)
-sqlstr <- paste("UPDATE index_quotes set duration=%s where date='%s' and index='%s'",
+sqlstr <- paste("UPDATE index_quotes set duration=%s, theta=%s where date='%s' and index='%s'",
"and series=%s and tenor='%s'")
-for(series in c(11, 13, 15, 17, 19, 21, 23){
+for(series in c(10, 11, 13, 15, 17, 19, 21, 23)){
indexquotes <- get.indexquotes(index, series)
maturities <- get.indexmaturity(index, series)
+ maturities <- maturities[maturities$tenor %in% tenors,]
durations <- matrix(0, nrow(indexquotes), length(tenors))
+ thetas <- matrix(0, nrow(indexquotes), length(tenors))
for (i in 1:nrow(indexquotes)){
tradedate <- indexquotes[i, "date"]
exportYC(tradedate)
@@ -65,21 +86,24 @@ for(series in c(11, 13, 15, 17, 19, 21, 23){
quotes <- data.frame(upfront=(100-as.numeric(indexquotes[i,-1]))/100, maturities)
sc <- cdshazardrate(quotes, recov, tradedate, cs)
durations[i,] <- fastduration(sc, cs, tradedate, maturities$maturity)
+ thetas[i,] <- fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront)
}
-
- df <- data.frame(date=indexquotes$date, durations)
- colnames(df) <- c("date", tenors)
- for(i in 1:nrow(df)){
+ df.durations <- data.frame(date=indexquotes$date, durations)
+ df.thetas <- data.frame(date=indexquotes$date, thetas)
+ colnames(df.durations) <- c("date", tenors)
+ colnames(df.thetas) <- c("date", tenors)
+ for(i in 1:nrow(df.durations)){
for(tenor in tenors){
if(!is.na(df[i,tenor])){
- stmt <- sprintf(sqlstr, df[i,tenor], df[i,"date"], index, series, tenor)
+ stmt <- sprintf(sqlstr, df.durations[i,tenor], df.thetas[i, tenor],
+ df[i,"date"], index, series, tenor)
dbSendQuery(serenitasdb, stmt)
}
}
}
+ ## nice plot, now I'm just showing off
+ ggplot(df, aes(x=date))+geom_line(aes(y=`3yr`, colour="3yr"))+
+ geom_line(aes(y=`5yr`, colour="5yr"))+
+ geom_line(aes(y=`7yr`, colour="7yr"))+ylab("duration")+labs(colour="tenor")
+ ggsave(filename=paste0("HY", series, " durations.png"))
}
-
-## nice plot, now I'm just showing off
-ggplot(df, aes(x=date))+geom_line(aes(y=`3yr`, colour="3yr"))+
- geom_line(aes(y=`5yr`, colour="5yr"))+
- geom_line(aes(y=`7yr`, colour="7yr"))+ylab("duration")+labs(colour="tenor")