use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct TrendSlopeRatio {
name: String,
short_period: usize,
long_period: usize,
closes: VecDeque<Decimal>,
}
impl TrendSlopeRatio {
pub fn new(
name: impl Into<String>,
short_period: usize,
long_period: usize,
) -> Result<Self, FinError> {
if short_period == 0 {
return Err(FinError::InvalidPeriod(short_period));
}
if long_period == 0 {
return Err(FinError::InvalidPeriod(long_period));
}
if short_period >= long_period {
return Err(FinError::InvalidPeriod(short_period));
}
Ok(Self {
name: name.into(),
short_period,
long_period,
closes: VecDeque::with_capacity(long_period + 1),
})
}
}
impl Signal for TrendSlopeRatio {
fn name(&self) -> &str {
&self.name
}
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.closes.push_back(bar.close);
if self.closes.len() > self.long_period + 1 {
self.closes.pop_front();
}
if self.closes.len() < self.long_period + 1 {
return Ok(SignalValue::Unavailable);
}
let current = *self.closes.back().unwrap();
let short_base = self.closes[self.closes.len() - 1 - self.short_period];
let long_base = *self.closes.front().unwrap();
#[allow(clippy::cast_possible_truncation)]
let short_slope = (current - short_base)
.checked_div(Decimal::from(self.short_period as u32))
.ok_or(FinError::ArithmeticOverflow)?;
#[allow(clippy::cast_possible_truncation)]
let long_slope = (current - long_base)
.checked_div(Decimal::from(self.long_period as u32))
.ok_or(FinError::ArithmeticOverflow)?;
if long_slope.is_zero() {
return Ok(SignalValue::Scalar(Decimal::ZERO));
}
let ratio = short_slope.checked_div(long_slope).ok_or(FinError::ArithmeticOverflow)?;
Ok(SignalValue::Scalar(ratio))
}
fn is_ready(&self) -> bool {
self.closes.len() >= self.long_period + 1
}
fn period(&self) -> usize {
self.long_period
}
fn reset(&mut self) {
self.closes.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(close: &str) -> OhlcvBar {
let p = Price::new(close.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: p, high: p, low: p, close: p,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_invalid_params() {
assert!(TrendSlopeRatio::new("tsr", 0, 10).is_err());
assert!(TrendSlopeRatio::new("tsr", 10, 5).is_err());
assert!(TrendSlopeRatio::new("tsr", 5, 5).is_err());
}
#[test]
fn test_unavailable_before_period() {
let mut tsr = TrendSlopeRatio::new("tsr", 2, 5).unwrap();
assert_eq!(tsr.update_bar(&bar("100")).unwrap(), SignalValue::Unavailable);
}
#[test]
fn test_flat_prices_zero() {
let mut tsr = TrendSlopeRatio::new("tsr", 2, 4).unwrap();
for _ in 0..5 {
tsr.update_bar(&bar("100")).unwrap();
}
let v = tsr.update_bar(&bar("100")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_linear_rise_gives_one() {
let mut tsr = TrendSlopeRatio::new("tsr", 2, 4).unwrap();
for i in 0..=4u32 {
tsr.update_bar(&bar(&(100 + i * 2).to_string())).unwrap();
}
let v = tsr.update_bar(&bar("110")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(1)));
}
#[test]
fn test_reset() {
let mut tsr = TrendSlopeRatio::new("tsr", 2, 4).unwrap();
for _ in 0..5 {
tsr.update_bar(&bar("100")).unwrap();
}
assert!(tsr.is_ready());
tsr.reset();
assert!(!tsr.is_ready());
}
}