feat: trade-flow microstructure indicators (part 2 of 4) (#113)

* feat(core): add 3 trade-flow microstructure indicators

SignedVolume (per-trade size signed by aggressor), CumulativeVolumeDelta
(running signed-volume total), and TradeImbalance (rolling buy/sell volume
imbalance over a trade window). All consume the Trade type, with full unit
coverage. Extends the Microstructure family.

* feat(bindings): expose trade-flow microstructure indicators

Python, Node and WASM bindings for SignedVolume, CumulativeVolumeDelta and
TradeImbalance. Each takes a trade via update(price, size, is_buy); Python and
Node expose a batch over three parallel arrays, WASM exposes per-trade update.
Regenerates node index.d.ts/.js.

* test(bindings,fuzz,bench): cover trade-flow microstructure indicators

Python and Node: reference values, streaming-vs-batch, lifecycle/repr and input
validation (zero window, negative size, non-positive price, mismatched batch
lengths). New indicator_update_trade fuzz target. Synthetic trade-tape benches
(signed_volume cheapest, trade_imbalance windowed/expensive).

* docs: add trade-flow indicators + bump counter to 227

README Microstructure family row gains signed volume / CVD / trade imbalance and
the counter goes 224 -> 227; CHANGELOG records the trade-flow indicators.
This commit is contained in:
kingchenc
2026-06-01 16:38:48 +02:00
committed by GitHub
parent 2be21df803
commit 5867f71450
23 changed files with 1141 additions and 33 deletions
+7
View File
@@ -59,6 +59,13 @@ test = false
doc = false
bench = false
[[bin]]
name = "indicator_update_trade"
path = "fuzz_targets/indicator_update_trade.rs"
test = false
doc = false
bench = false
[[bin]]
name = "tick_aggregator"
path = "fuzz_targets/tick_aggregator.rs"
@@ -0,0 +1,45 @@
#![no_main]
//! Fuzz trade-flow `Indicator<Input = Trade>` implementations with arbitrary
//! trade tapes.
//!
//! Each iteration consumes a byte stream, interprets it as a sequence of `f64`
//! values (8 bytes each), and packs consecutive values into `(price, size)`
//! trades whose aggressor side alternates with the sign of the size field.
//! Trades are built with `Trade::new_unchecked` so the fuzzer can explore
//! degenerate values (non-finite, negative) that the validating constructor
//! would reject — the indicators must never panic, streaming or batched.
use libfuzzer_sys::fuzz_target;
use wickra_core::{
BatchExt, CumulativeVolumeDelta, Indicator, Side, SignedVolume, Trade, TradeImbalance,
};
#[inline(never)]
fn drive<I>(make: impl Fn() -> I, trades: &[Trade])
where
I: Indicator<Input = Trade, Output = f64> + BatchExt,
{
let mut streaming = make();
for &trade in trades {
let _ = streaming.update(trade);
}
let _ = make().batch(trades);
}
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 trades: Vec<Trade> = floats
.chunks_exact(2)
.map(|c| {
let side = if c[1] >= 0.0 { Side::Buy } else { Side::Sell };
Trade::new_unchecked(c[0], c[1], side, 0)
})
.collect();
drive(SignedVolume::new, &trades);
drive(CumulativeVolumeDelta::new, &trades);
drive(|| TradeImbalance::new(5).unwrap(), &trades);
});