So I have written some code to pull csv files off yahoo finance using a "list" of Ticker symbols contained within a 1-dimensional array. The challenge I'm having is that one of the ticker symbols might not have any data (or may have been entered wrong). So I built a tryCatch command, but it's not working very well. Below is my code (which I access using source("Name of the code"), followed by the error it generates:
#URL Builder for Yahoo Finance
#Requests Input from User, Builds URL, downloads csv.file from site
#Requests are for:
#Ticker (2-4 letter - character string)
#Start Month (00 - 11 integer)
#Start Day (1 - 31 integer)
#Start Year (Four digit integer)
#End Month (00 - 11 integer)
#End Day (1 - 31 integer)
#End Year (Four digit integer)
#Retrieve Ticker File
setwd(personal_directory)
#tickers <- read.csv("Tickers.csv")
#Here are some example tickers, since you will not have the Ticker.csv file (the S ticker generates the error to be handled
tickers <- data.frame(Ticker = c("XOM", "DVN", "S"))
tickers <- tickers[order(tickers[,1]),]
setwd("Ticker Data")
#Functions
Get_Month_Begin <- function(){as.numeric(readline("Enter the start month 00 - 11(MM):>>> "))}
Get_Day_Begin <- function(){as.numeric(readline("Enter the start day (1-31) :>>> "))}
Get_Year_Begin <- function(){as.numeric(readline("Enter the start year (YYYY) :>>> "))}
Get_Month_End <- function(){as.numeric(readline("Enter the end month (MM) :>>> "))}
Get_Day_End <- function(){as.numeric(readline("Enter the end day (1-31) :>>> "))}
Get_Year_End <- function(){as.numeric(readline("Enter the end year :>>> "))}
#Function Calls
Month_Begin <- Get_Month_Begin()
Day_Begin <- Get_Day_Begin()
Year_Begin <- Get_Year_Begin()
Month_End <- Get_Month_End()
Day_End <- Get_Day_End()
Year_End <- Get_Year_End()
#Build URL
#Example URL: http://ichart.finance.yahoo.com/table.csvs=DVN&a=00&b=1&c=1992&d=11&e=31&f=2013&g=d&ignore=.csv
CSV_Base_URL <- "http://ichart.finance.yahoo.com/table.csv?s="
yahoo_data_date_format <- "%Y-%m-%d"
for(i in 1:nrow(tickers)){
Ticker <- tickers[i, 1]
CSV_URL_Complete <- paste(CSV_Base_URL,Ticker,"&a=",Month_Begin,"&b=",Day_Begin,"&c=",Year_Begin,"&d=",Month_End,"&e=",Day_End,"&f=",Year_End,"&g=d&ignore=.csv",sep="")
#Download CSV
options(warn=2)
potential_error <- tryCatch(Yahoo_Finance_TBL <- read.csv(CSV_URL_Complete), error = function(e) e)
if(!inherits(potential_error, "error")){
Yahoo_Finance_TBL <- Yahoo_Finance_TBL[,c(1,7)]
colnames(Yahoo_Finance_TBL) <- gsub(" ", ".", colnames(Yahoo_Finance_TBL))
Yahoo_Finance_TBL[, 1] <- as.Date(Yahoo_Finance_TBL[, 1], yahoo_data_date_format)
#Write CSV File
write.csv(Yahoo_Finance_TBL, file=paste(Ticker,"_Yahoo_Finance_File.csv", sep=""), row.names=FALSE)
}
}
This code generates the following error:
Error in if (file == "") file <- stdin() else { :
missing value where TRUE/FALSE needed
I know this is a problem in the if-condition, and I'm wondering if I need to put an "==TRUE" statement in there somewhere.
Thank you for your help!
NOTE: I have run the code without the for-loop, simply setting Ticker <- "S" to see what type of error is generated. This turns out to be a warning (as opposed to an error), so I wrote the following code (edited above):
options(warn = 2)
This makes all warnings errors, but there is still no joy.