R/get_symbols.R

Defines functions get_symbols

Documented in get_symbols

#' @name get_symbols
#' @export get_symbols
#' @title get_symbols
#' @description Download Market Data from Yahoo Finance.
#' @param symbols Market symbols, e.g. "AAPL" or "GOOG". You can specify multiple series as c("AAPL", "GOOG").
#' @param start_at Date character, e.g. "2018-12-13" in the format of "YYYY-MM-DD", or a date value
#' @param end_at Date character, e.g. "2019-12-13" in the format of "YYYY-MM-DD", or a date value
#' @param drop_na Boolean, indicating whether missings shall be removed
#' @examples get_symbols("GOOG", "2018-12-26", "2019-12-20")
#' @examples get_symbols("^GDAXI", "2018-12-26", "2019-12-20")
#' @examples get_symbols(c("GOOG", "^GDAXI"), "2018-12-26", "2019-12-20")
#' @examples get_symbols("BTC-USD", "2018-12-26", "2019-02-20", drop_na = FALSE)
#' @importFrom rlang .data
get_symbols <- function(symbols, start_at, end_at, drop_na = FALSE) {

  start_at <- as.numeric(as.POSIXct(start_at, format = "%Y-%m-%d", origin = "1970-01-01", tz = "GMT"))
  end_at <- as.numeric(as.POSIXct(end_at, format = "%Y-%m-%d", origin = "1970-01-01", tz = "GMT"))
  if (end_at < start_at) {
    stop("in get_symbols: to must be older than from")
  }
  if (start_at >= as.numeric(as.POSIXct(Sys.Date(), format = "%Y-%m-%d"))) {
    stop("The specified Date lies in the future")
  }


  symbol <- symbols[1]
  x <- lapply(symbols, function(symbol) {
    url <- paste0("https://query1.finance.yahoo.com/v7/finance/chart/",
                  symbol, "?&interval=1d&period1=", start_at, "&period2=", end_at)

    result <- httr::content(httr::GET(url), as = "parsed")$chart$result[[1]]
    if (is.null(result)) {
      stop("The specified symbol does not exist")
    }

    quote <- result$indicators$quote[[1]]

    for (i in 1:length(quote)) {
      quote[[i]][sapply(quote[[i]], is.null)] <- NA
    }

    Adj_Close <- result$indicators$adjclose[[1]]$adjclose
    Adj_Close[sapply(Adj_Close, is.null)] <- NA

    x <- data.frame(
      Open = unlist(quote$open),
      High = unlist(quote$high),
      Low = unlist(quote$low),
      Close = unlist(quote$close),
      Volume = unlist(quote$volume),
      Adj_Close = unlist(Adj_Close)
    )

    x <- data.frame(Symbol = symbol,
                    Date = lubridate::ymd(as.Date(as.POSIXct(unlist(result$timestamp),
                                                  origin = "1970-01-01", format = "%Y-%m-%d"))),
                    x, stringsAsFactors = FALSE)
    x <- tsibble::as_tsibble(x, index = .data$Date, key = .data$Symbol)
    return(x)
  })

  x1 <- x[[1]]
  if (length(x) > 1) {
    for (i in 2:length(x)) {
      x2 <- x[[i]]
      x1 <- rbind(x1, x2)
    }
  }

  if (drop_na == TRUE) {
    x1 <- tidyr::drop_na(x1)
    message("missings in x are removed")
  } else if (sum(is.na(x1)) > 0) {
    message("x contains missings. Consider setting drop_na = TRUE")
  }

  return(x1)

}
markushhh/YahooFinance documentation built on Jan. 24, 2020, 3:19 a.m.