aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/calibrate_tranches_BC.R16
-rw-r--r--R/calibration.R7
-rw-r--r--R/creditIndex.R46
-rw-r--r--sql/serenitasdb.sql28
4 files changed, 60 insertions, 37 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index a1ccc1b9..64aad871 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -30,8 +30,8 @@ if(!interactive()){
## args <- list(config=yaml::yaml.load_file(file.path(code.dir, "etc", "runs.yml")),
## until = Sys.Date()-1,
## update = TRUE)
- args <- list(update=TRUE, until = Sys.Date()-1)
- config <- list(runs=list(c("hy21", "3yr")))
+ args <- list(update = F, until = Sys.Date()-1)
+ config <- list(runs = list(c("hy29", "5yr")))
options(error=recover)
}
@@ -63,7 +63,7 @@ for(run in config$runs) {
hy23 = as.Date("2014-10-16"),
hy25 = as.Date("2015-10-01"),
hy27 = as.Date("2016-10-04"),
- hy29 = as.Date("2016-10-03"),
+ hy29 = as.Date("2017-10-03"),
ig9 = as.Date("2013-01-01"),
ig19 = as.Date("2013-05-01"),
ig21 = as.Date("2013-09-26"),
@@ -112,7 +112,7 @@ for(run in config$runs) {
}
temp <- BCindex.pv(index)
index$EL <- -temp$pl
- index$duration <- temp$cl-cdsAccrued(tradedate, 1)
+ index$duration <- temp$cl - cdsAccrued(tradedate, 1)
index$theta <- indextheta(index, tradedate)
## calibrate the tranches using base correlation
@@ -130,7 +130,7 @@ for(run in config$runs) {
temp <- BCtranche.pv(index, protection=TRUE)
index$tranches$duration <-
(temp$cl-cdsAccrued(tradedate, index$tranches$running))/index$tranches$running
- index$tranches$EL <- -temp$pl*diff(index$K)
+ index$tranches$EL <- -temp$pl * diff(index$K)
## save the index object
save(index, file=file.path(tranchedata.dir, "Objects",
@@ -141,9 +141,9 @@ for(run in config$runs) {
cat(csvheaders(index), sep="\n", file=filename)
}
cat(tocsv(index), sep="\n", file=filename, append=TRUE)
- r <- with(tosql(index),
- tryCatch(dbGetQuery(serenitasdb, stmt, params),
- error = function(e) logerror(e$message)))
+ tryCatch(dbWriteTable(serenitasdb, "risk_numbers_new",
+ as.data.frame(index), append=TRUE),
+ error = function(e) logerror(e$message))
loginfo("done")
}
}
diff --git a/R/calibration.R b/R/calibration.R
index 38f7d329..b6a05c91 100644
--- a/R/calibration.R
+++ b/R/calibration.R
@@ -69,7 +69,7 @@ set.tranchedata <- function(index, tradedate){
stop("index is NULL")
}
temp <- get.tranchequotes(index$name, index$tenor, tradedate)
- if(is.null(temp)){
+ if(nrow(temp) == 0) {
stop(paste(index$name, "no quote for day", as.character(tradedate)))
}
index$quotes <- data.frame(maturity=index$maturity,
@@ -83,7 +83,7 @@ set.tranchedata <- function(index, tradedate){
}else{
index$quotes$price <- snacpv(index$cs, index$quotes$refspread[1]*1e-4, index$quotes$spread,
- 0.4, tradedate)
+ if (index$type %in% c("IG", "EU")) 0.4 else 0.3, tradedate)
}
if(length(indexpv(index, tradedate=tradedate)) == 1) {
stop("why am I stopping?")
@@ -135,7 +135,8 @@ set.tranchedata <- function(index, tradedate){
}else{
dirtyquotes <- temp$trancheupfrontmid/100 - accrued
}
- index$tranches <- data.frame(upfront=temp$trancheupfrontmid,
+ index$tranches <- data.frame(id = temp$id,
+ upfront=temp$trancheupfrontmid,
running=temp$trancherunningmid * 1e-4,
quotes=dirtyquotes,
mkt.delta=temp$tranchedelta,
diff --git a/R/creditIndex.R b/R/creditIndex.R
index 7d8859a3..a2421869 100644
--- a/R/creditIndex.R
+++ b/R/creditIndex.R
@@ -99,29 +99,25 @@ tocsv <- function(index){
return(paste(row, collapse=","))
}
-tosql <- function(index){
- ## write sql representation of the index object
- params <- list(index$tradedate,
- index$type,
- index$series,
- index$tenor,
- index$quotes$price,
- index$basis,
- index$EL,
- index$duration,
- index$theta,
- sqlArray(as.integer(index$K.orig*100)),
- sqlArray(index$rho[-1]),
- sqlArray(index$tranches$mkt.delta),
- sqlArray(index$tranches$delta),
- sqlArray(index$tranches$fw.delta),
- sqlArray(index$tranches$gamma),
- sqlArray(index$tranches$theta),
- sqlArray(index$tranches$corr01),
- sqlArray(index$tranches$duration),
- sqlArray(index$tranches$EL))
- dollars <- paste(lapply(1:length(params), function(i)sprintf("$%s",i)),
- collapse=",")
- return(list(stmt=sprintf("INSERT INTO risk_numbers VALUES(%s)", dollars),
- params=params))
+as.data.frame.creditIndex <- function(index) {
+ data.frame(tranche_id = index$tranches$id,
+ date = index$tradedate,
+ index = index$type,
+ series = index$series,
+ tenor = index$tenor,
+ index_price = index$quotes$price,
+ index_basis = index$basis,
+ index_el = index$EL,
+ index_duration = index$duration,
+ index_theta = index$theta,
+ attach = as.integer(index$K.orig[-length(index$K.orig)]*100),
+ detach = as.integer(index$K.orig[-1]*100),
+ corr_at_detach = index$rho[-1],
+ delta = index$tranche$delta,
+ forward_delta = index$tranche$fw.delta,
+ gamma = index$tranches$gamma,
+ theta = index$tranches$theta,
+ corr01 = index$tranches$corr01,
+ duration = index$tranches$duration,
+ EL = index$tranches$EL)
}
diff --git a/sql/serenitasdb.sql b/sql/serenitasdb.sql
index 2716cba6..6c8bfdd8 100644
--- a/sql/serenitasdb.sql
+++ b/sql/serenitasdb.sql
@@ -81,6 +81,7 @@ CREATE TABLE quotes (
);
CREATE TABLE tranche_quotes (
+ id serial PRIMARY KEY,
QuoteDate timestamptz,
Index index_type,
Series smallint,
@@ -102,9 +103,11 @@ CREATE TABLE tranche_quotes (
CorrAtDetachment real,
Basis real,
QuoteSource varchar(4),
- PRIMARY KEY(QuoteDate, Index, Series, Version, Tenor, Attach, Detach, QuoteSource, trancherunningmid)
);
+CREATE INDEX tranche_quotes_idx ON
+tranche_quotes(index, series, DATE(quotedate AT TIME ZONE 'localtime'), tenor, attach ASC)
+
GRANT ALL ON quotes TO serenitas_user;
GRANT ALL ON tranche_quotes TO serenitas_user;
@@ -476,6 +479,29 @@ CREATE TABLE risk_numbers(
el float[],
PRIMARY KEY(date, index, series, tenor));
+CREATE TABLE risk_numbers_new(
+ id serial PRIMARY KEY,
+ tranche_id integer REFERENCES tranche_quotes(id),
+ date date,
+ index index_type,
+ series integer,
+ tenor tenor,
+ index_price float,
+ index_basis float,
+ "index_EL" float,
+ index_duration float,
+ index_theta float,
+ attach smallint,
+ detach smallint,
+ corr_at_detach float,
+ delta float,
+ forward_delta float,
+ gamma float,
+ theta float,
+ corr01 float,
+ duration float,
+ "EL" float);
+
GRANT ALL ON risk_numbers to serenitas_user;