use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct PercentRank {
name: String,
period: usize,
values: VecDeque<Decimal>,
}
impl PercentRank {
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,
values: VecDeque::with_capacity(period + 1),
})
}
}
impl Signal for PercentRank {
fn name(&self) -> &str {
&self.name
}
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.values.push_back(bar.close);
if self.values.len() > self.period + 1 {
self.values.pop_front();
}
if self.values.len() < self.period + 1 {
return Ok(SignalValue::Unavailable);
}
let current = bar.close;
let count = self.values
.iter()
.rev()
.skip(1) .take(self.period)
.filter(|&&v| v <= current)
.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 is_ready(&self) -> bool {
self.values.len() >= self.period + 1
}
fn period(&self) -> usize {
self.period
}
fn reset(&mut self) {
self.values.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(c: &str) -> OhlcvBar {
let cl = Price::new(c.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: cl, high: cl, low: cl, close: cl,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_pr_period_zero_fails() {
assert!(PercentRank::new("pr", 0).is_err());
}
#[test]
fn test_pr_unavailable_before_period() {
let mut pr = PercentRank::new("pr", 3).unwrap();
assert_eq!(pr.update_bar(&bar("100")).unwrap(), SignalValue::Unavailable);
assert!(!pr.is_ready());
}
#[test]
fn test_pr_max_when_always_rising() {
let mut pr = PercentRank::new("pr", 3).unwrap();
pr.update_bar(&bar("100")).unwrap();
pr.update_bar(&bar("101")).unwrap();
pr.update_bar(&bar("102")).unwrap();
let v = pr.update_bar(&bar("103")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(100)));
}
#[test]
fn test_pr_zero_when_always_falling() {
let mut pr = PercentRank::new("pr", 3).unwrap();
pr.update_bar(&bar("103")).unwrap();
pr.update_bar(&bar("102")).unwrap();
pr.update_bar(&bar("101")).unwrap();
let v = pr.update_bar(&bar("100")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_pr_reset() {
let mut pr = PercentRank::new("pr", 3).unwrap();
for c in ["100", "101", "102", "103"] {
pr.update_bar(&bar(c)).unwrap();
}
assert!(pr.is_ready());
pr.reset();
assert!(!pr.is_ready());
}
}