use rust_decimal::Decimal;
use std::collections::VecDeque;
use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
pub struct GapFillRatio {
period: usize,
prev_close: Option<Decimal>,
window: VecDeque<(u8, u8)>, gap_count: usize,
fill_count: usize,
}
impl GapFillRatio {
pub fn new(period: usize) -> Result<Self, FinError> {
if period == 0 {
return Err(FinError::InvalidPeriod(period));
}
Ok(Self {
period,
prev_close: None,
window: VecDeque::with_capacity(period),
gap_count: 0,
fill_count: 0,
})
}
}
impl Signal for GapFillRatio {
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let (had_gap, filled_gap) = if let Some(pc) = self.prev_close {
let bullish_gap = bar.open > pc;
let bearish_gap = bar.open < pc;
if bullish_gap {
let filled = if bar.low <= pc { 1u8 } else { 0u8 };
(1u8, filled)
} else if bearish_gap {
let filled = if bar.high >= pc { 1u8 } else { 0u8 };
(1u8, filled)
} else {
(0u8, 0u8)
}
} else {
(0u8, 0u8)
};
self.prev_close = Some(bar.close);
self.window.push_back((had_gap, filled_gap));
self.gap_count += had_gap as usize;
self.fill_count += filled_gap as usize;
if self.window.len() > self.period {
if let Some((og, of_)) = self.window.pop_front() {
self.gap_count -= og as usize;
self.fill_count -= of_ as usize;
}
}
if self.window.len() < self.period {
return Ok(SignalValue::Unavailable);
}
if self.gap_count == 0 {
return Ok(SignalValue::Scalar(Decimal::ZERO));
}
let ratio = Decimal::from(self.fill_count as u32) / Decimal::from(self.gap_count as u32);
Ok(SignalValue::Scalar(ratio))
}
fn is_ready(&self) -> bool { self.window.len() >= self.period }
fn period(&self) -> usize { self.period }
fn reset(&mut self) {
self.prev_close = None;
self.window.clear();
self.gap_count = 0;
self.fill_count = 0;
}
fn name(&self) -> &str { "GapFillRatio" }
}
#[cfg(test)]
mod tests {
use super::*;
use rust_decimal_macros::dec;
fn bar(o: &str, h: &str, l: &str, c: &str) -> BarInput {
BarInput {
open: o.parse().unwrap(),
high: h.parse().unwrap(),
low: l.parse().unwrap(),
close: c.parse().unwrap(),
volume: dec!(1000),
}
}
#[test]
fn test_gfr_gap_filled() {
let mut sig = GapFillRatio::new(2).unwrap();
sig.update(&bar("100", "100", "100", "100")).unwrap(); sig.update(&bar("105", "107", "99", "103")).unwrap(); let v = sig.update(&bar("103", "105", "102", "104")).unwrap(); assert_eq!(v, SignalValue::Scalar(dec!(1)));
}
#[test]
fn test_gfr_no_gaps() {
let mut sig = GapFillRatio::new(2).unwrap();
sig.update(&bar("100", "105", "98", "100")).unwrap();
sig.update(&bar("100", "103", "98", "100")).unwrap(); let v = sig.update(&bar("100", "102", "98", "100")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
}