#' Fetches stock historical data from DataHub
#'
#' @param code the code of the interest series
#' @param source_code the source code of the interest series
#' @param query optional, availible list(from = yyyy-mm-dd, to = yyyy-mm-dd)
#'
#' @return dataframe \describe{
#' \item{code}{interest series code}
#' \item{source_code}{source code}
#' \item{date}{series date}
#' \item{closing_price}{Closing price}
#' \item{opening_price}{Opening price}
#' \item{bid_price}{Bid price}
#' \item{ask_price}{Ask price}
#' \item{high_price}{High price of the day}
#' \item{low_price}{Low price of the day}
#' \item{volume}{Volume traded}
#' }
#'
#' @examples
#' fetch_interest('ICEAIR', 'NASDAQ')
#'
#' @export
fetch_stock <- function(code, source_code, query = list()) {
# Check input arguments
collection <- checkmate::makeAssertCollection()
checkmate::assertString(code, na.ok = FALSE, min.chars = 1, add = collection)
checkmate::assertString(source_code, na.ok = FALSE, min.chars = 1, add = collection)
checkmate::assertList(query, add = collection)
checkmate::reportAssertions(collection)
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/stock/",
toupper(code),
"/source/",
toupper(source_code),
"/quote")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json(),
query = query)
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
tryCatch({
parsed$date = as.Date(parsed$date)
return(parsed)
},
error=function(error_message) {
return(setNames(data.frame(matrix(ncol = 10, nrow = 0)), c("code", "source_code", "date",
"closing_price", "opening_price",
"bid_price", "ask_price",
"high_price", "low_price", "volume")))
})
}
#' Fetches all available stocks
#'
#' @return dataframe \describe{
#' \item{code}{interest code}
#' \item{name}{interest name}
#' \item{source_code}{source code}
#' \item{source_name}{source name, use this for caption}
#' \item{isin}{isin number}
#' \item{sector}{sector}
#' \item{description}{description of this series}
#' \item{last_update}{date this series was last updated}
#' \item{usage}{code example on how to call for the dataseries}
#' }
#'
#' @examples
#' fetch_stock_info()
#'
#' @export
fetch_stock_info <- function() {
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/stock")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json())
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
parsed <- parsed %>%
select(code, name, source_code, source_name, isin, sector, description, last_update) %>%
mutate(usage = paste0("fetch_stock('", code, "','", source_code, "')"))
parsed
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.