R/stock_candles.R

Defines functions stock_candles

Documented in stock_candles

#' @title Stock Candles
#' @description Get stock candles
#'  
#' @param symbol Stock ticker
#' @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
#' stock_candles('NVDA')
#' stock_candles('NVDA', resolution = '1')
#' stock_candles('NVDA', resolution = '5')
#' stock_candles('NVDA', resolution = '15')
#' stock_candles('NVDA', resolution = '30')
#' stock_candles('NVDA', resolution = '60')
#' stock_candles('NVDA', resolution = 'D')
#' stock_candles('NVDA', from = lubridate::today() - 365, resolution = 'W')
#' stock_candles('NVDA', from = lubridate::today() - 365, resolution = 'M')
stock_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('stock_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.