Learn R Programming

TTR (version 0.21-1)

MovingAverages: Moving Averages

Description

Calculate various moving averages (MA) of a series.

Usage

SMA(x, n=10)
  EMA(x, n=10, wilder=FALSE, ratio=NULL)
  WMA(x, n=10, wts=1:n)
 DEMA(x, n=10, v=1, wilder=FALSE, ratio=NULL)
EVWMA(price, volume, n=10)
ZLEMA(x, n=10, ratio=NULL)
 VWMA(price, volume, n=10)
 VWAP(price, volume, n=10)
  VMA(x, w, ratio=1)

Arguments

x
Price, volume, etc. series that is coercible to xts or matrix.
price
Price series that is coercible to xts or matrix.
volume
Volume series that is coercible to xts or matrix, that corresponds to price series, or a constant. See Notes.
n
Number of periods to average over.
v
The 'volume factor' (a number in [0,1]). See Notes.
w
Vector of weights (in [0,1]) the same length as x.
wts
Vector of weights. Length of wts vector must equal the length of x, or n (the default).
wilder
logical; if TRUE, a Welles Wilder type EMA will be calculated; see notes.
ratio
A smoothing/decay ratio. ratio overrides wilder in EMA, and provides additional smoothing in VMA.

Value

  • A object of the same class as x or price or a vector (if try.xts fails) containing the columns:
  • SMASimple moving average.
  • EMAExponential moving average.
  • WMAWeighted moving average.
  • DEMADouble-exponential moving average.
  • EVWMAElastic, volume-weighted moving average.
  • ZLEMAZero lag exponential moving average.
  • VWMAVolume-weighed moving average (same as VWAP).
  • VWAPVolume-weighed average price (same as VWMA).
  • VWAVariable-length moving average.

Warning

Some indicators (e.g. EMA, DEMA, EVWMA, etc.) are calculated using the indicators' own previous values, and are therefore unstable in the short-term. As the indicator receives more data, its output becomes more stable. See example below.

Details

SMA calculates the arithmetic mean of the series over the past n observations.

EMA calculates an exponentially-weighted mean, giving more weight to recent observations. See Warning section below.

WMA is similar to an EMA, but with linear weighting if the length of wts is equal to n. If the length of wts is equal to the length of x, the WMA will use the values of wts as weights.

DEMA is calculated as: DEMA = (1 + v) * EMA(x,n) - EMA(EMA(x,n),n) * v (with the corresponding wilder and ratio arguments).

EVWMA uses volume to define the period of the MA.

ZLEMA is similar to an EMA, as it gives more weight to recent observations, but attempts to remove lag by subtracting data prior to (n-1)/2 periods (default) to minimize the cumulative effect. VWMA and VWAP calculate the volume-weighted moving average price. VMA calculate a variable-length moving average based on the absolute value of w. Higher (lower) values of w will cause VMA to react faster (slower).

References

The following site(s) were used to code/document this indicator: http://www.fmlabs.com/reference/ExpMA.htm http://www.fmlabs.com/reference/WeightedMA.htm http://www.fmlabs.com/reference/DEMA.htm http://www.fmlabs.com/reference/T3.htm http://linnsoft.com/tour/techind/evwma.htm http://www.fmlabs.com/reference/ZeroLagExpMA.htm http://www.fmlabs.com/reference/VIDYA.htm

See Also

See wilderSum, which is used in calculating a Welles Wilder type MA.

Examples

Run this code
data(ttrc)
    ema.20 <-   EMA(ttrc[,"Close"], 20)
    sma.20 <-   SMA(ttrc[,"Close"], 20)
   dema.20 <-  DEMA(ttrc[,"Close"], 20)
  evwma.20 <- EVWMA(ttrc[,"Close"], ttrc[,"Volume"], 20)
  zlema.20 <- ZLEMA(ttrc[,"Close"], 20)

  ## Example of Tim Tillson's T3 indicator
  T3 <- function(x, n=10, v=1) DEMA(DEMA(DEMA(x,n,v),n,v),n,v)
  t3 <- T3(ttrc[,"Close"])
  
  ## Example of short-term instability of EMA
  ## (and other indicators mentioned above)
  x <- rnorm(100)
  tail( EMA(x[90:100],10), 1 )
  tail( EMA(x[70:100],10), 1 )
  tail( EMA(x[50:100],10), 1 )
  tail( EMA(x[30:100],10), 1 )
  tail( EMA(x[10:100],10), 1 )
  tail( EMA(x[ 1:100],10), 1 )

Run the code above in your browser using DataLab