#[non_exhaustive]pub struct StrategyContext<'a> {
pub candles: &'a [Candle],
pub index: usize,
pub position: Option<&'a Position>,
pub equity: f64,
pub indicators: &'a HashMap<String, Vec<Option<f64>>>,
pub extremes: Option<&'a PositionExtremes>,
pub indicator_index: Option<usize>,
}Expand description
Context passed to strategy on each candle.
Provides access to historical data, current position, and pre-computed indicators.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.candles: &'a [Candle]All candles up to and including current
index: usizeCurrent candle index (0-based)
position: Option<&'a Position>Current position (if any)
equity: f64Current portfolio equity
indicators: &'a HashMap<String, Vec<Option<f64>>>Pre-computed indicator values (keyed by indicator name)
extremes: Option<&'a PositionExtremes>Price extremes since the open position was entered.
None when no position is open, when no condition in the strategy
reads it, or when the context was built outside the engine’s bar loop —
conditions that need it fall back to scanning from the entry bar.
indicator_index: Option<usize>Index override for indicator /
indicator_prev lookups; None means index.
htf() keeps candles in base-bar index
space while its indicator map holds a per-bar [prev, curr] pair, so
indicator lookups address slot 1 while candle lookups keep index.
Implementations§
Source§impl<'a> StrategyContext<'a>
impl<'a> StrategyContext<'a>
Sourcepub fn current_candle(&self) -> &Candle
pub fn current_candle(&self) -> &Candle
Get current candle
Sourcepub fn previous_candle(&self) -> Option<&Candle>
pub fn previous_candle(&self) -> Option<&Candle>
Get previous candle (None if at start)
Sourcepub fn candle_at(&self, index: usize) -> Option<&Candle>
pub fn candle_at(&self, index: usize) -> Option<&Candle>
Get candle at specific index (None if out of bounds)
Sourcepub fn indicator_at(&self, name: &str, index: usize) -> Option<f64>
pub fn indicator_at(&self, name: &str, index: usize) -> Option<f64>
Get indicator value at specific index
Sourcepub fn indicator_prev(&self, name: &str) -> Option<f64>
pub fn indicator_prev(&self, name: &str) -> Option<f64>
Get indicator value at previous index
Sourcepub fn has_position(&self) -> bool
pub fn has_position(&self) -> bool
Check if we have a position
Sourcepub fn signal_long(&self) -> Signal
pub fn signal_long(&self) -> Signal
Create a Long signal from the current candle’s timestamp and close price.
Sourcepub fn signal_short(&self) -> Signal
pub fn signal_short(&self) -> Signal
Create a Short signal from the current candle’s timestamp and close price.
Sourcepub fn signal_exit(&self) -> Signal
pub fn signal_exit(&self) -> Signal
Create an Exit signal from the current candle’s timestamp and close price.
Sourcepub fn crossed_above(&self, fast_name: &str, slow_name: &str) -> bool
pub fn crossed_above(&self, fast_name: &str, slow_name: &str) -> bool
Check if crossover occurred (fast crosses above slow)
Sourcepub fn crossed_below(&self, fast_name: &str, slow_name: &str) -> bool
pub fn crossed_below(&self, fast_name: &str, slow_name: &str) -> bool
Check if crossover occurred (fast crosses below slow)
Sourcepub fn indicator_crossed_above(&self, name: &str, threshold: f64) -> bool
pub fn indicator_crossed_above(&self, name: &str, threshold: f64) -> bool
Check if indicator crossed above a threshold.
Returns true when prev <= threshold and current > threshold.
The inclusive lower bound (<=) means a signal fires even when the
previous bar sat exactly on the threshold, the same inclusive-previous
convention crossed_above uses for
indicator-vs-indicator crossings.
Sourcepub fn indicator_crossed_below(&self, name: &str, threshold: f64) -> bool
pub fn indicator_crossed_below(&self, name: &str, threshold: f64) -> bool
Check if indicator crossed below a threshold.
Returns true when prev >= threshold and current < threshold.
See indicator_crossed_above for the
rationale behind the inclusive/exclusive choice on each side.
Auto Trait Implementations§
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>
impl<'a> UnwindSafe for StrategyContext<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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