use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct CloseHighFrequency {
name: String,
period: usize,
scores: VecDeque<bool>,
}
impl CloseHighFrequency {
pub fn new(name: impl Into<String>, period: usize) -> Result<Self, FinError> {
if period == 0 {
return Err(FinError::InvalidPeriod(period));
}
Ok(Self {
name: name.into(),
period,
scores: VecDeque::with_capacity(period),
})
}
}
impl Signal for CloseHighFrequency {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { self.period }
fn is_ready(&self) -> bool { self.scores.len() >= self.period }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let two = Decimal::TWO;
let midpoint = (bar.high + bar.low) / two;
let in_upper = bar.close >= midpoint;
self.scores.push_back(in_upper);
if self.scores.len() > self.period {
self.scores.pop_front();
}
if self.scores.len() < self.period {
return Ok(SignalValue::Unavailable);
}
let count = self.scores.iter().filter(|&&s| s).count();
#[allow(clippy::cast_possible_truncation)]
let pct = Decimal::from(count as u32)
/ Decimal::from(self.period as u32)
* Decimal::ONE_HUNDRED;
Ok(SignalValue::Scalar(pct))
}
fn reset(&mut self) {
self.scores.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ohlcv::OhlcvBar;
use crate::signals::Signal;
use crate::types::{NanoTimestamp, Price, Quantity, Symbol};
use rust_decimal_macros::dec;
fn bar(h: &str, l: &str, c: &str) -> OhlcvBar {
let hp = Price::new(h.parse().unwrap()).unwrap();
let lp = Price::new(l.parse().unwrap()).unwrap();
let cp = Price::new(c.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: lp, high: hp, low: lp, close: cp,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_chf_invalid_period() {
assert!(CloseHighFrequency::new("chf", 0).is_err());
}
#[test]
fn test_chf_unavailable_during_warmup() {
let mut chf = CloseHighFrequency::new("chf", 3).unwrap();
for _ in 0..2 {
assert_eq!(chf.update_bar(&bar("110", "90", "100")).unwrap(), SignalValue::Unavailable);
}
assert!(!chf.is_ready());
}
#[test]
fn test_chf_all_closes_above_mid() {
let mut chf = CloseHighFrequency::new("chf", 3).unwrap();
for _ in 0..3 {
chf.update_bar(&bar("110", "90", "109")).unwrap();
}
let result = chf.update_bar(&bar("110", "90", "109")).unwrap();
assert_eq!(result, SignalValue::Scalar(dec!(100)));
}
#[test]
fn test_chf_all_closes_below_mid() {
let mut chf = CloseHighFrequency::new("chf", 3).unwrap();
for _ in 0..3 {
chf.update_bar(&bar("110", "90", "91")).unwrap();
}
let result = chf.update_bar(&bar("110", "90", "91")).unwrap();
assert_eq!(result, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_chf_half_above_half_below() {
let mut chf = CloseHighFrequency::new("chf", 4).unwrap();
chf.update_bar(&bar("110", "90", "109")).unwrap(); chf.update_bar(&bar("110", "90", "91")).unwrap(); chf.update_bar(&bar("110", "90", "109")).unwrap(); let result = chf.update_bar(&bar("110", "90", "91")).unwrap(); assert_eq!(result, SignalValue::Scalar(dec!(50)));
}
#[test]
fn test_chf_reset() {
let mut chf = CloseHighFrequency::new("chf", 3).unwrap();
for _ in 0..3 { chf.update_bar(&bar("110", "90", "105")).unwrap(); }
assert!(chf.is_ready());
chf.reset();
assert!(!chf.is_ready());
}
}