Search code examples
rquantmod

I need your help on creating indicator functions


Thanks for helping me. I want to create functions of indicators. It work on TTR::SMA but not for TRR::ATR or TTR::Donchian Channel

Here is the example of what's work. (SMA)

# Create SMA function
n = 10
sma <- function(x, return_format = "tibble", n = n) {
  # Convert tibble to xts
  if (!is.xts(x)) {
    x <- xts(x[,-1], order.by = x$Date)
  }
  # Get SMA 
  sma_xts <- SMA(x = x$Adjusted, n = n) # I want sma on Adjusted Price
  # Rename
  names(sma_xts) <- "SMA"
  # Return in xts format if tibble is not specified
  if (return_format == "tibble") {
    sma <- fortify.zoo(sma_xts, names = "Date")
  } else {
    sma <- sma_xts
  }
  sma
}

However, when I want to apply this with ATR or DonchianChannel, it showned NA.

# Create DonchianChannel
n = 10
dc <- function(x, return_format = "tibble", n = n) {
  # Convert tibble to xts
  if (!is.xts(x)) {
    x <- xts(x[,-1], order.by = x$Date)
  }
  # Get DonchianChannel
  dc_xts <- DonchianChannel(x = c(x$High,x$Low), n = n) # <<< I think here is the problem
  # Rename
  names(dc_xts) <- "DC"
  # Return in xts format if tibble is not specified
  if (return_format == "tibble") {
    
    dc <- fortify.zoo(dc_xts, names = "Date")
    
  } else {
    dc <- dc_xts
  }
  dc
}

My best guess is the input of function was wrong. Because SMA need x but DonchianChannel need HL. So I have tried many format but I still didn't get it.

SMA(x, n = 10, ...)
ATR(HLC, n = 14, maType, ...)
DonchianChannel(HL, n = 10, include.lag = FALSE)

Thank you guys for enlightening me :) I'm trying to be better.


Solution

  • You're encountering a few issues. The first is defining n outside the function. The promise of n will give an issue inside the DonchianChannel call. The second is that the DonchianChannel function doesn't expect an object x, but an object HL. The third issue is that the function returns 3 columns, not 1, so your rename will fail.

    I created a working example without renaming anything.

    library(quantmod)
    aapl <- getSymbols("AAPL", from = Sys.Date() - 366, to = Sys.Date(), auto.assign = FALSE)
    
    # set default values in the function definition, like n = 10
    dc <- function(x, return_format = "tibble", n = 10) {
      # Convert tibble to xts
      if (!is.xts(x)) {
        x <- xts(x[,-1], order.by = x$Date)
      }
      # Get DonchianChannel, note that donchian needs HL, not x
      dc_xts <- TTR::DonchianChannel(HL = cbind(x$High, x$Low), n = n) 
    
      # Return in xts format if tibble is not specified
      if (return_format == "tibble") {
        dc <- fortify.zoo(dc_xts, names = "Date")
      } else {
        dc <- dc_xts
      }
      dc
    }
    
    head(dc(aapl), 15)
             Date   high     mid    low
    1  2021-09-20     NA      NA     NA
    2  2021-09-21     NA      NA     NA
    3  2021-09-22     NA      NA     NA
    4  2021-09-23     NA      NA     NA
    5  2021-09-24     NA      NA     NA
    6  2021-09-27     NA      NA     NA
    7  2021-09-28     NA      NA     NA
    8  2021-09-29     NA      NA     NA
    9  2021-09-30     NA      NA     NA
    10 2021-10-01 147.47 143.290 139.11
    11 2021-10-04 147.47 142.870 138.27
    12 2021-10-05 147.47 142.870 138.27
    13 2021-10-06 147.47 142.870 138.27
    14 2021-10-07 147.47 142.870 138.27
    15 2021-10-08 145.96 142.115 138.27
    

    The ATR function needs a HLC object and in that order. If you make a function do use this function make sure any cbind is in the correct order. Always check the returns of the functions, because a lot of financial functions return more that one column.