R(quantmod),绘图自己的指示器,序列中的错误。默认值

R(quantmod),绘图自己的指示器,序列中的错误。默认值,r,xts,quantmod,indicator,R,Xts,Quantmod,Indicator,我想在quantmod中绘制我自己的简单指示器,如下所示: own.ind <- as.matrix(c(1,2,3), ncol=1, nrow=3) rownames(own.ind) <- c("2017-01-23", "2017-01-24", "2017-01-25") own.ind <- as.xts(own.ind) getSymbols("AAPL", from = as.Date("2017-01-23"), to = as.Date("2017-01-2

我想在quantmod中绘制我自己的简单指示器,如下所示:

own.ind <- as.matrix(c(1,2,3), ncol=1, nrow=3)
rownames(own.ind) <- c("2017-01-23", "2017-01-24", "2017-01-25")
own.ind <- as.xts(own.ind)
getSymbols("AAPL", from = as.Date("2017-01-23"), to = as.Date("2017-01-25"))
chartSeries(AAPL)
addTA(own.ind)
以及另外两项警告:

1: In min(tav * 0.975, na.rm = TRUE) :  no non-missing arguments to min; returning Inf
2: In max(tav * 1.05, na.rm = TRUE) :  no non-missing arguments to max; returning -Inf

怎么了?

Joe,您必须创建一个函数来添加序列作为指示器。要创建每个交易日增加1的指标(如示例所示),请执行以下操作:

myInd <- function(x) {
  x <- 1:NROW(x)
  return(x)
}
现在,让我们在价格图下方绘制新指标:

getSymbols("AAPL", from = "2017-01-23", to = "2017-01-25")
chartSeries(AAPL,TA=NULL)
addMyInd()

问题在于
own.ind
对象的索引与
AAPL
对象中的任何索引值都不对齐。这是因为默认情况下,
as.xts
将矩阵的行名转换为
POSIXct
POSIXct
对象有时区,
Date
对象没有时区

R> merge(Cl(AAPL), own.ind)
           AAPL.Close own.ind
2017-01-23     120.08      NA
2017-01-23         NA       1
2017-01-24     119.97      NA
2017-01-24         NA       2
2017-01-25     121.88      NA
2017-01-25         NA       3
因此,您需要将
dateFormat
参数指定为
as.xts
,或者直接使用
xts
构造函数和
as.Date

# use dateFormat
own.ind <- as.matrix(c(1,2,3), ncol=1, nrow=3)
rownames(own.ind) <- c("2017-01-23", "2017-01-24", "2017-01-25")
own.ind <- as.xts(own.ind, dateFormat = "Date")
merge(Cl(AAPL), own.ind)
#            AAPL.Close own.ind
# 2017-01-23     120.08       1
# 2017-01-24     119.97       2
# 2017-01-25     121.88       3

# use xts constructor and as.Date
own.ind <- xts(1:3, as.Date(c("2017-01-23", "2017-01-24", "2017-01-25")))
merge(Cl(AAPL), own.ind)
#            AAPL.Close own.ind
# 2017-01-23     120.08       1
# 2017-01-24     119.97       2
# 2017-01-25     121.88       3

这大大简化了问题。谢谢!霍尔梅尔。。。没有任何联系您的信息,但我想告诉您,我相信您的VXX dropbox提要中有一个漏洞,该漏洞已在多个博客网站上列出。目前,从2014年8月4日开始,您的订阅源的价格下降了4倍,之前的日期看起来很奇怪,其他订阅源(如雅虎历史版)也没有反映出来。事实上,我只是猜测你就是那个hvollmeier,如果不是的话,请忽略这个:-)很抱歉在这里这么离题:-(宾果,@hvollmeier。谢谢你的修复和欢迎数据。
getSymbols("AAPL", from = "2017-01-23", to = "2017-01-25")
chartSeries(AAPL,TA=NULL)
addMyInd()
R> merge(Cl(AAPL), own.ind)
           AAPL.Close own.ind
2017-01-23     120.08      NA
2017-01-23         NA       1
2017-01-24     119.97      NA
2017-01-24         NA       2
2017-01-25     121.88      NA
2017-01-25         NA       3
# use dateFormat
own.ind <- as.matrix(c(1,2,3), ncol=1, nrow=3)
rownames(own.ind) <- c("2017-01-23", "2017-01-24", "2017-01-25")
own.ind <- as.xts(own.ind, dateFormat = "Date")
merge(Cl(AAPL), own.ind)
#            AAPL.Close own.ind
# 2017-01-23     120.08       1
# 2017-01-24     119.97       2
# 2017-01-25     121.88       3

# use xts constructor and as.Date
own.ind <- xts(1:3, as.Date(c("2017-01-23", "2017-01-24", "2017-01-25")))
merge(Cl(AAPL), own.ind)
#            AAPL.Close own.ind
# 2017-01-23     120.08       1
# 2017-01-24     119.97       2
# 2017-01-25     121.88       3
chartSeries(AAPL, TA="addTA(own.ind)")