diff options
| -rw-r--r-- | R/yieldcurve.R | 69 |
1 files changed, 18 insertions, 51 deletions
diff --git a/R/yieldcurve.R b/R/yieldcurve.R index f592c8c8..1841c123 100644 --- a/R/yieldcurve.R +++ b/R/yieldcurve.R @@ -7,37 +7,11 @@ if(.Platform$OS.type == "unix"){ }
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
+ ## returns Markit rates from serenitasdb
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))) )
- }
- }
- }
+ sqlstr <- sprintf("SELECT * FROM %s_rates WHERE effective_date = $1", currency)
+ serenitasdb <- dbConn("serenitasdb")
+ return( dbGetQuery(serenitasdb, sqlstr, params = list(date)) )
}
thirdwed <- function(x) {
@@ -57,10 +31,12 @@ buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR"), futurequotes){ 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)
+ for(k in names(MarkitData[2:7])) {
+ v <- MarkitData[[k]]
+ if(is.na(v)) {
+ next
+ }
+ deposits[[paste0("d", tolower(k))]] <- v
}
}else{
for(i in seq_along(futurequotes)){
@@ -75,17 +51,12 @@ buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR"), futurequotes){ 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
- }
+ for(k in names(MarkitData[8:length(MarkitData)])) {
+ v <- MarkitData[[k]]
+ if(is.na(v)) {
+ next
}
+ swaps[[paste0("s", tolower(k))]] <- v
}
tsQuotes <- c(deposits, futures, swaps)
return( tsQuotes )
@@ -94,7 +65,6 @@ buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR"), futurequotes){ 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))
@@ -102,13 +72,10 @@ exportYC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"), useFutures futurequotes <- read.csv(futurefile, header=F)
}
}
- MarkitData <- getMarkitIRData(tradedate-1, currency)
- evaldate <- as.Date(MarkitData$effectiveasof[[1]])
+ MarkitData <- getMarkitIRData(tradedate, currency)
+ evaldate <- as.Date(tradedate)
setEvaluationDate(tradedate)
- if(evaldate!=tradedate){
- stop("wrong date")
- }
- sDate <- as.Date(MarkitData$deposits$spotdate[[1]])
+ sDate <- advance(calendar="WeekendsOnly", tradedate, 2, 0)
setCalendarContext(calendar="WeekendsOnly", fixingDays=2,
settleDate=sDate)
legparams <- switch(currency,
|
