Moving Averages
All functions · nseries package
Average
func (s Series) Average(n int) Series
Average returns the moving average of n observations. Before a full window is available, it returns the corresponding input value. Values of n below one, or above the input length, return a copy of the input.
EMA
func (s Series) EMA(n int) Series
EMA is an alias for XAverage.
HMA
func HMA(s Series, n int) Series
HMA is an alias for HullAverage.
HullAverage
func (s Series) HullAverage(n int) Series
HullAverage returns the Hull moving average over the last <n>{=html} values in the provided Series. If there is insufficient input to provide an average over <n>{=html} values, the original Series is returned.
The first bars pass the input through, as its inner weighted averages do, then mix pass-through and averaged values; the result is a clean Hull average from bar n + floor(sqrt(n)) - 2.
Mean
func (s Series) Mean(n int) Series
Mean is an alias for Average.
SMA
func (s Series) SMA(n int) Series
SMA is an alias for Average.
TEMA
func (s Series) TEMA(n int) Series
TEMA returns the triple-exponential moving average.
Bars 0 to n-2 pass the input through, being three nested XAverage passes, and the result is free of seed effects only from bar 3n-3.
WAverage
func (s Series) WAverage(n int) Series
WAverage returns a weighted moving average over the last <n>{=html} values in the provided Series. Bars 0 to n-2 pass the input through (result[i] = s[i]) and the first weighted average is at bar n-1; where n exceeds the length of the series the original Series is returned.
WMA
func (s Series) WMA(n int) Series
WMA is an alias for WAverage.
XAverage
func (s Series) XAverage(n int) Series
XAverage returns an exponential moving average over the last <n>{=html} values in the provided Series. Bars 0 to n-2 pass the input through (result[i] = s[i]); bar n-1 is the simple average of the first n values, which is the seed; later bars apply the exponential recursion with alpha = 2/(n+1). Where n exceeds the length of the series there is no seed, so the original Series is returned. A value of n less than one returns a copy of the original Series.
Zeros or other warm-up values of an upstream method enter the seed and decay only gradually, so trim an upstream warm-up before smoothing, as ZLEMA, AvgVelocity and the CaseyPercentC variants do.
XMA
func (s Series) XMA(n int) Series
XMA is an alias for XAverage.
ZLEMA
func (s Series) ZLEMA(n int) Series
ZLEMA returns Ehlers’s zero-lag EMA: the EMA of the compensated price 2*s[i] - s[i-lag], lag = floor((n-1)/2), from “Zero-Lag Data Smoothers” (TASC, July 2002). ZLEMAGain is the error-correcting form of Ehlers and Way (TASC, November 2010). At period 1 the mathematical lag is zero, and ZLEMA(1) returns an independent copy of the input. At period 2 the lag is also mathematically zero; ZLEMA(2) uses its normal recurrence with that zero lag and XAverage(2)’s existing initialisation.
For n >= 3 the warm-up reads 0, not NaN: bars 0 to lag-1 are 0 and the first valid index is lag, the first bar where the compensated price exists. From bar lag the EMA runs as if the series started there: it passes the compensated price through on bars lag to lag+n-2 and is seeded at bar lag+n-1 with the simple average of its first n values. Releases before this change fed 2*s[i] into the EMA’s seed on bars 0 to lag-1 (the missing s[i-lag] read as 0), so the first values were about twice the price and decayed slowly.
ZLEMAGain
func (s Series) ZLEMAGain(n int, gainMax int) Series
ZLEMAGain implements a gain-adjusted moving average. Based on code from Robert Pardo.
gainMax is in tenths: the gains searched are j/10 for j = -gainMax..gainMax, so gainMax 50 searches -5.0 to 5.0. Bars 0 to n-1 copy XAverage(n), which passes the input through on bars 0 to n-2.