diff options
| -rw-r--r-- | R/build_portfolios.R | 2 | ||||
| -rw-r--r-- | R/load_cf.R | 54 | ||||
| -rw-r--r-- | R/script_calibrate_tranches.R | 28 |
3 files changed, 42 insertions, 42 deletions
diff --git a/R/build_portfolios.R b/R/build_portfolios.R index acb61c17..34587f4d 100644 --- a/R/build_portfolios.R +++ b/R/build_portfolios.R @@ -31,7 +31,7 @@ if(length(args) >= 1){ workdate <- Sys.Date()
}
-calibration.date <- prevBusDay(workdate)
+calibration.date <- addBusDay(workdate, -1)
MarkitData <- getMarkitIRData(calibration.date)
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", calibration.date)), header=F)
diff --git a/R/load_cf.R b/R/load_cf.R index 35c792f2..14a4a3c5 100644 --- a/R/load_cf.R +++ b/R/load_cf.R @@ -13,9 +13,9 @@ if(.Platform$OS.type == "unix"){ }
if(length(args) >= 1){
- workdate <- as.Date(args[1])
+ tradedate <- as.Date(args[1])
}else{
- workdate <- Sys.Date()
+ tradedate <- Sys.Date()
}
source(file.path(root.dir, "code", "R", "etdb.R"))
@@ -26,7 +26,7 @@ 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", "index_definitions.R"))
-calibration.date <- prevBusDay(workdate)
+calibration.date <- addBusDay(tradedate, -1)
MarkitData <- getMarkitIRData(calibration.date)
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", calibration.date)), header=F)
@@ -46,8 +46,8 @@ sanitize.column <- function(vec){ return(as.numeric(vec))
}
-processzipfiles <- function(workdate=Sys.Date()){
- pricesdir <- file.path(root.dir, "Scenarios", paste0("Prices_", workdate))
+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)){
@@ -74,8 +74,8 @@ processzipfiles <- function(workdate=Sys.Date()){ return(list(dealnames=dealnames.hash, cusips=cusips.hash, zipfiles=zipfiles))
}
-getconfig <- function(dealname, workdate){
- configfile <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"),
+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))
@@ -84,15 +84,15 @@ getconfig <- function(dealname, workdate){ }
}
-getdealcf <- function(dealnames, zipfiles, workdate=Sys.Date()){
+getdealcf <- function(dealnames, zipfiles, tradedate=Sys.Date()){
cfdata <- list()
fields <- c("Cashflow", "Principal", "Interest")
n.scenarios <- 100
for(dealname in keys(dealnames)){
zip <- zipfiles[dealnames[[dealname]]]
- dealdata <- getdealdata(dealname, workdate)
+ dealdata <- getdealdata(dealname, tradedate)
alldates <- getdealschedule(dealdata, "1 month")
- config <- getconfig(dealname, workdate)
+ config <- getconfig(dealname, tradedate)
T <- ifelse(alldates>=L3m$params$tradeDate, yearFrac(L3m$params$tradeDate, alldates), 0)
DC <- DiscountCurve(L3m$params, L3m$tsQuotes, T)
@@ -121,7 +121,7 @@ getdealcf <- function(dealnames, zipfiles, workdate=Sys.Date()){ if("Error" %in% names(data)){
k(NULL)
}
- ## data <- fread(file.path(root.dir, "Scenarios", paste0("Prices_", workdate), filename),
+ ## data <- fread(file.path(root.dir, "Scenarios", paste0("Prices_", tradedate), filename),
## sep="\t", colClasses="character")
data <- data[-c(1,2),]
data[,Date:=as.Date(Date, "%b %d, %Y")]
@@ -157,7 +157,7 @@ getdealcf <- function(dealnames, zipfiles, workdate=Sys.Date()){ return( cfdata )
}
-getcusipcf <- function(params, cfdata, dist, workdate=Sys.Date()){
+getcusipcf <- function(params, cfdata, dist, tradedate=Sys.Date()){
cusipdata <- list()
cusips <- keys(params$cusips)
cusips <- cusips[dealnamefromcusip(cusips) %in% names(cfdata)]
@@ -170,8 +170,8 @@ getcusipcf <- function(params, cfdata, dist, workdate=Sys.Date()){ cusip <- cusips[i]
zip <- params$zipfiles[params$cusips[[cusip]]]
dealname <- dealnames[i]
- dealdata <- getdealdata(dealname, workdate)
- alldates <- getdealschedule(dealdata,"1 month", startdate=workdate, adjust=TRUE)
+ dealdata <- getdealdata(dealname, tradedate)
+ alldates <- getdealschedule(dealdata,"1 month", startdate=tradedate, 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,
@@ -180,7 +180,7 @@ getcusipcf <- function(params, cfdata, dist, workdate=Sys.Date()){ r <- matrix(0, n.scenarios, 5)
colnames(r) <- fields
sqlstring <- sprintf("select curr_balance, spread from historical_cusip_universe('%s', '%s')",
- cusip, workdate)
+ cusip, tradedate)
indicdata <- dbGetQuery(dbCon, sqlstring)
cusipdata[[cusip]] <- callCC(function(k){
for(j in 1:n.scenarios){
@@ -209,7 +209,7 @@ getcusipcf <- function(params, cfdata, dist, workdate=Sys.Date()){ duration = crossprod(cfdata[[dealname]]$weight, r[,"duration"]),
price = 100 * temp/indicdata$curr_balance,
delta = compute.delta(dist, cfdata[[dealname]]$weight,
- r[,"Cashflow"]/indicdata$curr_balance, workdate),
+ r[,"Cashflow"]/indicdata$curr_balance, tradedate),
fields=r))
})
cat("done", cusip, "\n")
@@ -218,11 +218,11 @@ getcusipcf <- function(params, cfdata, dist, workdate=Sys.Date()){ return(cusipdata)
}
-compute.delta <- function(indexdist, dealweight, cusip.pv, workdate=Sys.Date(), K1=0, K2=1){
- calibration.date <- prevBusDay(workdate)
+compute.delta <- function(indexdist, dealweight, cusip.pv, tradedate=Sys.Date(), K1=0, K2=1){
+ calibration.date <- addBusDay(tradedate)
## we assume the index is fully funded - need to be changed depending
## on how we fund the swaps (hence floating coupon instead of fixed)
- cs <- couponSchedule(nextIMMDate(workdate), hy21$maturity, "Q",
+ cs <- couponSchedule(nextIMMDate(tradedate), hy21$maturity, "Q",
"FLOAT", 0.05, 0, calibration.date)
nT <- dim(indexdist$L)[2]
Ngrid <- dim(indexdist$L)[1]
@@ -245,15 +245,15 @@ if(length(args)>=2){ cusips <- args[-1]
dealnames <- unique(dealnamefromcusip(cusips))
}else{
- params <- processzipfiles(workdate)
+ params <- processzipfiles(tradedate)
}
-cfdata <- getdealcf(params$dealnames, params$zipfiles, workdate)
+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, workdate)
-save.dir <- file.path(root.dir, "Scenarios", paste0("Prices_", workdate))
+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")
@@ -263,7 +263,7 @@ for(dealname in names(cfdata)){ "wapbasis = '%s' WHERE dealname= '%s' AND updatedate = '%s'"),
cfdata[[dealname]]$wapbasis*100,
dealname,
- strftime(workdate))
+ strftime(tradedate))
dbSendQuery(dbCon, sqlstring)
}
@@ -272,7 +272,7 @@ 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)&& (workdate %in% sqldata$updatedate)){
+ 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)
@@ -286,12 +286,12 @@ for(cusip in names(cusipdata)){ "%s WHERE cusip='%s' and updatedate='%s'"),
setstring,
cusip,
- strftime(workdate))
+ 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(workdate))
+ 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",
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R index 5dfe59e0..3f82052e 100644 --- a/R/script_calibrate_tranches.R +++ b/R/script_calibrate_tranches.R @@ -16,17 +16,17 @@ source(file.path(root.dir, "code", "R", "tranche_functions.R")) source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
-##figure out the workdate
+##figure out the tradedate
if(length(args) >= 1){
- workdate <- as.Date(args[1])
+ tradedate <- as.Date(args[1])
}else{
- workdate <- Sys.Date()
+ tradedate <- Sys.Date()
}
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
- sprintf("futures-%s.csv", workdate)), header=F)
+ sprintf("futures-%s.csv", tradedate)), header=F)
#retrieve yield curve data
-MarkitData <- getMarkitIRData(workdate)
+MarkitData <- getMarkitIRData(tradedate)
L1m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/12)
L2m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/6)
L3m <- buildMarkitYC(MarkitData, futurequotes[,2])
@@ -37,24 +37,24 @@ setCalendarContext("UnitedStates/GovernmentBond") ## calibrate HY21
## calibrate the single names curves
singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
- paste0("hy21_singlenames_", workdate, ".csv")))
+ paste0("hy21_singlenames_", tradedate, ".csv")))
nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
bps <- 1e-4
cdsdates <- as.Date(character(0))
for(tenor in paste0(1:5, "y")){
- cdsdates <- c(cdsdates, cdsMaturity(tenor, date=workdate+1))
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate+1))
}
hy21portfolio <- c()
for(i in 1:nrow(nondefaulted)){
SC <- new("creditcurve",
recovery=nondefaulted$recovery[i]/100,
- startdate=workdate,
+ startdate=tradedate,
issuer=as.character(nondefaulted$ticker[i]))
quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8]) /100,
running=rep(nondefaulted$running[i] * bps, 5))
- SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, workdate)
+ SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate)
hy21portfolio <- c(hy21portfolio, SC)
}
@@ -64,18 +64,18 @@ issuerweights <- rep(1/length(hy21portfolio), length(hy21portfolio)) K <- c(0, 0.15, 0.25, 0.35, 1)
Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
markit.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
- paste0("hy21_tranches_", workdate, ".csv")))
+ paste0("hy21_tranches_", tradedate, ".csv")))
tranche.upf <- markit.data$Mid
tranche.running <- c(0.05, 0.05, 0.05, 0.05)
# get the index ref
hy21$indexref <- markit.data$bidRefPrice[1]/100
-hy21portfolio.tweaked <- tweakcurves(hy21portfolio, hy21, workdate)
+hy21portfolio.tweaked <- tweakcurves(hy21portfolio, hy21, tradedate)
SurvProb <- SPmatrix(hy21portfolio.tweaked, hy21)
Ngrid <- 2 * nrow(nondefaulted) + 1
recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
-cs <- couponSchedule(nextIMMDate(workdate), hy21$maturity,"Q", "FIXED", 0.05, 0)
+cs <- couponSchedule(nextIMMDate(tradedate), hy21$maturity,"Q", "FIXED", 0.05, 0)
##calibrate by modifying the factor distribution
bottomup <- 1:3
@@ -151,9 +151,9 @@ dist <- list(L=L, R=R) write.table(data.frame(Z=Z, w=w.mod),
file=file.path(root.dir, "Scenarios", "Calibration",
- paste0("calibration-", workdate, ".csv")),
+ paste0("calibration-", tradedate, ".csv")),
col.names=T, row.names=F, sep=",")
save(singlenames.data, hy21, tranche.upf, dist,
file = file.path(root.dir, "Scenarios", "Calibration",
- paste0("marketdata-", workdate, ".RData")), compress="xz")
+ paste0("marketdata-", tradedate, ".RData")), compress="xz")
|
