【问题标题】:Create loop to download 10 year data from Oanda via quantmod package创建循环以通过 quantmod 包从 Oanda 下载 10 年数据
【发布时间】:2016-08-19 23:03:57
【问题描述】:

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

【问题讨论】:

  • 警告不正确。历史限制为 500 天 since 2009-01-09
  • 感谢您的评论。

标签: r loops quantmod forex


【解决方案1】:

您可以通过循环一个相隔 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)
}))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多