How to download multiple closing stock prices only with getSymbols into separate xts files?

╄→гoц情女王★ 提交于 2019-12-06 04:23:30
Bahae Omid

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

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

EDIT: Combined the two posts as per the first comment.

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