pub enum Rule {
FixedStoploss {
price: f64,
},
TrailingStop {
distance: f64,
peak_price: f64,
initialized: bool,
},
TakeProfit {
price: f64,
close_ratio: f64,
triggered: bool,
},
BreakevenWhen {
trigger_price: f64,
triggered: bool,
},
BreakevenAfterTargets {
after_n: u32,
triggered: bool,
},
TimeExit {
max_seconds: u64,
},
}Expand description
A composable management rule that is evaluated on every price update.
Rules are stored per-position and carry their own mutable state so that trigger-once semantics (e.g. breakeven) work correctly.
Variants§
FixedStoploss
Close the full remaining position when price crosses the stoploss level.
TrailingStop
Stoploss that follows the price at a fixed distance.
For a Buy position the stop trails below the highest bid seen; for a Sell position it trails above the lowest ask seen.
TakeProfit
Close close_ratio of the position when price reaches price.
Fires at most once.
BreakevenWhen
When price reaches trigger_price, move the stoploss to the entry
price (breakeven). Fires at most once.
BreakevenAfterTargets
After after_n take-profit hits, move the stoploss to entry.
Fires at most once.
TimeExit
Close the position if it has been open longer than max_seconds.
Implementations§
Source§impl Rule
impl Rule
pub fn fixed_stoploss(price: f64) -> Self
pub fn trailing_stop(distance: f64) -> Self
pub fn take_profit(price: f64, close_ratio: f64) -> Self
pub fn breakeven_when(trigger_price: f64) -> Self
pub fn breakeven_after_targets(after_n: u32) -> Self
pub fn time_exit(max_seconds: u64) -> Self
Sourcepub fn from_config(config: RuleConfig) -> Self
pub fn from_config(config: RuleConfig) -> Self
Build a live Rule from a declarative RuleConfig.
Source§impl Rule
impl Rule
Sourcepub fn is_stateful(&self) -> bool
pub fn is_stateful(&self) -> bool
Whether this rule requires tick-by-tick evaluation (not indexable as a static alert).
Sourcepub fn name(&self) -> &'static str
pub fn name(&self) -> &'static str
Human-readable name used for logging, record keeping, and RemoveRule.
Sourcepub fn evaluate(
&mut self,
data: &PositionView<'_>,
quote: &PriceQuote,
model: FillModel,
) -> Vec<Effect>
pub fn evaluate( &mut self, data: &PositionView<'_>, quote: &PriceQuote, model: FillModel, ) -> Vec<Effect>
Evaluate the rule against the current quote and position state.
Returns zero or more Effects. The engine is responsible for
applying these effects to the position afterwards.