如何仅使用getSymbols将多个收盘股价下载到单独的xts文件中? [英] How to download multiple closing stock prices only with getSymbols into separate xts files?

查看:512
本文介绍了如何仅使用getSymbols将多个收盘股价下载到单独的xts文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Quantmod包中的getSymbols执行以下操作:

How can I use getSymbols from the quantmod package to do the following:

  1. 下载多个股价历史记录
  2. 仅选择调整后的收盘价-即抑制开盘价/最高价/最低价和成交量数据
  3. 将每个价格历史记录另存为带有日期的单独的xts文件

我可以实现第1步和第2步,但是第3步遇到了麻烦.StackOverflow上有几篇关于下载和合并多个报价的价格的文章,但是我找不到用于下载和保存在单独文件中的说明.

I can implement steps 1 and 2, but I'm having trouble with step 3. StackOverflow has several posts on downloading and merging prices for multiple tickers, but I can't find instructions for downloading and saving in separate files.

这是我到目前为止所拥有的.任何有关实施最后一步的建议将不胜感激.预先感谢!

Here's what I have so far. Any advice on implementing the last step would be greatly appreciated. Thanks in advance!

library(quantmod)
symbols = c("GOOG","MSFT","AAPL")
getSymbols(symbols, from="2011-01-01")
ClosePrices <- lapply(symbols, function(x) Ad(get(x)))

我怀疑该解决方案是将ClosePrices文件分成多个单独的文件,每个股票代码一个,但是我不确定该怎么做.

I suspect that the solution is separating the ClosePrices file into separate files, one for each ticker, but I'm not sure how to do so.

推荐答案

对于第1部分,请参阅我的答案

for part 1, see my answer here. Your method won't work for index symbols like ^GSPC or in general any symbol starting with special characters (due to the automatic assignment).

关于第3部分,按照上面的链接中的描述,一旦您提取了所有符号并将它们存储到myList中,请尝试以下方法遍历列表并将列表中的元素导出到工作目录中:

as for part 3, once you fetched all your symbols and stored them into myList as described in the link above, try the following to loop through your list and export elements of the list to your working directory:

require(quantmod)

#Vector of symbols to fetch prices for
symbols <- c('MSFT','SBUX','GOOGL')

#Initialize a list to store the fetched prices
myList <- list()

#Loop through symbols, fetch prices, and store in myList
myList <-lapply(symbols, function(x) {getSymbols(x,auto.assign=FALSE)} )

#Housekeeping
names(myList) <- symbols

#Export to seperate files
quiet <- lapply(1:length(myList), function(x){    #looping through all elements of your list
  write.csv(myList[[x]],     #accessing the xth element of your list
            paste0(names(myList)[x],'.csv'),   #naming the exported element
            row.names=index(myList[[x]])   #include dates in the export
  )  #close write.csv
}  #close function
)  #close lapply

根据第一个评论合并两个帖子.

Combined the two posts as per the first comment.

这篇关于如何仅使用getSymbols将多个收盘股价下载到单独的xts文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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