r - quantmod::chart_Series() 错误?

标签 r finance xts quantmod

我想使用 quantmod::chart_Series() 绘制 SPX 图表,并在下面绘制 GDP 变化和 GDP 变化的 12 个月 SMA。无论我如何尝试(我使用什么组合),要么会发生错误,要么 quantmod::chart_Series() 仅显示部分绘图。

require(quantmod)

FRED.symbols <- c("GDPC96")

getSymbols(FRED.symbols, src="FRED")
SPX <- getSymbols("^GSPC", auto.assign=FALSE, from="1900-01-01")

subset="2000/"

chart_Series(SPX, subset=subset)
add_TA(GDPC96)
add_TA(ROC(GDPC96, type="discrete"))
add_TA(SMA(ROC(GDPC96, type="discrete"), n=4), on=3, col="blue")

编辑:实际上,在我看来,这是使用季度数据时的 quantmod::chart_series() 问题:

subset <- "2000/"
chart_Series(to.quarterly(SPX, drop.time=TRUE), subset=subset)
add_TA(SMA(Cl(to.quarterly(SPX, drop.time=TRUE))))

> subset <- "2000/"
> chart_Series(to.quarterly(SPX, drop.time=TRUE), subset=subset)
> add_TA(SMA(Cl(to.quarterly(SPX, drop.time=TRUE))))
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion

这确实会在主面板上生成 SPX 图,但会留下空白的第二个和第三个面板。 然后我尝试使用相同的数据索引、相同的长度等。

chart_Series(head(to.quarterly(SPX, drop.time="TRUE"), -1), subset=subset)
add_TA(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE))
add_TA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"))
add_TA(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"),    n=4), on=3, col="blue")

结果到处都是错误:

> chart_Series(head(to.quarterly(SPX, drop.time="TRUE"), -1), subset=subset)
> add_TA(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE))
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion
> add_TA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"))
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion
> add_TA(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4), on=3, col="blue")
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion

使用

tail(to.quarterly(SPX, drop.time="TRUE"))
tail(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE))
tail(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"))
tail(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4))

dput(to.quarterly(SPX, drop.time="TRUE"))
dput(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE))
dput(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"))
dput(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4))

我觉得一切都很好。

我的 session 信息():

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8          
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantmod_0.3-18 TTR_0.21-0      xts_0.8-7       zoo_1.7-7      
[5] Defaults_1.1-1  rj_1.1.0-4     

loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-0 tools_2.15.0  

有什么想法可以解决这些问题吗?

编辑:这似乎是一个 quantmod::chart_Series() 错误。如果我这样做:

subset <- "1990/"
test <- cbind(head(to.quarterly(SPX, drop.time="TRUE"), -1)[subset],
            to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)[subset],
            ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE),     type="discrete")[subset],
            SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4)[subset])

test$test <- 1

subset <- "2000/"
chart_Series(OHLC(test), subset=subset)
add_TA(test$test)
add_TA(test$GDPC96)

> test$test <- 1
> subset <- "2000/"
> chart_Series(OHLC(test), subset=subset)
> add_TA(test$test)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion
> add_TA(test$GDPC96)   
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
In addition: Warning messages:
1: In as_numeric(H) : NAs introduced by coercion
2: In as_numeric(H) : NAs introduced by coercion
3: In as_numeric(H) : NAs introduced by coercion
> traceback()
14: stop("'x' and 'y' lengths differ") at chart_Series.R#510
13: xy.coords(x, y) at chart_Series.R#510
12: plot.xy(xy.coords(x, y), type = type, ...) at chart_Series.R#510
11: lines.default(ta.x, as.numeric(ta.y[, i]), col = col, ...) at chart_Series.R#510
10: lines(ta.x, as.numeric(ta.y[, i]), col = col, ...) at chart_Series.R#510
9: plot_ta(x = current.chob(), ta = get("x"), on = NA, taType = NULL, 
       col = 1) at replot.R#238
8: eval(expr, envir, enclos) at replot.R#238
7: eval(aob, env) at replot.R#238
6: FUN(X[[12L]], ...) at replot.R#230
5: lapply(x$Env$actions, function(aob) {
       if (attr(aob, "frame") > 0) {
           x$set_frame(attr(aob, "frame"), attr(aob, "clip"))
           env <- attr(aob, "env")
           if (is.list(env)) {
               env <- unlist(lapply(env, function(x) eapply(x, eval)), 
                   recursive = FALSE)
           }
           eval(aob, env)
       }
   }) at replot.R#230
4: plot.replot(x, ...)
3: plot(x, ...)
2: print.replot(<environment>)
1: print(<environment>)

关于如何解决这个问题有什么想法吗?

最佳答案

几天前我也遇到过类似的错误。我发现问题出在 add_TA 中:

ta.x <- as.numeric(na.approx(ta.adj[, 1]))

na.approx 默认使用规则 = 1 的 approx,如果原始数据中的最后一个时间戳早于 TA 数据中的最后一个时间戳,则将尾随 NA 保留在列表中。更改该行以设置规则 = 2 解决了问题。

ta.x <- as.numeric(na.approx(ta.adj[, 1], rule=2))

关于r - quantmod::chart_Series() 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11148414/

相关文章:

r - “名称”属性的长度必须与向量的长度相同

r - tidyr:分隔列,同时保留第一列中的分隔符

python - 如何在 Python 中使用 Selenium 获取 h1 标签

python - Pandas 的 EMA 与股票的 EMA 不匹配?

r - 使用 zoo/xts 删除 R 中的特定值

r - 根据上次发生的条件进行更新

r - 是否有任何明确保证 dplyr 操作保留行顺序?

r - 使用交易信号计算金融时间序列的返回

从 xts 对象中删除重复的行

r - 将 NA 更改为插值扁条