diff options
| -rw-r--r-- | R/serenitasdb.R | 4 | ||||
| -rw-r--r-- | R/thetas-durations.R | 47 |
2 files changed, 28 insertions, 23 deletions
diff --git a/R/serenitasdb.R b/R/serenitasdb.R index 8282e14e..16b3d764 100644 --- a/R/serenitasdb.R +++ b/R/serenitasdb.R @@ -145,8 +145,8 @@ getlastdate <- function(indexname, tenor){ return( r$date ) } -get.indexquotes <- function(indexname, tenor){ - stmt <- "select * from index_quotes where index=$1 and series=$2 and tenor=$3 order by date" +get.indexquotes <- function(indexname, tenor) { + stmt <- "SELECT * from index_quotes WHERE index=$1 and series=$2 and tenor=$3 ORDER BY date" return ( with(indexsplit(indexname), dbGetQuery(serenitasdb, stmt, params=list(indextype, series, tenor))) ) } diff --git a/R/thetas-durations.R b/R/thetas-durations.R index b70b1726..f542358f 100644 --- a/R/thetas-durations.R +++ b/R/thetas-durations.R @@ -13,15 +13,21 @@ source(file.path(code.dir, "R", "serenitasdb.R")) source(file.path(code.dir, "R", "cds_functions_generic.R")) source(file.path(code.dir, "R", "yieldcurve.R")) -get.indexquotes.table <- function(indextype, series, tenors=c("3yr", "5yr", "7yr"), onlymissing=TRUE){ - stmt <- str_c("select date, tenor, closeprice from index_quotes ", - "where index=$1 and series=$2", - if(onlymissing) " and duration is Null" else NULL, - " order by date") - df <- dbGetQuery(serenitasdb, stmt, params = list(index, series)) - return( df %>% filter(tenor %in% tenors) - %>% mutate(tenor = factor(tenor, levels=tenors)) - %>% spread(tenor, closeprice) ) +get.indexquotes.table <- function(indextype, series, tenors=c("3yr", "5yr", "7yr"), + onlymissing=TRUE) { + stmt <- str_c("SELECT date, tenor, closeprice FROM index_quotes ", + "WHERE index=$1 AND series=$2", + if(onlymissing) " AND duration is NULL" else NULL, + " ORDER BY date") + df <- dbGetQuery(serenitasdb, stmt, params = list(indextype, series)) + df <- df %>% + filter(tenor %in% tenors) %>% + mutate(tenor = factor(tenor, levels=tenors)) + if(nrow(df) == 0) { + return( df ) + } else { + return( df %>% spread(tenor, closeprice)) + } } get.indexmaturity <- function(index, series){ @@ -82,7 +88,7 @@ for(index in c('IG', 'HY')) { tenors <- if(index=='IG') c("3yr", "5yr", "7yr", "10yr") else c("3yr", "5yr", "7yr") for(series in 18:28) { indexquotes <- get.indexquotes.table(index, series, tenors) - if(nrow(indexquotes)==0){ + if(nrow(indexquotes)==0) { next } maturities <- get.indexmaturity(index, series) @@ -100,12 +106,9 @@ for(index in c('IG', 'HY')) { c(fastduration(sc, cs, tradedate, maturities$maturity), fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront, coupon)) } - if(is.null(dim(durandthetas))){ - dim(durandthetas) <- c(1, length(durandthetas)) - } - ## non parallel version for easier debugging + ## ## non parallel version for easier debugging ## durandthetas <- c() - ## for(i in 1:nrow(indexquotes)){ + ## for(i in 1:nrow(indexquotes)) { ## tradedate <- indexquotes[i, "date"] ## exportYC(tradedate) ## cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), last_maturity,"Q", "FIXED", 1, @@ -115,27 +118,29 @@ for(index in c('IG', 'HY')) { ## durandthetas <- rbind(durandthetas, c(fastduration(sc, cs, tradedate, maturities$maturity), ## fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront, coupon))) ## } - + if(is.null(dim(durandthetas))) { + dim(durandthetas) <- c(1, length(durandthetas)) + } n <- nrow(maturities) df.durations <- data.frame(date=indexquotes$date, durandthetas[,1:n, drop=F]) df.thetas <- data.frame(date=indexquotes$date, durandthetas[,(n+1):(2*n), drop=F]) colnames(df.durations) <- c("date", maturities$tenor) colnames(df.thetas) <- c("date", maturities$tenor) - for(i in 1:nrow(df.durations)){ + for(i in 1:nrow(df.durations)) { for(tenor in maturities$tenor){ - if(!is.na(df.durations[i, tenor])){ + if(!is.na(df.durations[i, tenor])) { r <- dbSendQuery(serenitasdb, sqlstr.duration, params = list(df.durations[i, tenor], df.durations[i,"date"], index, series, tenor)) - if(dbHasCompleted(r)){ + if(dbHasCompleted(r)) { dbClearResult(r) } } - if(!is.na(df.thetas[i,tenor])){ + if(!is.na(df.thetas[i,tenor])) { r <- dbSendQuery(serenitasdb, sqlstr.theta, params = list(df.thetas[i, tenor], df.thetas[i, "date"], index, series, tenor)) - if(dbHasCompleted(r)){ + if(dbHasCompleted(r)) { dbClearResult(r) } } |
