pub struct SessionAwareness { /* private fields */ }Expand description
Determines trading status for a market session.
Implementations§
Source§impl SessionAwareness
impl SessionAwareness
Sourcepub fn new(session: MarketSession) -> Self
pub fn new(session: MarketSession) -> Self
Create a session classifier for the given market.
Sourcepub fn is_weekend(utc_ms: u64) -> bool
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.
Sourcepub fn status(&self, utc_ms: u64) -> Result<TradingStatus, StreamError>
pub fn status(&self, utc_ms: u64) -> Result<TradingStatus, StreamError>
Classify a UTC timestamp (ms) into a trading status.
Sourcepub fn session(&self) -> MarketSession
pub fn session(&self) -> MarketSession
The market session this classifier was constructed for.
Sourcepub fn next_open_ms(&self, utc_ms: u64) -> u64
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.
Sourcepub fn is_closed(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_extended(&self, utc_ms: u64) -> bool
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).
Sourcepub fn is_open(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_market_hours(&self, utc_ms: u64) -> bool
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.
Sourcepub fn minutes_until_open(&self, utc_ms: u64) -> u64
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.
Sourcepub fn time_until_open_ms(&self, utc_ms: u64) -> u64
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.
Sourcepub fn seconds_until_open(&self, utc_ms: u64) -> f64
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.
Sourcepub fn minutes_until_close(&self, utc_ms: u64) -> u64
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.
Sourcepub fn time_until_close_ms(&self, utc_ms: u64) -> u64
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).
Sourcepub fn bars_until_open(&self, utc_ms: u64, bar_duration_ms: u64) -> u64
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.
Sourcepub fn is_pre_market(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_after_hours(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_extended_hours(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_active(&self, utc_ms: u64) -> bool
pub fn is_active(&self, utc_ms: u64) -> bool
Returns true if the session is active: either Open or Extended (not Closed).
Sourcepub fn next_close_ms(&self, utc_ms: u64) -> u64
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.
Sourcepub fn session_label(&self, utc_ms: u64) -> &'static str
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".
Sourcepub fn is_liquid(&self, utc_ms: u64) -> bool
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.
Sourcepub fn session_progress(&self, utc_ms: u64) -> Option<f64>
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:
- The session is not currently
TradingStatus::Open. - The session never closes (e.g.
MarketSession::Crypto).
Returns 0.0 at the exact session open and 1.0 at the session close.
Values are clamped to [0.0, 1.0].
Sourcepub fn time_in_session_ms(&self, utc_ms: u64) -> Option<u64>
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.
Sourcepub fn remaining_session_ms(&self, utc_ms: u64) -> Option<u64>
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.
Sourcepub fn fraction_of_day_elapsed(&self, utc_ms: u64) -> f64
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.
Sourcepub fn minutes_since_open(&self, utc_ms: u64) -> u64
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.
Sourcepub fn remaining_until_close_ms(&self, utc_ms: u64) -> Option<u64>
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.
Sourcepub fn is_pre_open(&self, utc_ms: u64) -> bool
👎Deprecated since 2.2.0: Use is_pre_market instead
pub fn is_pre_open(&self, utc_ms: u64) -> bool
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.
Sourcepub fn day_fraction_remaining(&self, utc_ms: u64) -> f64
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.
Sourcepub fn is_regular_session(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_last_trading_hour(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_near_close(&self, utc_ms: u64, margin_ms: u64) -> bool
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.
Sourcepub fn open_duration_ms(&self) -> u64
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).
Sourcepub fn is_opening_range(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_mid_session(&self, utc_ms: u64) -> bool
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).
Sourcepub fn is_first_half(&self, utc_ms: u64) -> bool
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.
Sourcepub fn session_half(&self, utc_ms: u64) -> u8
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.
Sourcepub fn overlaps_with(&self, other: &SessionAwareness, utc_ms: u64) -> bool
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).
Sourcepub fn open_ms(&self, utc_ms: u64) -> u64
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.
Sourcepub fn progress_pct(&self, utc_ms: u64) -> f64
pub fn progress_pct(&self, utc_ms: u64) -> f64
Session progress as a percentage [0.0, 100.0].
Returns 0.0 outside the session.
Sourcepub fn remaining_ms(&self, utc_ms: u64) -> u64
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.
Sourcepub fn is_first_quarter(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_last_quarter(&self, utc_ms: u64) -> bool
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.
Sourcepub fn minutes_elapsed(&self, utc_ms: u64) -> f64
pub fn minutes_elapsed(&self, utc_ms: u64) -> f64
Minutes elapsed since the session opened.
Returns 0.0 if the session is not open.
Sourcepub fn is_power_hour(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_overnight(&self, utc_ms: u64) -> bool
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).
Sourcepub fn minutes_to_next_open(&self, utc_ms: u64) -> f64
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.
Sourcepub fn session_progress_pct(&self, utc_ms: u64) -> f64
👎Deprecated since 2.2.0: Use progress_pct instead
pub fn session_progress_pct(&self, utc_ms: u64) -> f64
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.
Sourcepub fn is_last_minute(&self, utc_ms: u64) -> bool
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.
Sourcepub fn week_of_month(date: NaiveDate) -> u32
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.
Sourcepub fn day_of_week_name(date: NaiveDate) -> &'static str
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".
Sourcepub fn is_expiry_week(date: NaiveDate) -> bool
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.
Sourcepub fn session_name(&self) -> &'static str
pub fn session_name(&self) -> &'static str
Human-readable name of the configured market session.
Sourcepub fn is_earnings_season(date: NaiveDate) -> bool
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.
Sourcepub fn is_lunch_hour(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_triple_witching(date: NaiveDate) -> bool
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.
Sourcepub fn trading_days_elapsed(from: NaiveDate, to: NaiveDate) -> u32
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.
Sourcepub fn fraction_remaining(&self, utc_ms: u64) -> Option<f64>
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.
Sourcepub fn minutes_since_close(&self, utc_ms: u64) -> f64
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).
Sourcepub fn is_opening_bell_minute(&self, utc_ms: u64) -> bool
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).
Sourcepub fn is_closing_bell_minute(&self, utc_ms: u64) -> bool
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.
Sourcepub fn is_market_holiday_adjacent(date: NaiveDate) -> bool
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.
Sourcepub fn is_fomc_blackout_window(date: NaiveDate) -> bool
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.