QuantConnect/Lean · error · AssertionError
Algorithm should have not run on extended hours for {self._g
Error message
Algorithm should have not run on extended hours for {self._gc.symbol} future, which did not enable extended market hours What it means
Asserted at the end of a futures regression algorithm (on_end_of_algorithm). It proves the Lean data-feed isolation contract: a future subscription created with extended_market_hours=False must NEVER receive OnData slices during that contract's extended (non-regular) exchange hours. The flag _gc_ran_on_extended_hours is set only when on_data fires while self._gc.exchange.hours.is_open(self.time, True) is True and is_open(self.time, False) is False for the Gold (GC) contract that was added with extended hours disabled.
Source
Thrown at Algorithm.Python/FuturesExtendedMarketHoursRegressionAlgorithm.py:72
time_of_day = self.time.time()
current_time_is_regular_hours = (time_of_day >= time(9, 30, 0) and time_of_day < time(16, 15, 0)) or (time_of_day >= time(16, 30, 0) and time_of_day < time(17, 0, 0))
current_time_is_extended_hours = not current_time_is_regular_hours and (time_of_day < time(9, 30, 0) or time_of_day >= time(18, 0, 0))
if es_is_in_regular_hours != current_time_is_regular_hours or es_is_in_extended_hours != current_time_is_extended_hours:
raise AssertionError("At {Time}, {_es.symbol} is either in regular hours but current time is in extended hours, or viceversa")
def on_end_of_algorithm(self):
if not self._es_ran_on_regular_hours:
raise AssertionError(f"Algorithm should have run on regular hours for {self._es.symbol} future, which enabled extended market hours")
if not self._es_ran_on_extended_hours:
raise AssertionError(f"Algorithm should have run on extended hours for {self._es.symbol} future, which enabled extended market hours")
if not self._gc_ran_on_regular_hours:
raise AssertionError(f"Algorithm should have run on regular hours for {self._gc.symbol} future, which did not enable extended market hours")
if self._gc_ran_on_extended_hours:
raise AssertionError(f"Algorithm should have not run on extended hours for {self._gc.symbol} future, which did not enable extended market hours")
View on GitHub (pinned to d2c3659f87)
Solutions
- Confirm GC was added with extended_market_hours=False (line 28) and not mutated later; re-run the unmodified algorithm to rule out an edit.
- Inspect Common/Data/market-hours for the CME/Gold entry: regular hours and extended hours ranges must be disjoint; if they overlap, is_open(extended=True) can be True while data still streams.
- Check DataManager / SubscriptionManager routing: a subscription with extendedMarketHours=False must not receive slices outside regular hours; trace the fillForward / extendedHoursDataSubscription flags in Engine/DataFeeds.
- Verify the test data folder (CME futures hour data) matches the exchange-hours definition; mismatched data coverage for extended periods can make slices appear outside regular hours.
Example fix
// before: GC accidentally subscribed to extended hours self._gc = self.add_future(Futures.Metals.GOLD, Resolution.HOUR, fill_forward=True, extended_market_hours=True) // after: extended hours disabled, as the test expects self._gc = self.add_future(Futures.Metals.GOLD, Resolution.HOUR, fill_forward=True, extended_market_hours=False)
Defensive patterns
Strategy: validation
Validate before calling
# before subscribing, decide and keep the extended-hours flag explicit
use_extended = False # GC contract
gc = self.add_future(Futures.Metals.GOLD, Resolution.HOUR, fill_forward=True, extended_market_hours=use_extended)
# guard: never assert extended-hours behavior you did not opt into
if not use_extended and self._gc_ran_on_extended_hours:
self.debug(f"unexpected extended-hours data for {gc.symbol}") Prevention
- Always pass extended_market_hours explicitly (True/False) when adding futures; never rely on the default.
- Keep a single source of truth for which subscriptions enable extended hours and assert against it.
- When editing exchange-hours data files, verify regular and extended ranges stay disjoint.
When it happens
Trigger: on_data runs and slice contains data for the GC canonical/mapped symbol while self._gc.exchange.hours.is_open(self.time, extended=True) is True but is_open(..., extended=False) is False, even though add_future(..., extended_market_hours=False) was used. This trips line 53 (_gc_ran_on_extended_hours |= ...) so on_end_of_algorithm line 72 raises.
Common situations: Lean engine regression after a change to the SecurityExchangeHours database, the extended-hours subscription routing in DataManager, fill-forward logic, or the futures canonical-symbol resolution. Also triggered when someone edits the algorithm to enable extended hours on GC, or when the exchange-hours JSON for CME metals is edited to fold extended ranges into regular hours.
Related errors
- Unexpected continuous future mapping event count: {len(conti
- Unexpected continuous future mapping event count: {len(conti
- Regression test failed: current open interest isn't in the s
- Regression test failed: open interest is zero for all contra
- OnData was not called
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/f55ec61b293060c1.
Report an issue: GitHub.