aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/calibrate_tranches_BC.R')
-rw-r--r--R/calibrate_tranches_BC.R69
1 files changed, 18 insertions, 51 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index 71e66a6e..e18a48d7 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -1,64 +1,31 @@
## parse command line arguments
-args <- commandArgs(trailingOnly=TRUE)
-if(length(args)==0 || args[1]=="-h" || args[1]=="--help"){
- cat("usage: Rscript calibrate_tranches_BC.R [-u] indexname [tenor]\n")
- cat(" Rscript calibrate_tranches_BC.R [-u] -c configfile\n")
- cat("\n")
- cat("If -u flag is provided, it will update from the last run date\n")
- cat("tenor is 5yr by default\n")
- cat("\n")
- cat("Examples:\n")
- cat("\tRscript calibrate_tranches_BC.R hy19 7yr: will run hy19 7yr from the beginning\n")
- cat("\tRscript calibrate_tranches_BC.R -c conf.yaml: will run the list of indices provided in conf.yml\n")
- quit("no")
-}
-
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
+library(optparse)
+option_list <- list(
+ make_option(c("-u", "--update"), action="store_true", default=FALSE,
+ help="Update from the last run date [default %default]"),
+ make_option(c("-c", "--config"), metavar="config_file",
+ help="Runs the list of indices provided in CONFIG_FILE"),
+ make_option("--index", help="Index name we want to run"),
+ make_option("--tenor", default="5yr", help="Tenor we want to run [default %default]"))
+args <- parse_args(OptionParser(option_list=option_list))
## default values
-updateflag <- FALSE
-tenor <- "5yr"
-if(args[1]=="-u"){
- updateflag <- TRUE
- if(length(args) >=2 && args[2]=="-c"){
- if(length(args) < 3){
- stop("Please provide a config file")
- }else{
- library(yaml)
- runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[3]))
- }
- }else{
- if(length(args) >= 2){
- index.name <- args[2]
- }else{
- stop("Please provind an index name")
- }
- if(length(args) >= 3){
- tenor <- args[3]
- }
- }
-}else if(args[1]=="-c"){
- if(length(args) < 2){
- stop("Please provide a config file")
- }else{
- library(yaml)
- runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[2]))
+if(is.null(args$config)){
+ if(is.null(args$index)){
+ stop("Please provide an index name")
}
+ runs <- list(name=args$index, tenor=args$tenor)
}else{
- index.name <- args[1]
- if(length(args) >= 2){
- tenor <- args[2]
- }
+ library(yaml)
+ runs <- yaml.load_file(file.path(root.dir,"code", "etc", args$config))
}
-if(!exists("runs")){
- runs <- list(name=index.name, tenor=tenor)
-}
-options(stringsAsFactors = FALSE)
+options(stringsAsFactors = FALSE)
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
source(file.path(root.dir, "code", "R", "calibration.R"), chdir=TRUE)
@@ -71,7 +38,7 @@ for(i in seq_along(runs$name)){
tenor <- runs$tenor[i]
filename <- file.path(root.dir,"Tranche_data","Runs",
paste(index.name,tenor,"csv",sep="."))
- if(updateflag){##ghetto way of getting the last row of the file
+ if(args$update){##ghetto way of getting the last row of the file
runfile <- read.csv(filename)
begin.date <- as.Date(runfile[nrow(runfile),1])+1
}else{
@@ -123,7 +90,7 @@ for(i in seq_along(runs$name)){
paste0(paste(index.name, tenor, as.character(tradedate), sep="_"),".RData")))
## save risk numbers into the csv file
- if(!updateflag && j==1){
+ if(!args$update && j==1){
cat(csvheaders(index), "\n", file=filename)
}
cat(tocsv(index), "\n", file=filename, append=TRUE)