aboutsummaryrefslogtreecommitdiffstats
path: root/R/tranches_RV_BC.R
blob: 5c81e357fc1fc04d61a2b58dcb88518243fd3e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
library(logging)
basicConfig()
removeHandler('basic.stdout')
addHandler(writeToFile, file=file.path(Sys.getenv("SERENITAS_LOG_DIR"), "tranches_RV_BC.log"))
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(c("-i1", "--index1"), help="Reference index name"),
    make_option(c("-i2", "--index2"), help="Mapped index name"),
    make_option(c("-t1", "--tenor1"), default="5yr",
                help="Tenor of index1 [default %default]"),
    make_option(c("-t2", "--tenor2"), default="5yr",
                help="Tenor of index2 [default %default]"),
    make_option("--until", default=Sys.Date()-1, type="character",
                help="last day to run [default %default]"))

args <- parse_args(OptionParser(option_list=option_list,
                                description=
                                "This script prices index2 using the skew from index1."))
if(is.null(args$config)){
    if(is.null(args$index1) || is.null(args$index2)){
        stop("Please provide both index1 and index2")
    }
    runs <- list(mappings=list(c(args$index1, args$tenor1, args$index2, args$tenor2)))
}else{
    library(yaml)
    runs <- yaml.load_file(file.path(Sys.getenv("SERENITAS_CONFIG_DIR"), args$config))
}

source("optimization.R")
source("calibration.R")
source("serenitasdb.R")
source("creditIndex.R")
source("tranche_functions.R")

for(r in runs$mappings){
    index.name1 <- r[1]
    tenor1 <- r[2]
    index.name2 <- r[3]
    tenor2 <- r[4]
    filename <- file.path(root.dir,"Tranche_data","Runs",
                          paste0(paste(index.name2, tenor2, "using", index.name1, tenor1),".csv"))
    if(!file.exists(filename)){
        args$update <- FALSE
    }
    if(args$update){
        runfile <- read.csv(filename)
        begin.date <- as.Date(runfile[nrow(runfile), 1])+1
    }else{
        begin.date <- switch(index.name1,
                             hy23 = as.Date("2014-10-16"),
                             hy21 = as.Date("2013-10-04"),
                             hy19 = as.Date("2013-10-04"),
                             ig21 = as.Date("2013-09-26"),
                             ig23 = as.Date("2014-10-14"),
                             ig25 = as.Date("2015-09-22"),
                             ig27 = as.Date("2015-09-27"),
                             hy25 = as.Date("2015-10-01"),
                             hy27 = as.Date("2016-10-04"),
                             xo24 = as.Date("2015-09-28"),
                             eu24 = as.Date("2015-09-23"))
    }
    if(begin.date > as.Date(args$until)){
        next
    }
    alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
    cal <- Calendar$new("UnitedStates/GovernmentBond")
    bus.dates <- alldates[cal$isBusinessDay(alldates)]
    addheaders <- TRUE
    for(j in seq_along(bus.dates)){
        tradedate <- bus.dates[j]
        index1 <- load.index(index.name1, tenor1, tradedate)
        index2 <- load.index(index.name2, tenor2, tradedate)
        if(any(c(is.null(index1), is.null(index2)))){
            loginfo(paste("skipping pair", index.name1, tenor1, index.name2, tenor2,
                          "for date", as.character(tradedate)))
            next
        }
        accrued2 <- cdsAccrued(tradedate, index2$tranches$running)
        mappedpv <- list()
        for(method in c("ATM", "TLP", "PM")){
            index2$rho <- adjust.skew(index1, index2, method)
            if(tolower(substr(index2$name, 1, 2)) %in% c("ig", "xo", "eu")){
                mappedpv[[method]] <- BCtranche.pv(index2, protection=TRUE)$bp + accrued2
            }else{
                mappedpv[[method]] <- BCtranche.pv(index2)$bp - accrued2
            }
        }
        row <- c(as.character(tradedate), index2$tranches$upfront, unlist(mappedpv)*100)
        if(addheaders && !args$update){
            headers <- c("date", paste(index2$name, row.names(index2$tranches), "Quotes"),
                         paste(index2$name, row.names(index2$tranches), "ATM PV"),
                         paste(index2$name, row.names(index2$tranches), "TLP PV"),
                         paste(index2$name, row.names(index2$tranches), "PM PV"))
            cat(paste(headers, collapse=","), "\n", file=filename)
        }
        addheaders <- FALSE
        cat(paste(row, collapse=","), sep="\n", file=filename, append=TRUE)
    }

}