5
$\begingroup$

I just started with quantmod package. If I want to select stocks based on their recent performance, then I need to loop through all the stocks in, say, NYSE. So I need:

  1. get all the stock symbols
  2. select those symbols that do have data
  3. loop through them one by one(say, each time, download the stock data as $X$, and do some analysis. Then loop to next one, and set it as $X$)

My question is: how can I do these? Here is part of my thought:

  1. use stockSymbols() function in TTR package: AllSym <- stockSymbols(). The first column of AllSym are all the potential symbols.

  2. Use getSymbols(AllSym[i,1]) to loop all the stock i:

Questions here:

  1. Not all the symbols from TTR have data. Some may have error when using getSymbols. I actually do not care about this certain stock. So how can I continue with the loop? [How can I use the try function?]

  2. The getSymbols function will automatically make the stock symbols as the variables. How can I copy(or set) it to $X$(instead of something like $APPL$)?

  3. How can I loop trough the variables with their names saved as strings in a vector? For example, if we do use getSymbols function to get data with name as the stock symbol. How can I loop through those stocks?[Note that their variable name are stored in first column AllSym as string]

Thank you very much!

$\endgroup$
10
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Is there any way to keep the loop going without break? $\endgroup$ – Mehdi Zare Sep 13 '17 at 19:31
  • $\begingroup$ most likely with a try() function $\endgroup$ – Rime Sep 18 '17 at 8:45
  • $\begingroup$ Check out errors and exceptions documentation at docs.python.org/3/tutorial/errors.html All it requires is a try: block and in the except: block just put pass unless you want to do something like print a statement and the stock that caused the error. $\endgroup$ – Shanemeister Apr 14 '18 at 17:06
1
$\begingroup$

You may refer this :

use the latest package in CRAN "BatchGetSymbols" for downloading OHLC data for multiple tickers in a single data frame.

You may not need to construct the loop for this.

Refer the following link for more information https://cran.r-project.org/web/packages/BatchGetSymbols/vignettes/BatchGetSymbols-vignette.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.