Skip to main content
`library` does not accept more than one package at a time
Source Link
Joshua Ulrich
  • 3.2k
  • 3
  • 22
  • 37

Try the following:

library(quantmod,)  # also loads xts, and TTR)

# Fetch all Symbols & store only the tickers to retrieve the data
symbols <- stockSymbols()
symbols <- symbols[,1]

Next we will specify where to to store data

dataset<- xts() # Only run once

The following code is the loop that will download OHLC data to your environment. It will then store the Adjusted Close of the downloaded companies and merge them in dataset

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
for(i in 1:length(symbols)) {
  symbols[i]-> symbol
# specify the "from" date to desired start date
  tryit <- try(getSymbols(symbol,from="2014-01-01", src='yahoo'))
  if(inherits(tryit, "try-error")){
    i <- i+1
  } else {
  # specify the "from" date to desired start date
  data <- getSymbols(symbol, from="2014-01-01", src='yahoo')
  dataset <- merge(dataset, Ad(get(symbols[i])))
  rm(symbol)
  }
  setTxtProgressBar(pb, i)
}

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) { 
  symbols[i]-> symbol
...

Keep on doing it until symbols is exhausted. Remember that it will download all the data unto your environment & that all the Adjusted Close prices are to be found in dataset

Try the following:

library(quantmod,xts,TTR)

# Fetch all Symbols & store only the tickers to retrieve the data
symbols <- stockSymbols()
symbols <- symbols[,1]

Next we will specify where to to store data

dataset<- xts() # Only run once

The following code is the loop that will download OHLC data to your environment. It will then store the Adjusted Close of the downloaded companies and merge them in dataset

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
for(i in 1:length(symbols)) {
  symbols[i]-> symbol
# specify the "from" date to desired start date
  tryit <- try(getSymbols(symbol,from="2014-01-01", src='yahoo'))
  if(inherits(tryit, "try-error")){
    i <- i+1
  } else {
  # specify the "from" date to desired start date
  data <- getSymbols(symbol, from="2014-01-01", src='yahoo')
  dataset <- merge(dataset, Ad(get(symbols[i])))
  rm(symbol)
  }
  setTxtProgressBar(pb, i)
}

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) { 
  symbols[i]-> symbol
...

Keep on doing it until symbols is exhausted. Remember that it will download all the data unto your environment & that all the Adjusted Close prices are to be found in dataset

Try the following:

library(quantmod)  # also loads xts and TTR

# Fetch all Symbols & store only the tickers to retrieve the data
symbols <- stockSymbols()
symbols <- symbols[,1]

Next we will specify where to to store data

dataset<- xts() # Only run once

The following code is the loop that will download OHLC data to your environment. It will then store the Adjusted Close of the downloaded companies and merge them in dataset

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
for(i in 1:length(symbols)) {
  symbols[i]-> symbol
# specify the "from" date to desired start date
  tryit <- try(getSymbols(symbol,from="2014-01-01", src='yahoo'))
  if(inherits(tryit, "try-error")){
    i <- i+1
  } else {
  # specify the "from" date to desired start date
  data <- getSymbols(symbol, from="2014-01-01", src='yahoo')
  dataset <- merge(dataset, Ad(get(symbols[i])))
  rm(symbol)
  }
  setTxtProgressBar(pb, i)
}

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) { 
  symbols[i]-> symbol
...

Keep on doing it until symbols is exhausted. Remember that it will download all the data unto your environment & that all the Adjusted Close prices are to be found in dataset

Source Link
Rime
  • 951
  • 1
  • 8
  • 19

Try the following:

library(quantmod,xts,TTR)

# Fetch all Symbols & store only the tickers to retrieve the data
symbols <- stockSymbols()
symbols <- symbols[,1]

Next we will specify where to to store data

dataset<- xts() # Only run once

The following code is the loop that will download OHLC data to your environment. It will then store the Adjusted Close of the downloaded companies and merge them in dataset

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
for(i in 1:length(symbols)) {
  symbols[i]-> symbol
# specify the "from" date to desired start date
  tryit <- try(getSymbols(symbol,from="2014-01-01", src='yahoo'))
  if(inherits(tryit, "try-error")){
    i <- i+1
  } else {
  # specify the "from" date to desired start date
  data <- getSymbols(symbol, from="2014-01-01", src='yahoo')
  dataset <- merge(dataset, Ad(get(symbols[i])))
  rm(symbol)
  }
  setTxtProgressBar(pb, i)
}

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# cool progress bar to see the % of completion
n <- length(symbols)
pb <- txtProgressBar(min = 0, max = n, style=3)


# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) { 
  symbols[i]-> symbol
...

Keep on doing it until symbols is exhausted. Remember that it will download all the data unto your environment & that all the Adjusted Close prices are to be found in dataset