## parse command line arguments args <- commandArgs(trailingOnly=TRUE) if(length(args)==0 || args[1]=="-h" || args[1]=="--help"){ cat("usage: Rscript calibrate_tranches_BC.R [-u] indexname [tenor]\n") cat(" Rscript calibrate_tranches_BC.R [-u] -c configfile\n") cat("\n") cat("If -u flag is provided, it will update from the last run date\n") cat("tenor is 5yr by default\n") cat("\n") cat("Examples:\n") cat("\tRscript calibrate_tranches_BC.R hy19 7yr: will run hy19 7yr from the beginning\n") cat("\tRscript calibrate_tranches_BC.R -c conf.yaml: will run the list of indices provided in conf.yml\n") quit("no") } if(.Platform$OS.type == "unix"){ root.dir <- "/home/share/CorpCDOs" }else{ root.dir <- "//WDSENTINEL/share/CorpCDOs" } ## default values updateflag <- FALSE tenor <- "5yr" if(args[1]=="-u"){ updateflag <- TRUE if(length(args) >=2 && args[2]=="-c"){ if(length(args) < 3){ stop("Please provide a config file") }else{ library(yaml) runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[3])) } }else{ if(length(args) >= 2){ index.name <- args[2] }else{ stop("Please provind an index name") } if(length(args) >= 3){ tenor <- args[3] } } }else if(args[1]=="-c"){ if(length(args) < 2){ stop("Please provide a config file") }else{ library(yaml) runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[2])) } }else{ index.name <- args[1] if(length(args) >= 2){ tenor <- args[2] } } if(!exists("runs")){ runs <- list(name=index.name, tenor=tenor) } 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")) library(lossdistrib) 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(index.name,tenor,"csv",sep=".")) if(updateflag){##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")) } alldates <- seq(begin.date, Sys.Date()-1, by="1 day") if(index.name=="ig19"){ alldates <- alldates[alldates!=as.Date("2013-11-29")] ##people are lazy the day after Thanksgiving } bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)))) for(j in seq_along(bus.dates)){ tradedate <- bus.dates[j] cat("calibrating", index.name, tenor, "for", as.character(tradedate), "\n", sep=" ") exportYC(tradedate) index <- creditIndex(index.name, tenor) index <- set.index.desc(index, tradedate) ## calibrate the single names curvesca index <- set.singlenamesdata(index, tradedate) index <- set.tranchedata(index, tradedate) temp <- BCindex.pv(index) index$EL <- -temp$pl index$duration <- temp$cl 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(!updateflag && j==1){ cat(csvheaders(index), "\n", file=filename) } cat(tocsv(index), "\n", file=filename, append=TRUE) cat("done\n") } }