The fuzz suite previously covered only `Rsi(14)` and `Ema(20)` — 2 of 71 indicators, no OHLCV coverage at all. Audit finding R9 asked for ATR/ADX/Stochastic/PSAR as a minimum; this commit goes further and brings every indicator under fuzz. - `indicator_update` (rewritten): drives every scalar-input indicator through one streaming pass + one batch call per iteration. Covers SMA, EMA, WMA, RSI, DEMA, TEMA, HMA, ROC, TRIX, SMMA, TRIMA, ZLEMA, KAMA, T3, MOM, CMO, TSI, PMO, StochRSI, DPO, PPO, Coppock, StdDev, UlcerIndex, HistoricalVolatility, LinearRegression, LinRegSlope, LinRegAngle, VHF, ZScore, MACD, BollingerBands. A `drive` helper marked `#[inline(never)]` keeps each indicator on its own panic backtrace frame. - `indicator_update_candle` (new): chunks the fuzz `f64` stream into `[open, high, low, close, volume]` tuples, builds candles via `Candle::new` (skipping ones that fail OHLCV validation — that path is fuzz-tested separately), then drives every candle-input indicator through streaming + batch. Covers ATR, NATR, TrueRange, ChaikinVolatility, Keltner, Donchian, PSAR, SuperTrend, ChandelierExit, ChandeKrollStop, ATRTrailingStop, ADX, Aroon, AroonOscillator, Vortex, MassIndex, ChoppinessIndex, CCI, WilliamsR, AwesomeOscillator, AcceleratorOscillator, UltimateOscillator, BalanceOfPower, OBV, MFI, VWAP, RollingVWAP, VWMA, ADL, VPT, CMF, ChaikinOscillator, ForceIndex, EaseOfMovement, TypicalPrice, MedianPrice, WeightedClose, Stochastic. - `fuzz/Cargo.toml` registers the new target; `fuzz/README.md` describes both expanded targets. - A `fuzz-smoke` CI job runs each of the five targets for 30 s on every push and pull-request — enough to catch a regression in the harness without slowing CI to a crawl. Long fuzz campaigns belong on dedicated infrastructure with persistent corpora.
131 lines
5.6 KiB
Rust
131 lines
5.6 KiB
Rust
#![no_main]
|
|
//! Fuzz OHLCV-input indicator updates with arbitrary candle sequences.
|
|
//!
|
|
//! Every candle-input indicator must tolerate any sequence of validated OHLCV
|
|
//! candles — extreme magnitudes, micro-spreads, zero-volume bars, abrupt
|
|
//! reversals — without panicking. The fuzzer chunks the raw `f64` stream into
|
|
//! `[open, high, low, close, volume]` tuples and constructs each candle via
|
|
//! `Candle::new`; entries that fail OHLCV-invariant validation are skipped so
|
|
//! the indicator only ever sees structurally-valid candles. Each iteration
|
|
//! then drives that candle stream through every candle-input indicator twice
|
|
//! (streaming `update` + batch).
|
|
//!
|
|
//! Audit finding R9: the previous fuzz suite had no candle-input coverage at
|
|
//! all. This target now covers every candle-input indicator including the
|
|
//! ones the audit named explicitly (ATR, ADX, Stochastic, PSAR) plus the
|
|
//! complete catalogue: Keltner, Donchian, SuperTrend, Chandelier Exit, ATR
|
|
//! Trailing Stop, Aroon, AwesomeOscillator, CCI, WilliamsR, MFI, OBV, VWAP,
|
|
//! RollingVWAP, ADL, VPT, ChaikinMoneyFlow, ChaikinOscillator, ForceIndex,
|
|
//! EaseOfMovement, NATR, AroonOscillator, ChandeKrollStop, Vortex, MassIndex,
|
|
//! ChoppinessIndex, TrueRange, ChaikinVolatility, AcceleratorOscillator,
|
|
//! BalanceOfPower, UltimateOscillator, VWMA, TypicalPrice, MedianPrice,
|
|
//! WeightedClose.
|
|
|
|
use libfuzzer_sys::fuzz_target;
|
|
use wickra_core::{
|
|
AcceleratorOscillator, Adl, Adx, Aroon, AroonOscillator, Atr, AtrTrailingStop,
|
|
AwesomeOscillator, BalanceOfPower, BatchExt, Candle, Cci, ChaikinMoneyFlow, ChaikinOscillator,
|
|
ChaikinVolatility, ChandeKrollStop, ChandelierExit, ChoppinessIndex, Donchian, EaseOfMovement,
|
|
ForceIndex, Indicator, Keltner, MassIndex, MedianPrice, Mfi, Natr, Obv, Psar, RollingVwap,
|
|
Stochastic, SuperTrend, TrueRange, TypicalPrice, UltimateOscillator, VolumePriceTrend, Vortex,
|
|
Vwap, Vwma, WeightedClose, WilliamsR,
|
|
};
|
|
|
|
/// Convert a flat `f64` stream into a `Vec<Candle>` by chunking it into
|
|
/// `[open, high, low, close, volume]` groups. Tuples that fail OHLCV
|
|
/// validation are dropped so the indicator under test only ever sees a
|
|
/// structurally-valid candle stream (the *parser* is fuzz-tested elsewhere;
|
|
/// this target focuses on indicator robustness).
|
|
fn candles_from(data: &[f64]) -> Vec<Candle> {
|
|
data.chunks_exact(5)
|
|
.enumerate()
|
|
.filter_map(|(i, ch)| {
|
|
// A monotonic timestamp avoids surprising any indicator that might
|
|
// care about ordering. The fuzz input drives OHLCV; time is just a
|
|
// tie-breaker.
|
|
Candle::new(ch[0], ch[1], ch[2], ch[3], ch[4], i as i64).ok()
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// Streaming + batch sweep through one candle-input indicator. `#[inline(never)]`
|
|
/// keeps each indicator on its own frame in any panic backtrace.
|
|
#[inline(never)]
|
|
fn drive<I, O>(make: impl Fn() -> I, candles: &[Candle])
|
|
where
|
|
I: Indicator<Input = Candle, Output = O> + BatchExt,
|
|
{
|
|
let mut streaming = make();
|
|
for c in candles {
|
|
let _ = streaming.update(*c);
|
|
}
|
|
let _ = make().batch(candles);
|
|
}
|
|
|
|
fuzz_target!(|data: Vec<f64>| {
|
|
let candles = candles_from(&data);
|
|
if candles.is_empty() {
|
|
return;
|
|
}
|
|
|
|
// --- Volatility & ATR family ---
|
|
drive(|| Atr::new(14).unwrap(), &candles);
|
|
drive(|| Natr::new(14).unwrap(), &candles);
|
|
drive(TrueRange::new, &candles);
|
|
drive(|| ChaikinVolatility::new(10, 10).unwrap(), &candles);
|
|
|
|
// --- Bands & Channels ---
|
|
drive(|| Keltner::new(20, 10, 2.0).unwrap(), &candles);
|
|
drive(|| Donchian::new(20).unwrap(), &candles);
|
|
|
|
// --- Trailing Stops ---
|
|
drive(|| Psar::new(0.02, 0.02, 0.20).unwrap(), &candles);
|
|
drive(|| SuperTrend::new(14, 3.0).unwrap(), &candles);
|
|
drive(|| ChandelierExit::new(22, 3.0).unwrap(), &candles);
|
|
drive(|| ChandeKrollStop::new(10, 1.0, 9).unwrap(), &candles);
|
|
drive(|| AtrTrailingStop::new(14, 3.0).unwrap(), &candles);
|
|
|
|
// --- Trend & Directional ---
|
|
drive(|| Adx::new(14).unwrap(), &candles);
|
|
drive(|| Aroon::new(14).unwrap(), &candles);
|
|
drive(|| AroonOscillator::new(14).unwrap(), &candles);
|
|
drive(|| Vortex::new(14).unwrap(), &candles);
|
|
drive(|| MassIndex::new(9, 25).unwrap(), &candles);
|
|
drive(|| ChoppinessIndex::new(14).unwrap(), &candles);
|
|
|
|
// --- Momentum & Oscillators ---
|
|
drive(|| Cci::new(20).unwrap(), &candles);
|
|
drive(|| WilliamsR::new(14).unwrap(), &candles);
|
|
drive(|| AwesomeOscillator::new(5, 34).unwrap(), &candles);
|
|
drive(|| AcceleratorOscillator::new(5, 34, 5).unwrap(), &candles);
|
|
drive(|| UltimateOscillator::new(7, 14, 28).unwrap(), &candles);
|
|
drive(BalanceOfPower::new, &candles);
|
|
|
|
// --- Volume ---
|
|
drive(Obv::new, &candles);
|
|
drive(|| Mfi::new(14).unwrap(), &candles);
|
|
drive(Vwap::new, &candles);
|
|
drive(|| RollingVwap::new(20).unwrap(), &candles);
|
|
drive(|| Vwma::new(20).unwrap(), &candles);
|
|
drive(Adl::new, &candles);
|
|
drive(VolumePriceTrend::new, &candles);
|
|
drive(|| ChaikinMoneyFlow::new(20).unwrap(), &candles);
|
|
drive(|| ChaikinOscillator::new(3, 10).unwrap(), &candles);
|
|
drive(|| ForceIndex::new(13).unwrap(), &candles);
|
|
drive(|| EaseOfMovement::with_divisor(14, 1e8).unwrap(), &candles);
|
|
|
|
// --- Price transformations ---
|
|
drive(TypicalPrice::new, &candles);
|
|
drive(MedianPrice::new, &candles);
|
|
drive(WeightedClose::new, &candles);
|
|
|
|
// --- Stochastic (multi-output) ---
|
|
{
|
|
let mut s = Stochastic::new(14, 3).unwrap();
|
|
for c in &candles {
|
|
let _ = s.update(*c);
|
|
}
|
|
let _ = Stochastic::new(14, 3).unwrap().batch(&candles);
|
|
}
|
|
});
|