下载雅虎股票价格在R

时间:2022-09-01 12:36:53

This is a newbie question in R. I am downloading yahoo finance monthly stock price data using R where the ticker names are read from a text file. I am using a loop to read the ticker names to download the data and putting them in a list. My problem is some ticker names may not be correct thus my code stops when it encounters this case. I want the following.

这是一个新手的问题在R。我正在下载雅虎财经每月的股票价格数据,使用R从文本文件中读取股票代码。我正在使用一个循环来读取股票代码以下载数据并将其放入列表中。我的问题是有些代码可能不正确,所以当遇到这种情况时,我的代码就会停止。我想要以下。

  1. skip the ticker name if it is not correct.
  2. 如果代码不正确,请跳过它。
  3. Each element in the list is a dataframe. I want the ticker names to be appended to variable names in element dataframes.
  4. 列表中的每个元素都是一个dataframe。我希望在元素dataframes中将ticker名称附加到变量名称。
  5. I need an efficient way to create a dataframe that has the closing prices as variables.
  6. 我需要一种有效的方法来创建一个以收盘价作为变量的dataframe。

Here is the sample code for the simplified version of my problem.

这是简化版问题的示例代码。

library(tseries)  
tckk <- c("MSFT", "C", "VIA/B", "MMM") # ticker names defined  
numtk <- length(tckk);  
ustart <- "2000-12-30";
uend <- "2007-12-30" # start and end date  
all_dat <- list(); # empty list to fill in the data  
for(i in 1:numtk)  
{  
  all_dat[[i]] <- xxx <- get.hist.quote(instrument = tckk[i], start=ustart, end=uend, quote = c("Open", "High", "Low", "Close"), provider = "yahoo", compression = "m")  
}   

The code stops at the third entry but I want to skip this ticker and move on to "MMM". I have heard about Trycatch() function but do not know how to use it.

代码在第三个条目停止,但是我想跳过这个代码,然后继续“MMM”。我听说过Trycatch()函数,但不知道如何使用它。

As per question 2, I want the variable names for the first element of the list to be "MSFTopen", "MSFThigh", "MSFTlow", and "MSFTclose". Is there a better to way to do it apart from using a combination of loop and paste() function.

根据问题2,我希望列表的第一个元素的变量名称为“MSFTopen”、“msf大腿”、“MSFTlow”和“MSFTclose”。除了使用循环和粘贴()函数的组合之外,还有更好的方法来实现它吗?

Finally, for question 3, I need a dataframe with three columns corresponding to closing prices. Again, I am trying to avoid a loop here.

最后,对于问题3,我需要一个数据aframe,包含与收盘价相对应的三列。同样,我试图避免这里的循环。

Thank you.

谢谢你!

7 个解决方案

#1


21  

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

最好的方法是使用quantmod并将结果存储为一个时间序列(在这种情况下,结果将是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=".")])
}

#2


21  

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.

这也有点晚了……如果你只想用R的基本函数获取数据而不处理任何附加包,只需使用函数read.csv(URL),其中的URL是指向Yahoo的正确位置的字符串。数据将作为一个dataframe被拉入,您需要将“Date”从字符串转换为日期类型,以使任何图看起来都很漂亮。下面是简单的代码片段。

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.

使用R的基本函数可以使您对数据操作有更多的控制。

#3


8  

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.

在TTR中,stockSymbols函数从纳斯达克网站获取仪器符号,并调整符号以与Yahoo!融资。它目前为美国证券交易所、纽约证券交易所和纳斯达克提供了6500个符号。您还可以查看stockSymbols中的代码,该代码调整代码以与Yahoo!兼容!财务可能调整你文件中的一些报价。

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.

注意:CRAN上的TTR版本的stockSymbols由于纳斯达克(nasdaq)的一个更改而被破坏,但是它在TTR -forge版本的TTR中被修复。

#4


3  

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)

#5


1  

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.

如果你的最终目标是获取数据,那么新包装tidyquant可能更适合这个目标。

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!

这将扩展到任意数量的股票,所以1000个股票报价的文件应该可以正常工作!

#6


1  

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

从上面的解决方案略作修改……(由于巴蒂尔和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
 }

#7


0  

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.

不幸的是,网址“ichart.finance.yahoo.com”已经死了,现在还没有工作。据我所知,雅虎关闭了它,看起来它不会被打开。

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

几天前,我发现了一个不错的替代品(https://eodhistoricaldata.com/),它的API与Yahoo Finance非常相似。

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

基本上,对于上面描述的R-script你只需要改变这一部分:

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.

然后添加一个API密钥,它将以与以前相同的方式工作。我为我的r -script节省了很多时间。

#1


21  

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

最好的方法是使用quantmod并将结果存储为一个时间序列(在这种情况下,结果将是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=".")])
}

#2


21  

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.

这也有点晚了……如果你只想用R的基本函数获取数据而不处理任何附加包,只需使用函数read.csv(URL),其中的URL是指向Yahoo的正确位置的字符串。数据将作为一个dataframe被拉入,您需要将“Date”从字符串转换为日期类型,以使任何图看起来都很漂亮。下面是简单的代码片段。

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.

使用R的基本函数可以使您对数据操作有更多的控制。

#3


8  

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.

在TTR中,stockSymbols函数从纳斯达克网站获取仪器符号,并调整符号以与Yahoo!融资。它目前为美国证券交易所、纽约证券交易所和纳斯达克提供了6500个符号。您还可以查看stockSymbols中的代码,该代码调整代码以与Yahoo!兼容!财务可能调整你文件中的一些报价。

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.

注意:CRAN上的TTR版本的stockSymbols由于纳斯达克(nasdaq)的一个更改而被破坏,但是它在TTR -forge版本的TTR中被修复。

#4


3  

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)

#5


1  

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.

如果你的最终目标是获取数据,那么新包装tidyquant可能更适合这个目标。

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!

这将扩展到任意数量的股票,所以1000个股票报价的文件应该可以正常工作!

#6


1  

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

从上面的解决方案略作修改……(由于巴蒂尔和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
 }

#7


0  

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.

不幸的是,网址“ichart.finance.yahoo.com”已经死了,现在还没有工作。据我所知,雅虎关闭了它,看起来它不会被打开。

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

几天前,我发现了一个不错的替代品(https://eodhistoricaldata.com/),它的API与Yahoo Finance非常相似。

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

基本上,对于上面描述的R-script你只需要改变这一部分:

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.

然后添加一个API密钥,它将以与以前相同的方式工作。我为我的r -script节省了很多时间。