|
| 1 | +"""Daily gaps must distinguish absent observations from scheduled closures.""" |
| 2 | + |
| 3 | +from datetime import date |
| 4 | + |
| 5 | +import polars as pl |
| 6 | +import pytest |
| 7 | +from polars.testing import assert_frame_equal |
| 8 | + |
| 9 | +from ml4t.data.providers.synthetic import SyntheticProvider |
| 10 | +from ml4t.data.utils.gaps import GapDetector |
| 11 | + |
| 12 | + |
| 13 | +def provider_frame(start: str, end: str, *, continuous: bool = False) -> pl.DataFrame: |
| 14 | + with SyntheticProvider(calendar_mode="continuous" if continuous else "equity") as provider: |
| 15 | + return provider.fetch_ohlcv("SPY", start, end, "daily") |
| 16 | + |
| 17 | + |
| 18 | +def test_provider_weekend_is_not_a_missing_equity_session() -> None: |
| 19 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 20 | + original = frame.clone() |
| 21 | + assert GapDetector().detect_gaps(frame) == [] |
| 22 | + assert_frame_equal(frame, original) |
| 23 | + |
| 24 | + |
| 25 | +def test_holiday_and_early_close_do_not_create_missing_daily_sessions() -> None: |
| 26 | + frame = provider_frame("2025-07-02", "2025-07-07") |
| 27 | + # The synthetic equity producer excludes weekends, but not exchange holidays. |
| 28 | + frame = frame.filter(pl.col("timestamp").dt.date() != date(2025, 7, 4)) |
| 29 | + assert GapDetector().detect_gaps(frame) == [] |
| 30 | + |
| 31 | + |
| 32 | +def test_all_assets_missing_a_session_does_not_redefine_the_calendar() -> None: |
| 33 | + frame = provider_frame("2025-07-14", "2025-07-18") |
| 34 | + panel = pl.concat( |
| 35 | + [frame.with_columns(pl.lit(symbol).alias("symbol")) for symbol in ("SPY", "QQQ")] |
| 36 | + ) |
| 37 | + panel = panel.filter(pl.col("timestamp").dt.date() != date(2025, 7, 16)) |
| 38 | + for asset in panel.partition_by("symbol"): |
| 39 | + gaps = GapDetector().detect_gaps(asset) |
| 40 | + assert len(gaps) == 1 |
| 41 | + assert gaps[0].missing_periods == 1 |
| 42 | + assert gaps[0].start.date() == date(2025, 7, 15) |
| 43 | + assert gaps[0].end.date() == date(2025, 7, 17) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("calendar, expected", [("NYSE", 0), ("24/7", 2), (None, 0)]) |
| 47 | +def test_explicit_calendar_controls_provider_weekend_classification(calendar, expected) -> None: |
| 48 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 49 | + gaps = GapDetector().detect_gaps(frame, calendar=calendar) |
| 50 | + assert sum(gap.missing_periods for gap in gaps) == expected |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "metadata, expected", |
| 55 | + [ |
| 56 | + ({}, 0), |
| 57 | + ({"calendar": None}, 0), |
| 58 | + ({"calendar": "24/7"}, 2), |
| 59 | + ({"time_contract_version": 2, "calendar": None}, 0), |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_feed_declaration_round_trip_preserves_calendar_selection(metadata, expected) -> None: |
| 63 | + from ml4t.specs import FeedSpec |
| 64 | + |
| 65 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 66 | + feed = FeedSpec.from_mapping(metadata) |
| 67 | + feed = FeedSpec.from_mapping(feed.to_dict()) |
| 68 | + gaps = GapDetector().detect_gaps(frame, feed_spec=feed) |
| 69 | + assert sum(gap.missing_periods for gap in gaps) == expected |
| 70 | + |
| 71 | + |
| 72 | +def test_explicit_calendar_beats_declared_metadata_and_crypto_hint() -> None: |
| 73 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 74 | + detector = GapDetector() |
| 75 | + assert ( |
| 76 | + detector.detect_gaps(frame, calendar="NYSE", feed_spec={"calendar": "24/7"}, is_crypto=True) |
| 77 | + == [] |
| 78 | + ) |
| 79 | + assert detector.detect_gaps(frame, calendar=None, feed_spec={"calendar": "24/7"}) == [] |
| 80 | + assert detector.detect_gaps(frame, feed_spec={"calendar": "NYSE"}, is_crypto=True) == [] |
| 81 | + assert sum(g.missing_periods for g in detector.detect_gaps(frame, is_crypto=True)) == 2 |
| 82 | + |
| 83 | + |
| 84 | +def test_continuous_provider_outage_remains_missing_on_a_real_schedule() -> None: |
| 85 | + frame = provider_frame("2025-07-11", "2025-07-14", continuous=True) |
| 86 | + frame = frame.filter( |
| 87 | + ~pl.col("timestamp").dt.date().is_in([date(2025, 7, 12), date(2025, 7, 13)]) |
| 88 | + ) |
| 89 | + gaps = GapDetector().detect_gaps(frame, calendar="24/7") |
| 90 | + assert len(gaps) == 1 |
| 91 | + assert gaps[0].missing_periods == 2 |
| 92 | + |
| 93 | + |
| 94 | +def test_calendar_none_does_not_classify_irregular_spacing_as_missing() -> None: |
| 95 | + frame = provider_frame("2025-07-01", "2025-07-31").gather_every(5) |
| 96 | + assert GapDetector().detect_gaps(frame, calendar=None) == [] |
| 97 | + |
| 98 | + |
| 99 | +def test_bad_calendar_fails_without_mutating_provider_input() -> None: |
| 100 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 101 | + original = frame.clone() |
| 102 | + with pytest.raises(ValueError, match="Unknown calendar"): |
| 103 | + GapDetector().detect_gaps(frame, calendar="not-an-exchange") |
| 104 | + assert_frame_equal(frame, original) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "metadata, expected", |
| 109 | + [({"calendar": None}, 1), ({"time_contract_version": 2, "calendar": None}, 0)], |
| 110 | +) |
| 111 | +def test_legacy_null_inherits_but_declared_null_disables_missing_session_checks( |
| 112 | + metadata, expected |
| 113 | +) -> None: |
| 114 | + from ml4t.specs import FeedSpec |
| 115 | + |
| 116 | + frame = provider_frame("2025-07-15", "2025-07-17") |
| 117 | + frame = frame.filter(pl.col("timestamp").dt.date() != date(2025, 7, 16)) |
| 118 | + feed = FeedSpec.from_mapping(FeedSpec.from_mapping(metadata).to_dict()) |
| 119 | + gaps = GapDetector().detect_gaps(frame, feed_spec=feed) |
| 120 | + assert sum(g.missing_periods for g in gaps) == expected |
| 121 | + |
| 122 | + |
| 123 | +def test_missing_early_close_session_is_still_one_missing_observation() -> None: |
| 124 | + frame = provider_frame("2025-07-02", "2025-07-07") |
| 125 | + frame = frame.filter(~pl.col("timestamp").dt.date().is_in([date(2025, 7, 3), date(2025, 7, 4)])) |
| 126 | + gaps = GapDetector().detect_gaps(frame) |
| 127 | + assert len(gaps) == 1 |
| 128 | + assert gaps[0].missing_periods == 1 |
| 129 | + |
| 130 | + |
| 131 | +def test_non_us_calendar_uses_its_own_holidays() -> None: |
| 132 | + frame = provider_frame("2025-07-03", "2025-07-07") |
| 133 | + frame = frame.filter(pl.col("timestamp").dt.date() != date(2025, 7, 4)) |
| 134 | + assert GapDetector().detect_gaps(frame, calendar="NYSE") == [] |
| 135 | + assert sum(g.missing_periods for g in GapDetector().detect_gaps(frame, calendar="LSE")) == 1 |
| 136 | + |
| 137 | + |
| 138 | +def test_date_session_labels_have_the_same_schedule_as_datetime_labels() -> None: |
| 139 | + frame = provider_frame("2025-07-11", "2025-07-14").with_columns(pl.col("timestamp").dt.date()) |
| 140 | + assert GapDetector().detect_gaps(frame) == [] |
| 141 | + assert sum(g.missing_periods for g in GapDetector().detect_gaps(frame, calendar="24/7")) == 2 |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.parametrize("semantics", ["event_time", "bar_close"]) |
| 145 | +def test_event_semantics_are_not_silently_reinterpreted_as_daily_labels(semantics) -> None: |
| 146 | + frame = provider_frame("2025-07-11", "2025-07-14") |
| 147 | + with pytest.raises(NotImplementedError, match="session_label"): |
| 148 | + GapDetector().detect_gaps(frame, feed_spec={"timestamp_semantics": semantics}) |
| 149 | + |
| 150 | + |
| 151 | +def test_null_timestamps_fail_without_input_mutation() -> None: |
| 152 | + frame = provider_frame("2025-07-11", "2025-07-14").with_columns( |
| 153 | + pl.lit(None).cast(pl.Datetime("us", "UTC")).alias("timestamp") |
| 154 | + ) |
| 155 | + original = frame.clone() |
| 156 | + with pytest.raises(ValueError, match="null timestamps"): |
| 157 | + GapDetector().detect_gaps(frame) |
| 158 | + assert_frame_equal(frame, original) |
0 commit comments