Skip to main content

SessionAwareness

Struct SessionAwareness 

Source
pub struct SessionAwareness { /* private fields */ }
Expand description

Determines trading status for a market session.

Implementations§

Source§

impl SessionAwareness

Source

pub fn new(session: MarketSession) -> Self

Create a session classifier for the given market.

Source

pub fn is_weekend(utc_ms: u64) -> bool

Returns true if utc_ms falls on a Saturday or Sunday (UTC).

Session-agnostic: always checks the UTC calendar day regardless of the configured MarketSession. Useful for skip-weekend logic in backtesting loops and scheduling.

Source

pub fn status(&self, utc_ms: u64) -> Result<TradingStatus, StreamError>

Classify a UTC timestamp (ms) into a trading status.

Source

pub fn session(&self) -> MarketSession

The market session this classifier was constructed for.

Source

pub fn next_open_ms(&self, utc_ms: u64) -> u64

Return the UTC millisecond timestamp of the next time this session enters TradingStatus::Open status.

If the session is already Open at utc_ms, returns utc_ms unchanged. For MarketSession::Crypto this always returns utc_ms.

Source

pub fn is_closed(&self, utc_ms: u64) -> bool

Returns true if the session is currently in TradingStatus::Closed status.

Shorthand for self.status(utc_ms) == Ok(TradingStatus::Closed). For MarketSession::Crypto this always returns false.

Source

pub fn is_extended(&self, utc_ms: u64) -> bool

Returns true if the session is currently in TradingStatus::Extended status.

For MarketSession::Crypto this always returns false (crypto is always Open). For equity, Extended covers pre-market (4:00–9:30 ET) and after-hours (16:00–20:00 ET).

Source

pub fn is_open(&self, utc_ms: u64) -> bool

Returns true if the session is currently in TradingStatus::Open status.

Shorthand for self.status(utc_ms).map(|s| s == TradingStatus::Open).unwrap_or(false). For MarketSession::Crypto this always returns true.

Source

pub fn is_market_hours(&self, utc_ms: u64) -> bool

Returns true if the session is currently tradeable: either TradingStatus::Open or TradingStatus::Extended.

For MarketSession::Crypto this always returns true. For equity, returns true during both regular hours and extended (pre/after-market) hours.

Source

pub fn minutes_until_open(&self, utc_ms: u64) -> u64

Returns the number of whole minutes until the next TradingStatus::Open transition.

Returns 0 if the session is already open. For MarketSession::Crypto always returns 0. Useful for scheduling reconnect timers and pre-open setup.

Source

pub fn time_until_open_ms(&self, utc_ms: u64) -> u64

Milliseconds until the next TradingStatus::Open transition.

Returns 0 if the session is already Open. For MarketSession::Crypto always returns 0.

Source

pub fn seconds_until_open(&self, utc_ms: u64) -> f64

Seconds until the session next opens as f64.

Returns 0.0 if the session is already open.

Source

pub fn minutes_until_close(&self, utc_ms: u64) -> u64

Whole minutes until the session enters TradingStatus::Closed.

Returns 0 if the session is already closed or will close in less than one minute. For MarketSession::Crypto returns u64::MAX (never closes). Useful for scheduling pre-close warnings or cooldown timers.

Source

pub fn time_until_close_ms(&self, utc_ms: u64) -> u64

Milliseconds until the session enters TradingStatus::Closed.

Returns 0 if the session is already Closed. For MarketSession::Crypto returns u64::MAX (never closes).

Source

pub fn bars_until_open(&self, utc_ms: u64, bar_duration_ms: u64) -> u64

Returns the number of complete bars of bar_duration_ms that fit before the next open.

Returns 0 if the session is already open or bar_duration_ms == 0. Useful for scheduling reconnects or pre-open setup tasks.

Source

pub fn is_pre_market(&self, utc_ms: u64) -> bool

Returns true if the equity session is currently in the pre-market window (4:00–9:30 ET).

Always returns false for non-equity sessions. Pre-market is a subset of TradingStatus::Extended; this method distinguishes it from after-hours.

Source

pub fn is_after_hours(&self, utc_ms: u64) -> bool

Returns true if the equity session is in the after-hours window (16:00–20:00 ET).

Always returns false for non-equity sessions. After-hours is a subset of TradingStatus::Extended; this method distinguishes it from pre-market.

Source

pub fn is_extended_hours(&self, utc_ms: u64) -> bool

Returns true if the current time is in extended hours (pre-market or after-hours) for UsEquity. Always false for other sessions.

Source

pub fn is_active(&self, utc_ms: u64) -> bool

Returns true if the session is active: either Open or Extended (not Closed).

Source

pub fn next_close_ms(&self, utc_ms: u64) -> u64

Return the UTC millisecond timestamp of the next time this session enters TradingStatus::Closed status.

If the session is already Closed, returns utc_ms unchanged. For MarketSession::Crypto, which never closes, returns u64::MAX.

Source

pub fn session_label(&self, utc_ms: u64) -> &'static str

Returns a human-readable label for the current session status.

For UsEquity: "open", "pre-market", "after-hours", or "closed". For Crypto: always "open". For Forex: "open" or "closed".

Source

pub fn is_liquid(&self, utc_ms: u64) -> bool

Returns true only during regular (non-extended) open hours.

For UsEquity: true when 9:30–16:00 ET on a trading day. For Crypto: always true. For Forex: same as is_open.

Source

pub fn session_progress(&self, utc_ms: u64) -> Option<f64>

Fraction [0.0, 1.0] of the current trading session elapsed at utc_ms.

Returns None when:

Returns 0.0 at the exact session open and 1.0 at the session close. Values are clamped to [0.0, 1.0].

Source

pub fn time_in_session_ms(&self, utc_ms: u64) -> Option<u64>

Milliseconds elapsed since the current session opened.

Returns None if the session is not currently TradingStatus::Open or if the session never closes (e.g. MarketSession::Crypto).

At session open this returns 0; this is the absolute counterpart to the fractional session_progress.

Source

pub fn remaining_session_ms(&self, utc_ms: u64) -> Option<u64>

Milliseconds remaining until the current session closes.

Returns None if the session is not currently TradingStatus::Open or if the session never closes (e.g. MarketSession::Crypto).

This is the complement of time_in_session_ms: remaining + elapsed == session_duration_ms.

Source

pub fn fraction_of_day_elapsed(&self, utc_ms: u64) -> f64

UTC fraction of the 24-hour day elapsed at utc_ms.

Returns a value in [0.0, 1.0) representing how far through the calendar day the timestamp is: 0.0 at midnight UTC, approaching 1.0 just before the next midnight. Independent of session status.

Source

pub fn minutes_since_open(&self, utc_ms: u64) -> u64

Minutes elapsed since the current session opened.

Returns 0 when the market is closed or for sessions without a defined open time (Crypto). Rounds down.

Source

pub fn remaining_until_close_ms(&self, utc_ms: u64) -> Option<u64>

Milliseconds remaining until the session closes, or None when the session is not currently in regular trading hours.

Unlike time_until_close_ms (which returns u64::MAX for always-open sessions and 0 when closed), this returns None for both the closed and always-open cases.

Source

pub fn is_pre_open(&self, utc_ms: u64) -> bool

👎Deprecated since 2.2.0:

Use is_pre_market instead

Returns true if the session is currently in the pre-open (pre-market) window — extended hours that precede the regular trading session.

Alias for is_pre_market. Always false for non-equity sessions.

Source

pub fn day_fraction_remaining(&self, utc_ms: u64) -> f64

Fraction of the 24-hour UTC day remaining at utc_ms.

Returns a value in (0.0, 1.0]1.0 exactly at midnight UTC, approaching 0.0 just before the next midnight. The complement of fraction_of_day_elapsed.

Source

pub fn is_regular_session(&self, utc_ms: u64) -> bool

Returns true if the market is in regular trading hours only (TradingStatus::Open), not extended hours or closed.

Source

pub fn is_last_trading_hour(&self, utc_ms: u64) -> bool

Returns true if the current time is within the final 60 minutes of the regular session.

Source

pub fn is_near_close(&self, utc_ms: u64, margin_ms: u64) -> bool

Returns true if the session is open and utc_ms is within margin_ms of the end of the regular session.

Returns false when outside the regular session (closed, extended, etc.). Uses the same remaining-time calculation as remaining_session_ms.

Source

pub fn open_duration_ms(&self) -> u64

Duration of the regular (non-extended) session in milliseconds.

For UsEquity this is 6.5 hours (23,400,000 ms); for Crypto it returns u64::MAX (always open); for Forex it returns the standard weekly duration (120 hours = 432,000,000 ms).

Source

pub fn is_opening_range(&self, utc_ms: u64) -> bool

Returns true if within the first 30 minutes of the regular session.

The opening range is a commonly watched period for establishing the day’s initial price range. Returns false outside the session.

Source

pub fn is_mid_session(&self, utc_ms: u64) -> bool

Returns true if the session is between 25 % and 75 % complete.

Useful for identifying the “mid-session” consolidation period. Returns false outside the session or when session progress is unavailable (e.g. Crypto).

Source

pub fn is_first_half(&self, utc_ms: u64) -> bool

Returns true if the session is in the first half (< 50%) of its duration.

Returns false outside the session.

Source

pub fn session_half(&self, utc_ms: u64) -> u8

Returns which half of the trading session we are in: 1 for the first half, 2 for the second half. Returns 0 if outside the session.

Source

pub fn overlaps_with(&self, other: &SessionAwareness, utc_ms: u64) -> bool

Returns true if both self and other are simultaneously open at utc_ms.

Useful for detecting session overlaps like the London/New York overlap (13:00–17:00 UTC).

Source

pub fn open_ms(&self, utc_ms: u64) -> u64

Milliseconds elapsed since the session opened at utc_ms.

Returns 0 if the session is not open.

Source

pub fn progress_pct(&self, utc_ms: u64) -> f64

Session progress as a percentage [0.0, 100.0].

Returns 0.0 outside the session.

Source

pub fn remaining_ms(&self, utc_ms: u64) -> u64

Milliseconds remaining until the session closes at utc_ms.

Returns 0 if the session is not open or already past close.

Source

pub fn is_first_quarter(&self, utc_ms: u64) -> bool

Returns true if the session is in the first 25% of its duration.

Returns false outside the session.

Source

pub fn is_last_quarter(&self, utc_ms: u64) -> bool

Returns true if the session is in the last 25% of its duration.

Returns false outside the session.

Source

pub fn minutes_elapsed(&self, utc_ms: u64) -> f64

Minutes elapsed since the session opened.

Returns 0.0 if the session is not open.

Source

pub fn is_power_hour(&self, utc_ms: u64) -> bool

Returns true if within the last 60 minutes of the regular session (the “power hour”).

Returns false outside the session.

Source

pub fn is_overnight(&self, utc_ms: u64) -> bool

Returns true if the session is overnight: the market is closed (or extended-hours-only) and the current UTC time falls between 20:00 ET (after-hours end) and 04:00 ET (pre-market start) on a weekday.

Always false for Crypto (never closes) and Forex (uses its own schedule).

Source

pub fn minutes_to_next_open(&self, utc_ms: u64) -> f64

Minutes until the next regular session open, as f64.

Returns 0.0 when the session is already open. Uses time_until_open_ms for the underlying calculation.

Source

pub fn session_progress_pct(&self, utc_ms: u64) -> f64

👎Deprecated since 2.2.0:

Use progress_pct instead

How far through the current session as a percentage (0.0–100.0).

Alias for progress_pct. Returns 0.0 if the session is not currently open.

Source

pub fn is_last_minute(&self, utc_ms: u64) -> bool

Returns true if the session is open and within the last 60 seconds of the regular trading session.

Source

pub fn week_of_month(date: NaiveDate) -> u32

Returns the week of the month (1–5) for date.

Week 1 contains day 1, week 2 contains day 8, etc.

Source

pub fn day_of_week_name(date: NaiveDate) -> &'static str

Returns the weekday name of date as a static string.

Returns one of "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", or "Sunday".

Source

pub fn is_expiry_week(date: NaiveDate) -> bool

Returns true if date falls in the final week of the month (day ≥ 22).

Weekly equity options expire on Fridays; monthly contracts expire the third Friday. The final calendar week of the month covers both cases and is often associated with elevated volatility and rebalancing.

Source

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

Human-readable name of the configured market session.

Source

pub fn is_earnings_season(date: NaiveDate) -> bool

Returns true if date falls in a typical US earnings season month (January, April, July, or October).

These months see heavy corporate earnings reports, increasing market volatility.

Source

pub fn is_lunch_hour(&self, utc_ms: u64) -> bool

Returns true if utc_ms falls within the midday 12:00–13:00 ET quiet period (3 hours into the 6.5-hour US equity session).

Always returns false for non-US-Equity sessions.

Source

pub fn is_triple_witching(date: NaiveDate) -> bool

Returns true if date is a triple witching day (third Friday of March, June, September, or December) — the quarterly expiration of index futures, index options, and single-stock options.

Source

pub fn trading_days_elapsed(from: NaiveDate, to: NaiveDate) -> u32

Count of weekdays (Mon–Fri) between from and to, inclusive.

Returns 0 if from > to. Does not account for US market holidays.

Source

pub fn fraction_remaining(&self, utc_ms: u64) -> Option<f64>

Fraction of the session remaining: 1.0 - session_progress.

Returns None if the session is not open.

Source

pub fn minutes_since_close(&self, utc_ms: u64) -> f64

Minutes elapsed since the most recent session close.

Returns 0.0 if the session is currently open or the last close time cannot be determined (e.g., before any session has ever closed).

Source

pub fn is_opening_bell_minute(&self, utc_ms: u64) -> bool

Returns true if utc_ms falls within the first 60 seconds of the regular trading session (the “opening bell” minute).

Source

pub fn is_closing_bell_minute(&self, utc_ms: u64) -> bool

Returns true if utc_ms falls within the final 60 seconds of the session.

Always returns false for non-US-Equity sessions or when the session is not open.

Source

pub fn is_market_holiday_adjacent(date: NaiveDate) -> bool

Returns true if date is the day immediately before or after a major US market holiday (Christmas, New Year’s Day, Independence Day, Thanksgiving Friday, or Labor Day). These adjacent days often see reduced liquidity.

Uses a fixed-rule approximation: does not account for observed holidays that shift when the holiday falls on a weekend.

Source

pub fn is_fomc_blackout_window(date: NaiveDate) -> bool

Returns true if date falls within an approximate FOMC blackout window.

The Fed’s blackout rule prohibits public commentary in the 10 calendar days before each FOMC decision. FOMC meetings are scheduled roughly 8 times per year; this heuristic marks days 18–31 of odd months (Jan, Mar, May, Jul, Sep, Nov) as blackout candidates — a conservative approximation.

Auto Trait Implementations§

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more