Skip to content

Commit c2cd0a4

Browse files
committed
ir curves
ir curve
1 parent 1b539da commit c2cd0a4

File tree

4 files changed

+36
-58
lines changed

4 files changed

+36
-58
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Package
22
Package: RTL
33
Title: Risk Tool Library - Trading, Risk, Analytics for Commodities
44
Version: 1.3.7
5-
Date: 2025-06-05
5+
Date: 2025-07-10
66
Authors@R: c(
77
person("Philippe", "Cote", , "pcote@ualberta.ca", role = c("aut", "cre")),
88
person("Nima", "Safaian", , "nima.safaian@gmail.com", role = "aut")

data-raw/DATASET.R

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,69 +1275,45 @@ planets <- planets %>%
12751275
usethis::use_data(planets, overwrite = T)
12761276

12771277
# Curves and Def - barchart
1278-
options(binman.skip.phantomjs = TRUE)
1279-
library(RSelenium)
1278+
1279+
library(chromote)
12801280
library(rvest)
12811281
library(tidyverse)
1282-
rD <- rsDriver(port = 4545L, browser = "firefox", chromever = NULL, check = FALSE, verbose = FALSE)
1283-
#rD <- rsDriver(port = 4444L, browser = "chrome",chromever = "latest", verbose = FALSE)
1284-
remDr <- rD[["client"]]
1285-
Sys.sleep(2)
1286-
1287-
remDr$navigate("https://www.chathamfinancial.com/technology/us-market-rates")
1288-
Sys.sleep(2)
1289-
remDr$findElement(using = 'css', value = 'div.rates:nth-child(8) > div:nth-child(1) > div:nth-child(2) > table:nth-child(1)')$clickElement() # Cross rates
1290-
#remDr$findElement(using = 'class', value = 'bc-table-wrapper')$clickElement()
1291-
page <- remDr$getPageSource()
1292-
chat <- rvest::read_html(page[[1]]) %>%
1293-
rvest::html_table()
1294-
libor <- chat %>% .[[4]] %>%
1295-
dplyr::rename(Name = 1, Last = 2) %>%
1296-
dplyr::select(1,2) %>%
1297-
dplyr::filter(Name %in% c("SOFR","1-month Term SOFR","3-month Term SOFR")) %>%
1298-
dplyr::mutate(Name = c("d1w", "d1m", "d3m"),
1299-
Last = readr::parse_number(Last)/100)
1300-
#libor <- rbind(dplyr::tibble(Name = "d1w", Last = libor$Last[1]),libor)
1301-
irs <- chat %>% .[[3]] %>%
1302-
dplyr::rename(Name = 1, Last = 2) %>%
1303-
dplyr::select(Name,Last) %>%
1304-
dplyr::slice(-1) %>%
1305-
dplyr::mutate(Name = paste0("s",c(2,3,5,7,10,15,30),"y"),
1306-
Last = readr::parse_number(Last)/100)
1307-
#remDr$navigate("https://www.cmegroup.com/markets/interest-rates/stirs/eurodollar.settlements.html")
1308-
remDr$navigate("https://www.cmegroup.com/markets/interest-rates/stirs/three-month-sofr.settlements.html")
13091282

1310-
Sys.sleep(3)
1311-
page <- remDr$getPageSource()
1312-
futs <- rvest::read_html(page[[1]]) %>%
1313-
rvest::html_table() %>%
1314-
.[[1]] %>%
1315-
dplyr::slice(-1) %>%
1316-
dplyr::slice(1:8) %>%
1317-
dplyr::transmute(Name = paste0("fut", 1:8), Last = Settle)
1283+
# 1. Start Chromote controller
1284+
Sys.setenv(
1285+
CHROMOTE_CHROME_ARGS = "--disable-web-security --allow-running-insecure-content --disable-features=IsolateOrigins,site-per-process --no-sandbox",
1286+
CHROMOTE_HEADLESS = "false" # Forces visible Chrome
1287+
)
13181288

1319-
remDr$close()
1320-
rD[["server"]]$stop()
1289+
# 2. Start Chromote controller
1290+
c <- Chromote$new()
13211291

1322-
# chromote
1292+
# 2. Start a single session (shared across steps)
1293+
b <- c$new_session()
13231294

1324-
library(chromote)
1325-
library(rvest)
1326-
library(dplyr)
1327-
library(readr)
1295+
# 3. Set the spoofed User-Agent BEFORE any navigation
1296+
ua <- "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
1297+
b$Network$enable()
1298+
b$Network$setUserAgentOverride(userAgent = ua)
13281299

1329-
Sys.setenv(CHROMOTE_CHROME = "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta")
1330-
b <- ChromoteSession$new()
1300+
# 4. Navigate to Chatham site
13311301
b$Page$navigate("https://www.chathamfinancial.com/technology/us-market-rates")
1332-
if (interactive()) b$view()
1333-
b$Page$loadEventFired() # Wait until fully loaded
1302+
repeat {
1303+
state <- tryCatch({
1304+
b$Runtime$evaluate("document.readyState")$result$value
1305+
}, error = function(e) NA)
1306+
if (state == "complete") break
1307+
Sys.sleep(1)
1308+
}
1309+
b$view()
13341310

1311+
# Extract LIBOR and IRS data
13351312
html <- b$DOM$getDocument()
13361313
page_html <- b$DOM$getOuterHTML(nodeId = html$root$nodeId)$outerHTML
13371314

13381315
chat <- read_html(page_html) %>% html_table()
13391316

1340-
# Same parsing logic as before
13411317
libor <- chat %>% .[[4]] %>%
13421318
rename(Name = 1, Last = 2) %>%
13431319
select(1, 2) %>%
@@ -1352,10 +1328,15 @@ irs <- chat %>% .[[3]] %>%
13521328
mutate(Name = paste0("s", c(2, 3, 5, 7, 10, 15, 30), "y"),
13531329
Last = parse_number(Last) / 100)
13541330

1355-
# Now go to CME site and scrape futures
1331+
# 5. Navigate to CME Group site
13561332
b$Page$navigate("https://www.cmegroup.com/markets/interest-rates/stirs/three-month-sofr.settlements.html")
1357-
b$Page$loadEventFired()
1358-
Sys.sleep(3)
1333+
repeat {
1334+
state <- tryCatch({
1335+
b$Runtime$evaluate("document.readyState")$result$value
1336+
}, error = function(e) NA)
1337+
if (state == "complete") break
1338+
Sys.sleep(1)
1339+
}
13591340

13601341
html2 <- b$DOM$getDocument()
13611342
page_html2 <- b$DOM$getOuterHTML(nodeId = html2$root$nodeId)$outerHTML
@@ -1367,14 +1348,11 @@ futs <- read_html(page_html2) %>%
13671348
slice(1:8) %>%
13681349
transmute(Name = paste0("fut", 1:8), Last = Settle)
13691350

1370-
1371-
13721351
# Discount Objects
13731352

13741353
library(RQuantLib)
13751354
# removing d1y fro LIBOR and s2y - causes negative rates
13761355
tsQuotes <- rbind(libor, irs, futs) %>% as_tibble() %>%
1377-
dplyr::mutate(Last = readr::parse_number(Last)) %>%
13781356
tidyr::pivot_wider(names_from = Name, values_from = Last) %>%
13791357
dplyr::select(-s2y,-d3m) %>%
13801358
transpose() %>% unlist() %>% as.list()
@@ -1401,10 +1379,10 @@ usSwapCurves[1:4] %>%
14011379
#usSwapCurves[1:4] %>% dplyr::as_tibble() %>% View()
14021380
cbind(usSwapCurves$times, usSwapCurves$discounts, usSwapCurves$zerorates, usSwapCurves$forwards) %>%
14031381
dplyr::as_tibble() %>% dplyr::rename(times = V1, discounts = V2, zerorates = V3, forwards = V4) %>%
1404-
arrow::write_feather(x = ., sink = "C:/Users/cotep/data/usd-ir.feather")
1382+
arrow::write_feather(x = ., sink = paste0(path.expand("~"),"/data/usd-ir.feather"))
14051383
usethis::use_data(tsQuotes, overwrite = T)
14061384

1407-
tsQuotes <- list(flat = 0.04)
1385+
tsQuotes <- list(flat = 0.045)
14081386
usSwapCurvesPar <- DiscountCurve(params, tsQuotes, times)
14091387

14101388
usethis::use_data(usSwapCurves, overwrite = T)

data/usSwapCurves.rda

8 Bytes
Binary file not shown.

data/usSwapCurvesPar.rda

-8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)