diff options
| -rw-r--r-- | R/yieldcurve.R | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/R/yieldcurve.R b/R/yieldcurve.R index a21937b9..42b5ae3f 100644 --- a/R/yieldcurve.R +++ b/R/yieldcurve.R @@ -1,28 +1,35 @@ library(XML)
+root.dir = file.path("//WDSENTINEL/share/CorpCDOs/")
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
- temp <- tempfile()
+ temp <- tempfile(tmpdir = file.path(root.dir, "/data/Yield Curves"))
while( TRUE ) {
- lastdate <- format(date-i,"%Y%m%d")
- filename <- paste("InterestRates_USD_", lastdate, sep="")
- 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)
- } else {
- cat("downloaded data for:", lastdate,"\n")
- close(con)
- # we unzip it
- unzip(temp)
- unlink(temp)
- break
+ 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)
+ } else {
+ cat("downloaded data for:", lastdate,"\n")
+ close(con)
+ ## we unzip it
+ unzip(temp, exdir = file.path(root.dir, "/data/Yield Curves"))
+ unlink(temp)
+ break
+ }
}
}
return( xmlToList(paste(filename,".xml", sep="")) )
|
