Skip to content

Commit

Permalink
feat: support for average and end-of-month HQM
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed May 2, 2024
1 parent 4eb10ed commit 7b6b1ca
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions R/yield-curve.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
tr_hqm <- function() {
url <- "https://home.treasury.gov/system/files/226/hqm_84_88.xls"
tr_hqm <- function(x = c("average", "end-of-month")) {
x <- match.arg(x)
x <- if (x == "average") "hqm" else "hqmeom"
start_year <- seq(1984L, 2028L, by = 5L)
urls <- vapply(start_year, \(year) {
sprintf(
"https://home.treasury.gov/system/files/226/hqm_%02d_%02d.xls",
year %% 100L, (year + 4L) %% 100L
"https://home.treasury.gov/system/files/226/%s_%02d_%02d.xls",
x, year %% 100L, (year + 4L) %% 100L
)
}, NA_character_)
if (x == "hqmeom") {
urls[[1L]] <- sub("88\\.xls$", "88_0.xls", urls[[1L]])
}

months <- rep.int(month.name, 5L)
res <- lapply(seq_along(urls), \(i) {
Expand All @@ -24,15 +28,16 @@ tr_hqm <- function() {
res <- tidyr::pivot_longer(res, -maturity,
names_to = "yearmonth", values_to = "yield", values_drop_na = TRUE
)
res$yearmonth <- paste("01", res$yearmonth, sep = "-") |>
as.Date(format = "%d-%B-%Y")
res$yearmonth <- paste("01", res$yearmonth, sep = "-") |> as.Date("%d-%B-%Y")
res[c("yearmonth", "maturity", "yield")]
})
do.call(rbind, res)
}

tr_hqm_pars <- function() {
url <- "https://home.treasury.gov/system/files/226/hqm_qh_pars.xls"
tr_hqm_pars <- function(x = c("average", "end-of-month")) {
x <- match.arg(x)
x <- if (x == "average") "hqm" else "hqmeom"
url <- sprintf("https://home.treasury.gov/system/files/226/%s_qh_pars.xls", x)
tf <- tempfile(fileext = ".xls")
on.exit(unlink(tf), add = TRUE)
utils::download.file(url, destfile = tf, quiet = TRUE, mode = "wb")
Expand All @@ -44,5 +49,38 @@ tr_hqm_pars <- function() {
res <- tidyr::pivot_longer(res, -yearmonth,
names_to = "maturity", values_to = "yield"
)
res$yearmonth <- paste("01", res$yearmonth) |> as.Date("%d %B %Y")
res
}

tr_tnc <- function() {
# TODO: there is an edge case for 1976-1977
start_year <- seq(1978L, 2027L, by = 5L)
urls <- vapply(start_year, \(year) {
sprintf(
"https://home.treasury.gov/system/files/226/tnc_%02d_%02d.xls",
year %% 100L, (year + 4L) %% 100L
)
}, NA_character_)

months <- rep.int(month.name, 5L)
res <- lapply(seq_along(urls), \(i) {
tf <- tempfile(fileext = ".xls")
on.exit(unlink(tf), add = TRUE)
utils::download.file(urls[[i]], destfile = tf, quiet = TRUE, mode = "wb")
res <- readxl::read_xls(tf, skip = 4L, .name_repair = \(nms) {
year <- start_year[[i]]
years <- rep(year:(year + 4L), each = 12L)
nms <- paste(months, years, sep = "-")
nms <- c("maturity", "tmp", nms)
nms
})
res <- res[, -2L]
res <- tidyr::pivot_longer(res, -maturity,
names_to = "yearmonth", values_to = "yield", values_drop_na = TRUE
)
res$yearmonth <- paste("01", res$yearmonth, sep = "-") |> as.Date("%d-%B-%Y")
res[c("yearmonth", "maturity", "yield")]
})
do.call(rbind, res)
}

0 comments on commit 7b6b1ca

Please sign in to comment.