aboutsummaryrefslogtreecommitdiffstats
path: root/R/load_cf.R
blob: 4a1b1823c10537211062e1415f3d5f461344ce39 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
library(RPostgreSQL)
library(RQuantLib)
library(yaml)
library(hash)
library(data.table)
options(stringsAsFactors = FALSE)
args <- commandArgs(trailingOnly=TRUE)

if(.Platform$OS.type == "unix"){
  root.dir <- "/home/share/CorpCDOs"
}else{
  root.dir <- "//WDSENTINEL/share/CorpCDOs"
}

if(length(args) >= 1){
  tradedate <- as.Date(args[1])
}else{
  tradedate <- Sys.Date()
}

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", "cds_utils.R"))
source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
source(file.path(root.dir, "code", "R", "interpweights.R"))
source(file.path(root.dir, "code", "R", "serenitasdb.R"))
source(file.path(root.dir, "code", "R", "creditIndex.R"))
source(file.path(root.dir, "code", "R", "tranche_functions.R"))
index <- creditIndex("hy23")
index <- set.index.desc(index, tradedate)

calibration.date <- addBusDay(tradedate, -1)
exportYC(calibration.date)
cs <- couponSchedule(IMMDate(calibration.date, noadj=TRUE), index$maturity, "Q", "FLOAT", 0, 0.05)

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

processzipfiles <- function(tradedate=Sys.Date()){
    pricesdir <- file.path(root.dir, "Scenarios", paste0("Prices_", tradedate))
    zipfiles <- file.path(pricesdir, list.files(pricesdir, "*.zip"))
    zipfiles <- zipfiles[order(file.info(zipfiles)$ctime)]
    for(n in seq_along(zipfiles)){
        zip <- zipfiles[n]
        allfiles <- unzip(zip, list=TRUE)$Name
        dealnames <- grep("COLLAT.*Scen100", allfiles, value=TRUE)
        dealnames <- unique(unlist(lapply(strsplit(dealnames, "-"), function(x)x[1])))
        allfiles <- unique(unlist(lapply(strsplit(allfiles, "-"), function(x)x[1])))
        allfiles <- allfiles[!(allfiles=="Total")]
        cusips <- setdiff(allfiles, dealnames)
        dealnames <- tolower(dealnames)
        if(n==1){
            dealnames.hash <- hash(dealnames, 1)
            cusips.hash <- hash(cusips, 1)
        }else{
            for( c in cusips){
                cusips.hash[c] <- n
            }
            for(d in dealnames){
                dealnames.hash[d] <- n
            }
        }
    }
    return(list(dealnames=dealnames.hash, cusips=cusips.hash, zipfiles=zipfiles))
}

getconfig <- function(dealname, tradedate){
    configfile <- file.path(root.dir, "Scenarios", paste("Intex curves", tradedate, sep="_"),
                            "csv", paste0(dealname, ".config"))
    if(file.exists(configfile)){
        return(yaml.load_file(configfile))
    }else{
        return(list(reinvflag=TRUE))
    }
}

getdealcf <- function(dealnames, zipfiles, tradedate=Sys.Date()){
    cfdata <- list()
    fields <-  c("Cashflow", "Principal", "Interest")
    n.scenarios <- 100
    indextodealnames <- invert(dealnames)
    for(k in keys(indextodealnames)){
        zip <- zipfiles[as.numeric(k)]
        tmp <- tempfile(tmpdir="/tmp")
        file.copy(zip, tmp)
        zip <- tmp
        for(dealname in indextodealnames[[k]]){
            dealdata <- getdealdata(dealname, tradedate)
            alldates <- getdealschedule(dealdata, "1 month")
            config <- getconfig(dealname, tradedate)

            T <- ifelse(alldates>=L3m$params$tradeDate, yearFrac(L3m$params$tradeDate, alldates), 0)
            DC <- DiscountCurve(L3m$params, L3m$tsQuotes, T)
            df <- data.table(Date=alldates,
                             Discounts=DC$discounts,
                             T=T, key="Date")
            cfdata[[dealname]] <- list(mv = dealdata$mv, currbal = dealdata$"Curr Collat Bal")

            if(is.na(dealdata$reinv_end_date)||!config$reinvflag){
                tranches <- "COLLAT"
            }else{
                tranches <- c("COLLAT_INITIAL", "COLLAT_REINVEST")
            }
            for(tranche in tranches){
                cfdata[[dealname]][[tranche]] <- callCC(function(k){
                    r <- matrix(0, n.scenarios, 3)
                    colnames(r) <- fields
                    for(i in 1:n.scenarios){
                        filename <- paste0(
                            paste(toupper(dealname), tranche, "CF", paste0("Scen", i), sep="-"), ".txt")
                        conn <- unz(zip, filename)
                        data <- read.table(conn, sep="\t", header=TRUE, colClasses="character", comment.char="")
                        data <- data.table(data)
                        if("Error" %in% names(data)){
                            k(NULL)
                        }
                        ## data <- fread(file.path(root.dir, "Scenarios", paste0("Prices_", tradedate), filename),
                        ##               sep="\t", colClasses="character")
                        data <- data[-c(1,2),][,`:=`(Date=as.Date(Date, "%b %d, %Y"),
                                                     Cashflow = sanitize.column(Cashflow),
                                                     Principal = sanitize.column(Principal),
                                                     Interest = sanitize.column(Interest))]
                        setkey(data, "Date")
                        r[i,] <- as.numeric(df[data, roll=TRUE][,list(sum(Cashflow*Discounts),
                                                         sum(Principal*Discounts),
                                                         sum(Interest*Discounts))])
                    }
                    k(r)
                })
            }
            if(length(cfdata[[dealname]])<2+length(tranches)){##meaning we existed early in the above loop
                cfdata[[dealname]] <- NULL
                next
            }
            cf <- rep(0,n.scenarios)
            for(tranche in tranches){
                cf <- cf+cfdata[[dealname]][[tranche]][,"Cashflow"]
            }
            cf <- cf-min(dealdata$"Principal Bal", 0)
            cfdata[[dealname]]$price <- cf/dealdata$mv
            cfdata[[dealname]]$wapbasis <- (mean(cf)- dealdata$mv)/dealdata$mv
            program <- tryCatch(KLfit(t(cf)/1e8, rep(1/n.scenarios, n.scenarios),
                                      dealdata$mv/1e8), error=function(e) e)
            if(inherits(program, "error")){
                cat("error computing the weights for deal:", dealname,"\n")
                cfdata[[dealname]] <- NULL
                next
            }else{
                cfdata[[dealname]]$weight <- program$weight
                cat(dealname, "\n")
            }
        }
        unlink(tmp)
    }
    return( cfdata )
}

getcusipcf <- function(params, cfdata, dist, tradedate=Sys.Date()){
    cusipdata <- list()
    cusips <- keys(params$cusips)
    dealnames <- dealnamefromcusip(cusips)
    cusips <- cusips[dealnames %in% names(cfdata)]
    dealnames <- dealnames[dealnames %in% names(cfdata)]
    n.scenarios <- 100
    intexfields <-  c("Cashflow", "Principal", "Interest", "Balance",
                      "Accum Interest Shortfall")
    fields <-  c("Cashflow", "Principal", "Interest", "wal", "duration")
    for(i in 1:length(cusips)){
        cusip <- cusips[i]
        zip <- params$zipfiles[params$cusips[[cusip]]]
        dealname <- dealnames[i]
        dealdata <- getdealdata(dealname, tradedate)
        alldates <- getdealschedule(dealdata, "1 month", adjust=TRUE)
        T <- ifelse(alldates>=L3m$params$tradeDate, yearFrac(L3m$params$tradeDate, alldates), 0)
        DC <- DiscountCurve(L3m$params, L3m$tsQuotes, T)
        df <- data.table(Date=alldates,
                         Discounts=DC$discounts,
                         T=T, key="Date")
        r <- matrix(0, n.scenarios, 5)
        colnames(r) <- fields
        sqlstring <- sprintf("select curr_balance, spread from historical_cusip_universe('%s', '%s')",
                             cusip, tradedate)
        indicdata <- dbGetQuery(dbCon, sqlstring)
        cusipdata[[cusip]] <- callCC(function(k){
            for(j in 1:n.scenarios){
                filename <- sprintf("%s-CF-Scen%s.txt", cusip, j)
                conn <- unz(zip, filename)
                data <- data.table(read.table(conn, sep="\t", header=TRUE, colClasses="character",
                                              check.names=FALSE))
                data <- data[-c(1,2),]
                if("Error" %in% names(data)){
                    k(NULL)
                }
                data[,`:=`(Date=as.Date(Date, "%b %d, %Y"),
                           Cashflow=sanitize.column(Cashflow),
                           Principal=sanitize.column(Principal),
                           Interest=sanitize.column(Interest),
                           Balance=sanitize.column(Balance),
                           `Accum Interest Shortfall`=sanitize.column(`Accum Interest Shortfall`))]
                data[,Balance:=pmax(Balance-`Accum Interest Shortfall`, 0)]
                setkey(data, "Date")
                r[j,] <- as.numeric(df[data, roll=TRUE][,list(Cashflow=temp <- sum(Cashflow*Discounts),
                                                   Principal=sum(Principal*Discounts),
                                                   Interest=sum(Interest*Discounts),
                                                   wal=sum(-diff(Balance)*T[-1])/indicdata$curr_balance,
                                                   duration=if(temp==0) 0 else sum(Cashflow * Discounts * T)/temp)])
            }
            k(list(currbal=indicdata$curr_balance,
                   spread=indicdata$spread,
                   Cashflow=temp <- crossprod(cfdata[[dealname]]$weight, r[,"Cashflow"]),
                   wal = crossprod(cfdata[[dealname]]$weight, r[,"wal"]),
                   duration = crossprod(cfdata[[dealname]]$weight, r[,"duration"]),
                   price = 100 * temp/indicdata$curr_balance,
                   delta = compute.delta(dist, cfdata[[dealname]], r[,"Cashflow"]/indicdata$curr_balance),
                   fields=r))
        })
        cat("done", cusip, "\n")
    }

    return(cusipdata)
}

compute.delta <- function(indexdist, dealdata, cusip.pv, K1=0, K2=1){
    dealweight <- dealdata$weight
    dealprice <- dealdata$price
    nT <- dim(indexdist$L)[2]
    Ngrid <- dim(indexdist$L)[1]
    scenariosl <- matrix(0, length(dealweight), nT)
    scenariosr <- matrix(0, length(dealweight), nT)
    for(t in 1:nT){
        scenariosl[,t] <- interpvalues(indexdist$L[,t], seq(0, 1, length=Ngrid), dealweight)
        scenariosr[,t] <- interpvalues(indexdist$R[,t], seq(0, 1, length=Ngrid), dealweight)
    }
    ## we assume the index is fully funded - need to be changed depending
    ## on how we fund the swaps (hence floating coupon instead of fixed)
    indexpv <- c()
    for(i in 1:length(dealweight)){
        indexpv <- c(indexpv, funded.tranche.pv(scenariosl[i,], scenariosr[i,], cs, K1, K2, TRUE))
    }
    ## model1 <- lm(cusip.pv~dealprice, weights=dealweight)
    ## model2 <- lm(dealprice~indexpv, weights=dealweight)
    ## return(model1$coef[2]/model2$coef[2])
    model <- lm(cusip.pv~indexpv, weights=dealweight)
    return( model$coef[2] )
}

if(length(args)>=2){
    cusips <- args[-1]
    dealnames <- unique(dealnamefromcusip(cusips))
}else{
    params <- processzipfiles(tradedate)
}

cfdata <- getdealcf(params$dealnames, params$zipfiles, tradedate)

## load dist into the environment
load(file.path(root.dir, "Scenarios", "Calibration", sprintf("marketdata-%s.RData", calibration.date)))
cusipdata <- getcusipcf(params, cfdata, dist, tradedate)
save.dir <- file.path(root.dir, "Scenarios", paste0("Prices_", tradedate))
save(cusipdata, cfdata, file=file.path(save.dir, "cashflows.RData"),
     compress="xz")

## upload wapbasis
for(dealname in names(cfdata)){
    sqlstring <- sprintf(paste0("UPDATE et_deal_model_numbers SET ",
                                "wapbasis = '%s' WHERE dealname= '%s' AND updatedate = '%s'"),
                         cfdata[[dealname]]$wapbasis*100,
                         dealname,
                         strftime(tradedate))
    dbSendQuery(dbCon, sqlstring)
}

## upload model data
for(cusip in names(cusipdata)){
    sqlstring <- sprintf(paste0("SELECT updatedate from et_cusip_model_numbers",
                                " WHERE cusip='%s'"), cusip)
    sqldata <- dbGetQuery(dbCon, sqlstring)
    if(nrow(sqldata)&& (tradedate %in% sqldata$updatedate)){
        columns <- c("price", "wal", "duration", "delta")
        values <- c(cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
                    cusipdata[[cusip]]$duration, cusipdata[[cusip]]$delta)
        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(tradedate))
    }else{
        columns <- c("Cusip", "price", "wal", "duration", "delta", "updatedate")
        values <- c(cusip, cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
                    cusipdata[[cusip]]$duration, cusipdata[[cusip]]$delta,
                    strftime(tradedate))
        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)
}