use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct BodySizeRank {
name: String,
period: usize,
bodies: VecDeque<Decimal>,
}
impl BodySizeRank {
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,
bodies: VecDeque::with_capacity(period),
})
}
}
impl Signal for BodySizeRank {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { self.period }
fn is_ready(&self) -> bool { self.bodies.len() >= self.period }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let body = bar.body_size();
self.bodies.push_back(body);
if self.bodies.len() > self.period {
self.bodies.pop_front();
}
if self.bodies.len() < self.period {
return Ok(SignalValue::Unavailable);
}
let current = *self.bodies.back().unwrap();
let n = (self.period - 1) as u32;
let below = self.bodies
.iter()
.take(self.period - 1)
.filter(|&&b| b < current)
.count() as u32;
if n == 0 {
return Ok(SignalValue::Scalar(Decimal::ZERO));
}
let rank = Decimal::from(below)
.checked_div(Decimal::from(n))
.ok_or(FinError::ArithmeticOverflow)?;
Ok(SignalValue::Scalar(rank))
}
fn reset(&mut self) {
self.bodies.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(o: &str, c: &str) -> OhlcvBar {
let op = Price::new(o.parse().unwrap()).unwrap();
let cp = Price::new(c.parse().unwrap()).unwrap();
let hp = if cp > op { cp } else { op };
let lp = if cp < op { cp } else { op };
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,
}
}
#[test]
fn test_bsr_invalid_period() {
assert!(BodySizeRank::new("bsr", 0).is_err());
}
#[test]
fn test_bsr_unavailable_before_period() {
let mut s = BodySizeRank::new("bsr", 3).unwrap();
assert_eq!(s.update_bar(&bar("100","105")).unwrap(), SignalValue::Unavailable);
assert_eq!(s.update_bar(&bar("100","103")).unwrap(), SignalValue::Unavailable);
assert!(!s.is_ready());
}
#[test]
fn test_bsr_largest_body_gives_one() {
let mut s = BodySizeRank::new("bsr", 4).unwrap();
s.update_bar(&bar("100","101")).unwrap(); s.update_bar(&bar("100","102")).unwrap(); s.update_bar(&bar("100","103")).unwrap(); if let SignalValue::Scalar(v) = s.update_bar(&bar("100","110")).unwrap() { assert_eq!(v, dec!(1), "largest body should rank 1.0: {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_bsr_smallest_body_gives_zero() {
let mut s = BodySizeRank::new("bsr", 4).unwrap();
s.update_bar(&bar("100","105")).unwrap();
s.update_bar(&bar("100","108")).unwrap();
s.update_bar(&bar("100","110")).unwrap();
if let SignalValue::Scalar(v) = s.update_bar(&bar("100","101")).unwrap() {
assert_eq!(v, dec!(0), "smallest body should rank 0.0: {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_bsr_in_range() {
let mut s = BodySizeRank::new("bsr", 3).unwrap();
let bars = [("100","103"), ("100","107"), ("100","105"), ("100","102")];
for (o, c) in &bars {
if let SignalValue::Scalar(v) = s.update_bar(&bar(o, c)).unwrap() {
assert!(v >= dec!(0) && v <= dec!(1), "rank out of [0,1]: {v}");
}
}
}
#[test]
fn test_bsr_reset() {
let mut s = BodySizeRank::new("bsr", 2).unwrap();
s.update_bar(&bar("100","105")).unwrap();
s.update_bar(&bar("100","108")).unwrap();
assert!(s.is_ready());
s.reset();
assert!(!s.is_ready());
}
}