R/get_bitcoin_price.R

Defines functions get_bitcoin_price

Documented in get_bitcoin_price

#' Hitting the Binance API to get the most recent price of a Bitcoin in USD
#'
#' This is a wrapper around the \code{binancer} package ...
#' @export
#' @param retried the number if retries previously done before the exponential backoff sleep
#' @importFrom binancer binance_coins_prices
get_bitcoin_price <- function(retried = 0) {
  tryCatch(
    ## not using data.table syntax here and falling back to data.frame
    ## so that this could run on systems without data.table as well
    data <- binance_coins_prices(),
    subset(data, symbol == 'BTC')$usd,
    error = function(e) {
      ## exponential backoff retries
      Sys.sleep(1 + retried^2)
      get_bitcoin_price(retried = retried + 1)
    })
}
eszterb99/mr documentation built on June 4, 2020, 12:38 a.m.