use std::borrow::Cow;
use chrono::{DateTime, Utc};
use chrono_tz::UTC;
use chrono_tz::{America, Asia, Europe, Tz, US};
use serde::{Deserialize, Serialize};
use super::profiles::{
CBOT_EXT_POST2013, CBOT_REGULAR_POST2013, CFE_EXTENDED, CFE_REGULAR, CME_EXT_POST2016,
CME_REGULAR, EUREX_ASIAN, EUREX_REGULAR, ICE_WRAP_20_18_EXT, MAINT_17_16_EXT, SGX_EXTENDED,
SGX_REGULAR,
};
use super::rule::ALL_DAYS;
use super::{Exchange, MarketHours, SessionRule};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FuturesSessionProfile {
pub tz: Tz,
pub regular: &'static [SessionRule],
pub extended: &'static [SessionRule],
pub has_daily_close: bool,
pub has_weekend_close: bool,
}
impl FuturesSessionProfile {
#[must_use]
pub fn is_open(&self, t: DateTime<Utc>) -> bool {
self.to_market_hours(Exchange::Unknown).is_open(t)
}
#[must_use]
pub fn to_market_hours(self, exchange: Exchange) -> MarketHours {
MarketHours {
exchange,
tz: self.tz,
regular: Cow::Borrowed(self.regular),
extended: Cow::Borrowed(self.extended),
has_daily_close: self.has_daily_close,
has_weekend_close: self.has_weekend_close,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MarketHoursKey {
GlobexEquityIndex,
GlobexEnergy,
GlobexGrains,
GlobexFx,
CfeVix,
Eurex,
IceUs,
Sgx,
AlwaysOpen,
}
static ALWAYS_OPEN_RULE: &[SessionRule] = &[SessionRule {
days: ALL_DAYS,
open_ssm: 0,
close_ssm: 24 * 3600,
}];
static FUTURES_GLOBEX_EQUITY_INDEX: FuturesSessionProfile = FuturesSessionProfile {
tz: US::Central,
regular: CME_REGULAR,
extended: CME_EXT_POST2016,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_GLOBEX_ENERGY: FuturesSessionProfile = FuturesSessionProfile {
tz: US::Central,
regular: &[],
extended: MAINT_17_16_EXT,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_GLOBEX_GRAINS: FuturesSessionProfile = FuturesSessionProfile {
tz: US::Central,
regular: CBOT_REGULAR_POST2013,
extended: CBOT_EXT_POST2013,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_CFE_VIX: FuturesSessionProfile = FuturesSessionProfile {
tz: US::Central,
regular: CFE_REGULAR,
extended: CFE_EXTENDED,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_EUREX: FuturesSessionProfile = FuturesSessionProfile {
tz: Europe::Berlin,
regular: EUREX_REGULAR,
extended: EUREX_ASIAN,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_ICE_US: FuturesSessionProfile = FuturesSessionProfile {
tz: America::New_York,
regular: &[],
extended: ICE_WRAP_20_18_EXT,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_SGX: FuturesSessionProfile = FuturesSessionProfile {
tz: Asia::Singapore,
regular: SGX_REGULAR,
extended: SGX_EXTENDED,
has_daily_close: true,
has_weekend_close: true,
};
static FUTURES_ALWAYS_OPEN: FuturesSessionProfile = FuturesSessionProfile {
tz: UTC,
regular: ALWAYS_OPEN_RULE,
extended: &[],
has_daily_close: false,
has_weekend_close: false,
};
#[must_use]
pub fn session_profile(key: MarketHoursKey) -> &'static FuturesSessionProfile {
match key {
MarketHoursKey::GlobexEquityIndex => &FUTURES_GLOBEX_EQUITY_INDEX,
MarketHoursKey::GlobexEnergy | MarketHoursKey::GlobexFx => &FUTURES_GLOBEX_ENERGY,
MarketHoursKey::GlobexGrains => &FUTURES_GLOBEX_GRAINS,
MarketHoursKey::CfeVix => &FUTURES_CFE_VIX,
MarketHoursKey::Eurex => &FUTURES_EUREX,
MarketHoursKey::IceUs => &FUTURES_ICE_US,
MarketHoursKey::Sgx => &FUTURES_SGX,
MarketHoursKey::AlwaysOpen => &FUTURES_ALWAYS_OPEN,
}
}
#[must_use]
pub fn hours_for_market_hours_key(key: MarketHoursKey) -> MarketHours {
session_profile(key).to_market_hours(Exchange::Unknown)
}