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 <cursoragent@cursor.com>
This commit is contained in:
+22
-46
@@ -3501,50 +3501,23 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
return val
|
return val
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _ensure_strategy_monitors(conn, mode: str) -> int:
|
def _revive_roll_monitors_light(conn) -> int:
|
||||||
"""策略页:仅当柜台持仓缺少 active 监控时才恢复/同步,避免每次打开都全量写库。"""
|
"""策略页:仅恢复滚仓组关联的误关监控,不做 CTP 同步(避免阻塞页面)。"""
|
||||||
if not _cached_ctp_status(mode).get("connected"):
|
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
|
return 0
|
||||||
seen: set[tuple[str, str]] = set()
|
for r in rows:
|
||||||
positions = list(trading_state.get_positions() or [])
|
execute_retry(
|
||||||
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(
|
|
||||||
conn,
|
conn,
|
||||||
int(mon["id"]),
|
"UPDATE trade_order_monitors SET status='active' WHERE id=?",
|
||||||
mon.get("symbol") or ths,
|
(int(r["id"]),),
|
||||||
mon.get("direction") or direction,
|
|
||||||
mode,
|
|
||||||
ctp=p,
|
|
||||||
capital=capital,
|
|
||||||
)
|
)
|
||||||
synced += 1
|
commit_retry(conn)
|
||||||
if synced:
|
return len(rows)
|
||||||
commit_retry(conn)
|
|
||||||
return synced
|
|
||||||
|
|
||||||
@app.route("/strategy")
|
@app.route("/strategy")
|
||||||
@login_required
|
@login_required
|
||||||
@@ -3553,10 +3526,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
conn = get_db()
|
conn = get_db()
|
||||||
try:
|
try:
|
||||||
init_strategy_tables(conn)
|
init_strategy_tables(conn)
|
||||||
ensure_monitor_order_columns(conn)
|
|
||||||
capital = _capital(conn)
|
capital = _capital(conn)
|
||||||
mode = get_trading_mode(get_setting)
|
_revive_roll_monitors_light(conn)
|
||||||
_ensure_strategy_monitors(conn, mode)
|
|
||||||
need_initial = conn.execute(
|
need_initial = conn.execute(
|
||||||
"SELECT id FROM roll_groups WHERE status='active' AND COALESCE(initial_lots, 0)=0 LIMIT 1"
|
"SELECT id FROM roll_groups WHERE status='active' AND COALESCE(initial_lots, 0)=0 LIMIT 1"
|
||||||
).fetchone()
|
).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"
|
"SELECT * FROM trend_pullback_plans WHERE status='active' ORDER BY id DESC LIMIT 1"
|
||||||
).fetchone()
|
).fetchone()
|
||||||
monitors_raw = conn.execute(
|
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()
|
).fetchall()
|
||||||
roll_ctx = _build_roll_context(conn)
|
roll_ctx = _build_roll_context(conn)
|
||||||
roll_groups = conn.execute(
|
roll_groups = conn.execute(
|
||||||
@@ -3628,7 +3604,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
enriched_groups = [
|
enriched_groups = [
|
||||||
_enrich_roll_group_row_fast(dict(g), filled_map) for g in roll_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(
|
return render_template(
|
||||||
"strategy.html",
|
"strategy.html",
|
||||||
capital=capital,
|
capital=capital,
|
||||||
|
|||||||
Reference in New Issue
Block a user