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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
require(RQuantLib)
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, 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(root.dir, "/data/Yield Curves"))
unlink(temp)
return( xmlToList(file.path(root.dir, "data/Yield Curves", filename.ext)) )
}
}
}
return( xmlToList(paste(filename,".xml", sep="")) )
}
basic.advance <- function(date, n, unit){
## advance a date by a given amount, whithout taking into account, business days
## n is the number of steps
## unit is the unit (belongs in "day", "month" or "year"
stopifnot(class(date)=="Date")
return(seq.Date(date, length=2, by=paste(n, unit))[2])
}
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, futurequotes, dt=0.25, tradeDate=Sys.Date()){
settleDate <- as.Date(MarkitData$swaps$spotdate)
params <- list(tradeDate=tradeDate,
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{
immdate <- settleDate
for(i in seq_along(futurequotes)){
short.term[[paste0("fut",i)]] <- futurequotes[i]
immdate <- nextthirdwed(immdate)
}
#advance last futures date by 3 months
lastfuturedate <- basic.advance(immdate, 3, "month")
## find out the 2 year swap rate maturity
s2ymaturity <- basic.advance(settleDate, 2, "year")
if(s2ymaturity == lastfuturedate){
short.term[["fut8"]] <- NULL
}
}
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 )
}
exportYC <- function(tradedate=Sys.Date(), calibration.date=tradedate){
## export the Yield Curve into the environment
require(RQuantLib)
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", calibration.date)), header=F)
##retrieve yield curve data
MarkitData <- getMarkitIRData(tradedate)
L1m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/12)
L2m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/6)
L3m <- buildMarkitYC(MarkitData, futurequotes[,2])
L6m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/2)
if(!isBusinessDay(calendar="UnitedStates/GovernmentBond",dates=as.Date(MarkitData$effectiveasof))){
setEvaluationDate(addBusDay(tradedate=as.Date(MarkitData$effectiveasof),-1))
}else{
setEvaluationDate(as.Date(MarkitData$effectiveasof))
}
setCalendarContext("TARGET")
assign("L1m", L1m, env = parent.env(environment()))
assign("L2m", L2m, env = parent.env(environment()))
assign("L3m", L3m, env = parent.env(environment()))
assign("L6m", L6m, env = parent.env(environment()))
}
|