Fix float PnL to match displayed entry price and contract multiplier.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 09:14:13 +08:00
parent 92c222584e
commit c8fef65de7
+15 -10
View File
@@ -990,14 +990,16 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
lots = int(p.get("lots") or 0)
entry = float(p.get("avg_price") or 0)
ctp_margin = float(p.get("margin") or 0)
float_pnl = p.get("pnl")
if float_pnl is not None:
float_pnl = round(float(float_pnl), 2)
mark = None
if ctp_status(mode).get("connected"):
mark = ctp_get_tick_price(mode, sym)
if mark is None or mark <= 0:
mark = entry if entry else None
float_pnl = None
if mark and entry and lots > 0:
float_pnl = calc_position_metrics(
direction, entry, entry, entry, lots, mark, capital, sym,
).get("float_pnl")
est = calc_position_metrics(
direction, entry, entry, entry, lots, mark or entry, capital, sym,
).get("margin")
@@ -1085,16 +1087,12 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
margin = None
position_pct = None
mark = None
float_pnl = ctp.get("pnl")
if float_pnl is not None:
float_pnl = round(float(float_pnl), 2)
float_pnl = None
if lots <= 0:
return None
if ctp:
if ctp.get("pnl") is not None:
float_pnl = round(float(ctp["pnl"]), 2)
if not mon:
ctp_lots = int(ctp.get("lots") or 0)
if ctp_lots > 0:
@@ -1124,7 +1122,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
if mark is None or mark <= 0:
mark = entry if entry else None
close_est = float(mark) if mark and mark > 0 else entry
if float_pnl is None and mark and entry:
if mark and entry and lots > 0:
pos_tmp = calc_position_metrics(
direction, entry, sl or entry, tp or entry, lots, mark, capital, sym,
)
@@ -1676,6 +1674,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
if not positions:
positions = _ctp_positions(mode, refresh_if_empty=False)
quotes: list[dict] = []
conn = get_db()
try:
for p in positions:
lots = int(p.get("lots") or 0)
if lots <= 0:
@@ -1683,10 +1683,13 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "")
if not ths:
continue
direction = (p.get("direction") or "long").strip().lower()
entry = float(p.get("avg_price") or 0)
mon = _find_active_monitor(conn, ths, direction)
if mon and float(mon.get("entry_price") or 0) > 0:
entry = float(mon["entry_price"])
if entry <= 0:
continue
direction = (p.get("direction") or "long").strip().lower()
mark = ctp_get_tick_price(mode, ths)
if not mark or mark <= 0:
continue
@@ -1705,6 +1708,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
"current_price": mark,
"float_pnl": float_pnl,
})
finally:
conn.close()
return {"ok": True, "quotes": quotes}
def _push_position_quotes_async() -> None: