aboutsummaryrefslogtreecommitdiffstats
path: root/R/load_cf.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/load_cf.R')
-rw-r--r--R/load_cf.R48
1 files changed, 25 insertions, 23 deletions
diff --git a/R/load_cf.R b/R/load_cf.R
index 008e11fa..4522142e 100644
--- a/R/load_cf.R
+++ b/R/load_cf.R
@@ -1,8 +1,8 @@
library(RQuantLib)
library(yaml)
library(hash)
+library(readr)
library(data.table)
-options(stringsAsFactors = FALSE)
args <- commandArgs(trailingOnly=TRUE)
##options(warn=2)
@@ -112,29 +112,34 @@ getdealcf <- function(dealnames, zipfiles, tradedate=Sys.Date()){
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")
+ 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)
+ ct <- list(col_datetime("%b %d, %Y"),
+ col_character(),
+ col_character(),
+ col_character(), col_skip(), col_skip(),
+ col_skip(), col_skip(), col_skip(),
+ col_skip(), col_skip(), col_skip())
+ data <- read_tsv(conn, col_types= ct)
if(nrow(data)<2){
cat(dealname, i, tranche, "\n")
break
flag <- FALSE
}
- data <- tryCatch({
- data[-c(1,2),][,`:=`(Date=as.Date(Date, "%b %d, %Y"),
- Cashflow = sanitize.column(Cashflow),
- Principal = sanitize.column(Principal),
- Interest = sanitize.column(Interest))]
- },warning=function(w){
- message("handling warning: ", conditionMessage(w))
- NULL})
+ data <- data[-c(1,2),]
if(is.null(data)){
break
flag <- FALSE
}
- setkey(data, "Date")
+ data <- data.table(data, key="Date")
+ data <- tryCatch({
+ data[,`:=`(Cashflow = sanitize.column(Cashflow),
+ Principal = sanitize.column(Principal),
+ Interest = sanitize.column(Interest))]
+ }, warning=function(w){
+ message("handling warning: ", conditionMessage(w))
+ NULL})
r[i,] <- as.numeric(df[data, roll=TRUE][,list(sum(Cashflow*Discounts),
sum(Principal*Discounts),
sum(Interest*Discounts))])
@@ -201,22 +206,19 @@ getcusipcf <- function(params, cfdata, dist, tradedate=Sys.Date()){
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 <- read_tsv(conn, col_types=list(col_datetime("%b %d, %Y"),
+ col_numeric(),
+ col_numeric(),
+ col_numeric(), col_numeric(), col_numeric(),
+ col_skip(), col_skip()))
if(nrow(data)<=2){
cat(cusip, "Scenario", j, "\n")
flag <- FALSE
break
}
data <- data[-c(1,2),]
- 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 <- data.table(data, key="Date")
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),