feat(bindings): expose RollingVWAP in Python, Node and WASM (R4)

The rolling-window VWAP indicator (`wickra_core::RollingVwap`) was only
available in the Rust crate, even though the README's Volume-family
table already advertised "VWAP (cumulative + rolling)" as a cross-
language feature. Users on Python, Node or in the browser had to fall
back to the cumulative `VWAP` or re-implement the rolling variant
themselves.

This commit closes the gap end-to-end:

- Python: `wickra.RollingVWAP(period)` — same constructor / `update` /
  `batch` / `reset` / `is_ready` / `warmup_period` surface as `VWAP`,
  plus a `period` property and a typed `__repr__`. The `__init__.py`
  re-exports it and `__all__` lists it; the `.pyi` stub matches.
- Node: `RollingVWAP(period)` — napi class with the same lifecycle,
  exported from `index.js` and declared in `index.d.ts`.
- WASM: `RollingVWAP(period)` — wasm-bindgen class with the same
  `Float64Array` I/O as `VWAP`.

Tests added:

- Python: `test_rolling_vwap_streaming_matches_batch` — exercises
  `update == batch` plus the full lifecycle on the shared OHLC fixture.
- Node: `RollingVWAP` row in the `candleScalar` parity table — covered
  by the generic streaming-vs-batch + lifecycle harness.
- WASM: dedicated `wasm-bindgen-test` mirrors the Python test.

The wiki page `Indicator-Vwap.md` drops the "Rust-only" caveat and
gains Python / Node / WASM examples.
This commit is contained in:
kingchenc
2026-05-23 01:43:00 +02:00
parent 3a6b5ebae3
commit efcd6216c1
10 changed files with 322 additions and 5 deletions
@@ -87,6 +87,7 @@ from ._wickra import (
# Volume
OBV,
VWAP,
RollingVWAP,
ADL,
VolumePriceTrend,
ChaikinMoneyFlow,
@@ -167,6 +168,7 @@ __all__ = [
# Volume
"OBV",
"VWAP",
"RollingVWAP",
"ADL",
"VolumePriceTrend",
"ChaikinMoneyFlow",
@@ -945,6 +945,22 @@ class VWAP:
def is_ready(self) -> bool: ...
def warmup_period(self) -> int: ...
class RollingVWAP:
def __init__(self, period: int) -> None: ...
def update(self, candle: CandleLike) -> Optional[float]: ...
def batch(
self,
high: NDArray[np.float64],
low: NDArray[np.float64],
close: NDArray[np.float64],
volume: NDArray[np.float64],
) -> NDArray[np.float64]: ...
@property
def period(self) -> int: ...
def reset(self) -> None: ...
def is_ready(self) -> bool: ...
def warmup_period(self) -> int: ...
class AwesomeOscillator:
def __init__(self, fast: int = 5, slow: int = 34) -> None: ...
def update(self, candle: CandleLike) -> Optional[float]: ...
+71
View File
@@ -1413,6 +1413,76 @@ impl PyVwap {
}
}
// ============================== Rolling VWAP ==============================
#[pyclass(name = "RollingVWAP", module = "wickra._wickra", skip_from_py_object)]
#[derive(Clone)]
struct PyRollingVwap {
inner: wc::RollingVwap,
}
#[pymethods]
impl PyRollingVwap {
#[new]
fn new(period: usize) -> PyResult<Self> {
Ok(Self {
inner: wc::RollingVwap::new(period).map_err(map_err)?,
})
}
fn update(&mut self, candle: &Bound<'_, PyAny>) -> PyResult<Option<f64>> {
let c = extract_candle(candle)?;
Ok(self.inner.update(c))
}
fn batch<'py>(
&mut self,
py: Python<'py>,
high: PyReadonlyArray1<'py, f64>,
low: PyReadonlyArray1<'py, f64>,
close: PyReadonlyArray1<'py, f64>,
volume: PyReadonlyArray1<'py, f64>,
) -> PyResult<Bound<'py, PyArray1<f64>>> {
let h = high
.as_slice()
.map_err(|_| PyValueError::new_err(NON_CONTIGUOUS))?;
let l = low
.as_slice()
.map_err(|_| PyValueError::new_err(NON_CONTIGUOUS))?;
let c = close
.as_slice()
.map_err(|_| PyValueError::new_err(NON_CONTIGUOUS))?;
let v = volume
.as_slice()
.map_err(|_| PyValueError::new_err(NON_CONTIGUOUS))?;
if h.len() != l.len() || l.len() != c.len() || c.len() != v.len() {
return Err(PyValueError::new_err(
"high, low, close, volume must be equal length",
));
}
let mut out = Vec::with_capacity(h.len());
for i in 0..h.len() {
let candle = wc::Candle::new(c[i], h[i], l[i], c[i], v[i], 0).map_err(map_err)?;
out.push(self.inner.update(candle).unwrap_or(f64::NAN));
}
Ok(out.into_pyarray(py))
}
#[getter]
fn period(&self) -> usize {
self.inner.period()
}
fn reset(&mut self) {
self.inner.reset();
}
fn is_ready(&self) -> bool {
self.inner.is_ready()
}
fn warmup_period(&self) -> usize {
self.inner.warmup_period()
}
fn __repr__(&self) -> String {
format!("RollingVWAP(period={})", self.inner.period())
}
}
// ============================== Awesome Oscillator ==============================
#[pyclass(
@@ -4433,6 +4503,7 @@ fn _wickra(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyKeltner>()?;
m.add_class::<PyDonchian>()?;
m.add_class::<PyVwap>()?;
m.add_class::<PyRollingVwap>()?;
m.add_class::<PyAo>()?;
m.add_class::<PyAroon>()?;
m.add_class::<PySmma>()?;
@@ -115,3 +115,23 @@ def test_obv_streaming_matches_batch(ohlc_series):
rows.append(streamer.update((float(c), float(c), float(c), float(c), float(v), 0)))
streamed = np.array([math.nan if x is None else x for x in rows], dtype=np.float64)
assert _equal_with_nan(batch, streamed)
def test_rolling_vwap_streaming_matches_batch(ohlc_series):
# RollingVWAP(20) on the shared OHLC series. Provides finite-memory VWAP
# parity coverage now that the indicator is exposed across all bindings.
high, low, close = ohlc_series
volume = np.linspace(100.0, 200.0, num=close.size, dtype=np.float64)
batch = ta.RollingVWAP(20).batch(high, low, close, volume)
streamer = ta.RollingVWAP(20)
rows = []
for h, l, c, v in zip(high, low, close, volume):
rows.append(streamer.update((float(c), float(h), float(l), float(c), float(v), 0)))
streamed = np.array([math.nan if x is None else x for x in rows], dtype=np.float64)
assert _equal_with_nan(batch, streamed)
assert streamer.period == 20
assert streamer.warmup_period() == 20
assert streamer.is_ready()
streamer.reset()
assert not streamer.is_ready()