aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/creditIndex.R9
-rw-r--r--R/serenitasdb.R9
-rw-r--r--R/thetas-durations.R9
3 files changed, 13 insertions, 14 deletions
diff --git a/R/creditIndex.R b/R/creditIndex.R
index e3002b3f..5fff04f8 100644
--- a/R/creditIndex.R
+++ b/R/creditIndex.R
@@ -102,15 +102,6 @@ tocsv <- function(index){
return(paste(row, collapse=","))
}
-sqlArray <- function(vec){
- vec[is.na(vec)] <- "NULL"
- return( sprintf("Array[%s]", paste(vec, collapse=",")) )
-}
-
-sqlQuote <- function(s){
- return( sprintf("'%s'", s) )
-}
-
tosql <- function(index){
## write sql representation of the index object
values <- c(sqlQuote(index$tradedate),
diff --git a/R/serenitasdb.R b/R/serenitasdb.R
index 071df331..89cc7733 100644
--- a/R/serenitasdb.R
+++ b/R/serenitasdb.R
@@ -4,6 +4,15 @@ serenitasdb <- dbConnect(RPostgres::Postgres(),
user = "serenitas_user",
host="debian")
+sqlArray <- function(vec){
+ vec[is.na(vec)] <- "NULL"
+ return( sprintf("Array[%s]", paste(vec, collapse=",")) )
+}
+
+sqlQuote <- function(slist){
+ return( unlist(lapply(slist, function(s) sprintf("'%s'", s)) ) )
+}
+
nameToBasketID <- function(name, date=Sys.Date()){
sqlstr <- "SELECT * from nametobasketid($1, $2)"
r <- dbGetQuery(serenitasdb, sqlstr, params=list(name, date))
diff --git a/R/thetas-durations.R b/R/thetas-durations.R
index 02001187..98ddd822 100644
--- a/R/thetas-durations.R
+++ b/R/thetas-durations.R
@@ -20,7 +20,7 @@ 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("Array[''", paste(tenors, collapse = "'', ''"), "'']::tenor[]")
+ 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")
@@ -36,10 +36,9 @@ get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr"), onlymi
get.indexmaturity <- function(index, series){
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)
+ "from index_maturity where index=$1",
+ "and series=$2 order by maturity")
+ df <- dbGetQuery(serenitasdb, sqlstr, params=list(index, series))
return( df )
}