require(RQuantLib) if(.Platform$OS.type == "unix"){ data.dir <- "/home/share/CorpCDOs/data" }else{ data.dir <- "//WDSENTINEL/share/CorpCDOs/data" } getMarkitIRData <- function(date=Sys.Date(), currency=c("USD", "EUR")) { ## downloads the latest available interest rates data from Markit ## before date and returns the parsed file into a list require(xml2) i <- 0 currency <- match.arg(currency) while( TRUE ) { lastdate <- format(date-i, "%Y%m%d") filename <- paste("InterestRates", currency, lastdate, sep="_") filename.ext <- paste0(filename,".xml") if( filename.ext %in% dir(file.path(data.dir, "Yield Curves"))){ return( as_list(read_xml(file.path(data.dir, "Yield Curves", filename.ext))) ) }else{ temp <- tempfile(tmpdir = file.path(data.dir, "Yield Curves")) download.file(paste0("http://www.markit.com/news/", filename, ".zip"), temp, quiet=T) con <- file(temp, "r") firstline <- readLines(con, 1, warn=FALSE) ## Markit returns a plain text file if there is no data. if(firstline == "Interest Rates not available, please check date entered") { i <- i + 1 close(con) unlink(temp) } else { cat("downloaded data for:", lastdate,"\n") close(con) ## we unzip it unzip(temp, exdir = file.path(data.dir, "Yield Curves")) unlink(temp) return( as_list(read_xml(file.path(data.dir, "Yield Curves", filename.ext))) ) } } } } thirdwed <- function(x) { d <- x - as.POSIXlt(x)$mday + 1 n <- (3-as.POSIXlt(d)$wday) %% 7 + 1 d + 14 + n - 1 } nextthirdwed <- function(x) { y <- thirdwed(x) thirdwed(y + 30 * (y < x)) } buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR"), futurequotes){ currency <- match.arg(currency) deposits <- list() futures <- list() swaps <- list() if(missing(futurequotes)){ keys <- unlist(lapply(MarkitData$deposits[5:length(MarkitData$deposits)], function(x)paste0("d",tolower(x$tenor))), use.names=FALSE) for(i in seq_along(keys)){ deposits[[keys[i]]] <- as.numeric(MarkitData$deposits[i+4]$curvepoint$parrate) } }else{ for(i in seq_along(futurequotes)){ futures[[paste0("fut",i)]] <- futurequotes[i] } ## get last imm date lastimmdate <- nextthirdwed(advance(dates=tradeDate, n=21, timeUnit=2, bdc=4)) lastfuturematurity <- advance(dates=lastimmdate, n=3, timeUnit=2, bdc=4) ## find out the 2 year swap rate maturity s2ymaturity <- advance(calendar="UnitedKingdom", dates=settleDate, 2, 3) if(s2ymaturity == lastfuturematurity){ futures[["fut8"]] <- NULL } } if(length(MarkitData$swaps)>8){## swaps quotes probably missing keys <- unlist(lapply(MarkitData$swaps[8:length(MarkitData$swaps)], function(x) paste0("s", tolower(x$tenor))),use.names=FALSE) for(i in seq_along(keys)){ swaps[[keys[i]]] <- as.numeric(MarkitData$swaps[i+7]$curvepoint$parrate) } if(.Platform$OS.type != "unix"){ for(missingtenor in c("s4y", "s6y", "s7y", "s8y", "s9y", "s12y", "s25y")){ swaps[[missingtenor]] <- NULL } } } tsQuotes <- c(deposits, futures, swaps) return( tsQuotes ) } exportYC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"), useFutures=FALSE){ ## export the Yield Curve into the environment currency <- match.arg(currency) require(RQuantLib) if(useFutures){ futurefile <- file.path(data.dir, "Yield Curves", sprintf("futures-%s.csv", tradedate)) if(file.exists(futurefile)){ futurequotes <- read.csv(futurefile, header=F) } } MarkitData <- getMarkitIRData(tradedate-1, currency) evaldate <- as.Date(MarkitData$effectiveasof[[1]]) setEvaluationDate(tradedate) if(evaldate!=tradedate){ stop("wrong date") } sDate <- as.Date(MarkitData$deposits$spotdate[[1]]) setCalendarContext(calendar="WeekendsOnly", fixingDays=2, settleDate=sDate) legparams <- switch(currency, USD = list(fixFreq="Semiannual", floatFreq="Quarterly", dayCounter="Thirty360"), EUR = list(fixFreq="Annual", floatFreq="Semiannual", dayCounter="Thirty360")) params <- list(tradeDate=tradedate, settleDate=sDate, interpWhat="discount", interpHow="loglinear") if(exists("futurequotes")){ quotes <- buildMarkitYC(MarkitData, currency, futurequotes[,2]) }else{ quotes <- buildMarkitYC(MarkitData, currency) } YC <<- list(params=params, tsQuotes=quotes, legparams=legparams) }