尝试复制答案时出现 Quantmod、getSymbols 错误 [英] Quantmod, getSymbols error trying to replicate answer

查看:55
本文介绍了尝试复制答案时出现 Quantmod、getSymbols 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了 Quantmod 包并且一直在玩 getSymbols.我希望能够像这个问题一样获取多只股票的数据:getSymbols 并使用 lapply、Cl 和 merge 来提取收盘价.

I just downloaded the package Quantmod and have been playing with getSymbols. I want to be able to get data for multiple stocks as in this question: getSymbols and using lapply, Cl, and merge to extract close prices.

不幸的是,当我试图复制答案时:

Unfortuantely, when I try to duplicate the answer:

tickers <- c("SPY","DIA","IWM","SMH","OIH","XLY",
         "XLP","XLE","XLI","XLB","XLK","XLU")
getSymbols(tickers, from="2001-03-01", to="2011-03-11")

我收到以下错误消息:

Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
cannot open URL 
'http://chart.yahoo.com/table.csv?s=SPY&a=2&b=01&c=2001&d=2&e=11&f=2011&g=d&q=q&y=0&z=SPY&x=.csv'
In addition: Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,
: cannot open: HTTP status was '0 (null)'

这是我的 sessionInfo()

Here is my sessionInfo()

R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantmod_0.4-0 TTR_0.22-0     xts_0.9-7      zoo_1.7-10     Defaults_1.1-1

loaded via a namespace (and not attached):
[1] grid_3.0.2      lattice_0.20-23 tools_3.0.2    

推荐答案

回应 OP 的评论:

In response to OP's comment:

因此,至少可以说,提供历史数据免费下载的网站似乎很古怪.它们不一定适用于所有有效符号,有时它们会无缘无故变得不可用.ichart.yahoo.com/table.csv 24 小时前为我工作,但目前(对我)不起作用.这可能是因为雅虎对我的 IP 进行了 24 小时锁定,如果他们检测到可解释为 DDOS 攻击的活动,他们就会这样做.或者可能是其他原因......

So the bottom line seems to be that sites which provide free downloads of historical data are quirky, to say the least. They do not necessarily work for all valid symbols, and sometimes they become unavailable for no apparent reason. ichart.yahoo.com/table.csv worked for me 24 hours ago, but does not work (for me) at the moment. This may be because Yahoo has imposed a 24 hour lockout on my IP, which they will do if they detect activity interpretable as a DDOS attack. Or it might be for some other reason...

下面用于查询 Google 的更新代码对除 DJIA 之外的所有内容都有效(再次,目前).请注意,如果您指定交易所和交易品种 (EXCHANGE:SYMBOL),则成功率更高.没有交换,我无法下载 SMH.最后,如果您遇到问题,请尝试取消对打印语句的注释并将 URL 粘贴到浏览器中以查看会发生什么.

The updated code below, which queries Google, does work (again, at the moment), for everything except DJIA. Note that there is more success if you specify the exchange and the symbol (EXCHANGE:SYMBOL). I was unable to download SMH without the exchange. Finally, if your are having problems try uncommenting the print statement and pasting the url into a browser to see what happens.

tickers <- c("SPY","DJIA","IWM","NYSEARCA:SMH","OIH","XLY",
             "XLP","XLE","XLI","XLB","XLK","XLU")

g <- function(x,from,to,output="csv") {
  uri      <- "http://www.google.com/finance/historical"
  q.symbol <- paste("q",x,sep="=")
  q.from   <- paste("startdate",from,sep="=")
  q.to     <- paste("enddate",to,sep="=")
  q.output <- paste("output",output,sep="=")
  query    <- paste(q.symbol,q.output,q.from,q.to,sep="&")
  url      <- paste(uri,query,sep="?")
  # print(url)
  try(assign(x,read.csv(url),envir=.GlobalEnv))
}
lapply(tickers,g,from="2001-03-01",to="2011-03-11",output="csv")

你可以从圣路易斯联邦储备银行下载大疆,非常可靠.不幸的是,你得到了所有这些(从 1896 年开始),而且它是一个时间序列.

You can download DJI from the St. Louis Fed, which is very reliable. Unfortunately, you get all of it (from 1896), and it's a time series.

getSymbols("DJIA",src="FRED")

原始回复:

这对我有用,除了 SMH 和 OIH.

This worked for me, for everything except SMH and OIH.

tickers <- c("SPY","DJIA","IWM","SMH","OIH","XLY",
                             "XLP","XLE","XLI","XLB","XLK","XLU")

f <- function(x) {
  uri    <- "http://ichart.yahoo.com/table.csv"
  symbol <- paste("s",x,sep="=")
  from   <- "a=2&b=1&c=2001"
  to     <- "d=2&e=11&f=2011"
  period <- "g=d"
  ignore <- "ignore=.csv"
  query  <- paste(symbol,from,to,period,ignore,sep="&")
  url    <- paste(uri,query,sep="?")
  try(assign(x,read.csv(url),envir=.GlobalEnv))
}
lapply(tickers,f)

这与 getSymbols(...) 之间的主要区别在于它使用 ichart.yahoo.com(如此处),而 getSymbols(...) 使用 chart.yahoo.com.前者似乎更可靠.根据我的经验,在 Yahoo 中使用 getSymbols(...) 是一件令人头疼的事.

The main difference between this and getSymbols(...) is that this uses ichart.yahoo.com (as documented here), whereas getSymbols(...) uses chart.yahoo.com. The former seems to be much more reliable. In my experience, using getSymbols(...) with Yahoo is a monumental headache.

如果@user2492310 的建议使用 src="google" 对您有用,那么显然这是要走的路.它对我不起作用.

If the suggestion of @user2492310, to use src="google" works for you, then clearly this is the way to go. It didn't work for me.

另一个注意事项:SMH 和 OIH 在 2001 年不存在.其他都至少可以追溯到 2000 年.因此,如果您提供,则 ichart.yahoo.com(和 chart.yahoo.com)可能会抛出错误交易品种操作范围之外的日期范围.

One other note: SMH and OIH did not exist in 2001. The others all go back to at least 2000. So it might be that ichart.yahoo.com (and chart.yahoo.com) throws an error if you provide a date range outside of the symbol's operating range.

这篇关于尝试复制答案时出现 Quantmod、getSymbols 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆