合成入口剔除TDX幽灵预挂条(下时段开盘占位,o=c且v=0):30m聚合与90/120m合成同步过滤

This commit is contained in:
2026-11-22 23:32:31 +08:00
parent dee3284682
commit a3ccb72d11
2 changed files with 11 additions and 2 deletions
+8 -1
View File
@@ -21,11 +21,16 @@ DERIVED: dict[str, int] = {"90m": 90, "120m": 120} # 单位:交易分钟
_GRID = 15 # 15m 网格:每根恰好 15 交易分钟
def is_phantom_bar(b: dict) -> bool:
"""TDX 幽灵预挂条:下一时段开盘前的占位 K(o=c=昨收、v=0),须剔除。"""
return b.get("v", 0) == 0 and b.get("o") == b.get("c")
def synthesize_traded(bars_15m: Sequence[dict], code: str, minutes: int) -> list[dict]:
"""输入交易分钟流序(TDX 顺序,夜盘在先)的 15m 序列,输出 90m/120m 周期K。
跨交易日重置;当日尾段不足 N 根保留为短K;dt = 组内最后一根 15m 的标签
(收盘时刻语义,与定型逻辑兼容)。
(收盘时刻语义,与定型逻辑兼容)。幽灵预挂条在入口剔除。
"""
if minutes % _GRID != 0:
raise ValueError(f"周期 {minutes} 不是 {_GRID} 的倍数,无法用 15m 网格合成")
@@ -51,6 +56,8 @@ def synthesize_traded(bars_15m: Sequence[dict], code: str, minutes: int) -> list
grp = []
for b in bars_15m:
if is_phantom_bar(b):
continue
d = b["dt"][:10]
if d != day: # 跨交易日重置(夜盘在先序:上一日尾根 15:00 → 下一日夜盘 21:15)
flush()
+3 -1
View File
@@ -11,7 +11,7 @@ from typing import Optional
from ..db import get_conn
from . import archive
from .derive import DERIVED, synthesize_traded
from .derive import DERIVED, is_phantom_bar, synthesize_traded
from .tdx import TdxError, client
_page_locks: dict[tuple, threading.Lock] = {}
@@ -60,6 +60,8 @@ def aggregate_30m(bars15: list[dict]) -> list[dict]:
"""
groups: dict[str, list[dict]] = {}
for b in bars15:
if is_phantom_bar(b):
continue # TDX 幽灵预挂条(下一时段开盘占位,v=0)
d = datetime.strptime(b["dt"], "%Y-%m-%d %H:%M")
if d.minute % 30 != 0:
d += timedelta(minutes=30 - d.minute % 30)