aboutsummaryrefslogtreecommitdiffstats
path: root/R/yieldcurve.R
blob: f17ab9269b02fb0b74489437c36d35e36fec822b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
getMarkitIRData <- function(date=Sys.Date()) {
    ## downloads the latest available interest rates data from Markit
    ## before date and returns the parsed file into a list
    require(XML)
    i <- 0
    while( TRUE ) {
        lastdate <- format(date-i, "%Y%m%d")
        filename <- paste("InterestRates", "USD", lastdate, sep="_")
        filename.ext <- paste0(filename,".xml")
        if( filename.ext %in% dir(file.path(root.dir, "/data/Yield Curves"))){
            return( xmlToList(file.path(root.dir, "data/Yield Curves", filename.ext)) )
        }else{
            temp <- tempfile(tmpdir = file.path(root.dir, "/data/Yield Curves"))
            download.file(paste("http://www.markit.com/news/", filename, ".zip", sep=""), temp, quiet=T)
            con <- file(temp, "r")
            firstline <- readLines(con, 1)
            ## 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(root.dir, "/data/Yield Curves"))
                unlink(temp)
                return( xmlToList(file.path(root.dir, "data/Yield Curves", filename.ext)) )
            }
        }
    }
    return( xmlToList(paste(filename,".xml", sep="")) )
}

buildMarkitYC <- function(MarkitData, futurequotes, dt=0.25){
    settledate <- as.Date(MarkitData$deposits$spotdate)
    params <- list(tradeDate=as.Date(MarkitData$effectiveasof, format="%Y-%m-%d"),
                   settleDate=settledate,
                   dt=dt,
                   interpWhat="discount",
                   interpHow="loglinear")

    short.term <- list()
    if(missing(futurequotes)){
        short.term <- list(d1m=as.numeric(MarkitData$deposits[5]$curvepoint$parrate),
                          d3m=as.numeric(MarkitData$deposits[7]$curvepoint$parrate))
    }else{
        for(i in seq_along(futurequotes)){
            short.term[[paste0("fut",i)]] <- futurequotes[i]
        }
    }

    tsQuotes <- c(short.term,
                  list(
                  s2y=as.numeric(MarkitData$swaps[8]$curvepoint$parrate),
                  s3y=as.numeric(MarkitData$swaps[9]$curvepoint$parrate),
                  ##s4y=as.numeric(MarkitData$swaps[10]$curvepoint$parrate),
                  s5y=as.numeric(MarkitData$swaps[11]$curvepoint$parrate),
                  ##s6y=as.numeric(MarkitData$swaps[12]$curvepoint$parrate),
                  ##s7y=as.numeric(MarkitData$swaps[13]$curvepoint$parrate),
                  ##s8y=as.numeric(MarkitData$swaps[14]$curvepoint$parrate),
                  ##s9y=as.numeric(MarkitData$swaps[15]$curvepoint$parrate),
                  s10y=as.numeric(MarkitData$swaps[16]$curvepoint$parrate),
                  ##s12y=as.numeric(MarkitData$swaps[17]$curvepoint$parrate),
                  s15y=as.numeric(MarkitData$swaps[18]$curvepoint$parrate),
                  s20y=as.numeric(MarkitData$swaps[19]$curvepoint$parrate),
                  ##s25y=as.numeric(MarkitData$swaps[20]$curvepoint$parrate),
                  s30y=as.numeric(MarkitData$swaps[21]$curvepoint$parrate)))
    YC.USD <- list(params=params, tsQuotes=tsQuotes)
    return( YC.USD )
}