Load multiple symbols using csv with quantmod

会有一股神秘感。 提交于 2019-12-19 02:40:12

问题


I am trying to load multiple symbols using a csv file rather than downloading from Yahoo. The original code works great and uses

load.packages('quantmod')
tickers = spl('TLT,IWM,GLD')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data,
           auto.assign = T)

when I try using the code below, however, it results in "subscript out of bounds" errors later in the script:

load.packages('quantmod')
tickers = spl('TLT,IWM,GLD')
data <- new.env()
getSymbols(tickers, src="csv", dir= "C:/Users/Admiral/Downloads/",
           env = data, auto.assign = T)

Anyone have thoughts why the second code set wont work? To test I've just downloaded csv data from Yahoo and saved locally (windows). I dont get the subscript errors if I just use one csv file. I've also tried the code below but get the same errors later in the script:

setSymbolLookup(tickers=list(src="csv", dir= "C:/Users/Admiral/Downloads/"))
getSymbols(tickers, auto.assign = T, from = '1980-01-01', env=data)

回答1:


I would do this using the FinancialInstrument package

require('quantmod')
require('FinancialInstrument')
tickers <- c("TLT", "IWM", "GLD")
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data)

# Now save the data in a directory
tmpdir <- tempdir()
saveSymbols.common(tickers, tmpdir, env=data)

#remove the data    
rm(list=tickers, pos=data)
ls(data) # see that there is nothing there
# Now load the data back from disk
getSymbols(tickers, src='FI', dir=tmpdir, env=data, split_method='common')
ls(data)

If you want to use getSymbols.csv, your data has must have the Date and 6 columns (OHLCVA)

#write data to csv files on disk
for (i in seq_along(tickers)) {      
  write.zoo(get(tickers[i], pos=data), file=paste0(tmpdir, "/", tickers[i], ".csv"), sep=",")
}
rm(list=tickers, pos=data) #remove from memory
getSymbols(tickers, src='csv', dir=tmpdir)#, env=data)  #load from csv files


来源:https://stackoverflow.com/questions/12238892/load-multiple-symbols-using-csv-with-quantmod

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