创建循环以通过quantmod包从Oanda下载10年数据

时间:2016-08-19 23:03:58

标签: r loops quantmod forex

我正在尝试使用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)

1 个答案:

答案 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)
}))