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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
library(RQuantLib)
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")
}
}
}
|