使用getSymbols提取FRED数据时出现问题

时间:2020-03-31 00:56:06

标签: r quantmod

library(FinancialInstrument)
library(TTR)
library(PerformanceAnalytics)
library(fredr)

symbols <- c("DEXUSEU", "DEXJPUS", "DEXSZUS", "DEXCAUS")

exchange_rate(symbols, currency = "USD", multiplier = 1)

创建一个用于存储符号的新环境

symEnv <- new.env()

getSymbols并将符号分配给symEnv环境

getSymbols(symbols, from = '2000-01-01', src= "FRED", to = '2015-12-31', env = symEnv)

每月调整后收盘价的xts对象

symbols.close <- do.call(merge, eapply(symEnv, MonthlyAd))

每月收益

monthly.returns <- ROC(x = symbols.close, n = 1, type = "discrete", na.pad = TRUE)

我收到此错误:

> getSymbols(symbols, from = '2000-01-01',src= "FRED", to = '2015-12-31', env = symEnv)
[1] "DEXUSEU" "DEXJPUS" "DEXSZUS" "DEXCAUS"
> # xts object of the monthly adjusted close prices
> symbols.close <- do.call(merge, eapply(symEnv, MonthlyAd))
Error in Ad(to.monthly(x, indexAt = "lastof", drop.time = TRUE, name = sym)) : 
  subscript out of bounds: no column name containing "Adjusted"
In addition: Warning message:
In to.period(x, "months", indexAt = indexAt, name = name, ...) :

显示回溯

 Rerun with Debug
 Error in Ad(to.monthly(x, indexAt = "lastof", drop.time = TRUE, name = sym)) : 
  subscript out of bounds: no column name containing "Adjusted" > 
> # monthly returns
> monthly.returns <- ROC(x = symbols.close, n = 1, type = "discrete", na.pad = TRUE)
Error in is.xts(x) : object 'symbols.close' not found
> 

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以仅使用lapply来获取所需的所有内容,并获取列表输出,而不是创建单独的环境。

我只使用quantmod和TTR来显示所有内容。您的示例中的其他软件包不需要摆脱错误。请注意,在请求原始数据时,quantmod对日期选项不执行任何操作。您需要稍后自己过滤数据。请参见代码中的示例。

library(quantmod)
#library(TTR)

symbols <- c("DEXUSEU", "DEXJPUS", "DEXSZUS", "DEXCAUS")


out <- lapply(symbols, getSymbols, src= "FRED", auto.assign = FALSE)
names(out) <- symbols


symbols.close <- do.call(merge, out)
head(symbols.close)
               DEXUSEU DEXJPUS DEXSZUS DEXCAUS
1971-01-04      NA  357.73  4.3180  1.0109
1971-01-05      NA  357.81  4.3117  1.0102
1971-01-06      NA  357.86  4.3113  1.0106
1971-01-07      NA  357.87  4.3103  1.0148
1971-01-08      NA  357.82  4.3109  1.0154
1971-01-11      NA  357.95  4.3102  1.0159

# filter data to years 2000 and higher. See examples with `?xts`
symbols.close <- symbols.close["2000/"]

head(symbols.close)

           DEXUSEU DEXJPUS DEXSZUS DEXCAUS
2000-01-03  1.0155  101.70  1.5808  1.4465
2000-01-04  1.0309  103.09  1.5565  1.4518
2000-01-05  1.0335  103.77  1.5526  1.4518
2000-01-06  1.0324  105.19  1.5540  1.4571
2000-01-07  1.0294  105.17  1.5623  1.4505
2000-01-10  1.0252  105.28  1.5704  1.4568

monthly.returns <- TTR::ROC(x = symbols.close, n = 1, type = "discrete", na.pad = TRUE)
head(monthly.returns)
                DEXUSEU       DEXJPUS       DEXSZUS      DEXCAUS
2000-01-03           NA            NA            NA           NA
2000-01-04  0.015164943  0.0136676500 -0.0153719636  0.003664017
2000-01-05  0.002522068  0.0065961781 -0.0025056216  0.000000000
2000-01-06 -0.001064344  0.0136841091  0.0009017133  0.003650641
2000-01-07 -0.002905850 -0.0001901321  0.0053410553 -0.004529545
2000-01-10 -0.004080047  0.0010459256  0.0051846636  0.004343330