Quantmod error, getSymbols trying to replicate a response

I just downloaded the Quantmod package and played with getSymbols . I want to be able to receive data for several stocks, as in this issue: getSymbols and use lapply, Cl and merge to extract close prices .

Unfortunately, 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") 

The following error message appears:

 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)' 

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 
+7
r quantmod
source share
1 answer

EDIT: In response to the OP comment:

Thus, apparently, sites that provide free download of historical data are fancy, to say the least. They do not necessarily work for all valid characters, and sometimes they become inaccessible for no apparent reason. ichart.yahoo.com/table.csv worked for me 24 hours ago, but at the moment it does not work (for me). Perhaps this is due to the fact that Yahoo has imposed a 24-hour block on my IP address, which they will do if they detect activity interpreted as a DDOS attack. Or it could be for some other reason ...

The updated code below that Google asks for works (again, for now), for everything except DJIA. Note that there is more success if you specify the exchange and the character ( EXCHANGE:SYMBOL ). I was unable to download SMH without sharing. Finally, if you are having problems, try uncommenting the print expression and paste the URL into the 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") 

You can download DJI from the St. Louis Fed, which is very reliable. Unfortunately, you get all this (since 1896), and these are time series.

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

Original answer:

It 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) 

The main difference between this and getSymbols(...) is that it uses ichart.yahoo.com (as described here ), while getSymbols(...) uses chart.yahoo.com . The first seems much more reliable. In my experience, using getSymbols(...) with Yahoo is a monumental headache.

If the suggestion @ user2492310, using src="google" works for you, then clearly this is the way to go. This did not work for me.

One more note: SMH and OIH did not exist in 2001. The rest will all return at least 2000. Thus, it may be that ichart.yahoo.com (and chart.yahoo.com) throws an error if you provide a date range outside of the character's working range.

+4
source share

All Articles