Persist CTP margin, ratio, and fees to DB; use exchange commission in trade logs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-26 11:36:56 +08:00
parent 9a55c61678
commit d3955309d9
3 changed files with 80 additions and 6 deletions
+46 -1
View File
@@ -299,6 +299,37 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
row["position_pct"] = round(margin / capital * 100, 2)
return rows
def _persist_ctp_snapshot_to_monitors(
conn,
rows: list[dict],
mode: str,
) -> None:
"""将柜台校正后的保证金、仓位占比、已扣开仓手续费写入 trade_order_monitors。"""
if not ctp_status(mode).get("connected"):
return
ensure_monitor_order_columns(conn)
for row in rows:
mid = row.get("monitor_id")
if not mid or row.get("order_state") == "pending":
continue
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:
continue
try:
execute_retry(
conn,
"""UPDATE trade_order_monitors SET
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)),
)
except Exception as exc:
logger.debug("persist monitor ctp snapshot %s: %s", mid, exc)
def _ensure_monitors_from_ctp(conn, mode: str) -> None:
"""CTP 有持仓但本地无监控时,自动补写一条 active 记录供展示。"""
if not ctp_status(mode).get("connected"):
@@ -742,10 +773,18 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
position_pct = None
if margin and capital > 0:
position_pct = round(float(margin) / float(capital) * 100, 2)
open_commission = _open_commission_from_ctp_trades(mode, sym, direction)
if open_commission is None:
fee_info = calc_fee_breakdown(
sym, entry, entry, lots, open_time_val or "", "",
trading_mode=mode,
)
open_commission = fee_info.get("open_fee")
execute_retry(
conn,
"""UPDATE trade_order_monitors SET lots=?, entry_price=?,
open_time=?, margin=?, position_pct=?, mark_price=?, float_pnl=?
open_time=?, margin=?, position_pct=?, mark_price=?, float_pnl=?,
open_fee=?
WHERE id=?""",
(
lots,
@@ -755,6 +794,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
position_pct,
float(mark) if mark else None,
float_pnl,
open_commission,
mid,
),
)
@@ -854,6 +894,10 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
sym, entry, close_est, lots, open_time or now_iso, now_iso, trading_mode=mode,
)
open_commission = _open_commission_from_ctp_trades(mode, sym, direction)
if open_commission is None and mon and mon.get("open_fee") is not None:
cached_fee = float(mon.get("open_fee") or 0)
if cached_fee > 0:
open_commission = cached_fee
if open_commission is not None:
display_fee = open_commission
fee_source = "ctp"
@@ -1170,6 +1214,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
_ensure_monitors_from_ctp(conn, mode)
rows = _build_trading_live_rows(conn, fast=fast)
rows = _apply_account_margin_to_rows(rows, mode, capital)
_persist_ctp_snapshot_to_monitors(conn, rows, mode)
pending_orders = _build_pending_orders(conn, mode)
risk = get_risk_status(conn, active_count=_effective_active_position_count(conn, mode))
return {