Files
wickra/fuzz/fuzz_targets/indicator_update_orderbook.rs
kingchenc fcb221ec03 feat: add 19 indicators for external feature-extractor coverage (377 -> 396) (#175)
Adds 19 streaming indicators so an external trading-bot feature extractor can replace its hand-built features with native, batch/streaming-equivalent ones. Each is a real gap (verified against the existing catalogue), production-only, with full Python/Node/WASM bindings, fuzz drivers, and tests. Five commits, one per family group; counter 377 -> 396.

## What's added

**Price Statistics (6)** — `LogReturn`, `RealizedVolatility` (raw quadratic variation, the un-annualised counterpart to `HistoricalVolatility`), `RollingQuantile`, `RollingIqr`, `RollingPercentileRank`, `SpreadAr1Coefficient` (pairwise AR(1) rho of the spread; complements `OuHalfLife`).

**Price Action (4)** — `CloseVsOpen`, `BodySizePct`, `WickRatio`, `HighLowRange` (stateless per-bar OHLC transforms).

**Regime / Trend / Jump labels (3)** — `TrendLabel` (sign of the rolling OLS slope), `JumpIndicator` (return outliers vs trailing volatility, measured as deviation from the trailing mean so steady drift is not flagged), `RegimeLabel` (volatility-quantile regime split).

**Risk / Performance (2)** — `WinRate`, `Expectancy` (R-multiple).

**Microstructure (4)** — `OrderFlowImbalance` (Cont-Kukanov-Stoikov OFI), `Vpin`, `AmihudIlliquidity`, `RollMeasure`. These reuse the existing `OrderBook` / `Trade` inputs (no new input type).

## Intentionally NOT added (already present, would be duplicates)

- **Population skew / kurtosis** — `skewness.rs` / `kurtosis.rs` are already population moments (divisor n).
- **Hurst R/S** — `hurst_exponent.rs` already uses rescaled-range (R/S) analysis.
- **Queue Imbalance** — exactly `OrderBookImbalanceTop1` ((bidSize - askSize) / (bidSize + askSize)).

## Verification

`cargo test -p wickra-core` (lib 3187 + doc 354), `cargo clippy --workspace --all-targets --all-features -D warnings` clean, node `npm run build && npm test` (471), python `pytest` (784). Counter consistent across `mod.rs`, lib block, README, and docs/README at 396.
2026-06-04 12:00:35 +02:00

54 lines
2.0 KiB
Rust

#![no_main]
//! Fuzz order-book `Indicator<Input = OrderBook>` implementations with
//! arbitrary depth snapshots.
//!
//! Each iteration consumes a byte stream, interprets it as a sequence of
//! `f64` values (8 bytes each), packs consecutive values into `(price, size)`
//! levels, and groups levels into order-book snapshots. Books are built with
//! `OrderBook::new_unchecked` so the fuzzer can explore degenerate shapes
//! (empty sides, crossed books, non-finite prices, negative sizes) that the
//! validating constructor would reject — the indicators must never panic on
//! any of them, streaming or batched.
use libfuzzer_sys::fuzz_target;
use wickra_core::{BatchExt, DepthSlope, Indicator, Level, Microprice, OrderBook, OrderBookImbalanceFull, OrderBookImbalanceTop1, OrderBookImbalanceTopN, OrderFlowImbalance, QuotedSpread};
#[inline(never)]
fn drive<I>(make: impl Fn() -> I, books: &[OrderBook])
where
I: Indicator<Input = OrderBook, Output = f64> + BatchExt,
{
let mut streaming = make();
for book in books {
let _ = streaming.update(book.clone());
}
let _ = make().batch(books);
}
fuzz_target!(|data: &[u8]| {
let floats: Vec<f64> = data
.chunks_exact(8)
.map(|c| f64::from_le_bytes(c.try_into().expect("8 bytes")))
.collect();
let levels: Vec<Level> = floats
.chunks_exact(2)
.map(|c| Level::new_unchecked(c[0], c[1]))
.collect();
// Group levels into snapshots of up to four levels (split into bids / asks).
let books: Vec<OrderBook> = levels
.chunks(4)
.map(|chunk| {
let half = chunk.len() / 2;
OrderBook::new_unchecked(chunk[..half].to_vec(), chunk[half..].to_vec())
})
.collect();
drive(OrderBookImbalanceTop1::new, &books);
drive(|| OrderBookImbalanceTopN::new(3).unwrap(), &books);
drive(OrderBookImbalanceFull::new, &books);
drive(Microprice::new, &books);
drive(QuotedSpread::new, &books);
drive(DepthSlope::new, &books);
drive(|| OrderFlowImbalance::new(20).unwrap(), &books);
});