2

I have the following code: It saves historic stock data from the csv file, what is exactly what i need, however all the data is in environment format and i cannot work with it. Is it possible to adjust the code so that the information would be saved as "data" for easier processing. Maybe there i a way to transfer the Environment to friendlier format, that can be a solution. All in all, i have information that i need but i don't know hot to use it now :)

#install.packages("quantmod")
library("quantmod")
#Script to download prices from yahoo
#and Save the prices to a RData file
#The tickers will be loaded from a csv file

#Script Parameters
tickerlist <- "sp500.csv"  #CSV containing tickers on rows
savefilename <- "stockdata.RData" #The file to save the data in
startDate = as.Date("2005-01-13") #Specify what date to get the prices from
maxretryattempts <- 5 #If there is an error downloading a price how many times to retry

#Load the list of ticker symbols from a csv, each row contains a ticker
stocksLst <- read.csv("sp500.csv", header = F, stringsAsFactors = F)
stockData <- new.env() #Make a new environment for quantmod to store data in
nrstocks = length(stocksLst[,1]) #The number of stocks to download

#Download all the stock data
for (i in 1:nrstocks){
for(t in 1:maxretryattempts){

   tryCatch(
       {
           #This is the statement to Try
           #Check to see if the variables exists
           #NEAT TRICK ON HOW TO TURN A STRING INTO A VARIABLE
           #SEE  http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/
            if(!is.null(eval(parse(text=paste("stockData$",stocksLst[i,1],sep=""))))){
                #The variable exists so dont need to download data for this stock
                #So lets break out of the retry loop and process the next stock
                #cat("No need to retry")
                break
            }

          #The stock wasnt previously downloaded so lets attempt to download it
          cat("(",i,"/",nrstocks,") ","Downloading ", stocksLst[i,1] , "\t\t Attempt: ", t , "/", maxretryattempts,"\n")
          getSymbols(stocksLst[i,1], env = stockData, src = "yahoo", from = startDate)
       }
    #Specify the catch function, and the finally function
   , error = function(e) print(e))
 }
}

#Lets save the stock data to a data file
tryCatch(
{
save(stockData, file=savefilename)
cat("Sucessfully saved the stock data to %s",savefilename)
}
, error = function(e) print(e))
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360

3 Answers3

1

Is the process of creating a new environment for your data (stockData <- new.env()) typical for this function?

Your for loop doesn't actually assign any objects, it just prints the result of getSymbols. You could store these in a list, i.e.

stockData[i] <- getSymbols(<stuff>)

Aside: Consider the newer tidyquant package which stores the same results in a tidy format (tibble): https://github.com/mdancho84/tidyquant

Jonathan Carroll
  • 3,897
  • 14
  • 34
1

Set up a test environment e containing IBM and MSFT. Then given a vector of those stocks plus GOOG and TSLA use setdiff to exclude the ones already in e downloading the remainder:

# test data
e <- new.env()
stks <- c("IBM", "MSFT", "GOOG", "TSLA")
getSymbols(stks[1:2], env = e)

# run
rest <- setdiff(stks, ls(e))
getSymbols(rest, env = e)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

If I understand the question, you are ultimately trying to get the stock prices for every stock in an index. This is a good usage case for the tidyquant package since the package already has error-handling built in. The errors are returned as NAs, and removed from the results while providing a warning message to the user.

Try this from the tidyquant package, v0.3.0 (current CRAN version):

    library(tidyquant)
    sp_500 <- tq_get("SP500", get = "stock.index") %>%
        tq_get(get = "stock.prices")

Note that the code chunk above is changing. We are deprecating the get = "stock.index" option from tq_get() for a distinct index function, tq_index(). Here's what it will be in the next CRAN version, and what it is released in the dev version, v0.3.0.9030:

    library(tidyquant)
    sp_500 <- tq_index("SP500") %>%
        tq_get(get = "stock.prices", complete_cases = TRUE)

The default is complete_cases = TRUE. Change complete_cases = FALSE if you would like to return values with NAs. Note that an NA is returned if stock prices cannot be retrieved such as due to an incorrect symbol. If an NA is present you will get a "nested" data frame and a warning message stating which symbol had the error.

Matt Dancho
  • 6,840
  • 3
  • 35
  • 26