use super::types::{CompareTarget, Condition, ConditionGroup, ConditionNode, Operator};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct IndicatorRef {
pub name: String,
}
impl IndicatorRef {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}
pub fn sma(period: usize) -> Self {
Self::new(format!("sma{period}"))
}
pub fn ema(period: usize) -> Self {
Self::new(format!("ema{period}"))
}
pub fn macd(fast: usize, slow: usize, signal: usize) -> Self {
Self::new(format!("macd_{fast}_{slow}_{signal}_line"))
}
pub fn macd_signal(fast: usize, slow: usize, signal: usize) -> Self {
Self::new(format!("macd_{fast}_{slow}_{signal}_signal"))
}
pub fn rsi(period: usize) -> Self {
Self::new(format!("rsi{period}"))
}
pub fn stoch_k(k_period: usize, d_period: usize) -> Self {
Self::new(format!("stoch_{k_period}_{d_period}_k"))
}
pub fn stoch_d(k_period: usize, d_period: usize) -> Self {
Self::new(format!("stoch_{k_period}_{d_period}_d"))
}
pub fn bb_upper(period: usize, std_dev: f64) -> Self {
Self::new(format!("bb_{period}_{std_dev}_upper"))
}
pub fn bb_middle(period: usize, std_dev: f64) -> Self {
Self::new(format!("bb_{period}_{std_dev}_middle"))
}
pub fn bb_lower(period: usize, std_dev: f64) -> Self {
Self::new(format!("bb_{period}_{std_dev}_lower"))
}
pub fn atr(period: usize) -> Self {
Self::new(format!("atr{period}"))
}
pub fn volume_sma(period: usize) -> Self {
Self::new(format!("volume_sma_{period}"))
}
pub fn obv() -> Self {
Self::new("obv")
}
pub fn pivot_points() -> Self {
Self::new("pivot_points")
}
pub fn adx(period: usize) -> Self {
Self::new(format!("adx{period}"))
}
pub fn wma(period: usize) -> Self {
Self::new(format!("wma{period}"))
}
pub fn dema(period: usize) -> Self {
Self::new(format!("dema{period}"))
}
pub fn tema(period: usize) -> Self {
Self::new(format!("tema{period}"))
}
pub fn cci(period: usize) -> Self {
Self::new(format!("cci{period}"))
}
pub fn williams_r(period: usize) -> Self {
Self::new(format!("williams_r{period}"))
}
pub fn roc(period: usize) -> Self {
Self::new(format!("roc{period}"))
}
pub fn stddev(period: usize) -> Self {
Self::new(format!("stddev{period}"))
}
pub fn crosses_above(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::CrossesAbove,
CompareTarget::Value(value),
))
}
pub fn crosses_above_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::CrossesAbove,
CompareTarget::Indicator(other.name),
))
}
pub fn crosses_below(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::CrossesBelow,
CompareTarget::Value(value),
))
}
pub fn crosses_below_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::CrossesBelow,
CompareTarget::Indicator(other.name),
))
}
pub fn is_above(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsAbove,
CompareTarget::Value(value),
))
}
pub fn is_above_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsAbove,
CompareTarget::Indicator(other.name),
))
}
pub fn is_below(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsBelow,
CompareTarget::Value(value),
))
}
pub fn is_below_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsBelow,
CompareTarget::Indicator(other.name),
))
}
pub fn equals(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::Equals,
CompareTarget::Value(value),
))
}
pub fn equals_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::Equals,
CompareTarget::Indicator(other.name),
))
}
pub fn is_between(self, lower: f64, upper: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsBetween,
CompareTarget::Range(lower, upper),
))
}
pub fn is_rising(self, bars: u32) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsRising(bars),
CompareTarget::None,
))
}
pub fn is_falling(self, bars: u32) -> ConditionNode {
ConditionNode::Condition(Condition::new(
self.name,
Operator::IsFalling(bars),
CompareTarget::None,
))
}
pub fn scaled(self, multiplier: f64) -> ScaledIndicatorRef {
ScaledIndicatorRef {
name: self.name,
multiplier,
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct ScaledIndicatorRef {
pub name: String,
pub multiplier: f64,
}
impl ScaledIndicatorRef {
pub fn is_above_value(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
format!("{}*{}", self.name, self.multiplier),
Operator::IsAbove,
CompareTarget::Value(value),
))
}
pub fn is_above_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
format!("{}*{}", self.name, self.multiplier),
Operator::IsAbove,
CompareTarget::Indicator(other.name),
))
}
pub fn is_below_value(self, value: f64) -> ConditionNode {
ConditionNode::Condition(Condition::new(
format!("{}*{}", self.name, self.multiplier),
Operator::IsBelow,
CompareTarget::Value(value),
))
}
pub fn is_below_indicator(self, other: IndicatorRef) -> ConditionNode {
ConditionNode::Condition(Condition::new(
format!("{}*{}", self.name, self.multiplier),
Operator::IsBelow,
CompareTarget::Indicator(other.name),
))
}
}
pub fn all_of(conditions: Vec<ConditionNode>) -> ConditionNode {
ConditionNode::Group(ConditionGroup::AllOf(conditions))
}
pub fn any_of(conditions: Vec<ConditionNode>) -> ConditionNode {
ConditionNode::Group(ConditionGroup::AnyOf(conditions))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn indicator_ref_convenience_constructors() {
let sma = IndicatorRef::sma(20);
assert_eq!(sma.name, "sma20");
let ema = IndicatorRef::ema(14);
assert_eq!(ema.name, "ema14");
let rsi = IndicatorRef::rsi(14);
assert_eq!(rsi.name, "rsi14");
let obv = IndicatorRef::obv();
assert_eq!(obv.name, "obv");
}
#[test]
fn batch_a_indicator_ref_convenience_constructors() {
let adx = IndicatorRef::adx(14);
assert_eq!(adx.name, "adx14");
let wma = IndicatorRef::wma(20);
assert_eq!(wma.name, "wma20");
let dema = IndicatorRef::dema(10);
assert_eq!(dema.name, "dema10");
let tema = IndicatorRef::tema(10);
assert_eq!(tema.name, "tema10");
let cci = IndicatorRef::cci(20);
assert_eq!(cci.name, "cci20");
let williams_r = IndicatorRef::williams_r(14);
assert_eq!(williams_r.name, "williams_r14");
let roc = IndicatorRef::roc(12);
assert_eq!(roc.name, "roc12");
let stddev = IndicatorRef::stddev(20);
assert_eq!(stddev.name, "stddev20");
}
#[test]
fn condition_building() {
let sma = IndicatorRef::sma(20);
let cond = sma.crosses_above(100.0);
assert!(matches!(cond, ConditionNode::Condition(_)));
}
#[test]
fn condition_grouping() {
let sma = IndicatorRef::sma(20);
let rsi = IndicatorRef::rsi(14);
let cond1 = sma.is_above(100.0);
let cond2 = rsi.is_below(70.0);
let group = all_of(vec![cond1, cond2]);
assert!(matches!(
group,
ConditionNode::Group(ConditionGroup::AllOf(_))
));
}
#[test]
fn scaled_indicator_ref() {
let atr = IndicatorRef::atr(14);
let scaled = atr.scaled(2.0);
assert_eq!(scaled.multiplier, 2.0);
}
#[test]
fn scaled_is_above_indicator_has_correct_semantics() {
let cond = IndicatorRef::atr(14)
.scaled(2.0)
.is_above_indicator(IndicatorRef::new("price"));
match cond {
ConditionNode::Condition(c) => {
assert_eq!(c.left, "atr14*2");
assert_eq!(c.operator, Operator::IsAbove);
assert_eq!(c.right, CompareTarget::Indicator("price".to_string()));
}
_ => panic!("expected Condition"),
}
}
#[test]
fn scaled_is_below_indicator_has_correct_semantics() {
let cond = IndicatorRef::atr(14)
.scaled(1.5)
.is_below_indicator(IndicatorRef::new("price"));
match cond {
ConditionNode::Condition(c) => {
assert_eq!(c.left, "atr14*1.5");
assert_eq!(c.operator, Operator::IsBelow);
assert_eq!(c.right, CompareTarget::Indicator("price".to_string()));
}
_ => panic!("expected Condition"),
}
}
#[test]
fn scaled_is_above_value_has_correct_semantics() {
let cond = IndicatorRef::atr(14).scaled(2.0).is_above_value(50.0);
match cond {
ConditionNode::Condition(c) => {
assert_eq!(c.left, "atr14*2");
assert_eq!(c.operator, Operator::IsAbove);
assert_eq!(c.right, CompareTarget::Value(50.0));
}
_ => panic!("expected Condition"),
}
}
}