在 R 中下载雅虎股票价格

发布于 2024-09-14 18:49:50 字数 1009 浏览 4 评论 0原文

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

  1. 如果股票代码名称不正确,请跳过该名称。
  2. 列表中的每个元素都是一个数据框。我希望将股票代码名称附加到元素数据帧中的变量名称中。
  3. 我需要一种有效的方法来创建一个以收盘价作为变量的数据框。

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

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")  
}   

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

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

最后,对于问题 3,我需要一个数据框,其中三列对应于收盘价。再次,我试图避免这里出现循环。

谢谢。

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.

  1. skip the ticker name if it is not correct.
  2. Each element in the list is a dataframe. I want the ticker names to be appended to variable names in element dataframes.
  3. I need an efficient way to create a dataframe that has the closing prices as variables.

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.

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.

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一杯敬自由 2024-09-21 18:49:50

最好的选择是使用 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=".")])
}

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=".")])
}
π浅易 2024-09-21 18:49:50

这也有点晚了...如果您想仅使用 R 的基本函数来获取数据而不处理任何附加包,只需使用函数 read.csv(URL),其中 URL 是指向雅虎正确位置的字符串。数据将作为数据框提取,您需要将“日期”从字符串转换为日期类型,以便任何绘图看起来都不错。简单的代码片段如下。

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

使用 R 的基本函数可以让您更好地控制数据操作。

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.

北城半夏 2024-09-21 18:49:50

我参加聚会有点晚了,但我认为这对其他迟到的人很有帮助。

TTR 中的 stockSymbols 函数从 nasdaq.com 获取金融工具符号,并调整符号以与 Yahoo! 兼容。金融。目前它返回约 6,500 个 AMEX、NYSE 和 NASDAQ 代码。您还可以查看 stockSymbols 中的代码,该代码调整股票代码以与 Yahoo! 兼容。财务可能会调整您文件中的一些代码。

注意:由于 nasdaq.com 上的更改,CRAN 上 TTR 版本中的 stockSymbols 已损坏,但在 TTR 版本中已修复代码>TTR。

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.

暮光沉寂 2024-09-21 18:49:50

我这样做是因为我需要历史价格表和每日更新文件才能运行其他软件包:

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)

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)
牵你的手,一向走下去 2024-09-21 18:49:50

如果您的最终目标是获取三列收盘价的 data.frame,那么新包 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)

这将扩展到任意数量的股票,因此 1000 个股票代码的文件应该可以正常工作!

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!

可爱咩 2024-09-21 18:49:50

对上述解决方案稍作修改...(感谢 Shane 和 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
 }

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
 }
兰花执着 2024-09-21 18:49:50

不幸的是,URL“ichart.finance.yahoo.com”已失效,现在无法使用。据我所知,雅虎已经关闭了它,看来不会再开放了。

几天前,我发现了一个不错的替代方案(https://eodhistoricaldata.com/),其 API 与雅虎财经非常相似。

基本上,对于上述 R 脚本,您只需将此部分更改

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

为:

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

然后添加 API 密钥,它将以与以前相同的方式工作。我为 R 脚本节省了大量时间。

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.

2024-09-21 18:49:50

也许可以尝试一下 BatchGetSymbols 库。与 Quantmod 相比,我喜欢它的一点是您可以为数据指定一个时间段。

library(BatchGetSymbols)

# set dates
first.date <- Sys.Date() - 60
last.date <- Sys.Date()
freq.data <- 'daily'
# set tickers
tickers <- c('FB','MMM','PETR4.SA','abcdef')

l.out <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         freq.data = freq.data,
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) # cache in tempdir()

Maybe give the BatchGetSymbols library a try. What I like about it over quantmod is that you can specify a time period for your data.

library(BatchGetSymbols)

# set dates
first.date <- Sys.Date() - 60
last.date <- Sys.Date()
freq.data <- 'daily'
# set tickers
tickers <- c('FB','MMM','PETR4.SA','abcdef')

l.out <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         freq.data = freq.data,
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) # cache in tempdir()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文