R/etf_industry_exposure.R

Defines functions etf_industry_exposure

Documented in etf_industry_exposure

#' @title ETF Industry Exposure
#' @description Get exchange traded industry exposure
#'   
#' @param etf ETF ticker - SPY, XLK, IYW
#' 
#' @importFrom stringr str_remove_all
#' @importFrom tibble as_tibble
#' @importFrom dplyr mutate select arrange desc
#' 
#' @return
#' @export
#' 
#' @examples
#' etf_industry_exposure('SPY')
#' etf_industry_exposure('XLK')
#' etf_industry_exposure('IYW')
etf_industry_exposure <- function(etf, ...){
  
  url <- finnhub_endpoint('etfs_industry')
  
  df <- retry_get(url, 
                  query = list(token = finnhub_key(),
                               symbol = etf))
  df %>% 
    .[['sectorExposure']] %>% 
    dplyr::mutate(exposure_perc = stringr::str_remove_all(exposure, '%') %>% as.numeric,
                  exposure_perc = exposure_perc / 100,
                  etf = etf,
                  query_date = lubridate::now()) %>% 
    dplyr::select(etf, industry, exposure_perc, exposure, query_date) %>% 
    dplyr::arrange(dplyr::desc(exposure_perc))
}
threadingdata/finnhubr documentation built on Aug. 10, 2020, 12:48 a.m.