rquantmodquantstrat

Adding a custom indicator to ChartSeries Quantmod


I have created the chandelier indicator. The function is:

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)
}

I have used quantmods function newTA to create:

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

I am getting an error enter image description here


Solution

  • That's a warning, not an error. Though it does indicate something is not quite right. The problem is that rollapplyr does not pad/fill with NA by default. So you need to change your function to:

    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)
    }
    

    Or you could replace the rollapplyr() call with runMax(Hi(x), n).