use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct WickRatio {
name: String,
period: usize,
ratios: VecDeque<Decimal>,
}
impl WickRatio {
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,
ratios: VecDeque::with_capacity(period),
})
}
}
impl Signal for WickRatio {
fn name(&self) -> &str { &self.name }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let range = bar.range();
let body = bar.body_size();
let wick = range - body;
let ratio = if body.is_zero() { Decimal::ZERO } else { wick / body };
self.ratios.push_back(ratio);
if self.ratios.len() > self.period { self.ratios.pop_front(); }
if self.ratios.len() < self.period { return Ok(SignalValue::Unavailable); }
let avg = self.ratios.iter().sum::<Decimal>() / Decimal::from(self.period as u32);
Ok(SignalValue::Scalar(avg))
}
fn is_ready(&self) -> bool { self.ratios.len() >= self.period }
fn period(&self) -> usize { self.period }
fn reset(&mut self) {
self.ratios.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ohlcv::OhlcvBar;
use crate::types::{NanoTimestamp, Price, Quantity, Symbol};
use rust_decimal_macros::dec;
fn bar_hloc(h: &str, l: &str, o: &str, c: &str) -> OhlcvBar {
let hp = Price::new(h.parse().unwrap()).unwrap();
let lp = Price::new(l.parse().unwrap()).unwrap();
let op = Price::new(o.parse().unwrap()).unwrap();
let cp = Price::new(c.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: op, high: hp, low: lp, close: cp,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
fn full_body_bar() -> OhlcvBar { bar_hloc("110", "90", "90", "110") }
fn doji_bar() -> OhlcvBar { bar_hloc("110", "90", "100", "100") }
#[test]
fn test_wr_invalid() {
assert!(WickRatio::new("w", 0).is_err());
}
#[test]
fn test_wr_unavailable_before_warmup() {
let mut w = WickRatio::new("w", 3).unwrap();
for _ in 0..2 {
assert_eq!(w.update_bar(&full_body_bar()).unwrap(), SignalValue::Unavailable);
}
}
#[test]
fn test_wr_full_body_is_zero() {
let mut w = WickRatio::new("w", 3).unwrap();
let mut last = SignalValue::Unavailable;
for _ in 0..5 { last = w.update_bar(&full_body_bar()).unwrap(); }
if let SignalValue::Scalar(v) = last {
assert_eq!(v, dec!(0));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_wr_doji_is_zero() {
let mut w = WickRatio::new("w", 3).unwrap();
let mut last = SignalValue::Unavailable;
for _ in 0..5 { last = w.update_bar(&doji_bar()).unwrap(); }
if let SignalValue::Scalar(v) = last {
assert_eq!(v, dec!(0));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_wr_small_body_high_wick() {
let mut w = WickRatio::new("w", 1).unwrap();
if let SignalValue::Scalar(v) = w.update_bar(&bar_hloc("115", "85", "99", "101")).unwrap() {
assert_eq!(v, dec!(14));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_wr_non_negative() {
let mut w = WickRatio::new("w", 3).unwrap();
for bar in [full_body_bar(), doji_bar(), bar_hloc("115", "85", "99", "101")] {
if let SignalValue::Scalar(v) = w.update_bar(&bar).unwrap() {
assert!(v >= dec!(0), "expected non-negative, got {v}");
}
}
}
#[test]
fn test_wr_reset() {
let mut w = WickRatio::new("w", 3).unwrap();
for _ in 0..5 { w.update_bar(&full_body_bar()).unwrap(); }
assert!(w.is_ready());
w.reset();
assert!(!w.is_ready());
}
}