Skip to content

Instantly share code, notes, and snippets.

@DavZim
Last active December 6, 2019 16:21
Show Gist options
  • Save DavZim/9092a773211dcf2c837c03066f2aef29 to your computer and use it in GitHub Desktop.
Save DavZim/9092a773211dcf2c837c03066f2aef29 to your computer and use it in GitHub Desktop.
library(tidyverse) # for the plot only
# required librariers that need to be installed (not loaded!): quantmod, dplyr, tibble 
# (the latter two are in the tidyverse anyway)

#' Downloads stock data from yahoo finance etc. using quantmod::getSymbols
#' 
#' @param tickers a vector of tickers
#' @param from start date
#' @param to end date
#' @param … additional arguments passed to getSymbols
#' 
#' @return a tibble (data\_frame) of the data
#' @export
#' 
#' @examples
#' get_stocks(c(“AMZN”, “GOOG”, “AAPL”), from = “2010-01-01”, to = “2015-01-01”)
get_stocks <- function(tickers, from, to, ...) {
  res <- lapply(tickers, function(ticker) {
    suppressWarnings({
      suppressMessages({
        xts_data <- quantmod::getSymbols(ticker, from = from, to = to, auto.assign = FALSE, ...)
      })
    })
    if (length(xts_data) == 0) return(NULL)
    
    df <- tibble::tibble(
      ticker = ticker,
      date = zoo::index(xts_data),
    )
    df <- dplyr::bind_cols(df, tibble::as_tibble(as.matrix(xts_data)))
    
    col_names <- names(xts_data)
    col_names <- gsub("[^.]+\\.", "", col_names)
    
    names(df) <- c("ticker", "date", tolower(col_names))
    return(df)
  })
  return(dplyr::bind_rows(res))
}

df <- get_stocks(c("AMZN", "GOOG", "AAPL"), from = "2000-01-01", to = "2015-01-01")

df
#> # A tibble: 10,157 x 8
#>    ticker date        open  high   low close   volume adjusted
#>    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#>  1 AMZN   2000-01-03  81.5  89.6  79.0  89.4 16117600     89.4
#>  2 AMZN   2000-01-04  85.4  91.5  81.8  81.9 17487400     81.9
#>  3 AMZN   2000-01-05  70.5  75.1  68    69.8 38457400     69.8
#>  4 AMZN   2000-01-06  71.3  72.7  64    65.6 18752000     65.6
#>  5 AMZN   2000-01-07  67    70.5  66.2  69.6 10505400     69.6
#>  6 AMZN   2000-01-10  72.6  72.6  65.6  69.2 14757900     69.2
#>  7 AMZN   2000-01-11  66.9  70    65    66.8 10532700     66.8
#>  8 AMZN   2000-01-12  67.9  68    63    63.6 10804500     63.6
#>  9 AMZN   2000-01-13  64.9  67.2  63.1  65.9 10448100     65.9
#> 10 AMZN   2000-01-14  66.8  68.6  64    64.2  6853600     64.2
#> # … with 10,147 more rows
df %>% count(ticker)
#> # A tibble: 3 x 2
#>   ticker     n
#>   <chr>  <int>
#> 1 AAPL    3773
#> 2 AMZN    3773
#> 3 GOOG    2611

ggplot(df, aes(x = date, y = adjusted, color = ticker)) +
  geom_line()

Created on 2019-12-06 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment