feat: add Market Breadth family with CrossSection input (#153)
## What Adds a new indicator input type and family for **market-breadth** analysis — indicators that aggregate the state of an entire universe of symbols at each tick, rather than a single instrument's price. This is the last open input-type on the expansion roadmap (S10) and unblocks the remaining breadth indicators (McClellan, TRIN, High-Low Index, ...). ## Core - **`CrossSection` input type** (`crates/wickra-core/src/cross_section.rs`) — one tick carrying the per-symbol state of the whole universe as a `Vec<Member>` + `timestamp`. Each `Member` precomputes a signed `change` (sign classifies advancing / declining / unchanged), a `volume`, and `new_high` / `new_low` extreme flags, so the breadth indicators stay stateless per tick. Both `Member` and `CrossSection` are `#[non_exhaustive]` for additive field growth. `CrossSection::new` validates the universe (non-empty, finite changes, finite non-negative volumes); `new_unchecked` skips validation for hot paths. `advancers()` / `decliners()` count by sign. - **`Error::InvalidCrossSection`** variant for the validation failures. - **`AdvanceDecline`** (`advance_decline.rs`) — the Advance/Decline Line: the running cumulative sum of net advancing-minus-declining issues. `Input = CrossSection`, `Output = f64`, ready after the first tick. - New **"Market Breadth"** `FAMILIES` group; indicator count **314 → 315**, family count nineteen → twenty. ## Bindings All custom (CrossSection is non-scalar, so no macros apply). The universe crosses each boundary as parallel arrays (`change`, `volume`, `new_high`, `new_low`): - **Python / Node** expose `update` + `batch` (one array group per tick). Node satisfies the completeness contract (`update`/`batch`/`reset`/`isReady`/`warmupPeriod`). - **WASM** exposes only `update` (the universe is ragged across ticks, matching the other multi-input wasm indicators) with numeric high/low flags. - Python `map_err` gains the new error arm; `__init__.py` gets a `# Market Breadth` section in both the import and `__all__` blocks. `index.d.ts` / `index.js` regenerated. ## Tests / Fuzz - Dedicated **streaming-vs-batch + reference-value + ragged-rejection** tests in Python (`test_new_indicators.py`) and Node (`indicators.test.js`) — kept out of the scalar/candle parametrize lists. - Rust unit tests cover every reject branch (empty / non-finite change / negative & non-finite volume) and every indicator branch. - New fuzz target `indicator_update_crosssection` drives `AdvanceDecline` over bounded ragged universes built with `new_unchecked`. ## Verify - `cargo fmt --all` clean - `cargo test -p wickra-core --lib` → 2593 passed; `--doc` → 298 passed - `cargo clippy --workspace --all-targets --all-features -- -D warnings` clean - `cd bindings/node && npm run build && npm test` → 398 passed - `maturin develop --release` + `pytest bindings/python/tests` → all passed - counter check: mod-count 315 == lib-block 315
This commit is contained in:
@@ -8143,6 +8143,78 @@ impl WasmCalendarSpread {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Market Breadth (CrossSection input) ----------
|
||||
//
|
||||
// A breadth tick is the per-symbol state of the whole universe, passed as four
|
||||
// equal-length parallel arrays (`change`, `volume`, `newHigh`, `newLow`). The
|
||||
// high/low flag arrays are numeric (non-zero is true) so the whole tick crosses
|
||||
// the wasm boundary as `Float64Array`s. The universe is ragged across ticks, so
|
||||
// only `update` is exposed (no `batch`), matching the other multi-input wasm
|
||||
// indicators.
|
||||
|
||||
fn build_cross_section(
|
||||
change: &[f64],
|
||||
volume: &[f64],
|
||||
new_high: &[f64],
|
||||
new_low: &[f64],
|
||||
) -> Result<wc::CrossSection, JsError> {
|
||||
if change.len() != volume.len()
|
||||
|| change.len() != new_high.len()
|
||||
|| change.len() != new_low.len()
|
||||
{
|
||||
return Err(JsError::new(
|
||||
"change, volume, newHigh and newLow must be equal length",
|
||||
));
|
||||
}
|
||||
let members = (0..change.len())
|
||||
.map(|i| wc::Member::new(change[i], volume[i], new_high[i] != 0.0, new_low[i] != 0.0))
|
||||
.collect();
|
||||
wc::CrossSection::new(members, 0).map_err(map_err)
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = AdvanceDecline)]
|
||||
pub struct WasmAdvanceDecline {
|
||||
inner: wc::AdvanceDecline,
|
||||
}
|
||||
|
||||
impl Default for WasmAdvanceDecline {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_class = AdvanceDecline)]
|
||||
impl WasmAdvanceDecline {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> WasmAdvanceDecline {
|
||||
Self {
|
||||
inner: wc::AdvanceDecline::new(),
|
||||
}
|
||||
}
|
||||
pub fn update(
|
||||
&mut self,
|
||||
change: Vec<f64>,
|
||||
volume: Vec<f64>,
|
||||
new_high: Vec<f64>,
|
||||
new_low: Vec<f64>,
|
||||
) -> Result<Option<f64>, JsError> {
|
||||
Ok(self
|
||||
.inner
|
||||
.update(build_cross_section(&change, &volume, &new_high, &new_low)?))
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.inner.reset();
|
||||
}
|
||||
#[wasm_bindgen(js_name = isReady)]
|
||||
pub fn is_ready(&self) -> bool {
|
||||
self.inner.is_ready()
|
||||
}
|
||||
#[wasm_bindgen(js_name = warmupPeriod)]
|
||||
pub fn warmup_period(&self) -> usize {
|
||||
self.inner.warmup_period()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user