R/api_quote_crypto.R

Defines functions api_quote_crypto

Documented in api_quote_crypto

#' RobinHood API: Quotes for cryptocurrency
#'
#' Returns a dataframe of current quotes
#'
#' @param RH object of class RobinHood
#' @param symbols_url (string) url of query with ticker symbols
#' @import httr magrittr
#' 
api_quote_crypto <- function(RH, symbols_url) {

  # URL and token
  url <- symbols_url
  token <- paste("Bearer", RH$tokens.access_token)

  # GET call
  dta <- GET(url,
             add_headers("Accept" = "application/json",
                         "Content-Type" = "application/json",
                         "Authorization" = token))

  # format return
  dta <- mod_json(dta, "fromJSON")
  dta <- as.data.frame(dta)

  # Convert numeric columns from character, have to convert from factor first
  dta <- dta %>%
    dplyr::mutate_at(c("ask_price", "bid_price", "mark_price",
                       "high_price", "low_price", "open_price",
                       "volume"),
                     as.character) %>%
    dplyr::mutate_at(c("ask_price", "bid_price", "mark_price",
                       "high_price", "low_price", "open_price",
                       "volume"),
                     as.numeric)

  # Reorder columns
  dta <- dta[, c("symbol", "ask_price", "bid_price",
                       "mark_price", "high_price", "low_price",
                       "open_price", "volume")]

  # Remove the USD from the end of the symbol
  dta$symbol <- gsub(pattern = "USD$", replacement = "", x = dta$symbol)

  return(dta)
}

Try the RobinHood package in your browser

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

RobinHood documentation built on March 1, 2021, 5:08 p.m.