Sync CTP monitors on strategy page load for roll trading.

Revive and sync active monitors from live positions before listing roll candidates, show ineligible monitors with reasons, and validate eligibility on preview.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-02 21:55:50 +08:00
parent 4657d26f5e
commit 3c53b2063f
3 changed files with 59 additions and 3 deletions
+40 -1
View File
@@ -3190,6 +3190,44 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
return val
return None
def _ensure_strategy_monitors(conn, mode: str) -> int:
"""策略页加载前:恢复误关监控并同步柜台,与持仓页逻辑一致。"""
if not _cached_ctp_status(mode).get("connected"):
return 0
capital = _capital(conn)
synced = 0
seen: set[tuple[str, str]] = set()
positions = list(trading_state.get_positions() or [])
if not positions:
positions = list(_ctp_positions(mode, refresh_if_empty=False) or [])
for p in positions:
lots = int(p.get("lots") or 0)
if lots <= 0:
continue
ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "")
direction = (p.get("direction") or "long").strip().lower()
key = (ths.lower(), direction)
if key in seen:
continue
seen.add(key)
mon = _find_or_revive_monitor(conn, ths, direction)
if not mon:
continue
mon = _restore_monitor_sl_tp_if_missing(conn, mon, ths, direction) or mon
_sync_monitor_from_ctp(
conn,
int(mon["id"]),
mon.get("symbol") or ths,
mon.get("direction") or direction,
mode,
ctp=p,
capital=capital,
)
synced += 1
if synced:
commit_retry(conn)
return synced
@app.route("/strategy")
@login_required
@_nav("strategy")
@@ -3199,13 +3237,14 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
init_strategy_tables(conn)
ensure_monitor_order_columns(conn)
capital = _capital(conn)
mode = get_trading_mode(get_setting)
_ensure_strategy_monitors(conn, mode)
active_trend = conn.execute(
"SELECT * FROM trend_pullback_plans WHERE status='active' ORDER BY id DESC LIMIT 1"
).fetchone()
monitors_raw = conn.execute(
"SELECT * FROM trade_order_monitors WHERE status='active' ORDER BY id DESC"
).fetchall()
mode = get_trading_mode(get_setting)
roll_ctx = _build_roll_context(conn)
roll_groups = conn.execute(
"""SELECT g.*, m.symbol_name, m.lots AS mon_lots, m.entry_price AS mon_entry,