F4: add StochRSI and Ultimate Oscillator

Completes the F4 family (Stochastic oscillators) end to end:

- Rust core: stoch_rsi.rs (Stochastic Oscillator applied to the RSI
  series, bounded [0,100]) and ultimate_oscillator.rs (Larry Williams'
  weighted three-timeframe buying-pressure oscillator). Each with a full
  Indicator impl, runnable doctest and reference / saturation / bounds /
  warmup / reset / batch==streaming tests.
- Python: PyStochRsi / PyUltimateOscillator PyO3 classes + module
  registration + .pyi stubs (defaults StochRSI=(14,14), UO=(7,14,28)).
- Node: explicit StochRsiNode and UltimateOscillatorNode; index.d.ts
  and index.js updated.
- WASM: WasmStochRsi via the scalar macro, explicit
  WasmUltimateOscillator.
- Wiki: Indicator-StochRsi.md and Indicator-UltimateOscillator.md plus
  rows in Indicators-Overview.md and entries in Home.md.

cargo fmt + clippy (core/wickra/data/wasm/node) clean; 278 core tests,
25 data tests and 39 doctests green.
This commit is contained in:
kingchenc
2026-05-22 18:02:44 +02:00
parent 7728151c87
commit e24e7726ce
13 changed files with 1181 additions and 3 deletions
+3 -1
View File
@@ -310,7 +310,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}
const { version, SMA, EMA, WMA, RSI, DEMA, TEMA, HMA, ROC, TRIX, SMMA, TRIMA, ZLEMA, T3, VWMA, MOM, CMO, TSI, PMO, MACD, BollingerBands, ATR, Stochastic, OBV, ADX, CCI, WilliamsR, MFI, PSAR, Keltner, Donchian, VWAP, AwesomeOscillator, Aroon, KAMA } = nativeBinding
const { version, SMA, EMA, WMA, RSI, DEMA, TEMA, HMA, ROC, TRIX, SMMA, TRIMA, ZLEMA, T3, VWMA, MOM, CMO, TSI, PMO, StochRSI, UltimateOscillator, MACD, BollingerBands, ATR, Stochastic, OBV, ADX, CCI, WilliamsR, MFI, PSAR, Keltner, Donchian, VWAP, AwesomeOscillator, Aroon, KAMA } = nativeBinding
module.exports.version = version
module.exports.SMA = SMA
@@ -331,6 +331,8 @@ module.exports.MOM = MOM
module.exports.CMO = CMO
module.exports.TSI = TSI
module.exports.PMO = PMO
module.exports.StochRSI = StochRSI
module.exports.UltimateOscillator = UltimateOscillator
module.exports.MACD = MACD
module.exports.BollingerBands = BollingerBands
module.exports.ATR = ATR
+94
View File
@@ -1144,6 +1144,100 @@ impl PmoNode {
// ============================== VWMA ==============================
// ============================== StochRSI ==============================
#[napi(js_name = "StochRSI")]
pub struct StochRsiNode {
inner: wc::StochRsi,
}
#[napi]
impl StochRsiNode {
#[napi(constructor)]
pub fn new(rsi_period: u32, stoch_period: u32) -> napi::Result<Self> {
Ok(Self {
inner: wc::StochRsi::new(rsi_period as usize, stoch_period as usize)
.map_err(map_err)?,
})
}
#[napi]
pub fn update(&mut self, value: f64) -> Option<f64> {
self.inner.update(value)
}
#[napi]
pub fn batch(&mut self, prices: Vec<f64>) -> Vec<f64> {
flatten(self.inner.batch(&prices))
}
#[napi]
pub fn reset(&mut self) {
self.inner.reset();
}
#[napi(js_name = "isReady")]
pub fn is_ready(&self) -> bool {
self.inner.is_ready()
}
#[napi(js_name = "warmupPeriod")]
pub fn warmup_period(&self) -> u32 {
self.inner.warmup_period() as u32
}
}
// ============================== Ultimate Oscillator ==============================
#[napi(js_name = "UltimateOscillator")]
pub struct UltimateOscillatorNode {
inner: wc::UltimateOscillator,
}
#[napi]
impl UltimateOscillatorNode {
#[napi(constructor)]
pub fn new(short: u32, mid: u32, long: u32) -> napi::Result<Self> {
Ok(Self {
inner: wc::UltimateOscillator::new(short as usize, mid as usize, long as usize)
.map_err(map_err)?,
})
}
#[napi]
pub fn update(&mut self, high: f64, low: f64, close: f64) -> napi::Result<Option<f64>> {
Ok(self.inner.update(cnd(high, low, close, 0.0)?))
}
#[napi]
pub fn batch(
&mut self,
high: Vec<f64>,
low: Vec<f64>,
close: Vec<f64>,
) -> napi::Result<Vec<f64>> {
if high.len() != low.len() || low.len() != close.len() {
return Err(NapiError::from_reason(
"high, low, close must be equal length".to_string(),
));
}
let mut out = Vec::with_capacity(high.len());
for i in 0..high.len() {
out.push(
self.inner
.update(cnd(high[i], low[i], close[i], 0.0)?)
.unwrap_or(f64::NAN),
);
}
Ok(out)
}
#[napi]
pub fn reset(&mut self) {
self.inner.reset();
}
#[napi(js_name = "isReady")]
pub fn is_ready(&self) -> bool {
self.inner.is_ready()
}
#[napi(js_name = "warmupPeriod")]
pub fn warmup_period(&self) -> u32 {
self.inner.warmup_period() as u32
}
}
#[napi(js_name = "VWMA")]
pub struct VwmaNode {
inner: wc::Vwma,
@@ -76,6 +76,35 @@ class TRIMA:
@property
def value(self) -> Optional[float]: ...
class StochRSI:
def __init__(self, rsi_period: int = 14, stoch_period: int = 14) -> None: ...
def update(self, value: float) -> Optional[float]: ...
def batch(self, prices: NDArray[np.float64]) -> NDArray[np.float64]: ...
def reset(self) -> None: ...
def is_ready(self) -> bool: ...
def warmup_period(self) -> int: ...
@property
def periods(self) -> Tuple[int, int]: ...
@property
def value(self) -> Optional[float]: ...
class UltimateOscillator:
def __init__(self, short: int = 7, mid: int = 14, long: int = 28) -> None: ...
def update(self, candle: CandleLike) -> Optional[float]: ...
def batch(
self,
high: NDArray[np.float64],
low: NDArray[np.float64],
close: NDArray[np.float64],
) -> NDArray[np.float64]: ...
def reset(self) -> None: ...
def is_ready(self) -> bool: ...
def warmup_period(self) -> int: ...
@property
def periods(self) -> Tuple[int, int, int]: ...
@property
def value(self) -> Optional[float]: ...
class MOM:
def __init__(self, period: int = 10) -> None: ...
def update(self, value: float) -> Optional[float]: ...
+128
View File
@@ -1519,6 +1519,132 @@ impl PyAroon {
}
}
// ============================== StochRSI ==============================
#[pyclass(name = "StochRSI", module = "wickra._wickra")]
#[derive(Clone)]
struct PyStochRsi {
inner: wc::StochRsi,
}
#[pymethods]
impl PyStochRsi {
#[new]
#[pyo3(signature = (rsi_period=14, stoch_period=14))]
fn new(rsi_period: usize, stoch_period: usize) -> PyResult<Self> {
Ok(Self {
inner: wc::StochRsi::new(rsi_period, stoch_period).map_err(map_err)?,
})
}
fn update(&mut self, value: f64) -> Option<f64> {
self.inner.update(value)
}
fn batch<'py>(
&mut self,
py: Python<'py>,
prices: PyReadonlyArray1<'py, f64>,
) -> PyResult<Bound<'py, PyArray1<f64>>> {
let slice = prices
.as_slice()
.map_err(|_| PyValueError::new_err(NON_CONTIGUOUS))?;
Ok(flatten(self.inner.batch(slice)).into_pyarray_bound(py))
}
#[getter]
fn periods(&self) -> (usize, usize) {
self.inner.periods()
}
#[getter]
fn value(&self) -> Option<f64> {
self.inner.value()
}
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 {
let (r, s) = self.inner.periods();
format!("StochRSI(rsi_period={r}, stoch_period={s})")
}
}
// ============================== Ultimate Oscillator ==============================
#[pyclass(name = "UltimateOscillator", module = "wickra._wickra")]
#[derive(Clone)]
struct PyUltimateOscillator {
inner: wc::UltimateOscillator,
}
#[pymethods]
impl PyUltimateOscillator {
#[new]
#[pyo3(signature = (short=7, mid=14, long=28))]
fn new(short: usize, mid: usize, long: usize) -> PyResult<Self> {
Ok(Self {
inner: wc::UltimateOscillator::new(short, mid, long).map_err(map_err)?,
})
}
fn update(&mut self, candle: &Bound<'_, PyAny>) -> PyResult<Option<f64>> {
let c = extract_candle(candle)?;
Ok(self.inner.update(c))
}
/// Batch over numpy columns: high, low, close (all 1-D, equal length).
fn batch<'py>(
&mut self,
py: Python<'py>,
high: PyReadonlyArray1<'py, f64>,
low: PyReadonlyArray1<'py, f64>,
close: 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))?;
if h.len() != l.len() || l.len() != c.len() {
return Err(PyValueError::new_err(
"high, low, close 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], 0.0, 0).map_err(map_err)?;
out.push(self.inner.update(candle).unwrap_or(f64::NAN));
}
Ok(out.into_pyarray_bound(py))
}
#[getter]
fn periods(&self) -> (usize, usize, usize) {
self.inner.periods()
}
#[getter]
fn value(&self) -> Option<f64> {
self.inner.value()
}
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 {
let (s, m, l) = self.inner.periods();
format!("UltimateOscillator(short={s}, mid={m}, long={l})")
}
}
// ============================== MOM ==============================
#[pyclass(name = "MOM", module = "wickra._wickra")]
@@ -2052,5 +2178,7 @@ fn _wickra(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyCmo>()?;
m.add_class::<PyTsi>()?;
m.add_class::<PyPmo>()?;
m.add_class::<PyStochRsi>()?;
m.add_class::<PyUltimateOscillator>()?;
Ok(())
}
+39
View File
@@ -83,6 +83,7 @@ wasm_scalar_indicator!(WasmMom, "MOM", wc::Mom, period: usize);
wasm_scalar_indicator!(WasmCmo, "CMO", wc::Cmo, period: usize);
wasm_scalar_indicator!(WasmTsi, "TSI", wc::Tsi, long: usize, short: usize);
wasm_scalar_indicator!(WasmPmo, "PMO", wc::Pmo, smoothing1: usize, smoothing2: usize);
wasm_scalar_indicator!(WasmStochRsi, "StochRSI", wc::StochRsi, rsi_period: usize, stoch_period: usize);
// ---------- KAMA (three params) ----------
@@ -330,6 +331,44 @@ impl WasmObv {
}
}
#[wasm_bindgen(js_name = UltimateOscillator)]
pub struct WasmUltimateOscillator {
inner: wc::UltimateOscillator,
}
#[wasm_bindgen(js_class = UltimateOscillator)]
impl WasmUltimateOscillator {
#[wasm_bindgen(constructor)]
pub fn new(short: usize, mid: usize, long: usize) -> Result<WasmUltimateOscillator, JsError> {
Ok(Self {
inner: wc::UltimateOscillator::new(short, mid, long).map_err(map_err)?,
})
}
pub fn update(&mut self, high: f64, low: f64, close: f64) -> Result<Option<f64>, JsError> {
let c = make_candle(high, low, close, 0.0)?;
Ok(self.inner.update(c))
}
pub fn batch(
&mut self,
high: &[f64],
low: &[f64],
close: &[f64],
) -> Result<Float64Array, JsError> {
if high.len() != low.len() || low.len() != close.len() {
return Err(JsError::new("high, low, close must be equal length"));
}
let mut out = Vec::with_capacity(high.len());
for i in 0..high.len() {
let c = make_candle(high[i], low[i], close[i], 0.0)?;
out.push(self.inner.update(c).unwrap_or(f64::NAN));
}
Ok(Float64Array::from(out.as_slice()))
}
pub fn reset(&mut self) {
self.inner.reset();
}
}
#[wasm_bindgen(js_name = VWMA)]
pub struct WasmVwma {
inner: wc::Vwma,