GetNextMarketClose lets an excluded extended-hours session hide a regular close at midnight

Closed
#10,110 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

A pull request for this has already been merged.

  • #10167 by @quantconnect-kronos — merged

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Start in SecurityExchangeHours.GetNextMarketClose, where GetNextOrPreviousSegment(time, isNextDay: true) feeds the next day's first segment into LocalMarketHours.GetMarketClose. Compare with how GetMarketClose already skips an extended-hours segment within a day, and see how #10090 handled the open side. Add a regression test using the issue's schedule that expects Monday 00:00 for the regular close, and confirm that the extended-hours-included case still returns the cross-midnight continuation.

Written by the indexing model from the issue text.

Description

priority:p2
Expected behavior

When extendedMarketHours is false, an extended-hours session at the start of the next day must not hide a regular session's close at midnight.

Actual behavior

For a Sunday regular session from 18:00 to 24:00, followed by a Monday post-market session from 00:00 to 02:00 and a regular session from 14:00 to 16:00, querying Sunday evening for the next regular close returns Monday 16:00 instead of Monday 00:00.

Reproduction
var schedule = Enum.GetValues<DayOfWeek>()
    .Select(day => LocalMarketHours.ClosedAllDay(day))
    .ToDictionary(x => x.DayOfWeek);
schedule[DayOfWeek.Sunday] = new LocalMarketHours(DayOfWeek.Sunday,
    new MarketHoursSegment(MarketHoursState.Market, TimeSpan.FromHours(18), TimeSpan.FromHours(24)));
schedule[DayOfWeek.Monday] = new LocalMarketHours(DayOfWeek.Monday,
    new MarketHoursSegment(MarketHoursState.PostMarket, TimeSpan.Zero, TimeSpan.FromHours(2)),
    new MarketHoursSegment(MarketHoursState.Market, TimeSpan.FromHours(14), TimeSpan.FromHours(16)));
var hours = new SecurityExchangeHours(TimeZones.NewYork, Enumerable.Empty<DateTime>(), schedule,
    new Dictionary<DateTime, TimeSpan>(), new Dictionary<DateTime, TimeSpan>());
var close = hours.GetNextMarketClose(new DateTime(2024, 1, 7, 19, 0, 0), false);
// Expected: 2024-01-08 00:00:00
// Actual:   2024-01-08 16:00:00
Cause

SecurityExchangeHours.GetNextMarketClose passes the next day's first segment start (GetNextOrPreviousSegment(time, isNextDay: true)) to LocalMarketHours.GetMarketClose without checking whether that segment is eligible for the requested hours. Within a day, GetMarketClose already ignores an extended-hours next segment when extended hours are excluded; across midnight it doesn't, so the 24:00 → 00:00 boundary is treated as a continuation.

The fix must take only an eligible next-day segment into account when extended hours are excluded, and keep cross-midnight continuity when extended hours are included. It's the mirror of #10090 for market opens. No market in the bundled market hours database has this layout, so only custom exchange hours are affected.

Dominant language
C#
Stars
22.6k
Forks
5.4k
Avg merge
11h 39m
Merged PRs (30d)
104

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantConnect/Lean

All issues in QuantConnect/Lean

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.