使用 csv 和 quantmod 加载多个符号 [英] Load multiple symbols using csv with quantmod

查看:37
本文介绍了使用 csv 和 quantmod 加载多个符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 csv 文件加载多个符号,而不是从 Yahoo 下载.原始代码运行良好并使用

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)

有人想过为什么第二个代码集不起作用吗?为了测试,我刚刚从雅虎下载了 csv 数据并保存在本地(Windows).如果我只使用一个 csv 文件,我不会收到下标错误.我也试过下面的代码,但后来在脚本中遇到了同样的错误:

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)

推荐答案

我会使用 FinancialInstrument 包来做到这一点

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)

如果你想使用 getSymbols.csv,你的数据必须有日期和 6 列 (OHLCVA)

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

这篇关于使用 csv 和 quantmod 加载多个符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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