0

Here are the codes I start:

mdate <- "2016-01-04"
edate <- "2016-03-09"
tickers <- c("ABG","ACH","ADM","AEG","AEM","AGQ","AGRO","AKOb","APO","ARCO","ASA") # actual tickers should be more than 2000

for(ticker in tickers)
  High_Raw <- cbind(High_Raw, getSymbols(ticker, from = mdate, to = edate, auto.assign = F))

As you expected the matrix shows Open_ABG, High_ABG, Low_ABG...... Open_ACH, High_ACH....and so on.

I want to organize these data like; Open_ABG, Open_ACH... Open_ASA, High_ABG, High ACH.... High_ASA, Low_ABG..... and so on

I know that I can use a code;

High_Raw <- cbind(High_Raw, getSymbols(ticker, from = mdate, to = edate, auto.assign = F))[,2] 
Low_Raw <- cbind(Low_Raw, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,3]

but, there are errors; High_Raw contains 50 tickers and Low_Raw has 100 tickers because of errors. Since I try to import more than 2000 companies' data, this way wouldn't work for this.

What can I do to do this?

  • Welcome to stackoverflow. I guess the function `getSymbol` is not base R. Could you specify the package you are you using? I would also suggest [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) post to help us help you. – DJJ May 26 '20 at 06:36
  • Also, we don't have `High_Raw` – Chris Ruehlemann May 26 '20 at 06:39
  • The package is quantmod and High_Raw is a new one that stores those stock data. – Dongchul Park May 26 '20 at 07:21

1 Answers1

0

Don't grow your objects in for loop. Use lapply :

library(dplyr)
library(quantmod)

tickers <- c("ABG","ACH","ADM")

data <- do.call(cbind.data.frame, lapply(tickers, function(x) 
               getSymbols(x, from = mdate, to = edate, auto.assign = F)))

You can then use :

data %>% 
  select(contains('Open'), contains('High'), contains('Low'), contains('Close'))

#           ABG.Open ACH.Open ADM.Open ABG.High ACH.High ....
#2016-01-04     66.2     7.87     36.1     67.3     7.91 ....
#2016-01-05     66.7     7.94     35.8     67.1     7.95 ....
#2016-01-06     63.8     7.82     35.7     63.8     7.88 ....
#2016-01-07     56.2     7.40     34.9     57.7     7.52 ....
#2016-01-08     55.8     7.62     34.8     56.5     7.64 ....
#2016-01-11     53.6     7.37     34.5     55.9     7.39 ....
#....

You could also include 'Volume' and 'Adjusted' if you want them in final output.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213