diff options
Diffstat (limited to 'R/load_cf.R')
| -rw-r--r-- | R/load_cf.R | 54 |
1 files changed, 27 insertions, 27 deletions
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",
|
