#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BarField {
Open,
High,
Low,
Close,
Volume,
HlMid,
Hlc3,
Ohlc4,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AggregateOp {
Highest,
Lowest,
Mean,
Sum,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DerivedOp {
Prev,
Slope,
PctChange,
ZScore { n: usize },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArithmeticOp {
Add,
Sub,
Mul,
Div,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Operand {
IndicatorValue {
role_idx: usize,
},
BarField(BarField),
Aggregate {
field: BarField,
op: AggregateOp,
n: usize,
},
Lookback {
role_idx: usize,
n: usize,
},
Derived {
role_idx: usize,
op: DerivedOp,
},
Constant(f64),
Zero,
Arithmetic {
op: ArithmeticOp,
left: Box<Operand>,
right: Box<Operand>,
},
}
impl Operand {
pub fn is_constant(&self) -> bool {
matches!(self, Operand::Constant(_) | Operand::Zero)
}
pub fn is_indicator(&self) -> bool {
matches!(
self,
Operand::IndicatorValue { .. } | Operand::Lookback { .. } | Operand::Derived { .. }
)
}
pub fn is_bar_field(&self) -> bool {
matches!(self, Operand::BarField(_) | Operand::Aggregate { .. })
}
pub fn add(left: Operand, right: Operand) -> Self {
Self::Arithmetic { op: ArithmeticOp::Add, left: Box::new(left), right: Box::new(right) }
}
pub fn mul(left: Operand, right: Operand) -> Self {
Self::Arithmetic { op: ArithmeticOp::Mul, left: Box::new(left), right: Box::new(right) }
}
}