aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/tests/test_yieldcurve.R108
1 files changed, 96 insertions, 12 deletions
diff --git a/R/tests/test_yieldcurve.R b/R/tests/test_yieldcurve.R
index 6b1930eb..97a4e95c 100644
--- a/R/tests/test_yieldcurve.R
+++ b/R/tests/test_yieldcurve.R
@@ -1,15 +1,99 @@
-source("yieldcurve.R")
-source("cds_utils.R")
-root.dir <- "/home/share/CorpCDOs/"
+library(RQuantLib)
-dates <- seq(as.Date("2013-05-01"), as.Date("2014-05-01"), by=1)
-for(i in 1:length(dates)){
- day <- dates[i]
- calibration.date <- addBusDay(day, -1)
- print(calibration.date)
- print(day)
- if(isBusinessDay(calendar="UnitedStates", day)){
- exportYC(calibration.date)
- test <- DiscountCurve(c(YC$params, list(dt=0.25)), YC$tsQuotes, 1, YC$legparams)
+data.dir <- "."
+
+getMarkitIRData <- function(date=Sys.Date(), currency="USD") {
+ ## downloads the latest available interest rates data from Markit
+ ## before date and returns the parsed file into a list
+ require(xml2)
+ i <- 0
+ 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))) )
+ }
+ }
+ }
+ return( as_list(read_xml(paste(filename,".xml", sep=""))) )
+}
+
+buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR")){
+ deposits <- list()
+ swaps <- list()
+ 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)
+ }
+ 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)
+ }
+ }
+ tsQuotes <- c(deposits, swaps)
+ return( tsQuotes )
+}
+
+YC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"){
+ ## export the Yield Curve into the environment
+ 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")
+ quotes <- buildMarkitYC(MarkitData, currency)
+ return( list(params=params, tsQuotes=quotes, legparams=legparams) )
+}
+
+dates <- seq(as.Date("2013-01-01"), as.Date("2016-05-16"), by=1)
+for(curr in c("USD", "EUR")) {
+ for(i in 1:length(dates)) {
+ day <- dates[i]
+ cal <- switch(curr,
+ USD = "UnitedStates/GovernmentBond",
+ EUR = "TARGET")
+ calibration.date <- advance(calendar=cal, day, -1, 0)
+ if(isBusinessDay(calendar=cal, day)){
+ yc <- YC(calibration.date, currency=curr)
+ print(day)
+ test <- DiscountCurve(c(yc$params, list(dt=0.25)), yc$tsQuotes, 1, yc$legparams)
+ cat(day, test$forwards, "\n")
+ }
}
}