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 Default for InstantaneousTrendlineFeatureExtractor {
24    fn default() -> Self {
25        Self::new()
26    }
27}
28
29impl InstantaneousTrendlineFeatureExtractor {
30    pub fn new() -> Self {
31        Self {
32            inner: InstantaneousTrendline::new(),
33        }
34    }
35}
36
37impl Next<f64> for InstantaneousTrendlineFeatureExtractor {
38    type Output = InstantaneousTrendlineFeatures;
39
40    fn next(&mut self, input: f64) -> Self::Output {
41        let trend = self.inner.next(input);
42        let strength = if input.is_nan() || trend.is_nan() {
43            f64::NAN
44        } else {
45            let denom = input.abs().max(1e-8);
46            ((input - trend).abs() / denom).min(1.0)
47        };
48        InstantaneousTrendlineFeatures { trend, strength }
49    }
50}