joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
325 stars 102 forks source link

TSI (True Strength Index) Indicator #103

Open ethanbsmith opened 3 years ago

ethanbsmith commented 3 years ago

interesting momentum indicator based on smoothing the first derivative of price. https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index

I crufted up a quick and dirty version to play with. Leaving this as a placeholder for a proper version when i get time

TSI <- function(x, n.first = 25, n.second = 13, n.signal = 7) {
  #True Strength Indicator
  #https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index
  x <- try.xts(x, error = as.matrix)
  pc <- x - lag.xts(x, na.pad = T) #force lag.xts to get na padding
  dspc <- EMA(EMA(pc, n = n.first), n = n.second)
  dsapc <- EMA(EMA(abs(pc), n = n.first), n = n.second)

  tsi <- 100 * (dspc/dsapc)
  signal <- EMA(tsi, n = n.signal)
  r <- cbind(tsi, signal)
  r <- reclass(r, x)
  if (!is.null(dim(r))) colnames(r) <- c("tsi", "signal")
  return(r)
}

addTSI <- newTA(TSI, preFUN = Cl, col = c("blue", "red"))