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.R30
1 files changed, 19 insertions, 11 deletions
diff --git a/R/thetas-durations.R b/R/thetas-durations.R
index 98ddd822..c441140d 100644
--- a/R/thetas-durations.R
+++ b/R/thetas-durations.R
@@ -31,6 +31,7 @@ get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr"), onlymi
"AS ct(date date, %s)")
stmt <- sprintf(sqlstr, index, series, arraystring1, arraystring2)
df <- dbGetQuery(serenitasdb, stmt)
+ df$date <- as.Date(df$date)
return( df )
}
@@ -39,6 +40,7 @@ get.indexmaturity <- function(index, series){
"from index_maturity where index=$1",
"and series=$2 order by maturity")
df <- dbGetQuery(serenitasdb, sqlstr, params=list(index, series))
+ df$maturity <- as.Date(df$maturity)
return( df )
}
@@ -95,10 +97,10 @@ convertNA <- function(x){
index <- 'HY'
tenors <- c("3yr", "5yr", "7yr")
recov <- 0.3
-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'")
+sqlstr.duration <- paste("UPDATE index_quotes set duration=$1 where date=$2 and index=$3",
+ "and series=$4 and tenor=$5")
+sqlstr.theta <- paste("UPDATE index_quotes set theta=$1 where date=$2 and index=$3",
+ "and series=$4 and tenor=$5")
for(series in c(16, 17, 18, 19, 20, 21, 22, 23)){
indexquotes <- get.indexquotes(index, series)
@@ -135,15 +137,21 @@ for(series in c(16, 17, 18, 19, 20, 21, 22, 23)){
colnames(df.thetas) <- c("date", tenors)
for(i in 1:nrow(df.durations)){
for(tenor in tenors){
- 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.durations[i, tenor])){
+ r <- dbSendQuery(serenitasdb, sqlstr.duration,
+ params = list(df.durations[i, tenor],
+ df.durations[i,"date"], index, series, tenor))
+ if(dbHasCompleted(r)){
+ dbClearResult(r)
+ }
}
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)
+ r <- dbSendQuery(serenitasdb, sqlstr.theta,
+ params = list(df.thetas[i, tenor],
+ df.thetas[i, "date"], index, series, tenor))
+ if(dbHasCompleted(r)){
+ dbClearResult(r)
+ }
}
}
}