diff options
Diffstat (limited to 'R/thetas-correct.R')
| -rw-r--r-- | R/thetas-correct.R | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/R/thetas-correct.R b/R/thetas-correct.R new file mode 100644 index 00000000..dbc738d3 --- /dev/null +++ b/R/thetas-correct.R @@ -0,0 +1,64 @@ +library(dplyr) + +code.dir <- "~/projects/code/" +source(file.path(code.dir, "R", "yieldcurve.R")) +source(file.path(code.dir, "R", "calibration.R"), chdir=TRUE) +source(file.path(code.dir, "R", "serenitasdb.R")) +source(file.path(code.dir, "R", "creditIndex.R")) + +data.src <- src_postgres("serenitasdb", "debian", 5432, "serenitas_user") + +index.closeprice <- function(index=c("IG", "HY", "EU", "XO"), series) { + require(lazyeval, quietly=T) + index <- match.arg(index) + index.quotes <- tbl(data.src, "index_quotes") + df <- index.quotes %>% filter_(lazyeval::interp(~series == s, s=series)) %>% + filter_(interp(~index == i, i=index)) %>% + arrange(date) + df <- df %>% select(date, tenor, closeprice) %>% + rename(price=closeprice) %>% + mutate(price=price/100) %>% collect() + return( df ) +} + +get.maturities <- function(index=c("IG", "HY", "EU", "XO"), series) { + index <- match.arg(index) + df <- tbl(data.src, "index_maturity") + df <- df %>% filter_(lazyeval::interp(~series == s, s=series)) %>% + filter_(interp(~index == i, i=index)) %>% + select(tenor, maturity, coupon) %>% + rename(spread=coupon) %>% mutate(spread=spread*1e-4) %>% + arrange(maturity) %>% collect() + return( df ) +} + +maturities <- get.maturities("IG", 26) +quotes <- index.closeprice("IG", 26) +index <- creditIndex("IG27", "10yr") +sqlstr <- paste("UPDATE index_quotes SET duration2=$1, theta2=$2", + "WHERE date=$3 AND index=$4 AND series=$5 AND tenor=$6") + +for(i in seq_along(quotes$date)) { + tradedate <- quotes$date[i] + exportYC(tradedate, "USD") + index <- set.index.desc(index, tradedate) + index <- set.singlenamesdata(index, tradedate) + index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), + index$maturity, "Q", "FIXED", 1, + 0, tradedate, IMMDate(tradedate, "prev")) + index$quotes <- quotes %>% filter(date == tradedate) %>% + left_join(maturities) %>% arrange(maturity) + tweak <- tweakcurves(index, tradedate) + index$portfolio <- NULL + index <- c(index, tweak) + theta <- numeric(length(index$maturities)) + duration <- numeric(length(index$maturities)) + for(j in seq_along(maturities$maturity)) { + maturity <- maturities$maturity[j] + theta <- index %>% indextheta(tradedate=tradedate, maturity=maturity) + duration <- index %>% indexduration(tradedate=tradedate, maturity=maturity) + dbSendQuery(serenitasdb, sqlstr, + params = list(duration, theta, tradedate, + "IG", 26, maturities$tenor[j])) + } +} |
