Fix slow CTP position sync after restart and link positions to 15m K-line.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-26 11:05:13 +08:00
parent deb9501cbe
commit 7a4a3f08e5
4 changed files with 80 additions and 10 deletions
+21 -4
View File
@@ -771,11 +771,17 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
"monitor_id": mon["id"] if mon else None,
})
row_key = _canonical_position_key(sym, direction)
ctp_st = ctp_status(mode)
sync_pending = (
mon is not None
and ctp is None
and bool(ctp_st.get("connected"))
)
return {
"key": row_key,
"source": "ctp" if ctp else "local",
"source_label": source_label,
"sync_pending": ctp is None and mon is not None,
"sync_pending": sync_pending,
"monitor_id": mon["id"] if mon else None,
"symbol_code": sym,
**_symbol_display_fields(sym),
@@ -1024,7 +1030,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
capital = _capital(conn)
if not fast and ctp_st.get("connected"):
_reconcile_pending(conn, mode, capital=capital)
if not fast:
if ctp_st.get("connected"):
_ensure_monitors_from_ctp(conn, mode)
rows = _build_trading_live_rows(conn, fast=fast)
pending_orders = _build_pending_orders(conn, mode)
@@ -2262,8 +2268,19 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
def _position_worker_refresh() -> dict:
_pos_refresh_tick["n"] += 1
# 每秒轻量刷新;每 5 秒做一次 CTP 持仓/挂单对账,避免频繁 query 导致 vnctptd 崩溃
return _refresh_trading_live_snapshot(fast=(_pos_refresh_tick["n"] % 5 != 0))
mode = get_trading_mode(get_setting)
connected = bool(ctp_status(mode).get("connected"))
# 已连接时每 2 秒完整对账;未连接时每 5 秒轻量刷新(禁止 query_position
if connected:
use_fast = _pos_refresh_tick["n"] % 2 != 0
else:
use_fast = _pos_refresh_tick["n"] % 5 != 0
payload = _refresh_trading_live_snapshot(fast=use_fast)
if connected and use_fast and any(
r.get("sync_pending") for r in (payload.get("rows") or [])
):
payload = _refresh_trading_live_snapshot(fast=False)
return payload
start_position_worker(
refresh_fn=_position_worker_refresh,