From 963ed141e53435b43da7228adc5e8839f777ca2c Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 30 Jun 2026 09:22:10 +0800 Subject: [PATCH] Persist CTP average entry price to monitor DB on every position sync. Co-authored-by: Cursor --- install_trading.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/install_trading.py b/install_trading.py index c8f0833..95ac033 100644 --- a/install_trading.py +++ b/install_trading.py @@ -401,7 +401,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se rows: list[dict], mode: str, ) -> None: - """将柜台校正后的保证金、仓位占比、已扣开仓手续费写入 trade_order_monitors。""" + """将柜台校正后的均价、手数、现价、浮盈、保证金等写入 trade_order_monitors。""" if not ctp_status(mode).get("connected"): return ensure_monitor_order_columns(conn) @@ -409,20 +409,37 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se mid = row.get("monitor_id") if not mid or row.get("order_state") == "pending": continue + entry_price = row.get("entry_price") + lots = row.get("lots") + mark_price = row.get("mark_price") + if mark_price is None: + mark_price = row.get("current_price") + float_pnl = row.get("float_pnl") margin = row.get("margin") position_pct = row.get("position_pct") open_fee = row.get("est_fee") - if margin is None and position_pct is None and open_fee is None: + if ( + entry_price is None and lots is None and mark_price is None + and float_pnl is None and margin is None + and position_pct is None and open_fee is None + ): continue try: execute_retry( conn, """UPDATE trade_order_monitors SET + entry_price=COALESCE(?, entry_price), + lots=COALESCE(?, lots), + mark_price=COALESCE(?, mark_price), + float_pnl=COALESCE(?, float_pnl), margin=COALESCE(?, margin), position_pct=COALESCE(?, position_pct), open_fee=COALESCE(?, open_fee) WHERE id=? AND status='active'""", - (margin, position_pct, open_fee, int(mid)), + ( + entry_price, lots, mark_price, float_pnl, + margin, position_pct, open_fee, int(mid), + ), ) except Exception as exc: logger.debug("persist monitor ctp snapshot %s: %s", mid, exc) @@ -1554,7 +1571,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se mon = _find_pending_monitor( conn, ths, p.get("direction") or "long", ) - if mon and not fast: + if mon: _sync_monitor_from_ctp( conn, int(mon["id"]), mon.get("symbol") or ths, mon.get("direction") or p.get("direction") or "long",