diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/build_SC.R | 14 | ||||
| -rw-r--r-- | R/load_cf.R | 63 |
2 files changed, 58 insertions, 19 deletions
diff --git a/R/build_SC.R b/R/build_SC.R index 631325aa..b763f3a0 100644 --- a/R/build_SC.R +++ b/R/build_SC.R @@ -200,9 +200,17 @@ generate_portfolio_helper <- function(cusips){ r <- c()
s <- c()
for(dealname in uniqdealnames){
- r <- c(r, paste(toupper(dealname), "COLLAT_INITIAL", sep=","))
- r <- c(r, paste(toupper(dealname), "COLLAT_REINVEST", sep=","))
- s <- c(s, rep(paste0(dealname, ".sss"), 2))
+ sqlstring <- sprintf(paste0("SELECT \"Reinv End Date\" FROM latest_clo_universe",
+ " WHERE dealname='%s'"), dealname)
+ data <- dbGetQuery(dbCon, sqlstring)
+ if(!is.na(data$"Reinv End Date")){
+ r <- c(r, paste(toupper(dealname), "COLLAT_INITIAL", sep=","))
+ r <- c(r, paste(toupper(dealname), "COLLAT_REINVEST", sep=","))
+ s <- c(s, rep(paste0(dealname, ".sss"), 2))
+ }else{
+ r <- c(r, paste(toupper(dealname), "COLLAT", sep=","))
+ s <- c(s, rep(paste0(dealname, ".sss"), 1))
+ }
}
for(i in 1:length(cusips)){
diff --git a/R/load_cf.R b/R/load_cf.R index 22c5386a..383d7649 100644 --- a/R/load_cf.R +++ b/R/load_cf.R @@ -12,7 +12,7 @@ source(file.path(root.dir, "code", "R", "cds_utils.R")) source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
-workdate <- as.Date("2013-02-11")
+workdate <- as.Date("2013-02-14")
MarkitData <- getMarkitIRData(workdate)
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
@@ -34,14 +34,14 @@ sanitize.column <- function(vec){ allfiles <- list.files(file.path(root.dir, "Scenarios", paste0("Prices_", workdate)), "*.txt")
allfiles <- unique(sapply(strsplit(allfiles, "-"), function(x) x[1]))
allfiles <- allfiles[!(allfiles=="Total")]
-dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Prices_", workdate)), "*COLLAT_INITIAL-CF-Scen1*")
+dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Prices_", workdate)), "*COLLAT_INITIAL-CF-Scen100")
+dealnames <- c(dealnames, list.files(file.path(root.dir, "Scenarios", paste0("Prices_", workdate)), "*COLLAT-CF-Scen100"))
dealnames <- unique(sapply(strsplit(dealnames, "-"), function(x) x[1]))
cusips <- setdiff(allfiles, dealnames)
dealnames <- tolower(dealnames)
getdealcf <- function(dealnames, workdate=Sys.Date()){
cfdata <- list()
- tranches <- c("COLLAT_REINVEST", "COLLAT_INITIAL")
fields <- c("Cashflow", "Principal", "Interest")
flag <- FALSE
n.scenarios <- 100
@@ -51,10 +51,16 @@ getdealcf <- function(dealnames, workdate=Sys.Date()){ colnames(r) <- fields
sqlstring <- sprintf("select marketvalue from latest_deal_model_numbers where dealname='%s'", dealname)
mv <- dbGetQuery(dbCon, sqlstring)$marketvalue
- sqlstring <- sprintf("select \"Curr Collat Bal\" from latest_clo_universe where dealname='%s'", dealname)
- currbal <- dbGetQuery(dbCon, sqlstring)$"Curr Collat Bal"
+ sqlstring <- sprintf(paste0("select \"Curr Collat Bal\", \"Reinv End Date\" from ",
+ "latest_clo_universe where dealname='%s'"), dealname)
+ sqldata <- dbGetQuery(dbCon, sqlstring)
cfdata[[dealname]]$mv <- mv
- cfdata[[dealname]]$currbal <- currbal
+ cfdata[[dealname]]$currbal <- sqldata$"Curr Collat Bal"
+ if(is.na(sqldata$"Reinv End Date")){
+ tranches <- "COLLAT"
+ }else{
+ tranches <- c("COLLAT_REINVEST", "COLLAT_INITIAL")
+ }
for(tranche in tranches){
for(i in 1:n.scenarios){
filename <- paste0(paste(toupper(dealname), tranche, "CF", paste0("Scen", i), sep="-"), ".txt")
@@ -121,8 +127,12 @@ getdealcf <- function(dealnames, workdate=Sys.Date()){ if(is.null(cfdata[[dealname]])){
next
}
- cf <- cfdata[[dealname]][["COLLAT_REINVEST"]][,"Cashflow"] +
- cfdata[[dealname]][["COLLAT_INITIAL"]][,"Cashflow"]
+ if(length(tranches)==2){
+ cf <- cfdata[[dealname]][[tranches[1]]][,"Cashflow"] +
+ cfdata[[dealname]][[tranches[2]]][,"Cashflow"]
+ }else{
+ cf <- cfdata[[dealname]][[tranches]][,"Cashflow"]
+ }
cfdata[[dealname]]$wapbasis <- (mean(cf)- cfdata[[dealname]]$mv)/cfdata[[dealname]]$mv
cat(dealname, "\n")
program <- KLfit(t(cf)/1e8, rep(1/n.scenarios, n.scenarios),
@@ -245,13 +255,34 @@ for(dealname in names(cfdata)){ ## upload model data
for(cusip in names(cusipdata)){
- columns <- c("Cusip", "price", "wal", "duration", "updatedate")
- values <- c(cusip, cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
- cusipdata[[cusip]]$duration, strftime(workdate))
- colstring <- paste(columns[!is.na(values)], collapse=",")
- valstring <- paste(values[!is.na(values)], collapse="','")
- sqlstring <- sprintf(paste0("INSERT INTO et_cusip_model_numbers",
- "(%s) VALUES('%s')"),
- colstring, valstring)
+ sqlstring <- sprintf(paste0("SELECT updatedate from et_cusip_model_numbers",
+ " WHERE cusip='%s'"), cusip)
+ sqldata <- dbGetQuery(dbCon, sqlstring)
+ if(nrow(sqldata)&& (workdate %in% sqldata$updatedate)){
+ columns <- c("price", "wal", "duration")
+ values <- c(cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
+ cusipdata[[cusip]]$duration)
+ index <- which(!is.na(values))
+ setstring <- character(length(index))
+ for(i in 1:length(index)){
+ setstring[i] <- sprintf("%s = %s", columns[index[i]], values[index[i]])
+ }
+ setstring <- paste(setstring, collapse=",")
+ sqlstring <- sprintf(paste0("UPDATE et_cusip_model_numbers SET ",
+ "%s WHERE cusip='%s' and updatedate='%s'"),
+ setstring,
+ cusip,
+ strftime(workdate))
+ }else{
+ columns <- c("Cusip", "price", "wal", "duration", "updatedate")
+ values <- c(cusip, cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
+ cusipdata[[cusip]]$duration, strftime(workdate))
+ colstring <- paste(columns[!is.na(values)], collapse=",")
+ valstring <- paste(values[!is.na(values)], collapse="','")
+ sqlstring <- sprintf(paste0("INSERT INTO et_cusip_model_numbers",
+ "(%s) VALUES('%s')"),
+ colstring, valstring)
+ }
dbSendQuery(dbCon, sqlstring)
}
+
|
