我正在尝试使用quantmod::getSymbols
下载批量Oanda外汇数据。帮助文件声明每个请求只能下载500天的数据,而我会收到来自warnings()
的5年数据上限的警告。不过,我试图创建一个从1997年到此日期下载数据的循环。这是我的代码:
library(xts)
library(quantmod)
date_from = c("1996-01-01", "2001-01-02", "2005-01-03", "2009-01-03", "2013-01-04")
date_to = c("2001-01-01", "2005-01-02", "2009-01-03", "2013-01-03", "2016-01-04")
for (i in 1:5) {
getSymbols("EUR/AUD", src="oanda", from = dates_from[i], to = date_to[i])
forex = for (i=1) EURAUD else NULL
final_Dataset<- rbind(c(forex, EURAUD))
}
我应该实施哪些更改?
修改1 我使它工作但它写得很糟糕。任何提议的更改都将非常感谢。
date_from = c("1996-01-01", "2001-01-02", "2005-01-03", "2009-01-03", "2013-01-04")
date_to = c("2001-01-01", "2005-01-02", "2009-01-03", "2013-01-03", "2016-01-04")
forex = vector(mode = 'list', length = 5)
for (i in 1:5) {
getSymbols("EUR/AUD", src="oanda", from = dates_from[i], to = date_to[i])
forex[[i]] = EURAUD
}
EUR_AUD = Reduce(rbind,forex)
答案 0 :(得分:0)
您可以通过循环相隔500天的日期向量来完成此操作。请注意,我将getSymbols
调用包裹在try
中,因为前2个开始日期不起作用。我不确定为什么。
require(quantmod)
Data <- do.call(rbind, lapply(dates, function(d) {
sym <- "EUR/AUD"
x <- try(getSymbols(sym, src="oanda", from=d, to=d+499, auto.assign=FALSE))
if (inherits(x, "try-error"))
return(NULL)
else
return(x)
}))