aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/thetas-durations.R (renamed from R/backtest.R)66
1 files changed, 45 insertions, 21 deletions
diff --git a/R/backtest.R b/R/thetas-durations.R
index 8ef74b7c..02001187 100644
--- a/R/backtest.R
+++ b/R/thetas-durations.R
@@ -19,12 +19,15 @@ source(file.path(root.dir, "code", "R", "serenitasdb.R"))
source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
-get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr")){
+get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr"), onlymissing=TRUE){
arraystring1 <- paste0("Array[''", paste(tenors, collapse = "'', ''"), "'']::tenor[]")
arraystring2 <- paste0('"', paste(tenors, collapse='" float, "'), '" float')
sqlstr <- paste("select * from crosstab('select date, tenor, closeprice from index_quotes",
- "where index=''%s''",
- "and series=%s order by date, tenor', 'select unnest(%s)')",
+ "where index=''%s''and series=%s")
+ if(onlymissing){
+ sqlstr <- paste(sqlstr, "and duration is Null")
+ }
+ sqlstr <- paste(sqlstr, "order by date, tenor', 'select unnest(%s)')",
"AS ct(date date, %s)")
stmt <- sprintf(sqlstr, index, series, arraystring1, arraystring2)
df <- dbGetQuery(serenitasdb, stmt)
@@ -93,10 +96,12 @@ convertNA <- function(x){
index <- 'HY'
tenors <- c("3yr", "5yr", "7yr")
recov <- 0.3
-sqlstr <- paste("UPDATE index_quotes set duration=%s, theta=%s where date='%s' and index='%s'",
- "and series=%s and tenor='%s'")
+sqlstr.duration <- paste("UPDATE index_quotes set duration=%s where date='%s' and index='%s'",
+ "and series=%s and tenor='%s'")
+sqlstr.theta <- paste("UPDATE index_quotes set theta=%s where date='%s' and index='%s'",
+ "and series=%s and tenor='%s'")
-for(series in c(9, 10, 11, 13, 15, 17, 19, 21, 23)){
+for(series in c(16, 17, 18, 19, 20, 21, 22, 23)){
indexquotes <- get.indexquotes(index, series)
maturities <- get.indexmaturity(index, series)
maturities <- maturities[maturities$tenor %in% tenors,]
@@ -114,25 +119,44 @@ for(series in c(9, 10, 11, 13, 15, 17, 19, 21, 23)){
c(fastduration(sc, cs, tradedate, maturities$maturity),
fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront))
}
- df.durations <- data.frame(date=indexquotes$date, durandthetas[,1:3])
- df.thetas <- data.frame(date=indexquotes$date, durandthetas[,4:6])
+ r <- c()
+ for(i in 1:nrow(indexquotes)){
+ tradedate <- indexquotes[i, "date"]
+ exportYC(tradedate)
+ cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), maturity,"Q", "FIXED", 1,
+ 0, tradedate, IMMDate(tradedate, "prev"))
+ quotes <- data.frame(upfront=(100-as.numeric(indexquotes[i,-1]))/100, maturities)
+ sc <- cdshazardrate(quotes, recov, tradedate, cs)
+ r <- rbind(r, c(fastduration(sc, cs, tradedate, maturities$maturity),
+ fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront)))
+ }
+ df.durations <- data.frame(date=indexquotes$date, r[,1:3])
+ df.thetas <- data.frame(date=indexquotes$date, r[,4:6])
colnames(df.durations) <- c("date", tenors)
colnames(df.thetas) <- c("date", tenors)
for(i in 1:nrow(df.durations)){
for(tenor in tenors){
- stmt <- sprintf(sqlstr, convertNA(df.durations[i,tenor]), convertNA(df.thetas[i, tenor]),
- df.durations[i,"date"], index, series, tenor)
- dbSendQuery(serenitasdb, stmt)
+ if(!is.na(df.durations[i,tenor])){
+ stmt <- sprintf(sqlstr.duration, df.durations[i,tenor],
+ df.durations[i,"date"], index, series, tenor)
+ dbSendQuery(serenitasdb, stmt)
+ }
+ if(!is.na(df.thetas[i,tenor])){
+ stmt <- sprintf(sqlstr.theta, df.thetas[i,tenor],
+ df.thetas[i,"date"], index, series, tenor)
+ dbSendQuery(serenitasdb, stmt)
+ }
}
}
- ## nice plot, now I'm just showing off
- ggplot(df.durations, 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"))
- ## plot thetas
- ggplot(df.thetas, 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("theta")+labs(colour="tenor")
- ggsave(filename=paste0("HY", series, " thetas.png"))
}
+
+## ## nice plot, now I'm just showing off
+## ggplot(df.durations, 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"))
+## ## plot thetas
+## ggplot(df.thetas, 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("theta")+labs(colour="tenor")
+## ggsave(filename=paste0("HY", series, " thetas.png"))