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
125
126
|
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="")) )
}
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, tradeDate=Sys.Date()){
settleDate <- advance(calendar="UnitedKingdom",dates=tradeDate, 2, 0)
names(settleDate) <- NULL
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{
for(i in seq_along(futurequotes)){
short.term[[paste0("fut",i)]] <- futurequotes[i]
}
## get last imm date
lastimmdate <- nextthirdwed(advance(dates=tradeDate, n=21, timeUnit=2, bdc=4))
lastfuturematurity <- advance(dates=lastimmdate, n=3, timeUnit=2, bdc=4)
## find out the 2 year swap rate maturity
s2ymaturity <- advance(calendar="UnitedKingdom", dates=settleDate, 2, 3)
if(s2ymaturity == lastfuturematurity){
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()){
## export the Yield Curve into the environment
require(RQuantLib)
futurefile <- file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", tradedate))
if(file.exists(futurefile)){
futurequotes <- read.csv(futurefile, header=F)
}
##retrieve yield curve data
evaldate <- adjust(calendar="UnitedKingdom", dates=tradedate)
names(evaldate) <- NULL
setEvaluationDate(evaldate)
MarkitData <- getMarkitIRData(tradedate)
if(exists("futurequotes")){
Libor <- buildMarkitYC(MarkitData, futurequotes[,2], evaldate)
}else{
Libor <- buildMarkitYC(MarkitData, ,evaldate)
}
L1m <- Libor
L2m <- Libor
L3m <- Libor
L6m <- Libor
L12m <- Libor
L1m$params$dt <- 1/12
L2m$params$dt <- 1/6
L3m$params$dt <- 1/4
L6m$params$dt <- 1/2
L12m$params$dt <- 1
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()))
assign("L12m", L12m, env = parent.env(environment()))
}
|