diff options
| -rw-r--r-- | R/tranches_RV_BC.R | 100 |
1 files changed, 73 insertions, 27 deletions
diff --git a/R/tranches_RV_BC.R b/R/tranches_RV_BC.R index 9a95256f..0cf94f43 100644 --- a/R/tranches_RV_BC.R +++ b/R/tranches_RV_BC.R @@ -4,6 +4,32 @@ if(.Platform$OS.type == "unix"){ 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)
@@ -11,34 +37,54 @@ source(file.path(root.dir, "code", "R", "serenitasdb.R")) source(file.path(root.dir, "code", "R", "creditIndex.R"))
library(lossdistrib)
-alldates <- seq(as.Date("2013-10-04"), as.Date("2014-09-02"), by="1 day")
-bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
-
-index.name1 <- "hy21"
-tenor1 <- "3yr"
-index.name2 <- "hy17"
-tenor2 <- "5yr"
-filename <- file.path(root.dir,"Tranche_data","Runs",
- paste0(paste(index.name2, tenor2, "using", index.name1, tenor1),".csv"))
+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")
+ bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
-for(i in seq_along(bus.dates)){
- tradedate <- bus.dates[i]
- index1 <- load.index(index.name1, tenor1, tradedate)
- index2 <- load.index(index.name2, tenor2, tradedate)
- tradedate <- bus.dates[i]
- accrued2 <- cdsAccrued(tradedate, index2$tranches$running)
- mappedpv <- list()
- for(method in c("ATM", "TLP", "PM")){
- index2$rho <- adjust.skew(index1, index2, method)
- mappedpv[[method]] <- BCtranche.pv(index2)$bp - accrued2
+ if(index.name2=="ig19"){
+ alldates <- alldates[alldates!=as.Date("2013-11-29")] ##people are lazy the day after Thanksgiving
}
- row <- c(as.character(tradedate), index2$tranches$upfront, unlist(mappedpv)*100)
- if(i==1){
- 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)
+ 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)
}
- cat(paste(row, collapse=","), "\n", file=filename, append=TRUE)
}
|
