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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
## parse command line arguments
config.dir <- Sys.getenv("SERENITAS_CONFIG_DIR")
tranchedata.dir <- file.path(Sys.getenv("SERENITAS_BASE_DIR"), "Tranche_data")
library(logging)
basicConfig()
if(!interactive()) {
removeHandler('basic.stdout')
addHandler(writeToFile, file=file.path(Sys.getenv("LOG_DIR"), "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="integer",
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")
}
config <- list(runs=list(c(args$index, args$tenor)))
} else {
config <- yaml::yaml.load_file(file.path(config.dir, args$config))
}
} else {
## args <- list(config=yaml::yaml.load_file(file.path(config.dir, "runs.yml")),
## until = Sys.Date()-1,
## update = TRUE)
args <- list(update = F, until = Sys.Date()-1)
config <- list(runs = list(c("hy29", "5yr")))
options(error=recover)
}
class(args$until) <- "Date"
options(stringsAsFactors = FALSE)
source("yieldcurve.R")
source("optimization.R")
source("calibration.R")
source("serenitasdb.R")
source("creditIndex.R")
source("tranche_functions.R")
for(run in config$runs) {
index.name <- run[1]
tenor <- run[2]
filename <- file.path(tranchedata.dir, "Runs",
paste(tolower(index.name), tenor, "csv", sep="."))
## if(!file.exists(filename)){
## args$update <- FALSE
## }
if(args$update && !is.na(begin.date <- getlastdate(index.name, tenor))) {
} 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"),
hy23 = as.Date("2014-10-16"),
hy25 = as.Date("2015-10-01"),
hy27 = as.Date("2016-10-04"),
hy29 = as.Date("2017-10-03"),
hy31 = as.Date("2018-10-02"),
ig9 = as.Date("2013-01-01"),
ig19 = as.Date("2013-05-01"),
ig21 = as.Date("2013-09-26"),
ig23 = as.Date("2014-10-14"),
ig25 = as.Date("2015-09-22"),
ig27 = as.Date("2016-09-27"),
ig29 = as.Date("2017-09-26"),
ig31 = as.Date("2018-09-25"),
xo22 = as.Date("2014-10-20"),
xo24 = as.Date("2015-09-28"),
xo26 = as.Date("2016-09-27"),
xo28 = as.Date("2017-09-28"),
eu9 = as.Date("2014-09-15"),
eu19 = as.Date("2013-04-03"),
eu21 = as.Date("2014-03-27"),
eu22 = as.Date("2014-10-22"),
eu24 = as.Date("2015-09-23"),
eu26 = as.Date("2016-09-27"),
eu28 = as.Date("2017-09-28"),
eu30 = as.Date("2018-09-25"),
xo30 = as.Date("2018-09-25"))
}
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 <- Calendar$new("TARGET")
} else {
curr <- "USD"
cal <- Calendar$new("UnitedStates/GovernmentBond")
}
bus.dates <- alldates[cal$isBusinessDay(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 <- tryCatch(set.tranchedata(index, tradedate),
error = function(e) logerror(e$message))
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, tryCatch(BCtranche.theta(index, method="TLP"),
error = function(e) {
logerror(e$message)
n <- nrow(index$tranches)
data.frame(theta=rep(NA, n),
fw.delta=rep(NA, n)) }))
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(tranchedata.dir, "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)
tryCatch(dbWriteTable(serenitasdb, "risk_numbers_old",
as.data.frame(index), append=TRUE),
error = function(e) logerror(e$message))
loginfo("done")
}
}
try(dbDisconnect(serenitasdb), TRUE)
|