无法从R中的谷歌下载数据

时间:2017-06-16 09:16:19

标签: r quantmod

我正在使用两个文件。第一个文件是实际代码,第二个文件是参考文件

##########################################
## Daily prices from Yahoo 
## thertrader@gmail.com - Nov. 2015
##########################################
library(quantmod)

startDate = "2014-01-01"
thePath = "D:\\daily\\data\\"
cattest <- file("D:\\daily\\error\\error.csv", open = "w") 
source(paste("D:\\Reference\\","listOfInstruments.r",sep=""))
cattest <- file("D:\\daily\\error\\error.csv", open = "a") 
for (ii in theInstruments)
tryCatch(
{
 print(ii)
 data = getSymbols(Symbols = ii, 
                   src = "google", 
                   from = startDate, 
                   auto.assign = TRUE,verbose = TRUE)
 colnames(data) = c("Date","Open","High","Low","Close","Volume")
 write.zoo(data,paste(thePath,ii,".csv",sep=""),sep=",",row.names=FALSE)
}

,error=function(e){cat(ii, "\n", file=cattest)})
close(cattest)

我正在尝试使用谷歌的以下代码下载数据,但它没有下载任何内容我在这里做错了什么

Reference file

##########################################
## List of securities (Yahoo tickers) 
## thertrader@gmail.com - Nov. 2015
##########################################
theInstruments = c("^GSPC",
                   "SPY",
                   "QQQ",
                   "DDM",
                   "EFA",
                   "EEM",
                   "EWJ")

错误似乎是

done.
Warning messages:
1: In fname %in% c("break", "next") :
  closing unused connection 3 (D:\daily\error\error.csv)
2: In download.file(paste(google.URL, "q=", Symbols.name, "&startdate=",  :
  cannot open URL 'http://finance.google.com/finance/historical?q=^GSPC&startdate=Jan+01,+2014&enddate=Jun+16,+2017&output=csv': HTTP status was '404 Not Found'

1 个答案:

答案 0 :(得分:1)

显然,您无法从Google下载S&amp; P 500索引数据。因此,要么使用Yahoo Finance,要么从工具列表中排除^GSPC

theInstruments = c("SPY",
                   "QQQ",
                   "DDM",
                   "EFA",
                   "EEM",
                   "EWJ")

startDate = "2014-01-01"
  getSymbols(theInstruments, src = "google",
             from = startDate)

新代码:

startDate = "2000-01-01"
thePath = "D:\\daily\\data\\"
source(paste(thePath,"code\\listOfInstruments.r",sep=""))

for (ii in theInstruments){
 print(ii)
 data = getSymbols(Symbols = ii, 
                   src = "yahoo", 
                   from = startDate, 
                   auto.assign = FALSE)
 colnames(data) = c("open","high","low","close","volume","adj.")
 write.zoo(data,paste(thePath,ii,".csv",sep=""),sep=",",row.names=FALSE)
}