R/generateRfUrl.R

#' Generate risk-free rate download URL.
#'
#' Generates URL to download risk-free rate of a specified maturity for a
#' desired period.
#'
#' \code{generateRfUrl} generates a URL to the ECB web-site which can be used to
#' download the risk-free rate data.
#'
#' @param maturity Numerical, maturity of yield in years (integer 1 to 30).
#' @param frequency Character, one of daily, weekly, monthly, quarterly,
#'   half-yearly, annual.
#' @param start Date (or an object that can be parsed using \code{as.Date}),
#'   start of the period.
#' @param end Date (or an object that can be parsed using \code{as.Date}),
#'   end of the period.
#'
#' @seealso \code{\link{downloadRfData}}.
generateRfUrl <- function(maturity,
                          frequency = "daily",
                          start = "2003-01-06",
                          end = Sys.Date()){
  # Error handling
  if(!(frequency %in% c("daily", "weekly", "monthly", "quarterly",
                        "half-yearly", "annual"))){
    stop("wrong frequency of risk-free rate")
  }

  # Format frequency to appropriate strings
  if (frequency == "daily") {
    trans = "N"
  } else if (frequency == "weekly") {
    trans = "WF"
  } else if (frequency == "monthly") {
    trans = "MF"
  } else if (frequency == "quarterly") {
    trans = "QF"
  } else if (frequency == "half-yearly") {
    trans = "BF"
  } else if (frequency == "annual") {
    trans = "AF"
  }

  # format date to appropriate strings used in URL
  start <- as.Date(start)
  end <- as.Date(end)
  start <- format(start,"%d-%m-%Y")
  end <- format(end,"%d-%m-%Y")

  # generate url
  fileUrl <- paste0(
    "http://sdw.ecb.europa.eu/quickviewexport.do?trans=", trans,
    "&start=", start,
    "&end=", end,
    "&SERIES_KEY=165.YC.B.U2.EUR.4F.G_N_A.SV_C_YM.SR_", maturity, "Y",
    "&type=csv")

  return(fileUrl)
}
nickto/PensionFundsLv documentation built on May 23, 2019, 5:08 p.m.