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
|
bbgConn <- blpConnect(host='192.168.1.108', port='8194')
nameToBasketID <- function(name){
index <- substr(name, 1, 2)
series <- substr(name, 3, nchar(name))
sqlstr <- "SELECT max(basketid) from index_desc where Index='%s' and Series=%s"
r <- dbGetQuery(dbCon, sprintf(sqlstr, index, series))
return(as.integer(r))
}
newbasketID <- function(series, offset){
maturities <- c("2017-06-20", "2019-06-20", "2021-06-20", "2024-06-20")
tenors <- c("Y3", "Y5", "Y7", "Y10")
sqlstr <- "INSERT INTO index_desc VALUES(%s, '%s', %s, '%s', '%s', 100, 0)"
indices <- c("HY", "IG")
for(i in seq_along(indices)){
for(j in seq_along(tenors)){
stmt <- sprintf(sqlstr, offset + 10*i, indices[i], series,
maturities[j], tenors[j])
dbSendQuery(dbCon, stmt)
}
}
}
cdslist <- function(indexname){
basketid <- nameToBasketID(indexname)
sqlstr <- "select * from CDS_Issuers where index_list @> '{%s}'"
return( dbGetQuery(dbCon, sprintf(sqlstr, basketid)))
}
download.cdscurves <- function(date=Sys.Date()){
tickers <- dbGetQuery(dbCon, "select cds_curve[6] from CDS_Issuers")$cds_curve
bbg.data <- bds(bbgConn, paste(tickers, "Curncy"), "cds_curve_info")
stmt <- "INSERT INTO cds_quotes VALUES('%s', '%s', 0, 0, %s, %s, %s, %s)"
for(i in 1:nrow(bbg.data)){
Source <- bbg.data[i,7]
if(Source==""){
Source <- "Null"
}else{
Source <- paste0("'", Source, "'")
}
dbSendQuery(dbCon, sprintf(stmt, format(date,"%m/%d/%y"),
bbg.data[i,2], bbg.data[i,4], bbg.data[i,5],
Source, bbg.data[i,6]))
}
}
write.tranchedata <- function(){
data <- c()
for(index in c("ig21", "ig19", "hy21", "hy19")){
df <- read.csv(paste0(index, "_tranches_bbgid.csv"), check.names=FALSE)
for(tenor in c("3Y", "5Y", "7Y")){
if(tenor %in% names(df)){
data <- rbind(data, bdp(bbgConn, paste(df[[tenor]], "Corp"), c("px_last", "tranche_delta", "underlying_reference_px_rt")))
}
}
}
write.csv(data, file=paste0("tranche_data_", Sys.Date(), ".csv"), row.names=TRUE)
}
blpDisconnect(bbgConn)
|