R/crypto_candles.R

Defines functions crypto_candles

Documented in crypto_candles

#' @title Cryptocurrency Candles
#' @description Get cryptocurrency candles
#'  
#' @param symbol Cryptocurrency symbol
#' @param resolution 1-minute, 5-minute, 15-minute, 30-minute, 60-minute, 
#' daily, weekly and monthly
#' @param from Begin date
#' @param to End date
#' @param format json
#' @param adjusted Adjusted prices ('true') or not ('false')
#' 
#' @importFrom tibble as_tibble
#' @importFrom purrr set_names
#' @importFrom dplyr mutate select everything
#' @importFrom lubridate as_datetime today
#' 
#' @return
#' @export
#' 
#' @examples
#' crypto_candles('COINBASE:ETC-BTC')
#' crypto_candles('COINBASE:ETC-BTC', resolution = '1')
#' crypto_candles('COINBASE:ETC-BTC', resolution = '5')
#' crypto_candles('COINBASE:ETC-BTC', resolution = '15')
#' crypto_candles('COINBASE:ETC-BTC', resolution = '30')
#' crypto_candles('COINBASE:ETC-BTC', resolution = '60')
#' crypto_candles('COINBASE:ETC-BTC', resolution = 'D', from = today() - 365, to = today())
#' crypto_candles('COINBASE:ETC-BTC', from = today() - 365, resolution = 'W')
#' crypto_candles('COINBASE:ETC-BTC', from = today() - 365, resolution = 'M')
crypto_candles <- function(symbol, 
                           resolution = 'D', 
                           from = lubridate::today() - 7,
                           to = lubridate::today(),
                           format='json',
                           adjusted='true',
                           ...){
  
  from <- date_to_unix(from)
  to <- date_to_unix(to)
  
  url <- finnhub_endpoint('crypto_candles')
  
  df <- retry_get(url, 
                  query = list(token = finnhub_key(),
                               symbol = symbol,
                               resolution = resolution,
                               from = from,
                               to = to,
                               format = format,
                               adjusted = adjusted)) 
  df %>% 
    tibble::as_tibble() %>% 
    purrr::set_names(c('close', 'high', 'low', 'open' ,'flag', 'unixtime', 'volume')) %>% 
    dplyr::mutate(date = lubridate::as_datetime(unixtime, tz = finnhub_timezone),
                  symbol = symbol) %>% 
    dplyr::select(symbol, date, dplyr::everything())
}
threadingdata/finnhubr documentation built on Aug. 10, 2020, 12:48 a.m.