Skip to content

Commit df10938

Browse files
committed
fix: let explicit result selectors override legacy calendar metadata
1 parent 63db2a4 commit df10938

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎docs/user-guide/configuration.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ CAGR uses `(N - 1) / periods_per_year` years for `N` recorded equity values. A l
314314
`calendar.time_contract_version` remains version 1, including its old null-as-inheritance
315315
behavior. Use `to_dict()` or `to_yaml()` to retain the v2 distinction between omission
316316
and explicit null; do not relabel an old payload without deciding what its null means.
317+
Direct result calls use the current selector contract: `result.to_daily_returns(calendar=None)`
318+
returns observations even when the result was built from a legacy configuration.
317319

318320
Calendar v2 on this development branch requires Specs source commit
319321
`701bde68744035050ede386efce498234c661c21`, pinned in the dependency declaration.

‎src/ml4t/backtest/result.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def to_daily_returns(
598598
"""Get daily returns as Polars Series for ml4t-diagnostic integration.
599599
600600
Omission or a named calendar aggregates bar-level equity to daily returns.
601-
In temporal v2, explicit None instead retains observation-level returns;
601+
Explicit None instead retains observation-level returns;
602602
use the matching declared annualization scale for downstream metrics.
603603
604604
Args:
@@ -607,7 +607,7 @@ def to_daily_returns(
607607
- "crypto": 365 days/year (24/7)
608608
- "NYSE", "NASDAQ": 252 days/year
609609
- "CME_Equity", etc: Uses pandas_market_calendars
610-
Omission uses the config calendar. In temporal v2, explicit None returns
610+
Omission resolves the feed calendar, then NYSE. Explicit None returns
611611
one return per observation without daily aggregation.
612612
session_aligned: If True, align to trading sessions (e.g., CME 5pm CT).
613613
If None, auto-detect from calendar (True for CME, False for crypto).
@@ -623,18 +623,12 @@ def to_daily_returns(
623623
>>> from ml4t.diagnostic.evaluation.metrics.risk_adjusted import sharpe_ratio
624624
>>> sharpe = sharpe_ratio(daily_returns.to_numpy(), annualization_factor=252)
625625
"""
626-
config_calendar = self.config.resolved_calendar if self.config else None
627-
v2 = self.config is not None and self.config.time_contract_version == 2
628-
if v2:
629-
cal = FeedSpec(time_contract_version=2, calendar=config_calendar).resolve_calendar(
630-
calendar=calendar
631-
)
632-
if cal is None:
633-
if session_aligned:
634-
raise ValueError("session_aligned=True requires a calendar")
635-
return self.to_returns_series().alias("daily_return")
636-
else:
637-
cal = calendar if isinstance(calendar, str) else config_calendar
626+
resolver = self._feed_spec() or FeedSpec(time_contract_version=2)
627+
cal = resolver.resolve_calendar(calendar=calendar)
628+
if cal is None:
629+
if session_aligned:
630+
raise ValueError("session_aligned=True requires a calendar")
631+
return self.to_returns_series().alias("daily_return")
638632
if session_aligned is None:
639633
session_aligned = self._auto_session_aligned(cal)
640634

‎tests/test_calendar_contract.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,18 @@ def test_daily_event_time_keeps_valid_cme_overnight_observation():
159159
assert len(result.equity_curve) == 3
160160

161161

162-
def test_result_explicit_none_returns_observations_without_daily_aggregation():
162+
@pytest.mark.parametrize("contract_version", [1, 2])
163+
def test_result_explicit_none_returns_observations_without_daily_aggregation(contract_version):
163164
times = [datetime(2025, 7, 11, hour, tzinfo=UTC) for hour in (10, 11, 12)]
164-
result, _ = run_engine(BacktestConfig(calendar="NYSE"), times=times)
165+
declaration = {"calendar": "NYSE"}
166+
if contract_version == 2:
167+
declaration["time_contract_version"] = 2
168+
config = BacktestConfig.from_dict({"calendar": declaration})
169+
result, _ = run_engine(config, times=times)
170+
assert len(result.to_daily_returns()) == 1
165171
assert result.to_daily_returns(calendar=None).to_list() == result.to_returns_series().to_list()
172+
with pytest.raises(ValueError, match="requires a calendar"):
173+
result.to_daily_returns(calendar=None, session_aligned=True)
166174

167175

168176
def test_named_result_calendar_override_controls_actual_overnight_aggregation():

0 commit comments

Comments
 (0)