diff options
Diffstat (limited to 'R/load_cf.R')
| -rw-r--r-- | R/load_cf.R | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/R/load_cf.R b/R/load_cf.R index 3c62e75e..f58a6016 100644 --- a/R/load_cf.R +++ b/R/load_cf.R @@ -1,18 +1,27 @@ library(RPostgreSQL)
library(RQuantLib)
+
+args <- commandArgs(trailingOnly=TRUE)
+
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
+if(length(args) >= 1){
+ workdate <- as.Date(args[1])
+}else{
+ workdate <- 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"))
-workdate <- as.Date("2013-02-11")
MarkitData <- getMarkitIRData(workdate)
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
@@ -85,7 +94,8 @@ getdealcf <- function(dealnames, workdate=Sys.Date()){ }
futuredates <- data$Date[data$Date>=L3m$params$tradeDate]
pastdates <- data$Date[data$Date<L3m$params$tradeDate]
- if(i==1||length(futuredates)>length(DC$times)){
+ if(length(futuredates)>0 &&
+ (i==1||length(futuredates)>length(DC$times))){
DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, futuredates))
}
pv <- c()
@@ -245,10 +255,30 @@ getcusipcf <- function(cusips, cfdata, dist, workdate=Sys.Date()){ return(cusipdata)
}
+compute.delta <- function(indexdist, dealweight, cusip.pv, workdate=Sys.Date()){
+ indexpv <- c()
+ cs <- couponSchedule(nextIMMDate(workdate), as.Date("2017-12-20"), "Q", "FLOAT", 0, 0.05)
+ 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)
+ }
+
+ indexpv <- c()
+ for(i in 1:length(dealweight)){
+ indexpv <- c(indexpv, funded.tranche.pv(scenariosl[i,], scenariosr[i,], cs, 0, 1))
+ }
+ model <- lm(cusip.pv~0+indexpv, weights=dealweight)
+ return(model$coef)
+}
+
cfdata <- getdealcf(dealnames, workdate)
availablecusips <- cusips[dealnamefromcusip(cusips) %in% names(cfdata)]
load(file.path(root.dir, "Scenarios", "Calibration", sprintf("marketdata-%s.RData", workdate)))
-cusipdata <- getcusipcf(availablecusips[1:3], cfdata, dist, workdate)
+cusipdata <- getcusipcf(availablecusips, cfdata, dist, workdate)
## upload wapbasis
for(dealname in names(cfdata)){
sqlstring <- sprintf(paste0("UPDATE et_deal_model_numbers SET ",
@@ -265,9 +295,9 @@ for(cusip in names(cusipdata)){ " WHERE cusip='%s'"), cusip)
sqldata <- dbGetQuery(dbCon, sqlstring)
if(nrow(sqldata)&& (workdate %in% sqldata$updatedate)){
- columns <- c("price", "wal", "duration")
+ columns <- c("price", "wal", "duration", "delta")
values <- c(cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
- cusipdata[[cusip]]$duration)
+ cusipdata[[cusip]]$duration, cusipdata[[cusip]]$delta)
index <- which(!is.na(values))
setstring <- character(length(index))
for(i in 1:length(index)){
@@ -280,9 +310,10 @@ for(cusip in names(cusipdata)){ cusip,
strftime(workdate))
}else{
- columns <- c("Cusip", "price", "wal", "duration", "updatedate")
+ columns <- c("Cusip", "price", "wal", "duration", "delta", "updatedate")
values <- c(cusip, cusipdata[[cusip]]$price, cusipdata[[cusip]]$wal,
- cusipdata[[cusip]]$duration, strftime(workdate))
+ cusipdata[[cusip]]$duration, cusipdata[[cusip]]$delta,
+ 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",
@@ -291,23 +322,3 @@ for(cusip in names(cusipdata)){ }
dbSendQuery(dbCon, sqlstring)
}
-
-compute.delta <- function(indexdist, dealweight, cusip.pv, workdate=Sys.Date()){
- indexpv <- c()
- cs <- couponSchedule(nextIMMDate(workdate), as.Date("2017-12-20"), "Q", "FLOAT", 0, 0.05)
- 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)
- }
-
- indexpv <- c()
- for(i in 1:length(dealweight)){
- indexpv <- c(indexpv, funded.tranche.pv(scenariosl[i,], scenariosr[i,], cs, 0, 1))
- }
- model <- lm(cusip.pv~0+indexpv, weights=dealweight)
- return(model$coef)
-}
|
