aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/calibrate_tranches_BC.R10
-rw-r--r--R/serenitasdb.R17
2 files changed, 15 insertions, 12 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index f4bb3668..38294f0f 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -47,9 +47,8 @@ for(i in seq_along(runs$name)){
if(!file.exists(filename)){
args$update <- FALSE
}
- if(args$update){##ghetto way of getting the last row of the file
- runfile <- read.csv(filename)
- begin.date <- as.Date(runfile[nrow(runfile),1])+1
+ if(args$update){
+ begin.date <- getlastdate(index.name, tenor)
}else{
begin.date <- switch(index.name,
hy10 = as.Date("2014-08-11"),
@@ -119,9 +118,8 @@ for(i in seq_along(runs$name)){
cat(csvheaders(index), sep="\n", file=filename)
}
cat(tocsv(index), sep="\n", file=filename, append=TRUE)
+ dbSendQuery(serenitasdb, tosql(index))
loginfo("done")
}
}
-## it's really ghetto, but that works for now...
-cmd <- paste("python3" , file.path(root.dir, "code", "python", "populate_risk_numbers.py"))
-system(cmd)
+dbDisconnect(serenitasdb)
diff --git a/R/serenitasdb.R b/R/serenitasdb.R
index 6e6d5344..7dcfcd7d 100644
--- a/R/serenitasdb.R
+++ b/R/serenitasdb.R
@@ -47,12 +47,8 @@ get.indexquotes <- function(indexname, date=Sys.Date()){
}
indexsplit <- function(indexname){
- r <- regexpr("(\\D*)(\\d*)", indexname, perl=T)
- cs <- attr(r, "capture.start")
- cl <- attr(r, "capture.length")
- index <- substr(indexname, cs[1], cs[1]+cl[1]-1)
- series <- substr(indexname, cs[2], cs[2]+cl[2]-1)
- return(list(index=toupper(index), series=series))
+ return(list(index = toupper(substr(indexname, 0, 2)),
+ series=substr(indexname, 3, nchar(indexname))))
}
get.tranchequotes <- function(indexname, tenor='5yr', date=Sys.Date()){
@@ -114,3 +110,12 @@ couponfromindex <- function(indexname, tenor){
r <- dbGetQuery(serenitasdb, sqlstr)
r$coupon
}
+
+getlastdate <- function(indexname, tenor){
+ temp <- indexsplit(indexname)
+ r <- dbGetQuery(serenitasdb,
+ sprintf(paste("SELECT max(date)+1 AS date FROM risk_numbers",
+ "WHERE index='%s' and series='%s' and tenor='%s'"),
+ temp$index, temp$series, tenor))
+ return(as.Date(r$date, format="%Y-%m-%d"))
+}