Schedule CTP disconnect 30 minutes after day and night session close.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 22:14:39 +08:00
parent 32838daae0
commit d324d30332
4 changed files with 141 additions and 19 deletions
+37
View File
@@ -246,3 +246,40 @@ def in_premarket_connect_window(
if mins is None:
return False
return 0 < mins <= float(minutes_before)
def in_postmarket_grace_window(
now: Optional[datetime] = None,
*,
minutes_after: int = 30,
) -> bool:
"""日盘 15:00 或夜盘 02:30 收盘后 minutes_after 分钟内(仍保持连接,便于收尾)。"""
if is_trading_session(now):
return False
d = _normalize_dt(now)
t = _minutes_of_day(d)
wd = d.weekday()
ma = max(1, int(minutes_after))
day_close = 15 * 60
night_close = 2 * 60 + 30
# 日盘收盘 15:00 后宽限(周一至周五)
if wd < 5 and day_close <= t < day_close + ma:
return True
# 夜盘收盘 02:30 后宽限(含周六凌晨结束周五夜盘)
if night_close <= t < night_close + ma:
return True
return False
def should_keep_ctp_connected(
now: Optional[datetime] = None,
*,
minutes_before: int = 30,
minutes_after: int = 30,
) -> bool:
"""是否处于应连接 CTP 的窗口:交易时段 + 盘前 + 盘后宽限。"""
if is_trading_session(now):
return True
if in_postmarket_grace_window(now, minutes_after=minutes_after):
return True
return in_premarket_connect_window(now, minutes_before=minutes_before)