Downloading Yahoo stock prices in R

≡放荡痞女 提交于 2019-11-28 15:40:20
Shane

Your best bet is to use quantmod and store the results as a time series (in this case, it will be xts):

library(quantmod)
library(plyr)
symbols <- c("MSFT","C","VIA/B","MMM")

#1
l_ply(symbols, function(sym) try(getSymbols(sym))) 
symbols <- symbols[symbols %in% ls()]

#2
sym.list <- llply(symbols, get) 

#3
data <- xts()
for(i in seq_along(symbols)) {
    symbol <- symbols[i]
    data <- merge(data, get(symbol)[,paste(symbol, "Close", sep=".")])
}
stotastic

This also a little late...If you want to grab data with just R's base functions without dealing with any add-on packages, just use the function read.csv(URL), where the URL is a string pointing to the right place at Yahoo. The data will be pulled in as a dataframe, and you will need to convert the 'Date' from a string to a Date type in order for any plots to look nice. Simple code snippet is below.

URL <- "http://ichart.finance.yahoo.com/table.csv?s=SPY"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")

Using R's base functions may give you more control over the data manipulation.

I'm a little late to the party, but I think this will be very helpful to other late comers.

The stockSymbols function in TTR fetches instrument symbols from nasdaq.com, and adjusts the symbols to be compatible with Yahoo! Finance. It currently returns ~6,500 symbols for AMEX, NYSE, and NASDAQ. You could also take a look at the code in stockSymbols that adjusts tickers to be compatible with Yahoo! Finance to possibly adjust some of the tickers in your file.

NOTE: stockSymbols in the version of TTR on CRAN is broken due to a change on nasdaq.com, but it is fixed in the R-forge version of TTR.

René Bauch

I do it like this, because I need to have the historic pricelist and a daily update file in order to run other packages:

library(fImport)

fecha1<-"03/01/2009"
fecha2<-"02/02/2010"

Sys.time()

y <- format(Sys.time(), "%y")    
m <- format(Sys.time(), "%m")    
d <- format(Sys.time(), "%d")
fecha3 <- paste(c(m,"/",d,"/","20",y), collapse="")

write.table(yahooSeries("GCI", from=fecha1, to=fecha2), file = "GCI.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)
write.table(yahooSeries("GCI", from=fecha2, to=fecha3), file = "GCIupdate.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)

GCI <- read.table("GCI.txt") 
GCI1 <- read.table("GCIupdate.txt")
GCI <- rbind(GCI1, GCI)
GCI <- unique(GCI)

write.table(GCI, file = "GCI.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)

If your ultimate goal is to get the data.frame of three columns of closing prices, then the new package tidyquant may be better suited for this.

library(tidyquant)

symbols <- c("MSFT", "C", "VIA/B", "MMM")

# Download data in tidy format. 
# Will remove VIA/B and warn you.
data <- tq_get(symbols)

# Ticker symbols as column names for closing prices
data %>% 
    select(.symbol, date, close) %>% 
    spread(key = .symbol, value = close)

This will scale to any number of stocks, so the file of 1000 tickers should work just fine!

Manoj Kumar

Slightly modified from the above solutions... (thanks Shane and Stotastic)

 symbols <- c("MSFT", "C", "MMM")

 # 1. retrieve data

 for(i in seq_along(symbols)) {
   URL <- paste0("http://ichart.finance.yahoo.com/table.csv?s=", symbols[i])
   dat <- read.csv(URL)
   dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
   assign(paste0(symbols[i]," _data"), dat)
   dat <- NULL
 }
Denis Alaev

Unfortunately, URL "ichart.finance.yahoo.com" is dead and not working now. As I know, Yahoo closed it and it seems it will not be opened.

Several days ago I found nice alternative (https://eodhistoricaldata.com/) with an API very similar to Yahoo Finance.

Basically, for R-script described above you just need to change this part:

URL <- paste0("ichart.finance.yahoo.com/table.csv?s=", symbols[i])

to this:

URL <- paste0("eodhistoricaldata.com/api/table.csv?s=", symbols[i])

Then add an API key and it will work in the same way as before. I saved a lot of time for my R-scripts on it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!