R/get_profiles.R

#' Compile Company Profile data from IEXTrader
#'
#' @import magrittr curl jsonlite profvis
#' @export
get_profiles <- function() {
  cat("Getting stock ticker data from IEXTrader.com...")

  # Stopwatch
  start_time <- proc.time()

  # Get a list of all stock symbols
  symbols <- get_symbols()

  # Only look at tradable symbols
  symbols <- symbols[symbols$isEnabled == TRUE, ]

  # Isolate the vector of symbols, remove special characters
  symbols <- symbols$symbol

  # Loop through the tickers and build the company profile
  company <- data.frame()

  for (i in 1:length(symbols)) {

    # Get company info and quote
    x <- get_company(symbols[i])
    y <- get_delayed_quote(symbols[i])
    z <- get_historical(symbols[i], type = "1m")

    z <- ifelse(class(z) == "character", 0, mean(z$volume))

    # If no data returned, skip to next iteration
    if (x[1] == "Unknown symbol") next
    if (y[1] == "Unknown symbol") next

    # Extract elements into a data.frame
    x <- data.frame(symbol    = ifelse(is.null(x$symbol), "", x$symbol),
                   company    = ifelse(is.null(x$companyName), "", x$companyName),
                   exchange   = ifelse(is.null(x$exchange), "", x$exchange),
                   industry   = ifelse(is.null(x$industry), "", x$industry),
                   sector     = ifelse(is.null(x$secto), "", x$sector),
                   issue      = ifelse(is.null(x$issueType), "", x$issueType),
                   volume     = ifelse(is.na(z), NA, z),
                   last_price = ifelse(is.na(y$delayedPrice), NA, y$delayedPrice))

    # Combine with result of the results
    company <- rbind(company, x)

    # pause to keep from being throttled by the API
    if(i %in% seq(100, 10000, 100)) pause(1)
  }

  # Remove fake companies
  company <- company[!(company$exchange %in% c("Investors Exchange", "")), ]

  # Change exchange labels to match RobinHood Markets
  company$exchange <- as.character(company$exchange)
  company$exchange[company$exchange == "New York Stock Exchange"] <- "NYSE"
  company$exchange[company$exchange == "NYSE Arca"]  <- "NYSE"
  company$exchange[company$exchange == "Nasdaq Global Select"]  <- "NASDAQ"
  company$exchange[company$exchange == "NYSE American"]  <- "NYSE"
  company$exchange[company$exchange == "NASDAQ Global Market"] <- "NASDAQ"
  company$exchange[company$exchange == "NASDAQ Capital Market"] <- "NASDAQ"
  company$exchange[company$exchange == "Cboe Global Markets EDGX"]  <- "BATS"
  company$exchange[company$exchange == "OTC Pink"]  <- "OTCM"

  # Fill in missing sectors
  company$sector <- as.character(company$sector)
  company$sector[company$sector == ""] <- "Unknown"

  # Fill in missing industries
  company$industry <- as.character(company$industry)
  company$industry[company$industry == ""] <- "Unknown"

  company$symbol <- as.character(company$symbol)

  # Stopwatch
  end_time <- proc.time() - start_time

  cat("..........COMPLETE (", round(end_time[3] / 60, 2), "minutes)")

  iex_profiles <- company

  return(iex_profiles)
}
JestonBlu/IEXTrader documentation built on May 29, 2019, 1:20 p.m.