Skip to main content

Rule

Enum Rule 

Source
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.

Fields

§price: f64
§

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.

Fields

§distance: f64
§peak_price: f64
§initialized: bool
§

TakeProfit

Close close_ratio of the position when price reaches price. Fires at most once.

Fields

§price: f64
§close_ratio: f64
§triggered: bool
§

BreakevenWhen

When price reaches trigger_price, move the stoploss to the entry price (breakeven). Fires at most once.

Fields

§trigger_price: f64
§triggered: bool
§

BreakevenAfterTargets

After after_n take-profit hits, move the stoploss to entry. Fires at most once.

Fields

§after_n: u32
§triggered: bool
§

TimeExit

Close the position if it has been open longer than max_seconds.

Fields

§max_seconds: u64

Implementations§

Source§

impl Rule

Source

pub fn fixed_stoploss(price: f64) -> Self

Source

pub fn trailing_stop(distance: f64) -> Self

Source

pub fn take_profit(price: f64, close_ratio: f64) -> Self

Source

pub fn breakeven_when(trigger_price: f64) -> Self

Source

pub fn breakeven_after_targets(after_n: u32) -> Self

Source

pub fn time_exit(max_seconds: u64) -> Self

Source

pub fn from_config(config: RuleConfig) -> Self

Build a live Rule from a declarative RuleConfig.

Source§

impl Rule

Source

pub fn is_stateful(&self) -> bool

Whether this rule requires tick-by-tick evaluation (not indexable as a static alert).

Source

pub fn name(&self) -> &'static str

Human-readable name used for logging, record keeping, and RemoveRule.

Source

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.

Trait Implementations§

Source§

impl Clone for Rule

Source§

fn clone(&self) -> Rule

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Rule

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Rule

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Rule

§

impl RefUnwindSafe for Rule

§

impl Send for Rule

§

impl Sync for Rule

§

impl Unpin for Rule

§

impl UnsafeUnpin for Rule

§

impl UnwindSafe for Rule

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V