Skip to main content

Condition

Trait Condition 

Source
pub trait Condition:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn evaluate(&self, ctx: &StrategyContext<'_>) -> bool;
    fn required_indicators(&self) -> Vec<(String, Indicator)>;
    fn description(&self) -> String;

    // Provided methods
    fn and<C: Condition>(self, other: C) -> And<Self, C>
       where Self: Sized { ... }
    fn or<C: Condition>(self, other: C) -> Or<Self, C>
       where Self: Sized { ... }
    fn not(self) -> Not<Self>
       where Self: Sized { ... }
}
Expand description

A condition that can be evaluated on each candle.

Conditions are the building blocks of trading strategies. They can be combined using and(), or(), and not() operations.

§Example

use finance_query::backtesting::condition::Condition;

fn my_custom_condition(ctx: &StrategyContext) -> bool {
    // Custom logic here
    true
}

Required Methods§

Source

fn evaluate(&self, ctx: &StrategyContext<'_>) -> bool

Evaluate the condition with the current strategy context.

Returns true if the condition is met, false otherwise.

Source

fn required_indicators(&self) -> Vec<(String, Indicator)>

Get the indicators required by this condition.

The backtest engine will pre-compute these indicators before running the strategy.

Source

fn description(&self) -> String

Get a human-readable description of this condition.

This is used for logging, debugging, and signal reporting.

Provided Methods§

Source

fn and<C: Condition>(self, other: C) -> And<Self, C>
where Self: Sized,

Combine this condition with another using AND logic.

The resulting condition is true only when both conditions are true.

§Example
let entry = rsi(14).below(30.0).and(price().above_ref(sma(200)));
Source

fn or<C: Condition>(self, other: C) -> Or<Self, C>
where Self: Sized,

Combine this condition with another using OR logic.

The resulting condition is true when either condition is true.

§Example
let exit = rsi(14).above(70.0).or(stop_loss(0.05));
Source

fn not(self) -> Not<Self>
where Self: Sized,

Negate this condition.

The resulting condition is true when this condition is false.

§Example
let not_overbought = rsi(14).above(70.0).not();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§