aboutsummaryrefslogtreecommitdiffstats
path: root/R/thetas-durations.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/thetas-durations.R')
-rw-r--r--R/thetas-durations.R18
1 files changed, 8 insertions, 10 deletions
diff --git a/R/thetas-durations.R b/R/thetas-durations.R
index 8aa01197..9c93e0a7 100644
--- a/R/thetas-durations.R
+++ b/R/thetas-durations.R
@@ -20,18 +20,16 @@ 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"), onlymissing=TRUE){
- arraystring1 <- paste0(sqlArray(sqlQuote(sqlQuote(tenors))), "::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")
+ query1 <- sprintf(paste("select date, tenor, closeprice from index_quotes",
+ "where index=%s and series=%s"), sqlQuote(index), series)
if(onlymissing){
- sqlstr <- paste(sqlstr, "and duration is Null")
+ query1 <- paste(query1, "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)
- df$date <- as.Date(df$date)
+ query1 <- paste(query1, "order by date, tenor")
+ query2 <- sprintf("select unnest(%s::tenor[])",sqlQuote(sqlArray(tenors)))
+ stmt <- paste0("SELECT * from crosstab($1, $2) AS ct(date date, ", arraystring2, ")")
+ arraystring2 <- paste(lapply(tenors, function(s)sprintf("\"%s\" float", s)), collapse=",")
+ df <- dbGetQuery(serenitasdb, stmt, params=list(query1, query2))
return( df )
}