From c5b822a92ee54a399c15663694c0416e5016bcb7 Mon Sep 17 00:00:00 2001 From: dekun Date: Thu, 2 Jul 2026 23:16:53 +0800 Subject: [PATCH] Speed up strategy page by removing blocking CTP sync on load. Replace per-request CTP monitor sync with a lightweight roll-monitor revive; skip mark-price enrichment on pending legs during HTML render. Co-authored-by: Cursor --- modules/trading/install.py | 68 ++++++++++++-------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/modules/trading/install.py b/modules/trading/install.py index 2cb77ac..e1953b6 100644 --- a/modules/trading/install.py +++ b/modules/trading/install.py @@ -3501,50 +3501,23 @@ 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: - """策略页:仅当柜台持仓缺少 active 监控时才恢复/同步,避免每次打开都全量写库。""" - if not _cached_ctp_status(mode).get("connected"): + def _revive_roll_monitors_light(conn) -> int: + """策略页:仅恢复滚仓组关联的误关监控,不做 CTP 同步(避免阻塞页面)。""" + rows = conn.execute( + """SELECT m.id FROM trade_order_monitors m + INNER JOIN roll_groups g ON g.order_monitor_id = m.id + WHERE g.status='active' AND m.status='closed'""" + ).fetchall() + if not rows: return 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 []) - missing: list[tuple[dict, str, str]] = [] - 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().split(".")[0], direction) - if key in seen: - continue - seen.add(key) - if _find_active_monitor(conn, ths, direction): - continue - missing.append((p, ths, direction)) - if not missing: - return 0 - capital = _capital(conn) - synced = 0 - for p, ths, direction in missing: - 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( + for r in rows: + execute_retry( conn, - int(mon["id"]), - mon.get("symbol") or ths, - mon.get("direction") or direction, - mode, - ctp=p, - capital=capital, + "UPDATE trade_order_monitors SET status='active' WHERE id=?", + (int(r["id"]),), ) - synced += 1 - if synced: - commit_retry(conn) - return synced + commit_retry(conn) + return len(rows) @app.route("/strategy") @login_required @@ -3553,10 +3526,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se conn = get_db() try: init_strategy_tables(conn) - ensure_monitor_order_columns(conn) capital = _capital(conn) - mode = get_trading_mode(get_setting) - _ensure_strategy_monitors(conn, mode) + _revive_roll_monitors_light(conn) need_initial = conn.execute( "SELECT id FROM roll_groups WHERE status='active' AND COALESCE(initial_lots, 0)=0 LIMIT 1" ).fetchone() @@ -3588,7 +3559,12 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se "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" + """SELECT m.* FROM trade_order_monitors m + WHERE m.status='active' + OR m.id IN ( + SELECT order_monitor_id FROM roll_groups WHERE status='active' + ) + ORDER BY m.id DESC""" ).fetchall() roll_ctx = _build_roll_context(conn) roll_groups = conn.execute( @@ -3628,7 +3604,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se enriched_groups = [ _enrich_roll_group_row_fast(dict(g), filled_map) for g in roll_groups ] - enriched_legs = [_enrich_roll_leg_row(dict(l), mode) for l in roll_legs] + enriched_legs = [dict(l) for l in roll_legs] return render_template( "strategy.html", capital=capital,