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
|
library(dplyr)
library(lazyeval)
library(logging)
basicConfig()
source("yieldcurve.R")
source("calibration.R")
source("serenitasdb.R")
source("creditIndex.R")
index.closeprice <- function(index=c("IG", "HY", "EU", "XO"), series) {
index <- match.arg(index)
index.quotes <- tbl(serenitasdb, "index_quotes")
df <- index.quotes %>% filter_(lazyeval::interp(~series == s, s=series)) %>%
filter_(interp(~index == i, i=index)) %>%
arrange(date)
df <- df %>% filter(theta2 %is% `NULL`) %>%
select(date, tenor, closeprice) %>%
rename(price=closeprice) %>%
mutate(price=price/100) %>% collect()
return( df )
}
get.maturities <- function(index=c("IG", "HY", "EU", "XO"), series) {
index <- match.arg(index)
df <- tbl(serenitasdb, "index_maturity")
df <- df %>% filter_(lazyeval::interp(~series == s, s=series)) %>%
filter_(interp(~index == i, i=index)) %>%
select(tenor, maturity, coupon) %>%
rename(spread=coupon) %>% mutate(spread=spread*1e-4) %>%
arrange(maturity) %>% collect()
return( df )
}
if(!interactive()) {
library(optparse)
option_list <- list(
make_option(c("-i", "--index"), help="Index name we want to run"),
make_option(c("-s", "--series"), help="Index series"))
args <- parse_args(OptionParser(option_list=option_list))
} else {
args <- list(index="HY", series=30)
options(error=recover)
}
maturities <- get.maturities(args$index, args$series)
quotes <- index.closeprice(args$index, args$series)
index <- creditIndex(paste0(args$index, args$series), "10yr")
sqlstr <- paste("UPDATE index_quotes SET duration2=$1, theta2=$2",
"WHERE date=$3 AND index=$4 AND series=$5 AND tenor=$6")
unique.dates <- quotes$date %>% unique()
for(i in seq_along(unique.dates)) {
tradedate <- unique.dates[i]
loginfo(paste("calibrating", index$name, tradedate))
exportYC(tradedate, "USD")
index <- set.index.desc(index, tradedate)
index$quotes <- quotes %>% filter(date == tradedate) %>%
inner_join(maturities, by="tenor") %>% arrange(maturity)
if(nrow(index$quotes) == 0) {
next
}
index <- set.singlenamesdata(index, tradedate)
if(is.null(index$portfolio)){
next
}
index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE),
index$maturity, "Q", "FIXED", 1,
0, tradedate, IMMDate(tradedate, "prev"))
tweak <- tweakcurves(index, tradedate)
index$portfolio <- tweak$portfolio
index$basis <- tweak$basis
acc <- cdsAccrued(tradedate, 1)
for(j in seq_along(maturities$maturity)) {
maturity <- maturities$maturity[j]
theta <- index %>% indextheta(tradedate=tradedate, maturity=maturity)
duration <- (index %>% indexduration(tradedate=tradedate, maturity=maturity)) - acc
dbSendQuery(serenitasdb, sqlstr,
params = list(duration, theta, tradedate,
args$index, args$series, maturities$tenor[j]))
}
}
|