aboutsummaryrefslogtreecommitdiffstats
path: root/R/load_cf.R
blob: a0603a8a8820a945dedea39a57978e05449ccc85 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
library(RPostgreSQL)
library(RQuantLib)
if(.Platform$OS.type == "unix"){
  root.dir <- "/home/share/CorpCDOs"
}else{
  root.dir <- "//WDSENTINEL/share/CorpCDOs"
}

source(file.path(root.dir, "code", "R", "etdb.R"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "utils.R"))
source(file.path(root.dir, "code", "R", "cds_utils.R"))

workdate <- as.Date("2013-01-24")

MarkitData <- getMarkitIRData(as.Date(workdate))
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
L2m <- buildMarkitYC(MarkitData, dt = 1/6)
L3m <-  buildMarkitYC(MarkitData)
L6m <- buildMarkitYC(MarkitData, dt = 1/2)
L12m <- buildMarkitYC(MarkitData, dt = 1)
setEvaluationDate(as.Date(MarkitData$effectiveasof))

sanitize.column <- function(vec){
  vec <- gsub(",", "", vec)
  index <- grep("\\(", vec)
  for(l in index){
    vec[l] <- -as.numeric(substr(vec[l], 2, nchar(vec[l])-1))
  }
  return( as.numeric(vec) )
}

fields <-  c("Cashflow", "Principal", "Interest")
tranches <- c("COLLAT_REINVEST", "COLLAT_INITIAL")
n.scenarios <- 100
cfdata <- list()
dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Prices_", workdate)), "*COLLAT_INITIAL-CF-Scen1*")
dealnames <- sapply(strsplit(dealnames, "-"), function(x) x[1])
dealnames <- tolower(unique(dealnames))
flag <- FALSE

for(dealname in dealnames){
  cfdata[[dealname]] <- list()
  r <- matrix(0, n.scenarios, 3)
  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"
  cfdata[[dealname]]$mv <- mv
  cfdata[[dealname]]$currbal <- currbal
  for(tranche in tranches){
    for(i in 1:n.scenarios){
      filename <- paste0(paste(toupper(dealname), tranche, "CF", paste0("Scen", i), sep="-"), ".txt")
      data <- read.table(file.path(root.dir, "Scenarios", paste0("Prices_", workdate), filename),
                         sep="\t", header=T)
      data <- data[-(1:2),1:4]
      data$Date <- as.Date(data$Date, "%b %d, %Y")
      if(any(is.na(data$Date))){
        sprintf("file: %s is messed up", filename)
        flag <- TRUE
        break
      }
      data <- data[data$Date >= Sys.Date(),]
      DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, data$Date))
      pv <- c()
      for(field in fields){
        data[,field] <- sanitize.column(data[,field])
        pv <- c(pv, crossprod(DC$discounts, data[,field]))
      }
      r[i,] <- pv
    }
    if(flag){
      cfdata[[dealname]] <- NULL
      flag <- FALSE
      break
    }else{
      cfdata[[dealname]][[tranche]]<- r
    }
  }
}


r <- c()
for(dealname in dealnames){
  r <- rbind(r, c(cfdata[[dealname]]$mv, cfdata[[dealname]]$currbal, apply(cfdata[[dealname]]$COLLAT_REINVEST, 2, mean)[1], apply(cfdata[[dealname]]$COLLAT_INITIAL, 2, mean)[1]))
}
colnames(r) <- c("mv", "currbal", "Reinvest", "Initial")
rownames(r) <- dealnames

flag <- FALSE
cusipdata <- list()
for(cusip in cusips){
    r <- rep(0, n.scenarios)
    sqlstring <- sprintf("select curr_balance from cusip_universe where cusip = '%s'", cusip)
    curr_balance <- dbGetQuery(dbCon, sqlstring)$curr_balance
    cusipdata[[cusip]]$currbal <- curr_balance
    for(i in 1:n.scenarios){
        filename <- sprintf("%s-CF-Scen%s.txt", cusip, i)
        data <- read.table(file.path(root.dir, "Scenarios", paste0("Prices_", workdate), filename),
                           sep = "\t", header=T)
        data <- data[-(1:2), 1:4]
        data$Date <- as.Date(data$Date, "%b %d, %Y")
        if(any(is.na(data$Date))){
            sprintf("file: %s is messed up", filename)
            flag <- TRUE
            break
        }
        data <- data[data$Date >= Sys.Date(),]
        DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, data$Date))
        data[,"Cashflow"] <- sanitize.column(data[,"Cashflow"])
        pv <- crossprod(DC$discounts, data[, "Cashflow"])
        r[i] <- pv

        if(flag){
            cusipsdata[[cusip]] <- NULL
            flag <- FALSE
            break
        }else{
            cusipdata[[cusip]]$Cashflow <- r
        }
    }
}

prices <- c()
for(cusip in names(cusipdata)){
    prices <- c(prices, mean(cusipdata[[cusip]]$Cashflow)/cusipdata[[cusip]]$currbal)
}
i <- 1
for(cusip in names(cusipdata)){
    cat(cusip, prices[i], "\n")
    i <- i+1
}