Skip to content

Commit 0a543b6

Browse files
committed
fix(calendar): preserve continuous modern CME labeling sessions
1 parent 065a4aa commit 0a543b6

6 files changed

Lines changed: 66 additions & 22 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.16.1
3+
rev: v0.17.0
44
hooks:
55
- id: ruff-format
6-
- id: ruff
6+
types_or: [python, pyi, jupyter]
7+
- id: ruff-check
78
args: [--fix]
89

910
- repo: https://github.com/pre-commit/pre-commit-hooks

‎docs/user-guide/labeling.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,12 @@ result = calendar_aware_labels(
426426
)
427427
```
428428

429-
The calendar prevents forward returns from crossing session breaks (e.g., CME overnight gap from 4:00 PM to 5:00 PM CT).
429+
The calendar prevents forward returns from crossing genuine session breaks, such as the
430+
`CME_Equity` daily maintenance period from 4:00 PM to 5:00 PM CT. Its historical 3:15 PM to
431+
3:30 PM pause applies only before trade date June 28, 2021. Modern labels can cross that
432+
former pause because trading is continuous until 4:00 PM.
433+
See the [CME exchange notice](https://www.cmegroup.com/notices/electronic-trading/2021/06/20210621.html)
434+
for the affected equity-index contracts.
430435

431436
## Sample Weighting
432437

‎pyproject.toml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ store = [
106106

107107
# Trading calendar support
108108
calendars = [
109-
"pandas-market-calendars>=4.0.0,<5.5",
109+
"pandas-market-calendars>=5.5.0",
110110
]
111111

112112
# Statistical diagnostics
@@ -141,7 +141,7 @@ dev = [
141141
"ipdb>=0.13.0",
142142
"pre-commit>=3.3.0",
143143
"duckdb>=0.9.0",
144-
"pandas-market-calendars>=4.0.0,<5.5",
144+
"pandas-market-calendars>=5.5.0",
145145
"ta-lib>=0.4.0,<0.8",
146146
"yfinance>=0.2.0",
147147
"lightgbm>=4.0.0",
@@ -162,7 +162,7 @@ docs = [
162162
all = [
163163
"ta-lib>=0.4.0,<0.8",
164164
"duckdb>=0.9.0",
165-
"pandas-market-calendars>=4.0.0,<5.5",
165+
"pandas-market-calendars>=5.5.0",
166166
"plotly>=5.15.0",
167167
"matplotlib>=3.7.0",
168168
"seaborn>=0.12.0",
@@ -209,7 +209,7 @@ dev = [
209209
"pre-commit>=3.3.0",
210210
"twine>=6.0.0",
211211
"pip-audit>=2.10.1",
212-
"pandas-market-calendars>=4.0.0,<5.5",
212+
"pandas-market-calendars>=5.5.0",
213213
"duckdb>=0.9.0",
214214
"matplotlib>=3.7.0",
215215
"pyarrow>=14.0.0",

‎src/ml4t/engineer/labeling/calendar.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _session_breaks(session: Any) -> list[tuple[Any, Any]]:
180180
)
181181
start = session[start_col]
182182
end = session.get(end_col)
183-
if not pd.isna(start) and not pd.isna(end):
183+
if not pd.isna(start) and not pd.isna(end) and start < end:
184184
intervals.append((start, end))
185185
return intervals
186186

‎tests/test_labeling_calendar.py‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,21 @@ def test_market_sessions_holidays_early_closes_and_dst(self):
225225
2024, 7, 3, 17, 0, tzinfo=UTC
226226
)
227227

228-
def test_cme_maintenance_break(self):
229-
"""Test that product-specific intraday breaks are represented."""
228+
@pytest.mark.parametrize("year,month,day,hour", [(2019, 1, 2, 21), (2021, 6, 25, 20)])
229+
def test_cme_historical_intraday_pause(self, year, month, day, hour):
230+
"""The pre-2021 pause remains a genuine non-trading interval."""
230231
pytest.importorskip("pandas_market_calendars")
231232
cal = PandasMarketCalendar("CME_Equity")
232233

233-
before = datetime(2024, 1, 2, 21, 10, tzinfo=UTC)
234-
during = datetime(2024, 1, 2, 21, 20, tzinfo=UTC)
235-
after = datetime(2024, 1, 2, 21, 35, tzinfo=UTC)
234+
before = datetime(year, month, day, hour, 10, tzinfo=UTC)
235+
during = datetime(year, month, day, hour, 20, tzinfo=UTC)
236+
after = datetime(year, month, day, hour, 35, tzinfo=UTC)
236237

237238
assert cal.is_trading_time(before)
238239
assert not cal.is_trading_time(during)
239240
assert cal.is_trading_time(after)
240-
assert cal.next_session_break(before) == datetime(2024, 1, 2, 21, 15, tzinfo=UTC)
241-
assert cal.next_session_break(after) == datetime(2024, 1, 2, 22, 0, tzinfo=UTC)
241+
assert cal.next_session_break(before) == datetime(year, month, day, hour, 15, tzinfo=UTC)
242+
assert cal.next_session_break(after) == datetime(year, month, day, hour + 1, 0, tzinfo=UTC)
242243

243244
def test_naive_datetimes_are_interpreted_as_utc(self):
244245
"""Test the adapter's explicit naive timestamp policy."""
@@ -260,6 +261,43 @@ def test_naive_datetimes_are_interpreted_as_utc(self):
260261
class TestCalendarAwareLabels:
261262
"""Tests for calendar_aware_labels function."""
262263

264+
@pytest.mark.parametrize(
265+
"year,month,day,hour",
266+
[(2021, 6, 28, 20), (2024, 1, 2, 21), (2024, 7, 2, 20)],
267+
)
268+
def test_cme_modern_labels_do_not_split_at_the_removed_pause(self, year, month, day, hour):
269+
"""The real calendar's retired pause must not truncate a label path."""
270+
pytest.importorskip("pandas_market_calendars")
271+
data = pl.DataFrame(
272+
{
273+
"timestamp": [
274+
datetime(year, month, day, hour, minute, tzinfo=UTC)
275+
for minute in (10, 15, 20, 25)
276+
],
277+
"close": [4800.0, 4810.0, 4820.0, 4830.0],
278+
}
279+
)
280+
config = LabelingConfig.triple_barrier(
281+
upper_barrier=0.001,
282+
lower_barrier=0.001,
283+
max_holding_period=2,
284+
)
285+
286+
result = calendar_aware_labels(
287+
data,
288+
config,
289+
calendar="CME_Equity",
290+
price_col="close",
291+
timestamp_col="timestamp",
292+
)
293+
294+
assert result["label"].to_list() == [1, 1, 1, 0]
295+
calendar = PandasMarketCalendar("CME_Equity")
296+
assert all(calendar.is_trading_time(timestamp) for timestamp in data["timestamp"])
297+
assert calendar.next_session_break(data["timestamp"][0]) == datetime(
298+
year, month, day, hour + 1, 0, tzinfo=UTC
299+
)
300+
263301
def test_basic_functionality_auto_calendar(self, sample_daily_data):
264302
"""Test basic labeling with auto calendar detection."""
265303
config = LabelingConfig.triple_barrier(

‎uv.lock‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)