【发布时间】:2013-07-13 19:09:04
【问题描述】:
所以我编写了一些代码,使用一维数组中包含的 Ticker 符号“列表”从 yahoo Finance 中提取 csv 文件。我面临的挑战是其中一个股票代码可能没有任何数据(或者可能输入错误)。所以我构建了一个 tryCatch 命令,但它运行得不是很好。下面是我的代码(我使用 source("Name of the code") 访问它,然后是它生成的错误:
#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)
}
}
此代码生成以下错误:
Error in if (file == "") file <- stdin() else { :
missing value where TRUE/FALSE needed
我知道这是 if 条件中的一个问题,我想知道是否需要在某处放置“==TRUE”语句。
感谢您的帮助!
注意: 我已经在没有 for 循环的情况下运行了代码,只需设置 Ticker
options(warn = 2)
这会使所有警告出错,但仍然没有乐趣。
【问题讨论】:
-
不是提供需要用户输入值的函数,而是为我们硬编码这些值,以便我们都可以重现相同的东西。例如而不是
Month_Begin <- Get_Month_Begin(),只是为了示例的缘故向我们展示Month_Begin <- 1。此外,我们无法重现setwd(personal_directory)和setwd("Ticker Data")之类的内容。 -
如果您还没有听说过
quantmod包:它有一个函数getSymbols,可以处理从雅虎和其他来源下载的数据。顺便说一句:另一个提供数据和R接口的有趣网站是quandl.com(最近才发现并有时间测试它)。 -
谢谢!我真的很感激。
标签: r for-loop error-handling