feat: 持仓监控数据库优先显示,修复开仓重复与同步前空白

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-25 14:06:19 +08:00
parent 86e61df993
commit 040436e9cc
4 changed files with 390 additions and 183 deletions
+19 -2
View File
@@ -464,8 +464,8 @@ def cancel_monitor_exit_orders(
return cancelled
def reconcile_monitors_without_position(conn, mode: str) -> int:
"""持仓已平时:关闭监控并撤销残留止盈止损挂单。"""
def reconcile_monitors_without_position(conn, mode: str, *, grace_sec: int = 120) -> int:
"""持仓已平时:关闭监控并撤销残留止盈止损挂单(新开仓 grace_sec 内不清理)"""
if not ctp_status(mode).get("connected"):
return 0
positions = ctp_list_positions(mode)
@@ -477,9 +477,26 @@ def reconcile_monitors_without_position(conn, mode: str) -> int:
direction = p.get("direction") or "long"
position_keys.add((sym, direction))
now_ts = time.time()
def _monitor_within_grace(mon: dict) -> bool:
raw = (mon.get("open_time") or mon.get("created_at") or "").strip()
if not raw:
return True
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M"):
try:
dt = datetime.strptime(raw[:19], fmt)
if (now_ts - dt.timestamp()) <= grace_sec:
return True
except ValueError:
continue
return False
closed = 0
for r in conn.execute("SELECT * FROM trade_order_monitors WHERE status='active'").fetchall():
mon = dict(r)
if _monitor_within_grace(mon):
continue
ms = mon.get("symbol") or ""
md = mon.get("direction") or "long"
matched = False