Variable-Length Windows
All functions · nseries package
AverageVariable
func (s Series) AverageVariable(p Series, minPeriod, maxPeriod int) Series
AverageVariable computes Average’s arithmetic mean with a period from p at each bar.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 equal Average(n) bit for bit; earlier bars are NaN where Average copies the input. A window containing NaN gives NaN; infinities of one sign give that infinity, and both signs give NaN; a finite window whose sum overflows gives that infinity. Zero results are +0. Work is amortised O(1) per bar for a steady period, with O(q) rebuilds when the window grows. During a run of invalid bars the queue only grows, so working space is O(len(s)) besides the result.
Uses include Arrington’s variable-length moving average. For finite data and finite periods, within TA-Lib’s period bounds (1 to 100000), TA-Lib MAVP with SMA agrees to rounding on overlapping output. MAVP starts at maxPeriod-1 regardless of the selected period and, from TA-Lib 0.8, treats NaN periods as minPeriod. XAverageAlphaS with per-bar alpha 2/(q+1) is the exponential analogue; TA-Lib MAVP(EMA) instead selects from a bank of fixed-period EMAs.
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.
HighestVariable
func (s Series) HighestVariable(p Series, minPeriod, maxPeriod int) Series
HighestVariable computes Highest’s window maximum with a period from p at each bar.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 equal Highest(n) bit for bit; earlier bars are NaN instead of its warm-up. A window containing NaN gives NaN; infinities are ordered normally. The builtin max rules apply: max(-0, +0) is +0, and a window of only -0 gives -0. Each window is re-read, so this is not a ratchet: growing the period brings older observations back. Work is O(q) per valid bar, with O(1) working space besides the result.
For a Castleman channel ending at the previous bar, use high.Shift(1).HighestVariable(p, minPeriod, maxPeriod), retaining bar t’s period. Shifting HighestVariable’s result instead uses bar t-1’s period. Shift(1) pads bar 0 with 0, which any window reaching bar 0 then reads. For the maximum since each event, see HighestSince; for a fixed window, Highest(n) or Quantile(n, 1).
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.
LowestVariable
func (s Series) LowestVariable(p Series, minPeriod, maxPeriod int) Series
LowestVariable computes Lowest’s window minimum with a period from p at each bar.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 equal Lowest(n) bit for bit; earlier bars are NaN instead of its warm-up. A window containing NaN gives NaN; infinities are ordered normally. The builtin min rules apply: min(-0, +0) is -0, and a window of only -0 gives -0. Each window is re-read, so this is not a ratchet: growing the period brings older observations back. Work is O(q) per valid bar, with O(1) working space besides the result.
For a Castleman channel ending at the previous bar, use low.Shift(1).LowestVariable(p, minPeriod, maxPeriod), retaining bar t’s period. Shifting LowestVariable’s result instead uses bar t-1’s period. Shift(1) pads bar 0 with 0, which any window reaching bar 0 then reads. For the minimum since each event, see LowestSince; for a fixed window, Lowest(n) or Quantile(n, 0).
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.
StdDevVariable
func (s Series) StdDevVariable(p Series, minPeriod, maxPeriod int) Series
StdDevVariable computes the population standard deviation with a period from p at each bar, as in StdDev and Arrington’s printed figures; the Chan ABZ’ port uses this form too, though the published spreadsheets use Excel’s sample STDEV.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 agree with StdDev(n) to about 1e-12 relative on ordinary prices, and to about 1e-16 times sqrt(n) times |mean|/sd relative in general; earlier bars are NaN instead of its zero warm-up. The kernels differ: StdDev’s running moments lose digits when the mean dwarfs the spread, overflow once the standard deviation passes about 1.3e154/sqrt(n), and lose precision once the deviations from the window mean fall below about 1e-154 in magnitude, where this method’s scaled kernel does not. A window containing NaN or either infinity gives NaN. Finite q = 1 and flat windows give +0; all zero results are +0. Each finite window uses the scaled moments kernel. Work is O(q) per valid bar, with O(min(maxPeriod, len(s))) scratch allocated once besides the result.
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.
SumVariable
func (s Series) SumVariable(p Series, minPeriod, maxPeriod int) Series
SumVariable computes Sum’s window total with a period from p at each bar.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 equal Sum(n) bit for bit; earlier bars are NaN where Sum gives expanding sums. A window containing NaN gives NaN; infinities of one sign give that infinity, and both signs give NaN; a finite window whose sum overflows gives that infinity. Zero results are +0. Work is amortised O(1) per bar for a steady period, with O(q) rebuilds when the window grows. During a run of invalid bars the queue only grows, so working space is O(len(s)) besides the result.
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.
WAverageVariable
func (s Series) WAverageVariable(p Series, minPeriod, maxPeriod int) Series
WAverageVariable computes WAverage’s linearly weighted mean with a period from p at each bar. Weights descend from q for the newest value to 1 for the oldest; products are rounded separately and summed newest first.
p is right-aligned with s. Periods are clamped in float64 to [minPeriod, maxPeriod], then truncated; -Inf selects minPeriod and +Inf selects maxPeriod. Bars before a shorter p’s overlap, NaN periods and periods exceeding the history so far give NaN, so the start is data-dependent, not a zero warm-up; a longer p’s surplus oldest values are unused. Invalid bounds (minPeriod < 1 or minPeriod > maxPeriod) return len(s) aligned zeros. maxPeriod may exceed len(s). Series are paired by position only; see “Time alignment” in the manual.
For a constant period n within the bounds, bars from n-1 equal WAverage(n) bit for bit with unfused multiply-adds and agree to rounding where the fixed method fuses them. Earlier bars are NaN instead of its warm-up. NaN propagates through the window; infinities follow the weighted sum. Zero results are +0. Work is O(q) per valid bar, with O(1) working space besides the result.
Sources: Arrington, “Building A Variable-Length Moving Average”, Stocks & Commodities (June 1991); TA-Lib MAVP.