Skip to content

Commit b75d05f

Browse files
author
Stefan Jansen
committed
fix: extend intraday grid queries through CME business trade dates
1 parent b097dae commit b75d05f

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎src/ml4t/data/sessions/assigner.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ def _session_intervals(row: Any) -> list[tuple[datetime, datetime]]:
5252
return intervals
5353

5454

55-
def _schedule_for_instants(calendar: Any, start: Any, end: Any) -> Any:
55+
def _schedule_for_instants(calendar: Any, start: Any, end: Any, *, padding_days: int = 1) -> Any:
5656
import pandas as pd
5757

5858
from ml4t.data.sessions._cme_crypto import CMEGlobexCryptoSchedule
5959

6060
if isinstance(calendar, CMEGlobexCryptoSchedule):
6161
return calendar.schedule_for_instants(start, end)
6262
return calendar.schedule(
63-
start_date=pd.Timestamp(start).normalize() - pd.Timedelta(days=1),
64-
end_date=pd.Timestamp(end).normalize() + pd.Timedelta(days=1),
63+
start_date=pd.Timestamp(start).normalize() - pd.Timedelta(days=padding_days),
64+
end_date=pd.Timestamp(end).normalize() + pd.Timedelta(days=padding_days),
6565
interruptions=True,
6666
)
6767

‎src/ml4t/data/utils/gaps.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ml4t.data.sessions.assigner import (
1515
SessionAssigner,
1616
_daily_session_labels,
17+
_schedule_for_instants,
1718
_session_intervals,
1819
)
1920
from ml4t.specs import (
@@ -247,11 +248,7 @@ def _detect_intraday_gaps(
247248
expr = expr.dt.replace_time_zone(feed.timezone)
248249
observed = df.select(expr.dt.convert_time_zone("UTC")).to_series().unique().sort().to_list()
249250
start, end = observed[0], observed[-1]
250-
schedule = assigner.calendar.schedule(
251-
start_date=(start - timedelta(days=2)).date(),
252-
end_date=(end + timedelta(days=2)).date(),
253-
interruptions=True,
254-
)
251+
schedule = _schedule_for_instants(assigner.calendar, start, end, padding_days=2)
255252
grid_intervals: list[tuple[datetime, datetime, int, int]] = []
256253
total_bars = 0
257254
for _, row in schedule.iterrows():

‎tests/test_cme_crypto_cutover.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ml4t.data import DataManager
1010
from ml4t.data.providers import SyntheticProvider
1111
from ml4t.data.sessions import SessionAssigner, SessionCompleter
12+
from ml4t.data.utils.gaps import GapDetector
1213
from ml4t.specs import FeedSpec
1314

1415

@@ -135,3 +136,33 @@ def test_cme_daily_labels_are_business_dates_not_each_open_weekend_civil_date():
135136
info = SessionCompleter("CME Globex Crypto").get_session_info(datetime(2026, 10, 19))
136137
assert info["market_open"].isoformat() == "2026-10-16T21:02:00+00:00"
137138
assert info["market_close"].isoformat() == "2026-10-19T21:00:00+00:00"
139+
140+
141+
@pytest.mark.parametrize(
142+
"day,clocks,missing",
143+
[
144+
("2026-09-05", ["12:00", "12:02"], 1),
145+
("2026-10-17", ["06:59", "09:00"], 0),
146+
("2026-09-07", ["20:59", "21:02"], 0),
147+
("2026-05-29", ["20:59", "21:30"], 0),
148+
],
149+
)
150+
def test_intraday_grid_uses_business_date_coverage_and_excludes_cme_maintenance(
151+
day, clocks, missing
152+
):
153+
frame = provider_rows(day, clocks)
154+
feed = FeedSpec(
155+
time_contract_version=2,
156+
calendar="CME Globex Crypto",
157+
data_frequency="minute",
158+
timestamp_semantics="event_time",
159+
bar_grid_anchor="session_open",
160+
bar_label="left",
161+
partial_bar_policy="exclude",
162+
)
163+
gaps = GapDetector().detect_gaps(frame, feed_spec=feed)
164+
assert sum(gap.missing_periods for gap in gaps) == missing
165+
if day == "2026-10-17":
166+
spot = GapDetector().detect_gaps(frame, calendar="24/7", feed_spec=feed)
167+
assert sum(gap.missing_periods for gap in spot) == 120
168+
assert GapDetector().detect_gaps(frame, calendar=None, feed_spec=feed) == []

0 commit comments

Comments
 (0)