Skip to main content

StrategyContext

Struct StrategyContext 

Source
pub struct StrategyContext<'a> { /* private fields */ }
Expand description

What a Strategy sees at a decision: the bars up to it and the open position. It places and cancels working orders by id.

An order placed with the id of a working order replaces it, keeping its original place among orders reached at the same point. Working orders stay until they fill or are cancelled. A quantity must be positive and finite and a price finite; an order with any other is ignored.

Implementations§

Source§

impl StrategyContext<'_>

Source

pub fn bars(&self) -> &[Bar]

Chart bars through this decision, oldest first. Live tick decisions include the forming bar; intrabar fill callbacks exclude the unfinished bar. See Self::is_forming_bar.

Source

pub fn position(&self) -> f64

The open position: positive when long, negative when short, 0 when flat.

Source

pub fn price(&self) -> f64

The latest close on a bar decision, or the execution price immediately after a fill.

Source

pub fn equity(&self) -> f64

Account equity marked at Self::price, after any fill commission.

Source

pub fn is_after_fill(&self) -> bool

Whether this is a recalculation immediately after a fill rather than a closed bar.

Source

pub fn is_forming_bar(&self) -> bool

Whether the current decision belongs to a still-forming live bar. Fill callbacks continue to exclude that bar from bars, even while this flag is true.

Source

pub fn entry( &mut self, id: impl Into<String>, side: OrderSide, quantity: impl Into<Option<f64>>, order_type: OrderType, )

Places an entry of quantity on side. Against an open position on the other side it closes that position first, then opens quantity on side; on the same side it adds. Pass None to use the settings’ order size, calculated when the order fills.

Source

pub fn exit( &mut self, id: impl Into<String>, quantity: Option<f64>, take_profit: Option<f64>, stop_loss: Option<f64>, )

Places an exit that reduces the open position by quantity, or closes it with None. It fills at the take_profit limit or the stop_loss stop, whichever the price reaches first, or at market without either. An exit only reduces the position open when it is placed or, placed while flat, the position the next entry opens; once that position is closed or reversed, the exit is cancelled.

Source

pub fn cancel(&mut self, id: &str)

Cancels the working order with this id, if any.

Source

pub fn exit_relative( &mut self, id: impl Into<String>, side: OrderSide, take_profit: f64, stop_loss: f64, )

Brackets entries on side at positive price distances from their average fill price. The bracket can precede its entry and protects it on its execution bar, including gaps and slippage. Adding an entry moves the bracket with the position’s average price. It closes the whole position, then protects any queued entries on that side. Without a queued entry it is cancelled when the position closes or reverses.

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for StrategyContext<'a>

§

impl<'a> Freeze for StrategyContext<'a>

§

impl<'a> RefUnwindSafe for StrategyContext<'a>

§

impl<'a> Send for StrategyContext<'a>

§

impl<'a> Sync for StrategyContext<'a>

§

impl<'a> Unpin for StrategyContext<'a>

§

impl<'a> UnsafeUnpin for StrategyContext<'a>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeSend for T
where T: Send + Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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.