EVOLUTION-MANAGER
Edit File: MovingAverages.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Moving Averages</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for SMA {TTR}"><tr><td>SMA {TTR}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Moving Averages</h2> <h3>Description</h3> <p>Calculate various moving averages (MA) of a series. </p> <h3>Usage</h3> <pre> 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, ...) VMA(x, w, ratio = 1, ...) HMA(x, n = 20, ...) ALMA(x, n = 9, offset = 0.85, sigma = 6, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>Price, volume, etc. series that is coercible to xts or matrix.</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>Number of periods to average over. Must be between 1 and <code>nrow(x)</code>, inclusive.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>any other passthrough parameters</p> </td></tr> <tr valign="top"><td><code>wilder</code></td> <td> <p>logical; if <code>TRUE</code>, a Welles Wilder type EMA will be calculated; see notes.</p> </td></tr> <tr valign="top"><td><code>ratio</code></td> <td> <p>A smoothing/decay ratio. <code>ratio</code> overrides <code>wilder</code> in <code>EMA</code>, and provides additional smoothing in <code>VMA</code>.</p> </td></tr> <tr valign="top"><td><code>v</code></td> <td> <p>The 'volume factor' (a number in [0,1]). See Notes.</p> </td></tr> <tr valign="top"><td><code>wts</code></td> <td> <p>Vector of weights. Length of <code>wts</code> vector must equal the length of <code>x</code>, or <code>n</code> (the default).</p> </td></tr> <tr valign="top"><td><code>price</code></td> <td> <p>Price series that is coercible to xts or matrix.</p> </td></tr> <tr valign="top"><td><code>volume</code></td> <td> <p>Volume series that is coercible to xts or matrix, that corresponds to price series, or a constant. See Notes.</p> </td></tr> <tr valign="top"><td><code>w</code></td> <td> <p>Vector of weights (in [0,1]) the same length as <code>x</code>.</p> </td></tr> <tr valign="top"><td><code>offset</code></td> <td> <p>Percentile at which the center of the distribution should occur.</p> </td></tr> <tr valign="top"><td><code>sigma</code></td> <td> <p>Standard deviation of the distribution.</p> </td></tr> </table> <h3>Details</h3> <p><code>SMA</code> calculates the arithmetic mean of the series over the past <code>n</code> observations. </p> <p><code>EMA</code> calculates an exponentially-weighted mean, giving more weight to recent observations. See Warning section below. </p> <p><code>WMA</code> is similar to an EMA, but with linear weighting if the length of <code>wts</code> is equal to <code>n</code>. If the length of <code>wts</code> is equal to the length of <code>x</code>, the WMA will use the values of <code>wts</code> as weights. </p> <p><code>DEMA</code> is calculated as: <code>DEMA = (1 + v) * EMA(x,n) - EMA(EMA(x,n),n) * v</code> (with the corresponding <code>wilder</code> and <code>ratio</code> arguments). </p> <p><code>EVWMA</code> uses volume to define the period of the MA. </p> <p><code>ZLEMA</code> is similar to an EMA, as it gives more weight to recent observations, but attempts to remove lag by subtracting data prior to <code>(n-1)/2</code> periods (default) to minimize the cumulative effect. </p> <p><code>VWMA</code> and <code>VWAP</code> calculate the volume-weighted moving average price. </p> <p><code>VMA</code> calculate a variable-length moving average based on the absolute value of <code>w</code>. Higher (lower) values of <code>w</code> will cause <code>VMA</code> to react faster (slower). </p> <p><code>HMA</code> a WMA of the difference of two other WMAs, making it very reponsive. </p> <p><code>ALMA</code> inspired by Gaussian filters. Tends to put less weight on most recent observations, reducing tendency to overshoot. </p> <h3>Value</h3> <p>A object of the same class as <code>x</code> or <code>price</code> or a vector (if <code>try.xts</code> fails) containing the columns: </p> <dl> <dt>SMA</dt><dd><p> Simple moving average. </p> </dd> <dt>EMA</dt><dd><p> Exponential moving average. </p> </dd> <dt>WMA</dt><dd><p> Weighted moving average. </p> </dd> <dt>DEMA</dt><dd><p> Double-exponential moving average. </p> </dd> <dt>EVWMA</dt><dd><p> Elastic, volume-weighted moving average. </p> </dd> <dt>ZLEMA</dt><dd><p> Zero lag exponential moving average. </p> </dd> <dt>VWMA</dt><dd><p> Volume-weighed moving average (same as <code>VWAP</code>). </p> </dd> <dt>VWAP</dt><dd><p> Volume-weighed average price (same as <code>VWMA</code>). </p> </dd> <dt>VWA</dt><dd><p> Variable-length moving average. </p> </dd> <dt>HMA</dt><dd><p> Hull moving average. </p> </dd> <dt>ALMA</dt><dd><p> Arnaud Legoux moving average. </p> </dd> </dl> <h3>Warning </h3> <p>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. </p> <h3>Note</h3> <p>For <code>EMA</code>, <code>wilder=FALSE</code> (the default) uses an exponential smoothing ratio of <code>2/(n+1)</code>, while <code>wilder=TRUE</code> uses Welles Wilder's exponential smoothing ratio of <code>1/n</code>. The <code>EMA</code> result is initialized with the <code>n</code>-period sample average at period <code>n</code>. The exponential decay is applied from that point forward. </p> <p>Since <code>WMA</code> can accept a weight vector of length equal to the length of <code>x</code> or of length <code>n</code>, it can be used as a regular weighted moving average (in the case <code>wts=1:n</code>) or as a moving average weighted by volume, another indicator, etc. </p> <p>Since <code>DEMA</code> allows adjusting <code>v</code>, it is technically Tim Tillson's generalized DEMA (GD). When <code>v=1</code> (the default), the result is the standard DEMA. When <code>v=0</code>, the result is a regular EMA. All other values of <code>v</code> 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. </p> <p>For <code>EVWMA</code>, if <code>volume</code> is a series, <code>n</code> should be chosen so the sum of the volume for <code>n</code> periods approximates the total number of outstanding shares for the security being averaged. If <code>volume</code> is a constant, it should represent the total number of outstanding shares for the security being averaged. </p> <h3>Author(s)</h3> <p>Joshua Ulrich, Ivan Popivanov (HMA, ALMA) </p> <h3>References</h3> <p>The following site(s) were used to code/document this indicator:<br /> <a href="https://www.fmlabs.com/reference/ExpMA.htm">https://www.fmlabs.com/reference/ExpMA.htm</a><br /> <a href="https://www.fmlabs.com/reference/WeightedMA.htm">https://www.fmlabs.com/reference/WeightedMA.htm</a><br /> <a href="https://www.fmlabs.com/reference/DEMA.htm">https://www.fmlabs.com/reference/DEMA.htm</a><br /> <a href="https://www.fmlabs.com/reference/T3.htm">https://www.fmlabs.com/reference/T3.htm</a><br /> <a href="https://www.linnsoft.com/techind/evwma-elastic-volume-weighted-moving-average">https://www.linnsoft.com/techind/evwma-elastic-volume-weighted-moving-average</a><br /> <a href="https://www.fmlabs.com/reference/ZeroLagExpMA.htm">https://www.fmlabs.com/reference/ZeroLagExpMA.htm</a><br /> <a href="https://www.fmlabs.com/reference/VIDYA.htm">https://www.fmlabs.com/reference/VIDYA.htm</a><br /> <a href="https://www.traderslog.com/hullmovingaverage">https://www.traderslog.com/hullmovingaverage</a><br /> <a href="https://web.archive.org/web/20180222085959/http://arnaudlegoux.com/">https://web.archive.org/web/20180222085959/http://arnaudlegoux.com/</a><br /> </p> <h3>See Also</h3> <p>See <code><a href="runFun.html">wilderSum</a></code>, which is used in calculating a Welles Wilder type MA. </p> <h3>Examples</h3> <pre> 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 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 ) </pre> <hr /><div style="text-align: center;">[Package <em>TTR</em> version 0.24.3 <a href="00Index.html">Index</a>]</div> </body></html>