aboutsummaryrefslogtreecommitdiffstats
path: root/R/load_cds_data.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/load_cds_data.R')
-rw-r--r--R/load_cds_data.R38
1 files changed, 38 insertions, 0 deletions
diff --git a/R/load_cds_data.R b/R/load_cds_data.R
new file mode 100644
index 00000000..ff41b6bd
--- /dev/null
+++ b/R/load_cds_data.R
@@ -0,0 +1,38 @@
+library(Rbbg)
+source("mlpdb.R")
+
+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)
+}
+
+setwd("/home/share/CorpCDOs/data/bloomberg/CDS")
+bbgConn <- blpConnect(host='192.168.1.108', port='8194')
+download.cdscurves()
+write.tranchedata()
+blpDisconnect(bbgConn)