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
|
## parse command line arguments
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", "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="character",
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")
}
runs <- list(name=args$index, tenor=args$tenor)
}else{
library(yaml)
runs <- yaml.load_file(file.path(root.dir,"code", "etc", args$config))
}
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"))
source(file.path(root.dir, "code", "R", "tranche_functions.R"))
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(tolower(index.name), tenor, "csv",sep="."))
if(!file.exists(filename)){
args$update <- FALSE
}
if(args$update){##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"),
ig23 = as.Date("2014-10-14"),
hy23 = as.Date("2014-10-16"),
xo22 = as.Date("2014-10-20"),
eu9 = as.Date("2014-11-03"),
eu21 = as.Date("2014-11-03"))
}
if(begin.date > as.Date(args$until)){
next
}
alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
bus.dates <- alldates[isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)]
for(j in seq_along(bus.dates)){
tradedate <- bus.dates[j]
loginfo(paste("calibrating", index.name, tenor, "for", as.character(tradedate)))
if(tolower(substr(index.name,1,2)) %in% c("xo", "eu")){
curr <- "EUR"
}else{
curr <- "USD"
}
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 <- set.tranchedata(index, tradedate)
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, 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(!args$update && j==1){
cat(csvheaders(index), sep="\n", file=filename)
}
cat(tocsv(index), sep="\n", file=filename, append=TRUE)
loginfo("done")
}
}
## it's really ghetto, but that works for now...
cmd <- paste("python3" , file.path(root.dir, "code", "python", "populate_risk_numbers.py"))
system(cmd)
|