diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/new_index_series.R | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/R/new_index_series.R b/R/new_index_series.R index b4b611cc..c979e3b9 100644 --- a/R/new_index_series.R +++ b/R/new_index_series.R @@ -24,37 +24,40 @@ newbasketID <- function(indextype='IG', series, mat_5yr, coupon){ ##TODO: fill the index indexname <- 'ITRX' ## or CDX -indextype <- 'XOVER' ## or IG or HY -oldseries <- 22 -newseries <- 23 -newbasketID('IG', 23, as.Date("2019-12-20")) +indextype <- 'EUR' ## or IG or HY or EUR +series <- 22 +newbasketID('EU', 22, as.Date("2019-12-20")) -oldindex <- bds(bbgConn, sprintf("%s %s CDSI S%s 5Y Corp", indexname, indextype, oldseries), "INDX_MEMBERS") -newindex <- bds(bbgConn, sprintf("%s %s CDSI S%s 5Y Corp", indexname, indextype, newseries), "INDX_MEMBERS") -oldtickers <- oldindex[,5] -tickers <- newindex[,5] +index <- bds(bbgConn, sprintf("%s %s CDSI S%s 5Y Corp", indexname, indextype, newseries), "INDX_MEMBERS") +tickers <- index[,5] -data <- bdp(bbgConn, paste(tickers, "Corp"), c("cds_company_id", "cds_restructuring_type", "cds_corp_tkr")) -olddata <- bdp(bbgConn, paste(oldtickers, "Corp"), c("cds_company_id", "cds_restructuring_type", "cds_corp_tkr")) +cds.fields <- c("cds_company_id", "cds_restructuring_type", "cds_corp_tkr", "sw_seniority") +data <- bdp(bbgConn, paste(tickers, "Corp"), cds.fields) data <- cbind(tickers, data) unique.tickers <- aggregate(data, by=list(data$cds_company_id), FUN=function(x)x[1])$tickers cdscurves <- bds(bbgConn, paste(unique.tickers, "Curncy"), "CDS_CURVE_INFO") curves <- aggregate(cdscurves[,2], by=list(cdscurves$ticker), FUN=function(x)x) -prep.data <- data.frame(Name=newindex[,1], company_id=data[,2], ticker=data[,4], - currency=newindex[,3], seniority=temp1[,4], doc_clause=data[,3]) -newrowids <- which(!data[,'cds_company_id'] %in% olddata[,'cds_company_id'], 'cds_company_id') -newids <- data[!data[,'cds_company_id'] %in% olddata[,'cds_company_id'], 'cds_company_id'] +prep.data <- data.frame(Name=index[,1], company_id=data$cds_company_id, ticker=data$cds_corp_tkr, + currency=index[,3], seniority=data$sw_seniority, + doc_clause=data$cds_restructuring_type) + +all.companyids <- dbGetQuery(dbCon, "SELECT company_id from cds_issuers") +curves <- curves[!prep.data$company_id %in% all.companyids$company_id,] +prep.data <- prep.data[!prep.data$company_id %in% all.companyids$company_id,] +prep.data[prep.data$doc_clause=="Modified-Modified Restructurin","doc_clause"] <- "Modified Modified Restructurin" sqlstr <- "INSERT INTO CDS_Issuers VALUES('%s', %s, '%s', '%s', '%s', '%s', '%s')" with(prep.data, - for(i in newids){ + for(i in 1:nrow(curves)){ stmt <- sprintf(sqlstr, gsub("'","''", Name[i]), company_id[i], ticker[i], currency[i], seniority[i], doc_clause[i], paste0("{\"", paste(curves[i,2], collapse="\",\""), "\"}")) dbSendQuery(dbCon, stmt) }) +sqlstr <- "SELECT basketid from index_version where index='%s' and series=%s" +basketid <- as.integer(dbGetQuery(dbCon, sprintf(sqlstr, 'EU', series))) ##fill index_list column sqlstr <- "UPDATE CDS_Issuers SET index_list=index_list||%s where company_id=%s" -for(id in prep.data[,2]){ - dbSendQuery(dbCon, sprintf(sqlstr, newid, id)) +for(id in data$cds_company_id){ + dbSendQuery(dbCon, sprintf(sqlstr, basketid, id)) } |
