## parse command line arguments if(.Platform$OS.type == "unix"){ root.dir <- "/home/share/CorpCDOs" }else{ root.dir <- "//WDSENTINEL/share/CorpCDOs" } library(logging) basicConfig() removeHandler('basic.stdout') addHandler(writeToFile, file=file.path(root.dir, "logs", "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="character", 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") } runs <- list(name=args$index, tenor=args$tenor) }else{ library(yaml) runs <- yaml.load_file(file.path(root.dir,"code", "etc", args$config)) } options(stringsAsFactors = FALSE) source(file.path(root.dir, "code", "R", "yieldcurve.R")) source(file.path(root.dir, "code", "R", "optimization.R")) source(file.path(root.dir, "code", "R", "calibration.R"), chdir=TRUE) source(file.path(root.dir, "code", "R", "serenitasdb.R")) source(file.path(root.dir, "code", "R", "creditIndex.R")) source(file.path(root.dir, "code", "R", "tranche_functions.R")) for(i in seq_along(runs$name)){ index.name <- runs$name[i] tenor <- runs$tenor[i] filename <- file.path(root.dir,"Tranche_data","Runs", paste(tolower(index.name), tenor, "csv",sep=".")) 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 }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"), ig9 = as.Date("2013-01-01"), ig19 = as.Date("2013-05-01"), ig21 = as.Date("2013-09-26"), ig23 = as.Date("2014-10-14"), hy23 = as.Date("2014-10-16"), xo22 = as.Date("2014-10-20"), eu9 = as.Date("2014-09-15"), eu21 = as.Date("2014-09-15"), eu22 = as.Date("2014-10-22")) } 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 <- "TARGET" }else{ curr <- "USD" cal <- "UnitedStates/GovernmentBond" } bus.dates <- alldates[isBusinessDay(calendar=cal, 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 <- set.tranchedata(index, tradedate) 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, BCtranche.theta(index, method="TLP")) 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(root.dir, "Tranche_data", "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) 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)