diff options
| -rw-r--r-- | R/calibration.R | 16 | ||||
| -rw-r--r-- | R/mlpdb.R | 17 |
2 files changed, 23 insertions, 10 deletions
diff --git a/R/calibration.R b/R/calibration.R index 86ca5065..674e1817 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -44,9 +44,8 @@ set.singlenamesdata <- function(index, tradedate){ } set.tranchedata <- function(index, tradedate){ - index$tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration", - paste0(index$name, "_tranches_", tradedate, ".csv")), header=TRUE) - index$indexref <- index$tranche.data$bidRefPrice[1]/100 + temp <- get.tranchequotes(index$name, tradedate) + index$indexref <- temp$refbasketprice[1]/100 index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate, IMMDate(tradedate, "prev")) index$portfolio <- tweakcurves(index, tradedate)$portfolio @@ -55,14 +54,11 @@ set.tranchedata <- function(index, tradedate){ if(nrow(negprob)>0){ stop(paste(index$portfolio[[negprob[1,]]]@issuer, "has negative probability, check single names data")) } - K <- c(0, 0.15, 0.25, 0.35, 1) + K <- c(0, temp$detach/100) index$K <- adjust.attachments(K, index$loss, index$factor) - index$tranche.upf <- index$tranche.data$Mid - if("Coupon" %in% names(index$tranche.data)){ - index$tranche.running <- index$tranche.data$Coupon - }else{ - index$tranche.running <- rep(0.05, nrow(index$tranche.data)) - } + index$tranche.upf <- temp$upfront + index$tranche.running <- temp$running*1e-4 + ## convert the quotes ## - we convert to protection terms x->1-x/100 ## - we remove accrued x->x-acc @@ -39,3 +39,20 @@ get.indexquotes <- function(indexname, date=Sys.Date()){ recovery_curve = arr.convert(r$recovery_curve)) return( quotes ) } + +indexsplit <- function(indexname){ + r <- regexpr("(\\D*)(\\d*)", indexname, perl=T) + cs <- attr(r, "capture.start") + cl <- attr(r, "capture.length") + index <- substr(indexname, cs[1], cs[1]+cl[1]-1) + series <- substr(indexname, cs[2], cs[2]+cl[2]-1) + return(list(index=toupper(index), series=series)) +} + +get.tranchequotes <- function(indexname, date=Sys.Date()){ + sqlstr <- paste("select * from quotes where index='%s'", + "and series=%s and quotedate='%s' order by attach asc") + temp <- indexsplit(indexname) + r <- dbGetQuery(mlpdbCon, sprintf(sqlstr, temp$index, temp$series, date)) + return( r ) +} |
