4e3c41ea80
* feat(family-15): add 17 risk/performance metrics Implements Family 15 pragmatically as standard `Indicator`s instead of a separate `wickra-metrics` crate. Input is scalar `f64` per bar — period return, equity sample, or per-trade P&L depending on the metric. Scalar `Indicator<f64>` (14): - SharpeRatio(period, risk_free) - SortinoRatio(period, mar) - CalmarRatio(period) - OmegaRatio(period, threshold) - MaxDrawdown(period) — rolling, peak-to-trough - AverageDrawdown(period) - DrawdownDuration — cumulative, bars under water (u32 output) - PainIndex(period) - ValueAtRisk(period, confidence) - ConditionalValueAtRisk(period, confidence) - ProfitFactor(period) - GainLossRatio(period) - RecoveryFactor — cumulative, net return / max drawdown - KellyCriterion(period) Two-series `Indicator<(f64, f64)>` for (asset, benchmark) returns (3): - TreynorRatio(period, risk_free) - InformationRatio(period) - Alpha(period, risk_free) — Jensen / CAPM Touchpoints: - 17 new files under `crates/wickra-core/src/indicators/`. - `mod.rs` + `lib.rs` re-exports. - Python bindings (`bindings/python/src/lib.rs`, `__init__.py`). - Node bindings (`bindings/node/src/lib.rs`, `index.js`). - WASM bindings (`bindings/wasm/src/lib.rs`). - Fuzz: scalar metrics appended to `indicator_update.rs`; new `indicator_update_pair.rs` fuzz target for `(f64, f64)` indicators. - Python tests: SCALAR + new PAIR parameter lists in `test_new_indicators.py`, reference-value cases in `test_known_values.py`. - Node tests: scalar factories + new pair-factory block in `bindings/node/__tests__/indicators.test.js`. - Benches: 5 Family-15 benches added in `crates/wickra/benches/indicators.rs`. - Docs: README family-table row + counter (71 -> 88), CHANGELOG entry under [Unreleased]. Note: Family 12 (statistik-regression, PR #51) introduces `node_pair_indicator!` and `wasm_pair_indicator!` macros for Pearson / Beta / Spearman. Family 15 needs the same pair-input pattern but Family 12 is not yet in main, so the three pair wrappers below are written by hand in this PR. When PR #51 lands, the trivial merge-conflict is resolved by keeping the macros from Family 12 and re-using them for Treynor / IR / Alpha (drop the three handwritten wrappers). cargo check --workspace --all-features: green. * fix(family-15): satisfy clippy doc_markdown / if_not_else / digit_grouping * fix(family-15): unused TreynorRatio import, duplicate pairFactories, _eq_nan inf handling * fix(family-15): node eq() handles matching infinities for ratio indicators * test(family-15): cover cold paths flagged by codecov patch
250 lines
11 KiB
Rust
250 lines
11 KiB
Rust
#![no_main]
|
|
//! Fuzz scalar-input indicator updates with arbitrary `f64` sequences.
|
|
//!
|
|
//! Every scalar indicator must tolerate any finite-or-not input stream — NaN,
|
|
//! ±inf, subnormals, abrupt jumps — without panicking. Each fuzz iteration
|
|
//! runs the **same** input sequence through every scalar indicator twice:
|
|
//! once as a streaming `update` loop and once as a full `batch` call. Neither
|
|
//! path may panic; `batch` is also expected to agree with the streaming path
|
|
//! (the `BatchExt` blanket implementation replays `update` internally, so the
|
|
//! agreement is structural — but exercising both paths surfaces any
|
|
//! state-mutation bugs in `update` that would only manifest mid-batch).
|
|
//!
|
|
//! Audit finding R9: the previous version covered only `Rsi(14)` and
|
|
//! `Ema(20)`. This target now covers every scalar indicator in the catalogue.
|
|
|
|
use libfuzzer_sys::fuzz_target;
|
|
use wickra_core::{
|
|
AdaptiveCycle, Alma, Apo, Autocorrelation, AverageDrawdown, BatchExt, Beta, BollingerBands,
|
|
CalmarRatio, CenterOfGravity, Cfo, Cmo, CoefficientOfVariation, ConditionalValueAtRisk,
|
|
ConnorsRsi, Coppock, CyberneticCycle, Decycler, DecyclerOscillator, Dema, DetrendedStdDev,
|
|
DoubleBollinger, Dpo, DrawdownDuration, EhlersStochastic, ElderImpulse, Ema,
|
|
EmpiricalModeDecomposition, Fama, FisherTransform, Frama, GainLossRatio, HilbertDominantCycle,
|
|
HistoricalVolatility, Hma, HurstExponent, Indicator, InstantaneousTrendline,
|
|
InverseFisherTransform, Jma, Kama, KellyCriterion, Kst, Kurtosis, LaguerreRsi, LinRegAngle,
|
|
LinRegChannel, LinRegSlope, LinearRegression, MaEnvelope, MacdIndicator, Mama, MaxDrawdown,
|
|
McGinleyDynamic, MedianAbsoluteDeviation, Mom, OmegaRatio, PainIndex, PearsonCorrelation,
|
|
PercentageTrailingStop, Pmo, Ppo, ProfitFactor, RSquared, RecoveryFactor, RenkoTrailingStop,
|
|
Roc, RoofingFilter, Rsi, RviVolatility, SharpeRatio, SineWave, Skewness, Sma, Smma,
|
|
SortinoRatio, SpearmanCorrelation, StandardError, StandardErrorBands, Stc, StdDev,
|
|
StepTrailingStop, StochRsi, SuperSmoother, Tema, Tii, Trima, Trix, Tsi, UlcerIndex,
|
|
ValueAtRisk, Variance, VerticalHorizontalFilter, Vidya, Wma, ZScore, ZeroLagMacd, Zlema, T3,
|
|
};
|
|
|
|
/// Drive a single streaming + batch run through one scalar indicator. Marked
|
|
/// `#[inline(never)]` so a panic backtrace pin-points the specific indicator.
|
|
#[inline(never)]
|
|
fn drive<I>(make: impl Fn() -> I, data: &[f64])
|
|
where
|
|
I: Indicator<Input = f64, Output = f64> + BatchExt,
|
|
{
|
|
let mut streaming = make();
|
|
for &x in data {
|
|
let _ = streaming.update(x);
|
|
}
|
|
let _ = make().batch(data);
|
|
}
|
|
|
|
fuzz_target!(|data: Vec<f64>| {
|
|
// Bounded periods keep each iteration cheap and bias the fuzzer toward
|
|
// adversarial input patterns rather than enormous windows. The constants
|
|
// mirror the README's "common defaults" so we cover the parameterisations
|
|
// most users actually instantiate.
|
|
drive(|| Sma::new(14).unwrap(), &data);
|
|
drive(|| Ema::new(20).unwrap(), &data);
|
|
drive(|| Wma::new(14).unwrap(), &data);
|
|
drive(|| Rsi::new(14).unwrap(), &data);
|
|
drive(|| Dema::new(14).unwrap(), &data);
|
|
drive(|| Tema::new(14).unwrap(), &data);
|
|
drive(|| Hma::new(14).unwrap(), &data);
|
|
drive(|| Roc::new(14).unwrap(), &data);
|
|
drive(|| Trix::new(14).unwrap(), &data);
|
|
drive(|| Smma::new(14).unwrap(), &data);
|
|
drive(|| Trima::new(14).unwrap(), &data);
|
|
drive(|| Zlema::new(14).unwrap(), &data);
|
|
drive(|| Kama::new(10, 2, 30).unwrap(), &data);
|
|
drive(|| Alma::new(9, 0.85, 6.0).unwrap(), &data);
|
|
drive(|| McGinleyDynamic::new(10).unwrap(), &data);
|
|
drive(|| Frama::new(16).unwrap(), &data);
|
|
drive(|| Vidya::new(14, 9).unwrap(), &data);
|
|
drive(|| Jma::new(14, 0.0, 2).unwrap(), &data);
|
|
drive(|| T3::new(14, 0.7).unwrap(), &data);
|
|
drive(|| Mom::new(14).unwrap(), &data);
|
|
drive(|| Cmo::new(14).unwrap(), &data);
|
|
drive(|| Tsi::new(25, 13).unwrap(), &data);
|
|
drive(|| Pmo::new(35, 20).unwrap(), &data);
|
|
drive(|| Tii::new(60, 30).unwrap(), &data);
|
|
drive(|| StochRsi::new(14, 14).unwrap(), &data);
|
|
drive(|| Dpo::new(14).unwrap(), &data);
|
|
drive(|| Ppo::new(12, 26).unwrap(), &data);
|
|
drive(|| Apo::new(12, 26).unwrap(), &data);
|
|
drive(|| Cfo::new(14).unwrap(), &data);
|
|
drive(|| ElderImpulse::classic(), &data);
|
|
drive(|| Stc::classic(), &data);
|
|
drive(|| Coppock::new(14, 11, 10).unwrap(), &data);
|
|
drive(|| StdDev::new(14).unwrap(), &data);
|
|
drive(|| UlcerIndex::new(14).unwrap(), &data);
|
|
drive(|| HistoricalVolatility::new(14, 252).unwrap(), &data);
|
|
drive(|| LinearRegression::new(14).unwrap(), &data);
|
|
drive(|| LinRegSlope::new(14).unwrap(), &data);
|
|
drive(|| LinRegAngle::new(14).unwrap(), &data);
|
|
drive(|| VerticalHorizontalFilter::new(14).unwrap(), &data);
|
|
drive(|| ZScore::new(14).unwrap(), &data);
|
|
drive(|| Variance::new(14).unwrap(), &data);
|
|
drive(|| CoefficientOfVariation::new(14).unwrap(), &data);
|
|
drive(|| Skewness::new(14).unwrap(), &data);
|
|
drive(|| Kurtosis::new(14).unwrap(), &data);
|
|
drive(|| StandardError::new(14).unwrap(), &data);
|
|
drive(|| DetrendedStdDev::new(14).unwrap(), &data);
|
|
drive(|| RSquared::new(14).unwrap(), &data);
|
|
drive(|| MedianAbsoluteDeviation::new(14).unwrap(), &data);
|
|
drive(|| Autocorrelation::new(14, 2).unwrap(), &data);
|
|
// HurstExponent needs `period >= 2 * chunks`; 16/4 is the cheapest fit
|
|
// that still exercises every code path.
|
|
drive(|| HurstExponent::new(16, 4).unwrap(), &data);
|
|
drive(|| RviVolatility::new(10).unwrap(), &data);
|
|
drive(|| LaguerreRsi::new(0.5).unwrap(), &data);
|
|
drive(|| ConnorsRsi::classic(), &data);
|
|
|
|
// KST is scalar-input but emits `KstOutput`, so it bypasses the generic
|
|
// `drive` helper. Streaming + batch are still both exercised.
|
|
{
|
|
let mut kst = Kst::classic();
|
|
for &x in &data {
|
|
let _ = kst.update(x);
|
|
}
|
|
let _ = Kst::classic().batch(&data);
|
|
}
|
|
|
|
// Zero-Lag MACD shares MACD's multi-output topology, so it gets the
|
|
// same hand-rolled streaming + batch drive as classic MACD below.
|
|
{
|
|
let mut z = ZeroLagMacd::classic();
|
|
for &x in &data {
|
|
let _ = z.update(x);
|
|
}
|
|
let _ = ZeroLagMacd::classic().batch(&data);
|
|
}
|
|
|
|
// --- Trailing Stops (scalar) ---
|
|
drive(|| PercentageTrailingStop::new(5.0).unwrap(), &data);
|
|
drive(|| StepTrailingStop::new(1.0).unwrap(), &data);
|
|
drive(|| RenkoTrailingStop::new(1.0).unwrap(), &data);
|
|
|
|
// Family 10 — Ehlers / Cycle scalar indicators.
|
|
drive(|| SuperSmoother::new(10).unwrap(), &data);
|
|
drive(|| FisherTransform::new(10).unwrap(), &data);
|
|
drive(|| InverseFisherTransform::new(1.0).unwrap(), &data);
|
|
drive(|| Decycler::new(20).unwrap(), &data);
|
|
drive(|| DecyclerOscillator::new(10, 30).unwrap(), &data);
|
|
drive(|| RoofingFilter::new(10, 48).unwrap(), &data);
|
|
drive(|| CenterOfGravity::new(10).unwrap(), &data);
|
|
drive(|| CyberneticCycle::new(10).unwrap(), &data);
|
|
drive(|| InstantaneousTrendline::new(20).unwrap(), &data);
|
|
drive(|| EhlersStochastic::new(20).unwrap(), &data);
|
|
drive(|| EmpiricalModeDecomposition::new(20, 0.5).unwrap(), &data);
|
|
drive(HilbertDominantCycle::new, &data);
|
|
drive(AdaptiveCycle::new, &data);
|
|
drive(SineWave::new, &data);
|
|
drive(|| Fama::new(0.5, 0.05).unwrap(), &data);
|
|
|
|
// Family 15 — Risk / Performance metrics (scalar inputs).
|
|
drive(|| SharpeRatio::new(20, 0.0).unwrap(), &data);
|
|
drive(|| SortinoRatio::new(20, 0.0).unwrap(), &data);
|
|
drive(|| CalmarRatio::new(20).unwrap(), &data);
|
|
drive(|| OmegaRatio::new(20, 0.0).unwrap(), &data);
|
|
drive(|| MaxDrawdown::new(20).unwrap(), &data);
|
|
drive(|| AverageDrawdown::new(20).unwrap(), &data);
|
|
drive(|| PainIndex::new(20).unwrap(), &data);
|
|
drive(|| ValueAtRisk::new(20, 0.95).unwrap(), &data);
|
|
drive(|| ConditionalValueAtRisk::new(20, 0.95).unwrap(), &data);
|
|
drive(|| ProfitFactor::new(20).unwrap(), &data);
|
|
drive(|| GainLossRatio::new(20).unwrap(), &data);
|
|
drive(|| KellyCriterion::new(20).unwrap(), &data);
|
|
|
|
// RecoveryFactor and DrawdownDuration produce non-`f64` outputs / have
|
|
// no `period` knob, so they cannot use the `drive` helper directly.
|
|
{
|
|
let mut rf = RecoveryFactor::new();
|
|
for &x in &data {
|
|
let _ = rf.update(x);
|
|
}
|
|
let _ = RecoveryFactor::new().batch(&data);
|
|
}
|
|
{
|
|
let mut dd = DrawdownDuration::new();
|
|
for &x in &data {
|
|
let _ = dd.update(x);
|
|
}
|
|
let _ = DrawdownDuration::new().batch(&data);
|
|
}
|
|
|
|
// MACD, Bollinger Bands and MAMA have non-`f64` outputs, so they cannot
|
|
// use the generic `drive` helper above. Streaming + batch are still both
|
|
// exercised.
|
|
{
|
|
let mut macd = MacdIndicator::new(12, 26, 9).unwrap();
|
|
for &x in &data {
|
|
let _ = macd.update(x);
|
|
}
|
|
let _ = MacdIndicator::new(12, 26, 9).unwrap().batch(&data);
|
|
}
|
|
{
|
|
let mut bb = BollingerBands::new(20, 2.0).unwrap();
|
|
for &x in &data {
|
|
let _ = bb.update(x);
|
|
}
|
|
let _ = BollingerBands::new(20, 2.0).unwrap().batch(&data);
|
|
}
|
|
{
|
|
let mut mama = Mama::new(0.5, 0.05).unwrap();
|
|
for &x in &data {
|
|
let _ = mama.update(x);
|
|
}
|
|
let _ = Mama::new(0.5, 0.05).unwrap().batch(&data);
|
|
}
|
|
|
|
// --- Family 05: scalar-input band/channel indicators (multi-output) ---
|
|
{
|
|
let mut env = MaEnvelope::new(20, 0.025).unwrap();
|
|
for &x in &data {
|
|
let _ = env.update(x);
|
|
}
|
|
let _ = MaEnvelope::new(20, 0.025).unwrap().batch(&data);
|
|
}
|
|
{
|
|
let mut ch = LinRegChannel::new(20, 2.0).unwrap();
|
|
for &x in &data {
|
|
let _ = ch.update(x);
|
|
}
|
|
let _ = LinRegChannel::new(20, 2.0).unwrap().batch(&data);
|
|
}
|
|
{
|
|
let mut seb = StandardErrorBands::new(21, 2.0).unwrap();
|
|
for &x in &data {
|
|
let _ = seb.update(x);
|
|
}
|
|
let _ = StandardErrorBands::new(21, 2.0).unwrap().batch(&data);
|
|
}
|
|
{
|
|
let mut db = DoubleBollinger::new(20, 1.0, 2.0).unwrap();
|
|
for &x in &data {
|
|
let _ = db.update(x);
|
|
}
|
|
let _ = DoubleBollinger::new(20, 1.0, 2.0).unwrap().batch(&data);
|
|
}
|
|
|
|
// Family 12: Two-series indicators — pair adjacent samples of `data`.
|
|
{
|
|
let mut p = PearsonCorrelation::new(14).unwrap();
|
|
let mut b = Beta::new(14).unwrap();
|
|
let mut s = SpearmanCorrelation::new(14).unwrap();
|
|
for w in data.windows(2) {
|
|
let pair = (w[0], w[1]);
|
|
let _ = p.update(pair);
|
|
let _ = b.update(pair);
|
|
let _ = s.update(pair);
|
|
}
|
|
}
|
|
});
|