|
3 | 3 | Tests market hours, holidays, session detection, and DST transitions. |
4 | 4 | """ |
5 | 5 |
|
6 | | -from datetime import datetime, timedelta |
| 6 | +import sys |
| 7 | +from datetime import UTC, datetime, timedelta |
7 | 8 | from zoneinfo import ZoneInfo |
8 | 9 |
|
| 10 | +import polars as pl |
| 11 | +import pytest |
| 12 | + |
9 | 13 | from ml4t.engineer.core.calendars.equity import EquityCalendar |
10 | 14 |
|
11 | 15 | # ============================================================================= |
@@ -136,6 +140,85 @@ def test_timezone_conversion(self) -> None: |
136 | 140 | assert calendar.is_session(dt_utc) is True |
137 | 141 |
|
138 | 142 |
|
| 143 | +@pytest.fixture |
| 144 | +def equity_session_frame() -> pl.DataFrame: |
| 145 | + return pl.DataFrame( |
| 146 | + { |
| 147 | + "timestamp": [ |
| 148 | + datetime(2024, 1, 2, 14, 29, tzinfo=UTC), |
| 149 | + datetime(2024, 1, 2, 14, 30, tzinfo=UTC), |
| 150 | + datetime(2024, 1, 2, 20, tzinfo=UTC), |
| 151 | + datetime(2024, 1, 2, 21, tzinfo=UTC), |
| 152 | + datetime(2024, 1, 2, 21, 1, tzinfo=UTC), |
| 153 | + datetime(2024, 7, 2, 13, 29, tzinfo=UTC), |
| 154 | + datetime(2024, 7, 2, 13, 30, tzinfo=UTC), |
| 155 | + datetime(2024, 7, 2, 19, tzinfo=UTC), |
| 156 | + datetime(2024, 7, 2, 20, tzinfo=UTC), |
| 157 | + datetime(2024, 7, 2, 20, 1, tzinfo=UTC), |
| 158 | + datetime(2024, 7, 3, 16, tzinfo=UTC), |
| 159 | + datetime(2024, 7, 3, 17, tzinfo=UTC), |
| 160 | + datetime(2024, 7, 3, 17, 1, tzinfo=UTC), |
| 161 | + datetime(2024, 7, 4, 19, tzinfo=UTC), |
| 162 | + ], |
| 163 | + "row": range(14), |
| 164 | + } |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.parametrize("calendar_zone", [None, "Asia/Tokyo"]) |
| 169 | +@pytest.mark.parametrize( |
| 170 | + "input_zone", ["UTC", "America/New_York", "Asia/Tokyo", "Australia/Sydney"] |
| 171 | +) |
| 172 | +def test_filter_sessions_keeps_exchange_rows_across_timestamp_midnight( |
| 173 | + equity_session_frame: pl.DataFrame, calendar_zone: str | None, input_zone: str |
| 174 | +) -> None: |
| 175 | + pytest.importorskip("pandas_market_calendars") |
| 176 | + calendar = EquityCalendar("NYSE", timezone=calendar_zone) |
| 177 | + represented = equity_session_frame.with_columns( |
| 178 | + pl.col("timestamp").dt.convert_time_zone(input_zone) |
| 179 | + ) |
| 180 | + expected_rows = [1, 2, 3, 6, 7, 8, 10, 11] |
| 181 | + assert calendar.filter_sessions(represented)["row"].to_list() == expected_rows |
| 182 | + assert [calendar.is_session(dt) for dt in represented["timestamp"]] == [ |
| 183 | + i in expected_rows for i in range(14) |
| 184 | + ] |
| 185 | + |
| 186 | + |
| 187 | +@pytest.mark.parametrize("calendar_zone", [None, "Asia/Tokyo"]) |
| 188 | +def test_naive_timestamps_use_configured_timezone_before_selecting_exchange_date( |
| 189 | + equity_session_frame: pl.DataFrame, calendar_zone: str | None |
| 190 | +) -> None: |
| 191 | + pytest.importorskip("pandas_market_calendars") |
| 192 | + calendar = EquityCalendar("NYSE", timezone=calendar_zone) |
| 193 | + naive = equity_session_frame.with_columns( |
| 194 | + pl.col("timestamp") |
| 195 | + .dt.convert_time_zone(calendar_zone or "America/New_York") |
| 196 | + .dt.replace_time_zone(None) |
| 197 | + ) |
| 198 | + assert calendar.filter_sessions(naive)["row"].to_list() == [1, 2, 3, 6, 7, 8, 10, 11] |
| 199 | + |
| 200 | + |
| 201 | +@pytest.mark.parametrize( |
| 202 | + "input_zone", ["UTC", "America/New_York", "Asia/Tokyo", "Australia/Sydney"] |
| 203 | +) |
| 204 | +def test_missing_calendar_provider_keeps_basic_weekday_filtering( |
| 205 | + monkeypatch: pytest.MonkeyPatch, input_zone: str |
| 206 | +) -> None: |
| 207 | + monkeypatch.setitem(sys.modules, "pandas_market_calendars", None) |
| 208 | + calendar = EquityCalendar("NYSE") |
| 209 | + frame = pl.DataFrame( |
| 210 | + { |
| 211 | + "timestamp": [ |
| 212 | + datetime(2024, 7, 2, 19, tzinfo=UTC), |
| 213 | + datetime(2024, 7, 2, 23, tzinfo=UTC), |
| 214 | + datetime(2024, 7, 6, 19, tzinfo=UTC), |
| 215 | + ], |
| 216 | + "row": ["open", "after_close", "weekend"], |
| 217 | + } |
| 218 | + ).with_columns(pl.col("timestamp").dt.convert_time_zone(input_zone)) |
| 219 | + assert calendar.filter_sessions(frame)["row"].to_list() == ["open"] |
| 220 | + |
| 221 | + |
139 | 222 | # ============================================================================= |
140 | 223 | # Next Open / Previous Close Tests |
141 | 224 | # ============================================================================= |
|
0 commit comments