use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct TrendStrengthIndex {
name: String,
period: usize,
closes: VecDeque<Decimal>,
}
impl TrendStrengthIndex {
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,
closes: VecDeque::with_capacity(period + 1),
})
}
}
impl Signal for TrendStrengthIndex {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { self.period }
fn is_ready(&self) -> bool { self.closes.len() > self.period }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.closes.push_back(bar.close);
if self.closes.len() > self.period + 1 {
self.closes.pop_front();
}
if self.closes.len() <= self.period {
return Ok(SignalValue::Unavailable);
}
let prices: Vec<Decimal> = self.closes.iter().copied().collect();
let first = prices[0];
let last = *prices.last().unwrap();
let net_move = (last - first).abs();
let path: Decimal = prices.windows(2)
.map(|w| (w[1] - w[0]).abs())
.sum();
if path.is_zero() {
return Ok(SignalValue::Unavailable);
}
let tsi = net_move
.checked_div(path)
.ok_or(FinError::ArithmeticOverflow)?
* Decimal::ONE_HUNDRED;
Ok(SignalValue::Scalar(tsi))
}
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(c: &str) -> OhlcvBar {
let p = Price::new(c.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_tsi_invalid_period() {
assert!(TrendStrengthIndex::new("tsi", 0).is_err());
}
#[test]
fn test_tsi_unavailable_during_warmup() {
let mut tsi = TrendStrengthIndex::new("tsi", 3).unwrap();
for p in &["100", "101", "102"] {
assert_eq!(tsi.update_bar(&bar(p)).unwrap(), SignalValue::Unavailable);
}
assert!(!tsi.is_ready());
}
#[test]
fn test_tsi_straight_trend_is_100() {
let mut tsi = TrendStrengthIndex::new("tsi", 4).unwrap();
for p in &["100", "101", "102", "103", "104"] {
tsi.update_bar(&bar(p)).unwrap();
}
if let SignalValue::Scalar(v) = tsi.update_bar(&bar("105")).unwrap() {
assert_eq!(v, dec!(100), "straight trend → TSI = 100");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_tsi_choppy_low() {
let mut tsi = TrendStrengthIndex::new("tsi", 4).unwrap();
for p in &["100", "110", "90", "110", "100"] {
tsi.update_bar(&bar(p)).unwrap();
}
if let SignalValue::Scalar(v) = tsi.update_bar(&bar("100")).unwrap() {
assert!(v <= dec!(25), "choppy/no-net-trend prices → low TSI: {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_tsi_flat_unavailable() {
let mut tsi = TrendStrengthIndex::new("tsi", 3).unwrap();
for _ in 0..5 {
tsi.update_bar(&bar("100")).unwrap();
}
assert_eq!(tsi.update_bar(&bar("100")).unwrap(), SignalValue::Unavailable);
}
#[test]
fn test_tsi_reset() {
let mut tsi = TrendStrengthIndex::new("tsi", 3).unwrap();
for p in &["100", "101", "102", "103"] { tsi.update_bar(&bar(p)).unwrap(); }
assert!(tsi.is_ready());
tsi.reset();
assert!(!tsi.is_ready());
}
}