Skip to main content

quantwave_core/features/
instantaneous_trendline.rs

1//! Instantaneous Trendline feature extractor wrapper.
2//!
3//! Wraps the zero-lag trendline. For richer ML use we expose the trend value
4//! + a simple derived "trend strength" (placeholder for phase/power in future).
5//!
6//! Source: quantwave-core/src/indicators/instantaneous_trendline.rs (returns f64 trend)
7
8use crate::indicators::instantaneous_trendline::InstantaneousTrendline;
9use crate::traits::Next;
10
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct InstantaneousTrendlineFeatures {
13    pub trend: f64,
14    /// Placeholder for future phase/power derived features (0.0 for now)
15    pub strength: f64,
16}
17
18#[derive(Debug, Clone)]
19pub struct InstantaneousTrendlineFeatureExtractor {
20    inner: InstantaneousTrendline,
21}
22
23impl InstantaneousTrendlineFeatureExtractor {
24    pub fn new() -> Self {
25        Self {
26            inner: InstantaneousTrendline::new(),
27        }
28    }
29}
30
31impl Next<f64> for InstantaneousTrendlineFeatureExtractor {
32    type Output = InstantaneousTrendlineFeatures;
33
34    fn next(&mut self, input: f64) -> Self::Output {
35        let trend = self.inner.next(input);
36        let strength = if input.is_nan() || trend.is_nan() {
37            f64::NAN
38        } else {
39            let denom = input.abs().max(1e-8);
40            ((input - trend).abs() / denom).min(1.0)
41        };
42        InstantaneousTrendlineFeatures { trend, strength }
43    }
44}