Home > Back-end >  getSymbols.yahoo "method" in a loop with several symbols and corresponding dates in R
getSymbols.yahoo "method" in a loop with several symbols and corresponding dates in R

Time:10-08

"Method" getSymbols.yahoo allows putting a vector in lieu of the Symbols argument allowing the download of several tickers without using a loop:

library(quantmod)

someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")

getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv())

However, this does not seem to work whe one adds a date vector of equal length in the from = argument, such as:

library(quantmod)

someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901, 
                       18659), origin = "1970-01-01")

getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv(), from = someDates)

My question:

  • Whilst respective orders of someSymbols and someDates must be kept and with (preferably) only the System Libraries and library(quantmod) (without which getSymbols.yahoo does not work), what piece of code could download each symbol with its corresponding date?

Thanks in advance.


Systems used:

  • R version: 4.1.1 (2021-08-10)
  • RStudio version: 1.4.1717
  • OS: macOS Catalina version 10.15.7 and macOS Big Sur version 11.6

CodePudding user response:

Use Map to pass one element at a time from each vector to the function.

library(quantmod)

someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901, 
                       18659), origin = "1970-01-01")

Map(\(x, y) getSymbols(
  Symbols = x, 
  env = globalenv(), 
  auto.assign = TRUE, 
  from = y,
  src = "yahoo"), someSymbols, someDates)

CodePudding user response:

We may use map2

library(quantmod)
library(purrr)
library(dplyr)
map2(someSymbols, someDates, ~ getSymbols(.x, 
                 env = globalenv(), 
                 auto.assign = TRUE,
                 from = .y, 
                 src = "yahoo"))

-checking

> head(RMO)
           RMO.Open RMO.High RMO.Low RMO.Close RMO.Volume RMO.Adjusted
2021-02-01    18.10    19.25   17.17     18.54    9507500        18.54
2021-02-02    19.01    19.42   18.01     18.64    5996100        18.64
2021-02-03    19.04    19.52   18.35     18.47    5531300        18.47
2021-02-04    18.98    19.16   17.93     18.44    5004800        18.44
2021-02-05    18.77    19.25   18.38     18.44    4862800        18.44
2021-02-08    18.73    20.00   18.52     19.36    5616900        19.36
> head(NKLA)
           NKLA.Open NKLA.High NKLA.Low NKLA.Close NKLA.Volume NKLA.Adjusted
2021-02-01     24.05    24.400    22.12      23.51    13861900         23.51
2021-02-02     23.74    23.970    22.56      23.00     8487200         23.00
2021-02-03     23.09    24.980    23.01      24.43    12034600         24.43
2021-02-04     24.46    25.225    23.82      24.42     8250700         24.42
2021-02-05     24.75    24.920    23.00      23.60     8481300         23.60
2021-02-08     23.51    23.980    23.17      23.50     7934000         23.50
  • Related