# library(dplyr)
# library(data.table)
# library(magrittr)
#' Static class to interface with Stooq.com
#'
#' This static class contains a set of method to fetch data from the
#' Stooq.com website. These are financial time series mainly.
#'
#' @export
StooqUtils <- R6::R6Class(classname = "StooqUtils",
public = list(),
private = list())
StooqUtils$lGetListOfTickersDataFromStooq <- function(
cTickers, dateStartDate, dateEndDate, cFrequency = "d") {
# 1. validate function parameters --------------------------------------------
# 1.1. cTickers symbol
if (!is.character(cTickers) | length(x = cTickers) == 0L) {
stop("Incorrect function lGetListOfTickersDataFromStooq parameter cTickers - ",
"either not a character vector or it is of length zero! ")
}
# 1.2. dateStartDate, dateEndDate
if (!BasicUtils$bIsScalarOfClass(objIn = dateStartDate, cClassName = "Date") |
!BasicUtils$bIsScalarOfClass(objIn = dateEndDate, cClassName = "Date")) {
stop("Incorrect function lGetListOfTickersDataFromStooq parameter ",
" dateStartDate or dateEndDate - either one of them is not a Date class scalar! ")
}
if (dateStartDate > dateEndDate) {
stop("Incorrect function lGetListOfTickersDataFromStooq parameters ",
" dateStartDate and dateEndDate - start date later than end date! ")
}
# 1.3. cFrequency
if (!BasicUtils$bIsScalarOfClass(objIn = cFrequency, cClassName = "character")) {
stop("Incorrect function lGetListOfTickersDataFromStooq parameter - cFrequency",
" it is a scalar character! ")
}
if (!any(cFrequency == c("d", "w", "m", "q", "y"))) {
stop("Incorrect function lGetListOfTickersDataFromStooq parameter - cFrequency",
" it is out of the allowed scope: 'd', 'w', 'q', 'y'! ")
}
# 2. fetch the data from Stooq -----------------------------------------------
# 2.0. prepare additional required variables
lDataOut <- vector(mode = "list", length = length(x = cTickers))
names(lDataOut) <- cTickers
cStartDate <- format(x = dateStartDate, format = "%Y%m%d")
cEndDate <- format(x = dateEndDate, format = "%Y%m%d")
for (cIterTicker in cTickers) {
message("Fetching data for ticker: ", cIterTicker, "; data frequency: ", cFrequency)
# 2.1. prepare the URL to fetch from
cDataUrl <- paste0("https://stooq.com/q/d/l/?s=", tolower(cIterTicker),
"&d1=", cStartDate, "&d2=", cEndDate, "&i=", cFrequency)
# 2.2. try to fetch the data
message("trying to fetch data from URL: ", cDataUrl)
res <- try(expr = { read.csv(file = cDataUrl) }, silent = TRUE)
# 2.2. if error, print warning and save the error message in the output
if (methods::is(object = res, class2 = "try-error")) {
warning("Failed to fetch the data from URL: cDataUrl; the following error ",
"occurred: ", res, "; skipping the ticker: ", cIterTicker,
"in the output", immediate. = TRUE)
next
}
if (!is.data.frame(x = res)) {
# 2.3. check if output is a data.frame
warning("Corrupt output returned during the fetching of the data for the ticker ",
cIterTicker, "!!! - skipping the ticker in the output",
immediate. = TRUE)
lDataOut[[cIterTicker]] <- NULL
next
} else {
# 2.4. if success, save all the output into list
if (nrow(x = res) == 0L) {
warning("Output fetching for the ticker: ", cIterTicker, " is of zero length!",
"; skipping the ticker in the output! ", immediate. = TRUE)
lDataOut[[cIterTicker]] <- NULL
}
lDataOut[[cIterTicker]] <- data.table::as.data.table(res)
}
}
return(lDataOut)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.