Getting data of multiple stocks into one xts object

Solution 1:

This is how you would do it with quantmod working with just xts objects

library(quantmod)
fr <- '2010-01-01'
to <- '2022-01-01'

DJIA_list <- c("MMM", "AXP", "AAPL") # extend after testing to the symbols you desire

mktdata <- new.env()

getSymbols(DJIA_list, env = mktdata, from = fr, to = to)

out <- eapply(mktdata, Ad)
out <- do.call(merge, out) # can combine with the above line if you understand what eapply() is doing)
#xts is a zoo object, so can use the write.zoo handler to write to .csv in the format of a data.frame
write.zoo(out, "dj.csv")

Solution 2:

You can get the price data easily with the tidyquant library.

library(tidyquant)
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
#> Loading required package: PerformanceAnalytics
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
#> Loading required package: quantmod
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> == Need to Learn tidyquant? ====================================================
#> Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)

DJIA_list <- c("MMM", "AXP", "AAPL", "BA", "CAT", "CVX", "CSCO", "KO", "DOW", "XOM", 
               "GS", "HD", "INTC", "IBM", "JNJ", "JPM", "MCD", "MRK", "MSFT","NKE",
               "PFE", "PG", "RTX", "TSLA", "TRV", "UNH", "VZ", "V", "WBA", "WMT")

stock_prices <- tq_get(DJIA_list,
                       get = "stock.prices",
                       from = "2010-01-01",
                       to = "2022-01-01")
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip

stock_prices %>%
  select(date, symbol, adjusted) %>%
  pivot_wider(names_from = symbol,
              values_from = adjusted) %>%
  tail()
#> # A tibble: 6 x 31
#>   date         MMM   AXP  AAPL    BA   CAT   CVX  CSCO    KO   DOW   XOM    GS
#>   <date>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2021-12-23  175.  164.  176.  204.  206.  116.  61.9  58.2  55.1  61.0  385.
#> 2 2021-12-27  177.  164.  180.  203.  206.  119.  63.0  58.7  55.9  61.9  388.
#> 3 2021-12-28  178.  164.  179.  206.  207.  119.  63.2  58.9  56.6  61.7  388.
#> 4 2021-12-29  178.  163.  179.  204.  207.  118.  63.6  59.0  57.1  61.2  386.
#> 5 2021-12-30  178.  164.  178.  203.  206.  117.  63.2  58.8  56.8  60.8  386.
#> 6 2021-12-31  178.  163.  178.  201.  207.  117.  63.0  59.2  56.7  61.2  383.
#> # ... with 19 more variables: HD <dbl>, INTC <dbl>, IBM <dbl>, JNJ <dbl>,
#> #   JPM <dbl>, MCD <dbl>, MRK <dbl>, MSFT <dbl>, NKE <dbl>, PFE <dbl>,
#> #   PG <dbl>, RTX <dbl>, TSLA <dbl>, TRV <dbl>, UNH <dbl>, VZ <dbl>, V <dbl>,
#> #   WBA <dbl>, WMT <dbl>

Created on 2022-01-14 by the reprex package (v2.0.1)