stock-bot/apps/stock/engine/src/indicators/stochastic.rs
2026-10-20 18:36:19 -04:00

346 lines
11 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use super::common::RollingWindow;
use super::sma::SMA;
use super::{IncrementalIndicator, Indicator, IndicatorError, IndicatorResult, PriceData};
/// Stochastic Oscillator Indicator
///
/// %K = 100 × (Close - Lowest Low) / (Highest High - Lowest Low)
/// %D = SMA of %K
pub struct Stochastic {
k_period: usize,
d_period: usize,
smooth_k: usize,
high_window: RollingWindow<f64>,
low_window: RollingWindow<f64>,
close_window: RollingWindow<f64>,
k_sma: SMA,
d_sma: SMA,
k_values: RollingWindow<f64>,
}
impl Stochastic {
pub fn new(k_period: usize, d_period: usize, smooth_k: usize) -> Result<Self, IndicatorError> {
if k_period == 0 || d_period == 0 || smooth_k == 0 {
return Err(IndicatorError::InvalidParameter(
"K, D, and smoothing periods must be greater than 0".to_string(),
));
}
Ok(Self {
k_period,
d_period,
smooth_k,
high_window: RollingWindow::new(k_period),
low_window: RollingWindow::new(k_period),
close_window: RollingWindow::new(k_period),
k_sma: SMA::new(smooth_k.max(1))?,
d_sma: SMA::new(d_period)?,
k_values: RollingWindow::new(d_period),
})
}
/// Standard Fast Stochastic (14, 3)
pub fn fast() -> Result<Self, IndicatorError> {
Self::new(14, 3, 1)
}
/// Standard Slow Stochastic (14, 3, 3)
pub fn slow() -> Result<Self, IndicatorError> {
Self::new(14, 3, 3)
}
/// Calculate raw %K value
fn calculate_raw_k(high: &[f64], low: &[f64], close: f64) -> Option<f64> {
if high.is_empty() || low.is_empty() {
return None;
}
let highest = high.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
let lowest = low.iter().cloned().fold(f64::INFINITY, f64::min);
let range = highest - lowest;
if range > 0.0 {
Some(100.0 * (close - lowest) / range)
} else {
Some(50.0) // If no range, return middle value
}
}
/// Calculate Stochastic for a series of price data
pub fn calculate_series(
high: &[f64],
low: &[f64],
close: &[f64],
k_period: usize,
d_period: usize,
smooth_k: usize,
) -> Result<(Vec<f64>, Vec<f64>), IndicatorError> {
if k_period == 0 || d_period == 0 || smooth_k == 0 {
return Err(IndicatorError::InvalidParameter(
"K, D, and smoothing periods must be greater than 0".to_string(),
));
}
if high.len() != low.len() || high.len() != close.len() {
return Err(IndicatorError::InvalidParameter(
"High, low, and close arrays must have the same length".to_string(),
));
}
if high.len() < k_period {
return Err(IndicatorError::InsufficientData {
required: k_period,
actual: high.len(),
});
}
// Calculate raw %K values
let mut raw_k_values = Vec::with_capacity(high.len() - k_period + 1);
for i in k_period - 1..high.len() {
let start = i + 1 - k_period;
if let Some(k) = Self::calculate_raw_k(&high[start..=i], &low[start..=i], close[i]) {
raw_k_values.push(k);
}
}
// Smooth %K if requested
let k_values = if raw_k_values.len() < smooth_k {
vec![]
} else if smooth_k > 1 {
SMA::calculate_series(&raw_k_values, smooth_k)?
} else {
raw_k_values
};
// Calculate %D (SMA of %K)
let d_values = if k_values.len() >= d_period {
SMA::calculate_series(&k_values, d_period)?
} else {
vec![]
};
Ok((k_values, d_values))
}
}
impl Indicator for Stochastic {
fn calculate(&mut self, data: &PriceData) -> Result<IndicatorResult, IndicatorError> {
if data.high.len() != data.low.len() || data.high.len() != data.close.len() {
return Err(IndicatorError::InvalidParameter(
"Price data arrays must have the same length".to_string(),
));
}
let (k_values, d_values) = Self::calculate_series(
&data.high,
&data.low,
&data.close,
self.k_period,
self.d_period,
self.smooth_k,
)?;
Ok(IndicatorResult::Stochastic {
k: k_values,
d: d_values,
})
}
fn reset(&mut self) {
self.high_window.clear();
self.low_window.clear();
self.close_window.clear();
self.k_sma.reset();
self.d_sma.reset();
self.k_values.clear();
}
fn is_ready(&self) -> bool {
self.high_window.is_full() && self.d_sma.is_ready()
}
}
impl IncrementalIndicator for Stochastic {
fn update(&mut self, value: f64) -> Result<Option<f64>, IndicatorError> {
// For incremental updates, we assume value is close price
// In real usage, you'd need to provide high/low/close separately
self.high_window.push(value);
self.low_window.push(value);
self.close_window.push(value);
if self.high_window.is_full() {
// Calculate raw %K
if let Some(raw_k) = Self::calculate_raw_k(
&self.high_window.as_slice(),
&self.low_window.as_slice(),
value,
) {
// Smooth %K if needed
let k_value = if self.smooth_k > 1 {
self.k_sma.update(raw_k)?
} else {
Some(raw_k)
};
if let Some(k) = k_value {
self.k_values.push(k);
// Calculate %D
if let Some(d) = self.d_sma.update(k)? {
return Ok(Some(d));
}
}
}
}
Ok(None)
}
fn current(&self) -> Option<f64> {
self.d_sma.current()
}
}
/// Structure to hold Stochastic values
pub struct StochasticValues {
pub k: f64,
pub d: f64,
}
impl Stochastic {
/// Get current %K and %D values
pub fn current_values(&self) -> Option<StochasticValues> {
if let (Some(&k), Some(d)) = (self.k_values.iter().last(), self.d_sma.current()) {
Some(StochasticValues { k, d })
} else {
None
}
}
/// Update with separate high, low, close values
pub fn update_hlc(
&mut self,
high: f64,
low: f64,
close: f64,
) -> Result<Option<StochasticValues>, IndicatorError> {
self.high_window.push(high);
self.low_window.push(low);
self.close_window.push(close);
if self.high_window.is_full() {
if let Some(raw_k) = Self::calculate_raw_k(
&self.high_window.as_slice(),
&self.low_window.as_slice(),
close,
) {
let k_value = if self.smooth_k > 1 {
self.k_sma.update(raw_k)?
} else {
Some(raw_k)
};
if let Some(k) = k_value {
self.k_values.push(k);
if let Some(d) = self.d_sma.update(k)? {
return Ok(Some(StochasticValues { k, d }));
}
}
}
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stochastic_calculation() {
let high = vec![
127.01, 127.62, 126.59, 127.35, 128.17, 128.43, 127.37, 126.42, 126.90, 126.85, 125.65,
125.72, 127.16, 127.72, 127.69, 128.0,
];
let low = vec![
125.36, 126.16, 124.93, 126.09, 126.82, 126.48, 126.03, 124.83, 126.39, 125.72, 124.56,
124.57, 125.07, 126.86, 126.63, 126.0,
];
let close = vec![
125.36, 126.16, 124.93, 126.09, 126.82, 126.48, 126.03, 124.83, 126.39, 125.72, 124.56,
124.57, 125.07, 126.86, 126.63, 127.0,
];
let (k_values, d_values) =
Stochastic::calculate_series(&high, &low, &close, 14, 3, 1).unwrap();
// 14-bar K + 3-value D needs 16 bars, not the old 15-bar fixture.
assert_eq!(k_values.len(), 3);
assert_eq!(d_values.len(), 1);
assert!((d_values[0] - k_values.iter().sum::<f64>() / 3.0).abs() < 1e-10);
let (_, warming_d) =
Stochastic::calculate_series(&high[..15], &low[..15], &close[..15], 14, 3, 1).unwrap();
assert!(warming_d.is_empty());
// %K should be between 0 and 100
for k in &k_values {
assert!(*k >= 0.0 && *k <= 100.0);
}
}
#[test]
fn test_stochastic_extremes() {
// When close is at highest high, %K should be 100
let high = vec![10.0; 14];
let low = vec![5.0; 14];
let mut close = vec![7.5; 14];
close[13] = 10.0; // Last close at high
let (k_values, _) = Stochastic::calculate_series(&high, &low, &close, 14, 3, 1).unwrap();
assert!((k_values[0] - 100.0).abs() < 1e-10);
// When close is at lowest low, %K should be 0
close[13] = 5.0; // Last close at low
let (k_values, _) = Stochastic::calculate_series(&high, &low, &close, 14, 3, 1).unwrap();
assert!(k_values[0].abs() < 1e-10);
}
#[test]
fn test_stochastic_invalid_periods_do_not_panic() {
for (k, d, smooth) in [(0, 3, 1), (3, 0, 1), (3, 3, 0)] {
assert!(Stochastic::new(k, d, smooth).is_err());
assert!(
Stochastic::calculate_series(&[10.0; 5], &[0.0; 5], &[5.0; 5], k, d, smooth)
.is_err()
);
}
}
#[test]
fn test_stochastic_golden_values_and_incremental_parity() {
let high = vec![10.0, 11.0, 12.0, 13.0, 14.0, 15.0];
let low = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
let close = vec![5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
for smooth in [1, 2] {
let (k, d) = Stochastic::calculate_series(&high, &low, &close, 3, 2, smooth).unwrap();
for value in k.iter().chain(d.iter()) {
assert!((value - 100.0 * 7.0 / 12.0).abs() < 1e-10);
}
let mut incremental = Stochastic::new(3, 2, smooth).unwrap();
let mut actual = Vec::new();
for i in 0..high.len() {
if let Some(value) = incremental.update_hlc(high[i], low[i], close[i]).unwrap() {
actual.push(value.d);
}
}
assert_eq!(actual.len(), d.len());
for (a, b) in actual.iter().zip(d.iter()) {
assert!((a - b).abs() < 1e-10);
}
}
let (k, d) =
Stochastic::calculate_series(&high[..3], &low[..3], &close[..3], 3, 2, 2).unwrap();
assert!(k.is_empty() && d.is_empty());
}
}