chore: checkpoint contaminated Gate 2 findings

This commit is contained in:
2026-08-03 08:46:51 -04:00
parent 0c6b6b90a1
commit 56d7c7880e
21 changed files with 1593 additions and 892 deletions

View File

@@ -1,163 +1,96 @@
using System;
using System.Collections.Generic;
using NT8.Core.Logging;
namespace NT8.Core.Calendar
{
/// <summary>
/// CME Holiday Calendar - determines if a given date is a CME holiday or early-close day.
/// Covers standard US futures holidays observed by CME Group (CBOT, CME, COMEX, NYMEX).
/// CME equity-index holiday calendar used by strategy guards and provenance tags.
/// This deliberately models full closures and common early closes only; exchange
/// notices remain authoritative for exceptional schedules.
/// </summary>
public static class CmeHolidayCalendar
{
private static readonly ILogger _logger = new BasicLogger("CmeHolidayCalendar");
/// <summary>
/// Checks if the given date is a CME full holiday (market closed all day).
/// </summary>
/// <param name="date">Date to check (date portion only).</param>
/// <returns>True if CME holiday, false otherwise.</returns>
public static bool IsHoliday(DateTime date)
{
var d = date.Date;
int year = d.Year;
// Fixed-date holidays (observed on nearest weekday if weekend)
if (IsObservedHoliday(d, year, 1, 1)) return true; // New Year's Day
if (IsObservedHoliday(d, year, 7, 4)) return true; // Independence Day
if (IsObservedHoliday(d, year, 12, 25)) return true; // Christmas Day
// Monday holidays (3rd Monday Jan, 3rd Monday Feb, last Monday May, 1st Monday Sep)
if (IsNthWeekday(d, year, 1, DayOfWeek.Monday, 3)) return true; // MLK Jr. Day
if (IsNthWeekday(d, year, 2, DayOfWeek.Monday, 3)) return true; // Presidents Day
if (IsLastWeekday(d, year, 5, DayOfWeek.Monday)) return true; // Memorial Day
if (IsNthWeekday(d, year, 9, DayOfWeek.Monday, 1)) return true; // Labor Day
// Thanksgiving: 4th Thursday November
if (IsNthWeekday(d, year, 11, DayOfWeek.Thursday, 4)) return true;
// Good Friday (Friday before Easter)
if (d == GetGoodFriday(year)) return true;
return false;
}
/// <summary>
/// Checks if the given date is a CME early-close day (market closes at 1:00 PM ET or 1:15 PM ET).
/// </summary>
/// <param name="date">Date to check.</param>
/// <returns>True if early close, false otherwise.</returns>
public static bool IsEarlyClose(DateTime date)
{
var d = date.Date;
int year = d.Year;
// Day after Thanksgiving (Black Friday) - early close 1:00 PM ET
var thanksgiving = GetNthWeekday(year, 11, DayOfWeek.Thursday, 4);
if (d == thanksgiving.AddDays(1)) return true;
// Christmas Eve (Dec 24) - if not weekend, early close 1:15 PM ET
if (IsObservedHoliday(d, year, 12, 24)) return true;
// New Year's Eve (Dec 31) - if not weekend, early close 1:15 PM ET
if (IsObservedHoliday(d, year, 12, 31)) return true;
// Day before Independence Day (Jul 3) - if not weekend, early close
if (IsObservedHoliday(d, year, 7, 3)) return true;
return false;
}
/// <summary>
/// Gets the trading session type for a date: Regular, EarlyClose, or Closed.
/// </summary>
public static SessionType GetSessionType(DateTime date)
{
if (IsHoliday(date)) return SessionType.Closed;
if (IsEarlyClose(date)) return SessionType.EarlyClose;
return SessionType.Regular;
}
/// <summary>
/// Determines if strategy should skip ORB logic for this date.
/// </summary>
public static bool ShouldSkipOrbWindow(DateTime date)
{
return GetSessionType(date) != SessionType.Regular;
return IsHoliday(date);
}
// ──────────────────────────────────────────────────────────────
// Helpers
// ──────────────────────────────────────────────────────────────
/// <summary>
/// Checks if date matches a fixed holiday, with weekend observance rules.
/// If holiday falls on Saturday, observed Friday. If Sunday, observed Monday.
/// </summary>
private static bool IsObservedHoliday(DateTime date, int year, int month, int day)
public static bool IsHoliday(DateTime date)
{
var actual = new DateTime(year, month, day);
var observed = GetObservedDate(actual);
return date == observed;
DateTime day = date.Date;
int year = day.Year;
if (day == ObservedFixedHoliday(year, 1, 1))
return true;
if (day == NthWeekdayOfMonth(year, 1, DayOfWeek.Monday, 3))
return true;
if (day == NthWeekdayOfMonth(year, 2, DayOfWeek.Monday, 3))
return true;
if (day == GetEasterSunday(year).AddDays(-2))
return true;
if (day == LastWeekdayOfMonth(year, 5, DayOfWeek.Monday))
return true;
if (day == ObservedFixedHoliday(year, 7, 4))
return true;
if (day == NthWeekdayOfMonth(year, 9, DayOfWeek.Monday, 1))
return true;
if (day == NthWeekdayOfMonth(year, 11, DayOfWeek.Thursday, 4))
return true;
if (day == ObservedFixedHoliday(year, 12, 25))
return true;
return false;
}
/// <summary>
/// Returns the observed date for a fixed holiday (weekend -> nearest weekday).
/// </summary>
private static DateTime GetObservedDate(DateTime actual)
public static bool IsEarlyClose(DateTime date)
{
if (actual.DayOfWeek == DayOfWeek.Saturday)
return actual.AddDays(-1); // Observed Friday
if (actual.DayOfWeek == DayOfWeek.Sunday)
return actual.AddDays(1); // Observed Monday
return actual;
DateTime day = date.Date;
int year = day.Year;
DateTime thanksgiving = NthWeekdayOfMonth(year, 11, DayOfWeek.Thursday, 4);
if (day == thanksgiving.AddDays(1))
return true;
DateTime christmasEve = new DateTime(year, 12, 24);
if (christmasEve.DayOfWeek != DayOfWeek.Saturday
&& christmasEve.DayOfWeek != DayOfWeek.Sunday
&& day == christmasEve)
return true;
DateTime independenceEve = new DateTime(year, 7, 3);
if (independenceEve.DayOfWeek != DayOfWeek.Saturday
&& independenceEve.DayOfWeek != DayOfWeek.Sunday
&& day == independenceEve)
return true;
return false;
}
/// <summary>
/// Checks if date is the Nth occurrence of a weekday in a month (e.g., 3rd Monday).
/// </summary>
private static bool IsNthWeekday(DateTime date, int year, int month, DayOfWeek dow, int n)
private static DateTime ObservedFixedHoliday(int year, int month, int day)
{
var target = GetNthWeekday(year, month, dow, n);
return date == target;
DateTime holiday = new DateTime(year, month, day);
if (holiday.DayOfWeek == DayOfWeek.Saturday)
return holiday.AddDays(-1);
if (holiday.DayOfWeek == DayOfWeek.Sunday)
return holiday.AddDays(1);
return holiday;
}
/// <summary>
/// Returns the Nth occurrence of a weekday in a month.
/// </summary>
private static DateTime GetNthWeekday(int year, int month, DayOfWeek dow, int n)
private static DateTime NthWeekdayOfMonth(int year, int month, DayOfWeek weekday, int occurrence)
{
var first = new DateTime(year, month, 1);
int offset = ((int)dow - (int)first.DayOfWeek + 7) % 7;
var firstOccurrence = first.AddDays(offset);
return firstOccurrence.AddDays(7 * (n - 1));
DateTime date = new DateTime(year, month, 1);
int offset = ((int)weekday - (int)date.DayOfWeek + 7) % 7;
return date.AddDays(offset + ((occurrence - 1) * 7));
}
/// <summary>
/// Checks if date is the last occurrence of a weekday in a month (e.g., last Monday).
/// </summary>
private static bool IsLastWeekday(DateTime date, int year, int month, DayOfWeek dow)
private static DateTime LastWeekdayOfMonth(int year, int month, DayOfWeek weekday)
{
var target = GetLastWeekday(year, month, dow);
return date == target;
DateTime date = new DateTime(year, month, DateTime.DaysInMonth(year, month));
int offset = ((int)date.DayOfWeek - (int)weekday + 7) % 7;
return date.AddDays(-offset);
}
/// <summary>
/// Returns the last occurrence of a weekday in a month.
/// </summary>
private static DateTime GetLastWeekday(int year, int month, DayOfWeek dow)
private static DateTime GetEasterSunday(int year)
{
var lastDay = new DateTime(year, month, DateTime.DaysInMonth(year, month));
int offset = ((int)lastDay.DayOfWeek - (int)dow + 7) % 7;
return lastDay.AddDays(-offset);
}
/// <summary>
/// Computes Good Friday for a given year using Anonymous Gregorian algorithm.
/// </summary>
private static DateTime GetGoodFriday(int year)
{
// Anonymous Gregorian algorithm for Easter Sunday
int a = year % 19;
int b = year / 100;
int c = year % 100;
@@ -165,26 +98,14 @@ namespace NT8.Core.Calendar
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int h = ((19 * a) + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int l = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * l) / 451;
int month = (h + l - 7 * m + 114) / 31;
int day = ((h + l - 7 * m + 114) % 31) + 1;
var easter = new DateTime(year, month, day);
return easter.AddDays(-2); // Good Friday = 2 days before Easter
}
/// <summary>
/// Enum for session type.
/// </summary>
public enum SessionType
{
Regular,
EarlyClose,
Closed
int l = (32 + (2 * e) + (2 * i) - h - k) % 7;
int m = (a + (11 * h) + (22 * l)) / 451;
int month = (h + l - (7 * m) + 114) / 31;
int day = ((h + l - (7 * m) + 114) % 31) + 1;
return new DateTime(year, month, day);
}
}
}
}

View File

@@ -9,10 +9,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Orders\" />
</ItemGroup>
</Project>
</Project>