indexes_rs/v2/stochastic/
types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
4pub struct StochasticResult {
5    pub k: f64, // Fast stochastic (%K)
6    pub d: f64, // Slow stochastic (%D - moving average of %K)
7}
8
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct OHLCData {
11    pub high: f64,
12    pub low: f64,
13    pub close: f64,
14}
15
16impl OHLCData {
17    pub fn new(high: f64, low: f64, close: f64) -> Self {
18        Self { high, low, close }
19    }
20}
21
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub enum StochasticSignal {
24    Overbought, // Both K and D > 80
25    Oversold,   // Both K and D < 20
26    Bullish,    // K > D (bullish crossover)
27    Bearish,    // K < D (bearish crossover)
28    Neutral,
29}
30
31#[derive(Debug, Clone, PartialEq)]
32pub enum StochasticError {
33    InvalidPeriod,
34    InvalidSmoothingPeriod,
35    InsufficientData,
36    InvalidPrice,
37}
38
39impl std::fmt::Display for StochasticError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            StochasticError::InvalidPeriod => write!(f, "Period must be greater than 0"),
43            StochasticError::InvalidSmoothingPeriod => {
44                write!(f, "Smoothing period must be greater than 0")
45            }
46            StochasticError::InsufficientData => {
47                write!(f, "Not enough data to calculate Stochastic")
48            }
49            StochasticError::InvalidPrice => {
50                write!(f, "Invalid price data (NaN, Infinite, or high < low)")
51            }
52        }
53    }
54}
55
56impl std::error::Error for StochasticError {}