quantwave_core/traits.rs
1/// The core trait for streaming indicators.
2/// Every indicator maintains an internal state and processes data points one by one.
3pub trait Next<Input> {
4 type Output;
5
6 /// Process the next input and return the updated output.
7 fn next(&mut self, input: Input) -> Self::Output;
8
9 /// Process a batch of inputs eagerly.
10 /// This allows implementations to override with vectorized logic,
11 /// while falling back to scalar processing by default.
12 fn next_batch(&mut self, inputs: &[Input]) -> Vec<Self::Output>
13 where
14 Input: Copy,
15 {
16 let mut outputs = Vec::with_capacity(inputs.len());
17 for &input in inputs {
18 outputs.push(self.next(input));
19 }
20 outputs
21 }
22}
23
24/// A trait for algorithms that smooth a series of values (e.g., SMA, EMA).
25/// This allows indicators like SuperTrend or Keltner Channels to be generic over their MA type.
26pub trait SmoothingAlgorithm: Next<f64, Output = f64> + Clone + Send + Sync {}
27
28/// A trait for indicator configurations that can build their respective streaming state machines.
29pub trait IndicatorConfig {
30 type Indicator: Next<f64>;
31
32 /// Build a new instance of the indicator state machine.
33 fn build(&self) -> Self::Indicator;
34}
35
36/// Blanket implementation for types that satisfy the SmoothingAlgorithm requirements.
37impl<T> SmoothingAlgorithm for T where T: Next<f64, Output = f64> + Clone + Send + Sync {}