R/latest.R

Defines functions riingo_crypto_latest riingo_iex_latest_single riingo_iex_latest_single_safely riingo_iex_latest retrieve_latest_col_ordering riingo_latest_single riingo_latest_single_safely riingo_latest

Documented in riingo_crypto_latest riingo_iex_latest riingo_latest

# ------------------------------------------------------------------------------
# Tiingo prices

#' The latest day's worth of data for a given ticker
#'
#' This returns only the most recent day of daily data for the supplied ticker(s).
#'
#' @inheritParams riingo_prices
#'
#' @examples
#'
#' \dontrun{
#'
#' # The latest available day of daily data for QQQ
#' riingo_latest("QQQ")
#'
#' }
#'
#' @export
#'
riingo_latest <- function(ticker) {
  assert_x_inherits(ticker, "ticker", class = "character")

  results <- purrr::map(ticker, riingo_latest_single_safely)

  validate_not_all_null(results)

  vctrs::vec_rbind(!!!results)
}

riingo_latest_single_safely <- function(ticker) {
  riingo_single_safely(
    .f = riingo_latest_single,
    ticker = ticker
  )
}

riingo_latest_single <- function(ticker) {
  type <- "tiingo"
  endpoint <- "latest"

  # URL construction
  riingo_url <- construct_url(
    type, endpoint, ticker
  )

  # Download
  cont_df <- content_downloader(riingo_url, ticker)

  # Clean
  riingo_data <- clean_json_df(cont_df, type, endpoint)

  # For single days, the ordering is not the same as historical prices so we reorder to be consistent
  col_ordering <- retrieve_latest_col_ordering()
  riingo_data <- riingo_data[,col_ordering]

  # Add ticker
  riingo_data_with_ticker <- tibble::add_column(riingo_data, ticker = ticker, .before = 1L)

  riingo_data_with_ticker
}

retrieve_latest_col_ordering <- function() {
  c("date", "close", "high", "low", "open", "volume", "adjClose", "adjHigh",
    "adjLow", "adjOpen", "adjVolume", "divCash", "splitFactor")
}

# ------------------------------------------------------------------------------
# IEX prices

#' The latest day's worth of intraday data for a given ticker
#'
#' This returns only the most recent day of intraday data for the supplied ticker(s).
#'
#' @inheritParams riingo_prices
#'
#' @examples
#'
#' \dontrun{
#'
#' # The latest available day of intraday data for QQQ
#' riingo_iex_latest("QQQ")
#'
#' riingo_iex_latest("QQQ", "1hour")
#'
#' }
#'
#' @export
riingo_iex_latest <- function(ticker, resample_frequency = "1min") {
  assert_x_inherits(ticker, "ticker", class = "character")
  assert_x_inherits(resample_frequency, "resample_frequency", class = "character")
  assert_resample_freq_is_fine(resample_frequency)

  results <- purrr::map(
    .x = ticker,
    .f = riingo_iex_latest_single_safely,
    resample_frequency = resample_frequency
  )

  validate_not_all_null(results)

  vctrs::vec_rbind(!!!results)
}

riingo_iex_latest_single_safely <- function(ticker, resample_frequency) {
  riingo_single_safely(
    .f = riingo_iex_latest_single,
    ticker = ticker,
    resample_frequency = resample_frequency
  )
}

riingo_iex_latest_single <- function(ticker, resample_frequency) {

  type <- "iex"
  endpoint <- "latest"

  # URL construction
  riingo_url <- construct_url(
    type, endpoint, ticker,
    resampleFreq = resample_frequency
  )

  # Download
  cont_df <- content_downloader(riingo_url, ticker)

  # Clean
  riingo_data <- clean_json_df(cont_df, type, endpoint)

  # Add ticker
  riingo_data$ticker <- NULL # to be consistent, don't use the provided meta ticker column
  riingo_data <- tibble::add_column(riingo_data, ticker = ticker, .before = 1L)

  riingo_data
}

# ------------------------------------------------------------------------------
# Crypto prices

#' The latest day's worth of intraday data for a given cryptocurrency
#'
#' This returns only the most recent day of intraday data for the supplied ticker(s).
#'
#' @inheritParams riingo_crypto_prices
#'
#' @examples
#'
#' \dontrun{
#'
#' # The latest available day of intraday data for QQQ
#' riingo_crypto_latest("btcusd")
#'
#'
#' }
#'
#' @export
riingo_crypto_latest <- function(ticker, resample_frequency = "1min", base_currency = NULL,
                                 exchanges = NULL, convert_currency = NULL, raw = FALSE) {
  riingo_crypto_prices(ticker,
                       start_date = NA_character_,
                       end_date = NA_character_,
                       resample_frequency,
                       base_currency,
                       exchanges,
                       convert_currency,
                       raw)
}

Try the riingo package in your browser

Any scripts or data that you put into this service are public.

riingo documentation built on Sept. 13, 2020, 5:15 p.m.