aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/serenitasdb.R26
1 files changed, 17 insertions, 9 deletions
diff --git a/R/serenitasdb.R b/R/serenitasdb.R
index 65373a23..736d2250 100644
--- a/R/serenitasdb.R
+++ b/R/serenitasdb.R
@@ -40,27 +40,35 @@ cdslist <- function(indexname, date=Sys.Date()){
return( dbGetQuery(serenitasdb, sqlstr, params=list(basketid)))
}
-arr.convert <- function(arr, ncol=8){
- arr <- unlist(lapply(arr, function(x)strsplit(substr(x,2,nchar(x)-1),",",fixed=TRUE)))
+arr.convert <- function(arr, col.names){
+ if(missing(col.names)){
+ ncol <- str_count(arr[1], ",") + 1
+ }else{
+ ncol <- length(col.names)
+ }
+ arr <- str_split_fixed(str_sub(arr, 2, -2), ",", ncol)
arr[arr=="NULL"] <- NA
- arr <- matrix(as.numeric(arr), nrow=length(arr)/ncol, ncol=ncol, byrow=T)
- colnames(arr) <- c("6m", "1y", "2y", "3y", "4y", "5y", "7y", "10y")
+ storage.mode(arr) <- "numeric"
+ if(!missing(col.names)){
+ colnames(arr) <- col.names
+ }
return(arr)
}
get.indexquotes <- function(indexname, date=Sys.Date()){
r <- dbGetQuery(serenitasdb, "SELECT * from curve_quotes($1, $2)",
params = list(indexname, date))
+ tenors <- c("6m", paste0(c(1, 2, 3, 4, 5, 7, 10), "y"))
quotes <- list(tickers=r[,1],
- spread_curve = arr.convert(r$spread_curve),
- upfront_curve =arr.convert(r$upfront_curve),
- recovery_curve = arr.convert(r$recovery_curve))
+ spread_curve = arr.convert(r$spread_curve, tenors),
+ upfront_curve =arr.convert(r$upfront_curve, tenors),
+ recovery_curve = arr.convert(r$recovery_curve, tenors))
return( quotes )
}
indexsplit <- function(indexname){
- return(list(index = toupper(substr(indexname, 0, 2)),
- series=substr(indexname, 3, nchar(indexname))))
+ return(list(index = str_to_upper(str_sub(indexname, 0, 2)),
+ series=str_sub(indexname, 3, -1)))
}
get.tranchequotes <- function(indexname, tenor='5yr', date=Sys.Date()){