1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
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", "tranches_RV_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("-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]"),
make_option("--until", default=Sys.Date()-1,
help="last day to run [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 > as.Date(args$until)){
next
}
alldates <- seq(begin.date, as.Date(args$until), 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)
if(any(c(is.null(index1), is.null(index2)))){
loginfo(paste("skipping pair ", index.name1, tenor1, index.name2, tenor2))
break
}
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)
}
}
|