use super::operator::OperatorClass;
use super::operand::Operand;
use super::window::Window;
use super::composition::Guard;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventTrigger {
Above,
Below,
Either,
Bullish,
Bearish,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ZoneBounds {
pub lo: f64,
pub hi: f64,
}
impl ZoneBounds {
pub fn new(lo: f64, hi: f64) -> Self {
debug_assert!(lo < hi, "ZoneBounds: lo must be strictly less than hi");
Self { lo, hi }
}
}
#[derive(Debug, Clone)]
pub struct Event {
pub operator_class: OperatorClass,
pub left_operand: Option<Operand>,
pub right_operand: Option<Operand>,
pub zone_bounds: Option<ZoneBounds>,
pub pattern_id: Option<u32>,
pub window_n: Window,
pub direction: EventTrigger,
pub guards: Vec<Guard>,
}
impl Event {
pub fn cross(
left: Operand,
right: Operand,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::Cross,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
pub fn threshold(
left: Operand,
right: Operand,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::ThresholdCompare,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
pub fn zone_enter(left: Operand, bounds: ZoneBounds, guards: Vec<Guard>) -> Self {
Self {
operator_class: OperatorClass::ZoneEnter,
left_operand: Some(left),
right_operand: None,
zone_bounds: Some(bounds),
pattern_id: None,
window_n: Window::CurrentBar,
direction: EventTrigger::Either,
guards,
}
}
pub fn candle_pattern(pattern_id: u32, guards: Vec<Guard>) -> Self {
Self {
operator_class: OperatorClass::CandlePattern,
left_operand: None,
right_operand: None,
zone_bounds: None,
pattern_id: Some(pattern_id),
window_n: Window::CurrentBar,
direction: EventTrigger::Either,
guards,
}
}
pub fn zone_exit(left: Operand, bounds: ZoneBounds, guards: Vec<Guard>) -> Self {
Self {
operator_class: OperatorClass::ZoneExit,
left_operand: Some(left),
right_operand: None,
zone_bounds: Some(bounds),
pattern_id: None,
window_n: Window::CurrentBar,
direction: EventTrigger::Either,
guards,
}
}
pub fn nbar_extreme(
left: Operand,
n: usize,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::NBarExtreme,
left_operand: Some(left),
right_operand: None,
zone_bounds: None,
pattern_id: None,
window_n: Window::NBars(n),
direction,
guards,
}
}
pub fn pivot(
left: Operand,
l: usize,
r: usize,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::Pivot,
left_operand: Some(left),
right_operand: None,
zone_bounds: None,
pattern_id: None,
window_n: Window::PivotLR { l, r },
direction,
guards,
}
}
pub fn direction(
left: Operand,
right: Option<Operand>,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::Direction,
left_operand: Some(left),
right_operand: right,
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
pub fn divergence(
left: Operand,
right: Operand,
n: usize,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::Divergence,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::NBars(n),
direction,
guards,
}
}
pub fn regime_gate(
left: Operand,
right: Operand,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::RegimeGate,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
pub fn sequence(
arm: Operand,
fire: Operand,
n: usize,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::Sequence,
left_operand: Some(arm),
right_operand: Some(fire),
zone_bounds: None,
pattern_id: None,
window_n: Window::NBars(n),
direction: EventTrigger::Either,
guards,
}
}
pub fn volatility_regime(
left: Operand,
right: Operand,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::VolatilityRegime,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
pub fn volume_event(
left: Operand,
right: Operand,
direction: EventTrigger,
guards: Vec<Guard>,
) -> Self {
Self {
operator_class: OperatorClass::VolumeEvent,
left_operand: Some(left),
right_operand: Some(right),
zone_bounds: None,
pattern_id: None,
window_n: Window::CurrentBar,
direction,
guards,
}
}
}