if(.Platform$OS.type == "unix"){ root.dir <- "/home/share/CorpCDOs" }else{ root.dir <- "//WDSENTINEL/share/CorpCDOs" } 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("-i1", "--index1"), help="Reference index name"), make_option(c("-i2", "--index2"), help="Mapped index name"), make_option(c("-t1", "--tenor1"), default="5yr", help="Tenor of index1 [default %default]"), make_option(c("-t2", "--tenor2"), default="5yr", help="Tenor of index2 [default %default]")) args <- parse_args(OptionParser(option_list=option_list, description= "This script prices index2 using the skew from index1.")) if(is.null(args$config)){ if(is.null(args$index) || is.null(args$index2)){ stop("Please provide both index1 and index2") } runs <- list(name1=args$index1, name2=args$index2, tenor1=args$tenor1, tenor2=args$tenor2) }else{ library(yaml) runs <- yaml.load_file(file.path(root.dir, "code", "etc", args$config)) } 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$name1)){ with(runs, {index.name1 <<- name1[i] index.name2 <<- name2[i] tenor1 <<- tenor1[i] tenor2 <<- tenor2[i]}) filename <- file.path(root.dir,"Tranche_data","Runs", paste0(paste(index.name2, tenor2, "using", index.name1, tenor1),".csv")) if(args$update){ runfile <- read.csv(filename) begin.date <- as.Date(runfile[nrow(runfile), 1])+1 }else{ begin.date <- switch(index.name1, hy21 = as.Date("2013-10-04"), hy19 = as.Date("2013-10-04"), ig21 = as.Date("2013-09-26")) } if(begin.date == Sys.Date()){ next } alldates <- seq(begin.date, Sys.Date()-1, by="1 day") if(index.name2=="ig19"){ alldates <- alldates[alldates!=as.Date("2013-11-29")] ##people are lazy the day after Thanksgiving } bus.dates <- alldates[isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)] for(j in seq_along(bus.dates)){ tradedate <- bus.dates[j] index1 <- load.index(index.name1, tenor1, tradedate) index2 <- load.index(index.name2, tenor2, tradedate) accrued2 <- cdsAccrued(tradedate, index2$tranches$running) mappedpv <- list() for(method in c("ATM", "TLP", "PM")){ index2$rho <- adjust.skew(index1, index2, method) if(length(grep("ig", index2$name))>0){ mappedpv[[method]] <- BCtranche.pv(index2, protection=TRUE)$bp + accrued2 }else{ mappedpv[[method]] <- BCtranche.pv(index2)$bp - accrued2 } } row <- c(as.character(tradedate), index2$tranches$upfront, unlist(mappedpv)*100) if(j==1 && !args$update){ headers <- c("date", paste(index2$name, row.names(index2$tranches), "Quotes"), paste(index2$name, row.names(index2$tranches), "ATM PV"), paste(index2$name, row.names(index2$tranches), "TLP PV"), paste(index2$name, row.names(index2$tranches), "PM PV")) cat(paste(headers, collapse=","), "\n", file=filename) } cat(paste(row, collapse=","), "\n", file=filename, append=TRUE) } }