diff options
Diffstat (limited to 'R/bloomberg_cds.R')
| -rw-r--r-- | R/bloomberg_cds.R | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/R/bloomberg_cds.R b/R/bloomberg_cds.R new file mode 100644 index 00000000..79717a36 --- /dev/null +++ b/R/bloomberg_cds.R @@ -0,0 +1,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) |
