向ChartSeries Quantmod添加自定义指标

时间:2018-11-27 01:00:37

标签: r quantmod quantstrat

我创建了枝形吊灯指示器。功能是:

chandlier_exit_long = function(x,k = 3,  
 n = 22){
 high = rollapplyr(Hi(x), width = n, FUN 
 = max)
 atr = ATR(x, n)[, "atr"]
  z = high - atr
 names(z) = "Chandelier_Exit_Long"
 return(z)
}

我已经使用了quantmods函数newTA来创建:

  addChandExitLong = newTA(FUN = 
  chandlier_exit_long, preFUN = OHLC, col 
  = "red")

我遇到错误 enter image description here

1 个答案:

答案 0 :(得分:1)

这是警告,不是错误。尽管它确实表明某些事情不太正确。问题在于,rollapplyr默认情况下不填充/填充NA。因此,您需要将功能更改为:

chandlier_exit_long = function(x, k = 3, n = 22){
  high = rollapplyr(Hi(x), n, FUN = max, fill = NA)  # add `fill = NA`
  atr = ATR(x, n)[, "atr"]
  z = high - atr
  names(z) = "Chandelier_Exit_Long"
  return(z)
}

或者您可以将rollapplyr()调用替换为runMax(Hi(x), n)