use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::indicators::ema::Ema;
use crate::ohlcv::Candle;
use crate::traits::Indicator;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct KasePermissionStochasticOutput {
pub fast: f64,
pub slow: f64,
}
#[derive(Debug, Clone)]
pub struct KasePermissionStochastic {
length: usize,
smooth: usize,
window: VecDeque<(f64, f64)>,
fast_ema: Ema,
slow_ema: Ema,
}
impl KasePermissionStochastic {
pub fn new(length: usize, smooth: usize) -> Result<Self> {
if length == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
length,
smooth,
window: VecDeque::with_capacity(length),
fast_ema: Ema::new(smooth)?,
slow_ema: Ema::new(smooth)?,
})
}
pub fn classic() -> Self {
Self::new(9, 3).expect("classic Kase Permission Stochastic parameters are valid")
}
pub const fn periods(&self) -> (usize, usize) {
(self.length, self.smooth)
}
}
impl Indicator for KasePermissionStochastic {
type Input = Candle;
type Output = KasePermissionStochasticOutput;
fn update(&mut self, candle: Candle) -> Option<KasePermissionStochasticOutput> {
self.window.push_back((candle.high, candle.low));
if self.window.len() > self.length {
self.window.pop_front();
}
if self.window.len() < self.length {
return None;
}
let highest = self.window.iter().map(|w| w.0).fold(f64::MIN, f64::max);
let lowest = self.window.iter().map(|w| w.1).fold(f64::MAX, f64::min);
let raw_k = if highest > lowest {
100.0 * (candle.close - lowest) / (highest - lowest)
} else {
50.0
};
let fast = self.fast_ema.update(raw_k)?;
let slow = self.slow_ema.update(fast)?;
Some(KasePermissionStochasticOutput { fast, slow })
}
fn reset(&mut self) {
self.window.clear();
self.fast_ema.reset();
self.slow_ema.reset();
}
fn warmup_period(&self) -> usize {
self.length + 2 * self.smooth - 2
}
fn is_ready(&self) -> bool {
self.slow_ema.is_ready()
}
fn name(&self) -> &'static str {
"KasePermissionStochastic"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn candle(high: f64, low: f64, close: f64, ts: i64) -> Candle {
Candle::new(f64::midpoint(high, low), high, low, close, 1.0, ts).unwrap()
}
#[test]
fn rejects_zero_period() {
assert!(matches!(
KasePermissionStochastic::new(0, 3),
Err(Error::PeriodZero)
));
assert!(matches!(
KasePermissionStochastic::new(9, 0),
Err(Error::PeriodZero)
));
}
#[test]
fn accessors_and_metadata() {
let k = KasePermissionStochastic::classic();
assert_eq!(k.periods(), (9, 3));
assert_eq!(k.warmup_period(), 13);
assert_eq!(k.name(), "KasePermissionStochastic");
assert!(!k.is_ready());
}
#[test]
fn warmup_emits_at_expected_bar() {
let mut k = KasePermissionStochastic::new(3, 2).unwrap();
let candles: Vec<Candle> = (0..8).map(|i| candle(11.0, 9.0, 10.5, i)).collect();
let out = k.batch(&candles);
assert!(out[3].is_none());
assert!(out[4].is_some());
}
#[test]
fn top_of_range_is_high() {
let mut k = KasePermissionStochastic::new(5, 3).unwrap();
let candles: Vec<Candle> = (0_i64..40)
.map(|i| {
let base = 100.0 + i as f64;
candle(base + 2.0, base - 2.0, base + 2.0, i)
})
.collect();
let last = k.batch(&candles).last().unwrap().unwrap();
assert!(last.fast > 80.0, "fast {} should be high", last.fast);
assert!(last.slow > 80.0, "slow {} should be high", last.slow);
}
#[test]
fn flat_window_defaults_to_neutral() {
let mut k = KasePermissionStochastic::new(4, 2).unwrap();
let candles: Vec<Candle> = (0..20).map(|i| candle(10.0, 10.0, 10.0, i)).collect();
let last = k.batch(&candles).last().unwrap().unwrap();
assert_relative_eq!(last.fast, 50.0, epsilon = 1e-9);
assert_relative_eq!(last.slow, 50.0, epsilon = 1e-9);
}
#[test]
fn reset_clears_state() {
let mut k = KasePermissionStochastic::classic();
let candles: Vec<Candle> = (0..40).map(|i| candle(11.0, 9.0, 10.5, i)).collect();
k.batch(&candles);
assert!(k.is_ready());
k.reset();
assert!(!k.is_ready());
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80_i64)
.map(|i| {
let base = 100.0 + (i as f64 * 0.2).sin() * 5.0;
candle(base + 2.0, base - 2.0, base + (i as f64 * 0.3).cos(), i)
})
.collect();
let mut a = KasePermissionStochastic::classic();
let mut b = KasePermissionStochastic::classic();
assert_eq!(
a.batch(&candles),
candles.iter().map(|c| b.update(*c)).collect::<Vec<_>>()
);
}
}