use rust_decimal::Decimal;
use std::collections::VecDeque;
use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
pub struct CloseAbovePrevHigh {
period: usize,
prev_high: Option<Decimal>,
window: VecDeque<u8>,
count: usize,
}
impl CloseAbovePrevHigh {
pub fn new(period: usize) -> Result<Self, FinError> {
if period == 0 {
return Err(FinError::InvalidPeriod(period));
}
Ok(Self { period, prev_high: None, window: VecDeque::with_capacity(period), count: 0 })
}
}
impl Signal for CloseAbovePrevHigh {
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
if let Some(ph) = self.prev_high {
let hit: u8 = if bar.close > ph { 1 } else { 0 };
self.window.push_back(hit);
self.count += hit as usize;
if self.window.len() > self.period {
if let Some(old) = self.window.pop_front() {
self.count -= old as usize;
}
}
}
self.prev_high = Some(bar.high);
if self.window.len() < self.period {
return Ok(SignalValue::Unavailable);
}
Ok(SignalValue::Scalar(Decimal::from(self.count as u32)))
}
fn is_ready(&self) -> bool { self.window.len() >= self.period }
fn period(&self) -> usize { self.period }
fn reset(&mut self) { self.prev_high = None; self.window.clear(); self.count = 0; }
fn name(&self) -> &str { "CloseAbovePrevHigh" }
}
#[cfg(test)]
mod tests {
use super::*;
use rust_decimal_macros::dec;
fn bar(h: &str, c: &str) -> BarInput {
BarInput {
open: c.parse().unwrap(),
high: h.parse().unwrap(),
low: c.parse().unwrap(),
close: c.parse().unwrap(),
volume: dec!(1000),
}
}
#[test]
fn test_caph_all_breakouts() {
let mut sig = CloseAbovePrevHigh::new(3).unwrap();
sig.update(&bar("100", "100")).unwrap(); sig.update(&bar("105", "105")).unwrap(); sig.update(&bar("110", "110")).unwrap(); let v = sig.update(&bar("115", "115")).unwrap(); assert_eq!(v, SignalValue::Scalar(dec!(3)));
}
#[test]
fn test_caph_no_breakouts() {
let mut sig = CloseAbovePrevHigh::new(3).unwrap();
sig.update(&bar("110", "100")).unwrap(); sig.update(&bar("110", "100")).unwrap(); sig.update(&bar("110", "100")).unwrap(); let v = sig.update(&bar("110", "100")).unwrap(); assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
}