Search code examples
rfor-loopalpha-vantage

Loop of SP100 using Alphavantage in R


I am about to write my Bachelors Thesis in Economics. For this, I need a dataset containing daily stock prices of all companies in the S&P100. I have done this writing each company on a separate line and using alphavantage.

Can anyone help me write the below code using a loop instead?

library(alphavantager)

av_api_key("KEY")

args(av_get)

AAPL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AAPL", outputsize = "full")
ABBV <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABBV", outputsize = "full")
ABT  <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABT", outputsize = "full")
ACN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ACN", outputsize = "full")
AGN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AGN", outputsize = "full")
Sys.sleep(20)
AIG <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AIG", outputsize = "full")
ALL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ALL", outputsize = "full")

Solution

  • You could use lapply to get everything in one go and in 1 giant list. I gave an example below with 2 stocks on how it works. In case of timing issues you can incorporate a sleep in the lapply function. If afterwards you want everything in one giant data.frame you can use bind_rows from dplyr to combine all the data.frames in the list into 1 giant data.frame for further data.manipulation.

    library(alphavantager)
    
    av_api_key("KEY")
    
    symbols <- c("AAPL", "ABBV")
    
    my_list_of_stocks <- lapply(symbols, function(x) av_get(symbol = x, av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
    names(my_list_of_stocks) <- symbols
    
    str(my_list_of_stocks)
    
    List of 2
     $ AAPL:Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   5302 obs. of  6 variables:
      ..$ timestamp: Date[1:5302], format: "1998-01-02" "1998-01-05" "1998-01-06" "1998-01-07" ...
      ..$ open     : num [1:5302] 13.6 16.5 15.9 18.8 17.4 ...
      ..$ high     : num [1:5302] 16.2 16.6 20 19 18.6 ...
      ..$ low      : num [1:5302] 13.5 15.2 14.8 17.3 16.9 ...
      ..$ close    : num [1:5302] 16.2 15.9 18.9 17.5 18.2 ...
      ..$ volume   : num [1:5302] 6411700 5820300 16182800 9300200 6910900 ...
      ..- attr(*, "spec")=
      .. .. cols(
      .. ..   timestamp = col_date(format = ""),
      .. ..   open = col_double(),
      .. ..   high = col_double(),
      .. ..   low = col_double(),
      .. ..   close = col_double(),
      .. ..   volume = col_double()
      .. .. )
     $ ABBV:Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1529 obs. of  6 variables:
      ..$ timestamp: Date[1:1529], format: "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-07" ...
    

    Combine everything in big data.frame.

    # merge everything in one giant data.frame
    library(dplyr)
    my_data <- bind_rows(my_list_of_stocks, .id = "symbol")
    
    # A tibble: 6,831 x 7
       symbol timestamp   open  high   low close   volume
       <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>
     1 AAPL   1998-01-02  13.6  16.2  13.5  16.2  6411700
     2 AAPL   1998-01-05  16.5  16.6  15.2  15.9  5820300
     3 AAPL   1998-01-06  15.9  20    14.8  18.9 16182800
     4 AAPL   1998-01-07  18.8  19    17.3  17.5  9300200
     5 AAPL   1998-01-08  17.4  18.6  16.9  18.2  6910900
     6 AAPL   1998-01-09  18.1  19.4  17.5  18.2  7915600
     7 AAPL   1998-01-12  17.4  18.6  17.1  18.2  4610700
     8 AAPL   1998-01-13  18.6  19.6  18.5  19.5  5686200
     9 AAPL   1998-01-14  19.9  19.9  19.2  19.8  5261300
    10 AAPL   1998-01-15  19.2  19.8  18.6  19.2  4993500
    # ... with 6,821 more rows