use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
pub struct PivotDistance {
name: String,
prev_high: Option<Decimal>,
prev_low: Option<Decimal>,
prev_close: Option<Decimal>,
last_pivot: Option<Decimal>,
}
impl PivotDistance {
pub fn new(name: impl Into<String>) -> Result<Self, FinError> {
Ok(Self {
name: name.into(),
prev_high: None,
prev_low: None,
prev_close: None,
last_pivot: None,
})
}
pub fn last_pivot(&self) -> Option<Decimal> { self.last_pivot }
}
impl Signal for PivotDistance {
fn name(&self) -> &str { &self.name }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let result = match (self.prev_high, self.prev_low, self.prev_close) {
(Some(ph), Some(pl), Some(pc)) => {
let pivot = (ph + pl + pc) / Decimal::from(3u32);
self.last_pivot = Some(pivot);
if pivot.is_zero() {
Ok(SignalValue::Scalar(Decimal::ZERO))
} else {
let dist = (bar.close - pivot) / pivot * Decimal::from(100u32);
Ok(SignalValue::Scalar(dist))
}
}
_ => Ok(SignalValue::Unavailable),
};
self.prev_high = Some(bar.high);
self.prev_low = Some(bar.low);
self.prev_close = Some(bar.close);
result
}
fn is_ready(&self) -> bool { self.last_pivot.is_some() }
fn period(&self) -> usize { 1 }
fn reset(&mut self) {
self.prev_high = None;
self.prev_low = None;
self.prev_close = None;
self.last_pivot = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ohlcv::OhlcvBar;
use crate::types::{NanoTimestamp, Price, Quantity, Symbol};
use rust_decimal_macros::dec;
fn bar_hlc(h: &str, l: &str, c: &str) -> OhlcvBar {
let hp = Price::new(h.parse().unwrap()).unwrap();
let lp = Price::new(l.parse().unwrap()).unwrap();
let cp = Price::new(c.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: cp, 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_pd_unavailable_first_bar() {
let mut p = PivotDistance::new("p").unwrap();
assert_eq!(
p.update_bar(&bar_hlc("105", "95", "100")).unwrap(),
SignalValue::Unavailable
);
}
#[test]
fn test_pd_at_pivot_is_zero() {
let mut p = PivotDistance::new("p").unwrap();
p.update_bar(&bar_hlc("105", "95", "100")).unwrap();
if let SignalValue::Scalar(v) = p.update_bar(&bar_hlc("100", "100", "100")).unwrap() {
assert_eq!(v, dec!(0));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_pd_above_pivot_positive() {
let mut p = PivotDistance::new("p").unwrap();
p.update_bar(&bar_hlc("110", "90", "100")).unwrap();
if let SignalValue::Scalar(v) = p.update_bar(&bar_hlc("105", "105", "105")).unwrap() {
assert_eq!(v, dec!(5));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_pd_below_pivot_negative() {
let mut p = PivotDistance::new("p").unwrap();
p.update_bar(&bar_hlc("110", "90", "100")).unwrap();
if let SignalValue::Scalar(v) = p.update_bar(&bar_hlc("95", "95", "95")).unwrap() {
assert_eq!(v, dec!(-5));
} else { panic!("expected Scalar"); }
}
#[test]
fn test_pd_pivot_accessor() {
let mut p = PivotDistance::new("p").unwrap();
p.update_bar(&bar_hlc("110", "90", "100")).unwrap();
p.update_bar(&bar_hlc("100", "100", "100")).unwrap();
assert_eq!(p.last_pivot(), Some(dec!(100)));
}
#[test]
fn test_pd_reset() {
let mut p = PivotDistance::new("p").unwrap();
p.update_bar(&bar_hlc("105", "95", "100")).unwrap();
p.update_bar(&bar_hlc("100", "100", "100")).unwrap();
assert!(p.is_ready());
p.reset();
assert!(!p.is_ready());
assert!(p.last_pivot().is_none());
}
}