我正在尝试使用csv文件加载多个符号,而不是从Yahoo下载。原始代码效果很好并使用
load.packages('quantmod')
tickers = spl('TLT,IWM,GLD')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data,
auto.assign = T)
但是,当我尝试使用下面的代码时,它会导致脚本中的“下标越界”错误:
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)
任何人都在想为什么第二个代码集不起作用?测试我刚刚从Yahoo下载了csv数据并保存在本地(windows)。如果我只使用一个csv文件,我不会得到下标错误。我也尝试过以下代码,但稍后会在脚本中遇到相同的错误:
setSymbolLookup(tickers=list(src="csv", dir= "C:/Users/Admiral/Downloads/"))
getSymbols(tickers, auto.assign = T, from = '1980-01-01', env=data)
答案 0 :(得分:3)
我会使用FinancialInstrument包
来做到这一点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)
如果您想使用getSymbols.csv
,您的数据必须包含日期和6列(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