Files
wickra/docs/wiki/indicators/trend/Indicator-Vwma.md
T
kingchenc 780a176072 F2: add ZLEMA, T3 and VWMA advanced moving averages
Completes the F2 family (Advanced MAs) end to end:

- Rust core: zlema.rs (Zero-Lag EMA over the de-lagged series
  2·price − price[lag]), t3.rs (Tillson's six-EMA cascade with the
  volume-factor polynomial), vwma.rs (volume-weighted rolling mean with
  a zero-volume fallback to the unweighted mean). Each with a full
  Indicator impl, runnable doctest and reference-value / warmup /
  reset / batch==streaming / non-finite tests.
- Python: PyZlema / PyT3 / PyVwma PyO3 classes + module registration
  + .pyi stubs (T3 defaults v=0.7).
- Node: ZlemaNode via the scalar macro, explicit T3Node and VwmaNode
  classes; index.d.ts and index.js updated.
- WASM: WasmZlema / WasmT3 via the scalar macro, explicit WasmVwma.
- Wiki: Indicator-Zlema.md, Indicator-T3.md, Indicator-Vwma.md plus
  rows in Indicators-Overview.md and entries in Home.md.

cargo fmt + clippy (core/wickra/data/wasm/node) clean; 232 core tests,
25 data tests and 33 doctests green.
2026-05-22 17:45:02 +02:00

5.4 KiB
Raw Blame History

VWMA

Volume-Weighted Moving Average — a rolling mean of closes where each bar is weighted by its own traded volume.

Quick reference

Field Value
Family Trend
Sub-category Volume-weighted averages
Input type Candle (uses close and volume)
Output type f64
Output range unbounded; tracks the input price scale
Default parameters period is required (no default in either binding)
Warmup period period
Interpretation Trend line that leans toward high-conviction (high-volume) bars.

Formula

VWMA_t = Σ(close_i · volume_i) / Σ(volume_i)   over the last `period` bars

A heavy bar pulls the average toward its close; a thin bar barely moves it. Both the numerator (Σ price·volume) and denominator (Σ volume) are maintained as O(1) rolling sums, so update is O(1) regardless of period.

If every bar in the window has zero volume the weighted mean is undefined (0 / 0). VWMA then falls back to the plain unweighted mean of the period closes, so the output is always finite and defined.

Parameters

Name Type Default Valid range Description
period usize none >= 1 Rolling window length in bars. period = 0 errors with Error::PeriodZero.

There is no Python #[pyo3(signature = …)] default for VWMA, so wickra.VWMA(period) requires the period explicitly.

Inputs / Outputs

From crates/wickra-core/src/indicators/vwma.rs:

impl Indicator for Vwma {
    type Input = Candle;
    type Output = f64;
    // update(&mut self, input: Candle) -> Option<f64>
}

VWMA is a candle-input indicator: it reads close and volume from each Candle. In Python the streaming update accepts a 6-tuple or a dict; the batch helper takes close and volume numpy arrays. Node and WASM expose update(close, volume) and batch(close, volume).

Warmup

Vwma::new(period).warmup_period() == period. The first period 1 candles fill the rolling window; the period-th update() produces the first weighted mean.

Edge cases

  • Constant closes. Closes all equal to c give VWMA = c regardless of the volumes (Σ c·v / Σ v = c), and the zero-volume fallback also yields c (constant_series_yields_the_constant pins this).
  • Zero-volume window. If every bar in the window has volume = 0, VWMA returns the unweighted mean of the period closes (zero_volume_window_falls_back_to_unweighted_mean pins this).
  • Candle validation. Candle::new already rejects NaN/infinite fields and negative volume, so update never sees an invalid bar — there is no separate non-finite guard.
  • Reset. vwma.reset() clears the window and all three rolling sums.

Examples

Rust

use wickra::{Candle, Indicator, Vwma};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut vwma = Vwma::new(2)?;
    // (close, volume): (10, 1) then (20, 3).
    let a = Candle::new(10.0, 10.0, 10.0, 10.0, 1.0, 0)?;
    let b = Candle::new(20.0, 20.0, 20.0, 20.0, 3.0, 1)?;
    println!("{:?}", vwma.update(a));
    println!("{:?}", vwma.update(b));
    Ok(())
}

Output:

None
Some(17.5)

The window holds two bars: (10·1 + 20·3) / (1 + 3) = 70 / 4 = 17.5. The heavier bar at 20 dominates, so the result sits well above the simple mean of 15. This matches the reference_value test in crates/wickra-core/src/indicators/vwma.rs.

Python

import numpy as np
import wickra as ta

vwma = ta.VWMA(2)
close = np.array([10.0, 20.0, 30.0])
volume = np.array([1.0, 3.0, 1.0])
print(vwma.batch(close, volume))
print("warmup_period =", vwma.warmup_period())

Output:

[ nan 17.5 22.5]
warmup_period = 2

Node

const ta = require('wickra');
const vwma = new ta.VWMA(2);
console.log(vwma.batch([10, 20, 30], [1, 3, 1]));
console.log('warmupPeriod:', vwma.warmupPeriod());

Output:

[ NaN, 17.5, 22.5 ]
warmupPeriod: 2

Interpretation

Vwma is a trend line that respects participation. Compared with an equal-weighted Sma of the same period, it reacts faster to moves backed by heavy volume and lags moves on thin volume. The classic read is the Vwma-vs-Sma relationship: Vwma above Sma means recent strength was volume-backed (more trustworthy); Vwma below Sma means the up-moves came on light volume. It is a session-independent cousin of Vwap — VWAP weights by volume since the start of the stream, VWMA over a fixed rolling window.

Common pitfalls

  • Feeding it scalar prices. VWMA needs volume; it takes a Candle, not an f64. Use Sma/Wma for a pure price series.
  • Assuming a zero-volume window is an error. It is not — VWMA falls back to the unweighted mean. If that fallback matters to you, screen the window's total volume yourself.

References

The volume-weighted moving average is a standard volume-weighted rolling mean; the rolling-sum formulation here matches the common pandas implementation (close*volume).rolling(n).sum() / volume.rolling(n).sum(), with an explicit zero-volume fallback added for robustness.

See also