## parse command line arguments code.dir <- Sys.getenv("CODE_DIR") tranchedata.dir <- file.path(Sys.getenv("BASE_DIR"), "Tranche_data") library(logging) basicConfig() if(!interactive()) { removeHandler('basic.stdout') addHandler(writeToFile, file=file.path(Sys.getenv("LOG_DIR"), "calibrate_tranches_BC.log")) library(optparse) option_list <- list( make_option(c("-u", "--update"), action="store_true", default=FALSE, help="Update from the last run date [default %default]"), make_option(c("-c", "--config"), metavar="config_file", help="Runs the list of indices provided in CONFIG_FILE"), make_option(c("-i", "--index"), help="Index name we want to run"), make_option("--tenor", default="5yr", help="Tenor we want to run [default %default]"), make_option("--until", default=Sys.Date()-1, type="integer", help="last date to run [default %default]")) args <- parse_args(OptionParser(option_list=option_list)) ## default values if(is.null(args$config)) { if(is.null(args$index)) { stop("Please provide an index name") } config <- list(runs=list(c(args$index, args$tenor))) } else { config <- yaml::yaml.load_file(file.path(code.dir, "etc", args$config)) } } else { ## args <- list(config=yaml::yaml.load_file(file.path(code.dir, "etc", "runs.yml")), ## until = Sys.Date()-1, ## update = TRUE) args <- list(update = F, until = Sys.Date()-1) config <- list(runs = list(c("hy29", "5yr"))) options(error=recover) } class(args$until) <- "Date" options(stringsAsFactors = FALSE) source("yieldcurve.R") source("optimization.R") source("calibration.R") source("serenitasdb.R") source("creditIndex.R") source("tranche_functions.R") for(run in config$runs) { index.name <- run[1] tenor <- run[2] filename <- file.path(tranchedata.dir, "Runs", paste(tolower(index.name), tenor, "csv", sep=".")) ## if(!file.exists(filename)){ ## args$update <- FALSE ## } if(args$update && !is.na(begin.date <- getlastdate(index.name, tenor))) { } else { begin.date <- switch(index.name, hy10 = as.Date("2014-08-11"), hy15 = as.Date("2014-06-10"), hy17 = as.Date("2013-01-01"), hy19 = as.Date("2013-02-01"), hy21 = as.Date("2013-10-04"), hy23 = as.Date("2014-10-16"), hy25 = as.Date("2015-10-01"), hy27 = as.Date("2016-10-04"), hy29 = as.Date("2017-10-03"), ig9 = as.Date("2013-01-01"), ig19 = as.Date("2013-05-01"), ig21 = as.Date("2013-09-26"), ig23 = as.Date("2014-10-14"), ig25 = as.Date("2015-09-22"), ig27 = as.Date("2016-09-27"), ig29 = as.Date("2017-09-26"), xo22 = as.Date("2014-10-20"), xo24 = as.Date("2015-09-28"), xo26 = as.Date("2016-09-27"), xo28 = as.Date("2017-09-28"), eu9 = as.Date("2014-09-15"), eu19 = as.Date("2013-04-03"), eu21 = as.Date("2014-03-27"), eu22 = as.Date("2014-10-22"), eu24 = as.Date("2015-09-23"), eu26 = as.Date("2016-09-27"), eu28 = as.Date("2017-09-28")) } if(begin.date > as.Date(args$until)) { next } alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day") if(tolower(substr(index.name,1,2)) %in% c("xo", "eu")) { curr <- "EUR" cal <- Calendar$new("TARGET") } else { curr <- "USD" cal <- Calendar$new("UnitedStates/GovernmentBond") } bus.dates <- alldates[cal$isBusinessDay(alldates)] for(j in seq_along(bus.dates)) { tradedate <- bus.dates[j] loginfo(paste("calibrating", index.name, tenor, "for", as.character(tradedate))) exportYC(tradedate, curr) index <- creditIndex(index.name, tenor) index <- set.index.desc(index, tradedate) ## calibrate the single names curves index <- set.singlenamesdata(index, tradedate) index <- tryCatch(set.tranchedata(index, tradedate), error = function(e) logerror(e$message)) if(is.null(index)) { loginfo(paste("skipping", index.name, tenor)) next } temp <- BCindex.pv(index) index$EL <- -temp$pl index$duration <- temp$cl - cdsAccrued(tradedate, 1) index$theta <- indextheta(index, tradedate) ## calibrate the tranches using base correlation index$rho <- build.skew(index) ## compute various risk numbers index$tranches <- cbind(index$tranches, BCtranche.delta(index)) index$tranches <- cbind(index$tranches, tryCatch(BCtranche.theta(index, method="TLP"), error = function(e) { logerror(e$message) n <- nrow(index$tranches) data.frame(theta=rep(NA, n), fw.delta=rep(NA, n)) })) index$tranches$corr01 <- BCtranche.corr01(index) 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) ## save the index object save(index, file=file.path(tranchedata.dir, "Objects", paste0(paste(index.name, tenor, as.character(tradedate), sep="_"),".RData"))) ## save risk numbers into the csv file if(!args$update && j==1){ cat(csvheaders(index), sep="\n", file=filename) } cat(tocsv(index), sep="\n", file=filename, append=TRUE) tryCatch(dbWriteTable(serenitasdb, "risk_numbers_new", as.data.frame(index), append=TRUE), error = function(e) logerror(e$message)) loginfo("done") } } try(dbDisconnect(serenitasdb), TRUE)