Skip to main content

Module condition

Module condition 

Source
Expand description

Condition system for building strategy entry/exit rules.

This module provides a composable way to define trading conditions using indicator references and comparison operations.

§Example

use finance_query::backtesting::refs::*;
use finance_query::backtesting::condition::*;

// Simple condition
let oversold = rsi(14).below(30.0);

// Compound conditions
let entry = rsi(14).crosses_below(30.0)
    .and(price().above_ref(sma(200)));

let exit = rsi(14).crosses_above(70.0)
    .or(stop_loss(0.05));

Structs§

Above
Condition: indicator is above a threshold.
AboveRef
Condition: indicator is above another indicator.
All
A condition that evaluates to true when ALL inner conditions are true.
And
Condition: both conditions must be true (AND logic).
Any
A condition that evaluates to true when ANY inner condition is true.
Below
Condition: indicator is below a threshold.
BelowRef
Condition: indicator is below another indicator.
Between
Condition: indicator is between two thresholds.
ConditionBuilder
Builder for creating complex multi-condition combinations.
ConstantCondition
A condition that always returns the same value.
CrossesAbove
Condition: indicator crosses above a threshold.
CrossesAboveRef
Condition: indicator crosses above another indicator.
CrossesBelow
Condition: indicator crosses below a threshold.
CrossesBelowRef
Condition: indicator crosses below another indicator.
Equals
Condition: indicator equals a value (within tolerance).
HasPosition
Condition: check if we have any position.
HeldForBars
Condition: position has been held for at least N bars.
InLoss
Condition: position P/L is negative (in loss).
InProfit
Condition: position P/L is positive (in profit).
IsLong
Condition: check if we have a long position.
IsShort
Condition: check if we have a short position.
NoPosition
Condition: check if we have no position.
Not
Condition: negation of a condition (NOT logic).
Or
Condition: at least one condition must be true (OR logic).
StopLoss
Condition: position P/L is at or below the stop-loss threshold.
TakeProfit
Condition: position P/L is at or above the take-profit threshold.
TrailingStop
Condition: trailing stop triggered when price retraces from peak/trough.
TrailingTakeProfit
Condition: trailing take-profit triggered when profit retraces from peak.

Traits§

Condition
A condition that can be evaluated on each candle.

Functions§

always_false
Convenience function to create a condition that always returns false.
always_true
Convenience function to create a condition that always returns true.
has_position
Create a condition that checks if we have any position.
held_for_bars
Create a condition that checks if position has been held for at least N bars.
in_loss
Create a condition that checks if position is at a loss.
in_profit
Create a condition that checks if position is profitable.
is_long
Create a condition that checks if we have a long position.
is_short
Create a condition that checks if we have a short position.
no_position
Create a condition that checks if we have no position.
stop_loss
Create a stop-loss condition.
take_profit
Create a take-profit condition.
trailing_stop
Create a trailing stop condition.
trailing_take_profit
Create a trailing take-profit condition.