use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
pub struct GapDirectionStreak {
name: String,
streak: u32,
last_direction: i8,
prev_close: Option<Decimal>,
seen_bars: usize,
}
impl GapDirectionStreak {
pub fn new(name: impl Into<String>) -> Result<Self, FinError> {
Ok(Self { name: name.into(), streak: 0, last_direction: 0, prev_close: None, seen_bars: 0 })
}
}
impl Signal for GapDirectionStreak {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { 1 }
fn is_ready(&self) -> bool { self.seen_bars >= 2 }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.seen_bars += 1;
let pc = self.prev_close;
self.prev_close = Some(bar.close);
let Some(prev_close) = pc else {
return Ok(SignalValue::Unavailable);
};
let direction: i8 = if bar.open > prev_close {
1
} else if bar.open < prev_close {
-1
} else {
0
};
if direction == 0 || direction != self.last_direction {
self.streak = if direction == 0 { 0 } else { 1 };
} else {
self.streak += 1;
}
self.last_direction = direction;
Ok(SignalValue::Scalar(Decimal::from(self.streak)))
}
fn reset(&mut self) {
self.streak = 0;
self.last_direction = 0;
self.prev_close = None;
self.seen_bars = 0;
}
}
#[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(o: &str, c: &str) -> OhlcvBar {
let op = Price::new(o.parse().unwrap()).unwrap();
let cp = Price::new(c.parse().unwrap()).unwrap();
let high = if cp > op { cp } else { op };
let low = if cp < op { cp } else { op };
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: op, high, low, close: cp,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_gds_first_bar_unavailable() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
assert_eq!(gds.update_bar(&bar("100", "102")).unwrap(), SignalValue::Unavailable);
assert!(!gds.is_ready());
}
#[test]
fn test_gds_ready_after_second_bar() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "102")).unwrap(); gds.update_bar(&bar("104", "106")).unwrap(); assert!(gds.is_ready());
}
#[test]
fn test_gds_first_gap_up_gives_one() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "102")).unwrap(); if let SignalValue::Scalar(v) = gds.update_bar(&bar("105", "107")).unwrap() {
assert_eq!(v, dec!(1));
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_gds_consecutive_gap_ups_increment() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "100")).unwrap();
gds.update_bar(&bar("102", "102")).unwrap(); gds.update_bar(&bar("104", "104")).unwrap(); if let SignalValue::Scalar(v) = gds.update_bar(&bar("106", "106")).unwrap() {
assert_eq!(v, dec!(3));
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_gds_direction_change_resets_to_one() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "100")).unwrap();
gds.update_bar(&bar("102", "102")).unwrap(); gds.update_bar(&bar("104", "104")).unwrap(); if let SignalValue::Scalar(v) = gds.update_bar(&bar("101", "101")).unwrap() {
assert_eq!(v, dec!(1));
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_gds_flat_open_resets_to_zero() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "100")).unwrap();
gds.update_bar(&bar("102", "102")).unwrap(); if let SignalValue::Scalar(v) = gds.update_bar(&bar("102", "103")).unwrap() {
assert_eq!(v, dec!(0));
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_gds_period_is_one() {
let gds = GapDirectionStreak::new("gds").unwrap();
assert_eq!(gds.period(), 1);
}
#[test]
fn test_gds_reset() {
let mut gds = GapDirectionStreak::new("gds").unwrap();
gds.update_bar(&bar("100", "102")).unwrap();
gds.update_bar(&bar("104", "106")).unwrap();
assert!(gds.is_ready());
gds.reset();
assert!(!gds.is_ready());
assert_eq!(gds.update_bar(&bar("100", "102")).unwrap(), SignalValue::Unavailable);
}
}