aboutsummaryrefslogtreecommitdiffstats
path: root/R/load_cds_data.R
blob: 63e74835479dabce14eded9b021b32c453207b84 (plain)
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
library(Rbbg)
source("serenitasdb.R")

download.cdscurves <- function(date=Sys.Date()){
    tickers <- dbGetQuery(serenitasdb, "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(serenitasdb, 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)