SMA  R Documentation 
Calculate various moving averages (MA) of a series.
SMA(x, n = 10, ...)
EMA(x, n = 10, wilder = FALSE, ratio = NULL, ...)
DEMA(x, n = 10, v = 1, wilder = FALSE, ratio = NULL)
WMA(x, n = 10, wts = 1:n, ...)
EVWMA(price, volume, n = 10, ...)
ZLEMA(x, n = 10, ratio = NULL, ...)
VWAP(price, volume, n = 10, ...)
HMA(x, n = 20, ...)
ALMA(x, n = 9, offset = 0.85, sigma = 6, ...)
x 
Price, volume, etc. series that is coercible to xts or matrix. 
n 
Number of periods to average over. Must be between 1 and

... 
any other passthrough parameters 
wilder 
logical; if 
ratio 
A smoothing/decay ratio. 
v 
The 'volume factor' (a number in [0,1]). See Notes. 
wts 
Vector of weights. Length of 
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. 
offset 
Percentile at which the center of the distribution should occur. 
sigma 
Standard deviation of the distribution. 
SMA
calculates the arithmetic mean of the series over the past
n
observations.
EMA
calculates an exponentiallyweighted 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
(n1)/2
periods (default) to minimize the cumulative effect.
VWMA
and VWAP
calculate the volumeweighted moving average
price.
HMA
a WMA of the difference of two other WMAs, making it very
reponsive.
ALMA
inspired by Gaussian filters. Tends to put less weight on most
recent observations, reducing tendency to overshoot.
A object of the same class as x
or price
or a vector
(if try.xts
fails) containing the columns:
Simple moving average.
Exponential moving average.
Weighted moving average.
Doubleexponential moving average.
Elastic, volumeweighted moving average.
Zero lag exponential moving average.
Volumeweighed moving average (same as VWAP
).
Volumeweighed average price (same as VWMA
).
Variablelength moving average.
Hull moving average.
Arnaud Legoux moving average.
Some indicators (e.g. EMA, DEMA, EVWMA, etc.) are calculated using the indicators' own previous values, and are therefore unstable in the shortterm. As the indicator receives more data, its output becomes more stable. See example below.
For EMA
, wilder=FALSE
(the default) uses an exponential
smoothing ratio of 2/(n+1)
, while wilder=TRUE
uses Welles
Wilder's exponential smoothing ratio of 1/n
. The EMA
result
is initialized with the n
period sample average at period n
.
The exponential decay is applied from that point forward.
Since WMA
can accept a weight vector of length equal to the length of
x
or of length n
, it can be used as a regular weighted moving
average (in the case wts=1:n
) or as a moving average weighted by
volume, another indicator, etc.
Since DEMA
allows adjusting v
, it is technically Tim Tillson's
generalized DEMA (GD). When v=1
(the default), the result is the
standard DEMA. When v=0
, the result is a regular EMA. All other
values of v
return the GD result. This function can be used to
calculate Tillson's T3 indicator (see example below). Thanks to John Gavin
for suggesting the generalization.
For EVWMA
, if volume
is a series, n
should be chosen so
the sum of the volume for n
periods approximates the total number of
outstanding shares for the security being averaged. If volume
is a
constant, it should represent the total number of outstanding shares for the
security being averaged.
Joshua Ulrich, Ivan Popivanov (HMA, ALMA)
The following site(s) were used to code/document this
indicator:
https://www.fmlabs.com/reference/ExpMA.htm
https://www.fmlabs.com/reference/WeightedMA.htm
https://www.fmlabs.com/reference/DEMA.htm
https://www.fmlabs.com/reference/T3.htm
https://www.linnsoft.com/techind/evwmaelasticvolumeweightedmovingaverage
https://www.fmlabs.com/reference/ZeroLagExpMA.htm
https://www.fmlabs.com/reference/VIDYA.htm
https://www.traderslog.com/hullmovingaverage
https://web.archive.org/web/20180222085959/http://arnaudlegoux.com/
See wilderSum
, which is used in calculating a Welles
Wilder type MA.
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)
alma < ALMA(ttrc[,"Close"])
hma < HMA(ttrc[,"Close"])
## 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 shortterm 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 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.