R/strength_SMA.R

Defines functions strength_SMA

Documented in strength_SMA

#' Creates a ranking by strength of simple moving average deviation
#' over time.
#' Uses the \code{TTR::ROC} function for rate-of-change value.
#' Uses the \code{TTR:SMA} function for moving average of the value.
#' Applies the \code{TTR::runSD} running standard deviations.
#' Computes strength as \code{strength <- (x - sma) / rd}.
#' Uses the discrete ROC type.
#' @param x price history as XTS, more than one column of asset prices.
#' @param roc_n ROC lookback period in same periodicity as \code{x};
#' default 1.
#' @param sigma_n running standard deviation period lookback;
#' default 5.
#' @param sma_n simple moving average period lookback;
#' default 10
#' @return a row rank as determined by \code{row_rank}
#' @seealso row_rank, TTR::ROC, TTR::runSD
#' @examples
#' \dontrun{
#' library(quantmod)
#' getSymbols("XLP",auto.assign=TRUE)
#' getSymbols("XLV",auto.assign=TRUE)
#' ranking <- strength_SMA(cbind(Cl(XLP),Cl(XLV)))
#' tail(ranking)
#' }
strength_SMA <- function(x, roc_n = 1, sigma_n = 5, sma_n = 10) {
  roc <- TTR::ROC(x, n = roc_n, type = "discrete")
  sigma <- apply(roc, 2, TTR::runSD, n = sigma_n)
  sma <- apply(x, 2, TTR::SMA, n = sma_n)
  strength <- (x - sma) / sigma
  row_rank(strength)
}
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.