"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(live): close the us_equity session at the early bell on half-days
Yufeng Hed4c159808fe7
diff --git a/agent/src/live/runtime/triggers.py b/agent/src/live/runtime/triggers.pyindex 4279870cd..3705761e1 100644--- a/agent/src/live/runtime/triggers.py+++ b/agent/src/live/runtime/triggers.py@@ -47,8 +47,11 @@ class _MarketSpec:Empty == every day (24/7 markets such as crypto).always_open: Short-circuit for 24/7 markets; when ``True`` the time /weekday / holiday checks are skipped entirely.- holidays: Full-day market closures (no half-days modelled). This is a- deliberately small, static set — see module docstring limitation.+ holidays: Full-day market closures. This is a deliberately small,+ static set — see module docstring limitation.+ early_closes: Half-day sessions, mapped to their early close bell+ (exclusive, same convention as ``close_time``). Dates here must+ not also appear in ``holidays``."""tz: str@@ -57,12 +60,13 @@ class _MarketSpec:weekdays: frozenset[int] = frozenset()always_open: bool = Falseholidays: frozenset[date] = field(default_factory=frozenset)+ early_closes: Mapping[date, time] = field(default_factory=dict)# US market holidays. LIMITATION: this is a hand-maintained static set (no-# half-day early closes, no rolling computation). It covers the current and-# next calendar year; extend as needed. A production deploy that needs decades-# of coverage should swap in `pandas_market_calendars` behind this same spec.+# rolling computation). It covers the current and next calendar year; extend as+# needed. A production deploy that needs decades of coverage should swap in+# `pandas_market_calendars` behind this same spec._US_EQUITY_HOLIDAYS: frozenset[date] = frozenset({# 2026@@ -90,6 +94,17 @@ _US_EQUITY_HOLIDAYS: frozenset[date] = frozenset(})+# NYSE 13:00 ET early closes within the holiday window above. The recurring+# cases are the day after Thanksgiving and Christmas Eve when it is a weekday+# and not itself the observed holiday; Independence Day adds no entry here+# because in both covered years its early-close candidate (Jul 3) is either the+# observed full-day closure (2026) or a Saturday (2027).+_US_EQUITY_EARLY_CLOSES: Mapping[date, time] = {+ date(2026, 11, 27): time(13, 0), # day after Thanksgiving+ date(2026, 12, 24): time(13, 0), # Christmas Eve (Thursday)+ date(2027, 11, 26): time(13, 0), # day after Thanksgiving+}+_WEEKDAYS_MON_FRI = frozenset({0, 1, 2, 3, 4})# Market registry. Keys match the AssetClass-style identifiers the runner uses@@ -101,6 +116,7 @@ MARKET_SPECS: Mapping[str, _MarketSpec] = {close_time=time(16, 0),weekdays=_WEEKDAYS_MON_FRI,holidays=_US_EQUITY_HOLIDAYS,+ early_closes=_US_EQUITY_EARLY_CLOSES,),"crypto": _MarketSpec(tz="UTC",@@ -263,7 +279,8 @@ def market_is_open_at(market: str, now_ms: int) -> bool:Deterministic — reads no clock. 24/7 markets (``always_open``) are alwaysopen. Session markets are open only on permitted weekdays, outside the- holiday set, and within ``[open_time, close_time)`` local time.+ holiday set, and within ``[open_time, close)`` local time, where the close+ bell moves earlier on a listed early-close day.Args:market: A key into :data:`MARKET_SPECS`.@@ -287,7 +304,8 @@ def market_is_open_at(market: str, now_ms: int) -> bool:return Falseif local_dt.date() in spec.holidays:return False- return spec.open_time <= local_dt.time() < spec.close_time+ close = spec.early_closes.get(local_dt.date(), spec.close_time)+ return spec.open_time <= local_dt.time() < closedef due_now(trigger: Trigger, now_ms: int, *, event_state: Mapping[str, object] | None = None) -> bool:diff --git a/agent/tests/test_runtime_triggers.py b/agent/tests/test_runtime_triggers.pyindex 067ce7b48..10f35caab 100644--- a/agent/tests/test_runtime_triggers.py+++ b/agent/tests/test_runtime_triggers.py@@ -69,6 +69,47 @@ def test_us_equity_open_windows(now_ms: int, expected: bool) -> None:assert market_is_open_at("us_equity", now_ms) is expected+# 2026-11-27 is the Friday after Thanksgiving: NYSE closes at 13:00 ET.+_BF_2026 = (2026, 11, 27)+# 2026-12-24 is a Thursday Christmas Eve: NYSE closes at 13:00 ET.+_XMAS_EVE_2026 = (2026, 12, 24)+# 2027-11-26 is the Friday after Thanksgiving 2027.+_BF_2027 = (2027, 11, 26)+++ ("now_ms", "expected"),+ [+ (_ms(*_BF_2026, 9, 30, "America/New_York"), True),+ (_ms(*_BF_2026, 12, 59, "America/New_York"), True),+ # The early bell is exclusive, same convention as the 16:00 close.+ (_ms(*_BF_2026, 13, 0, "America/New_York"), False),+ (_ms(*_BF_2026, 13, 30, "America/New_York"), False),+ (_ms(*_BF_2026, 15, 30, "America/New_York"), False),+ (_ms(*_XMAS_EVE_2026, 12, 59, "America/New_York"), True),+ (_ms(*_XMAS_EVE_2026, 13, 0, "America/New_York"), False),+ (_ms(*_BF_2027, 12, 59, "America/New_York"), True),+ (_ms(*_BF_2027, 13, 0, "America/New_York"), False),+ ],+)+def test_us_equity_early_close_windows(now_ms: int, expected: bool) -> None:+ assert market_is_open_at("us_equity", now_ms) is expected+++def test_us_equity_observed_christmas_stays_a_full_closure() -> None:+ # 2027-12-24 is the observed Christmas holiday, not an early close: the+ # morning session must not trade either.+ assert market_is_open_at("us_equity", _ms(2027, 12, 24, 10, 0, "America/New_York")) is False+++def test_early_close_table_is_internally_consistent() -> None:+ spec = triggers.MARKET_SPECS["us_equity"]+ for day, bell in spec.early_closes.items():+ assert day.weekday() in spec.weekdays+ assert day not in spec.holidays+ assert spec.open_time < bell < spec.close_time++def test_us_equity_uses_ny_local_not_utc() -> None:# 14:00 UTC on the Friday is 10:00 ET — inside RTH despite 14:00 looking# like afternoon if (wrongly) read as local.@@ -183,6 +224,7 @@ def test_market_field_default_and_factory_are_both_intact() -> None:@pytest.mark.parametrize("decorate", [False, True])def test_market_factory_binds_to_subclass(decorate: bool) -> None:"""The deferred factory must retain normal classmethod inheritance."""+class DerivedTrigger(Trigger):pass