Persist CTP average entry price to monitor DB on every position sync.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 09:22:10 +08:00
parent f41276806e
commit 963ed141e5
+21 -4
View File
@@ -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",