diff options
Diffstat (limited to 'R/backtest.R')
| -rw-r--r-- | R/backtest.R | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/R/backtest.R b/R/backtest.R index 493ffbc5..8ef74b7c 100644 --- a/R/backtest.R +++ b/R/backtest.R @@ -1,14 +1,19 @@ -## parse command line arguments +library(ggplot2) +library(lubridate) +library(doParallel) + if(.Platform$OS.type == "unix"){ root.dir <- "/home/share/CorpCDOs" }else{ root.dir <- "//WDSENTINEL/share/CorpCDOs" } -library(ggplot2) -library(lubridate) -library(doParallel) -registerDoParallel(8) +hostname <- system("hostname", intern=TRUE) +if(hostname=="debian"){ + registerDoParallel(4) +}else{ + registerDoParallel(8) +} source(file.path(root.dir, "code", "R", "serenitasdb.R")) source(file.path(root.dir, "code", "R", "cds_functions_generic.R")) @@ -17,7 +22,8 @@ source(file.path(root.dir, "code", "R", "yieldcurve.R")) get.indexquotes <- function(index, series, tenors=c("3yr", "5yr", "7yr")){ arraystring1 <- paste0("Array[''", paste(tenors, collapse = "'', ''"), "'']::tenor[]") arraystring2 <- paste0('"', paste(tenors, collapse='" float, "'), '" float') - sqlstr <- paste("select * from crosstab('select date, tenor, closeprice from index_quotes where index=''%s''", + sqlstr <- paste("select * from crosstab('select date, tenor, closeprice from index_quotes", + "where index=''%s''", "and series=%s order by date, tenor', 'select unnest(%s)')", "AS ct(date date, %s)") stmt <- sprintf(sqlstr, index, series, arraystring1, arraystring2) @@ -90,10 +96,11 @@ recov <- 0.3 sqlstr <- paste("UPDATE index_quotes set duration=%s, theta=%s where date='%s' and index='%s'", "and series=%s and tenor='%s'") -for(series in c(10, 11, 13, 15, 17, 19, 21, 23)){ +for(series in c(9, 10, 11, 13, 15, 17, 19, 21, 23)){ indexquotes <- get.indexquotes(index, series) maturities <- get.indexmaturity(index, series) maturities <- maturities[maturities$tenor %in% tenors,] + indexquotes <- indexquotes[indexquotes$date<=maturities$maturity[3],] durations <- matrix(0, nrow(indexquotes), length(tenors)) thetas <- matrix(0, nrow(indexquotes), length(tenors)) maturity <- maturities[nrow(maturities), "maturity"] @@ -129,6 +136,3 @@ for(series in c(10, 11, 13, 15, 17, 19, 21, 23)){ geom_line(aes(y=`7yr`, colour="7yr"))+ylab("theta")+labs(colour="tenor") ggsave(filename=paste0("HY", series, " thetas.png")) } - -alldates <- seq(begin.date, as.Date(Sys.Date()), by="1 day") -bus.dates <- alldates[isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)] |
