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
+43 -38
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) lots = int(p.get("lots") or 0)
entry = float(p.get("avg_price") or 0) entry = float(p.get("avg_price") or 0)
ctp_margin = float(p.get("margin") 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 mark = None
if ctp_status(mode).get("connected"): if ctp_status(mode).get("connected"):
mark = ctp_get_tick_price(mode, sym) mark = ctp_get_tick_price(mode, sym)
if mark is None or mark <= 0: if mark is None or mark <= 0:
mark = entry if entry else None 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( est = calc_position_metrics(
direction, entry, entry, entry, lots, mark or entry, capital, sym, direction, entry, entry, entry, lots, mark or entry, capital, sym,
).get("margin") ).get("margin")
@@ -1085,16 +1087,12 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
margin = None margin = None
position_pct = None position_pct = None
mark = None mark = None
float_pnl = ctp.get("pnl") float_pnl = None
if float_pnl is not None:
float_pnl = round(float(float_pnl), 2)
if lots <= 0: if lots <= 0:
return None return None
if ctp: if ctp:
if ctp.get("pnl") is not None:
float_pnl = round(float(ctp["pnl"]), 2)
if not mon: if not mon:
ctp_lots = int(ctp.get("lots") or 0) ctp_lots = int(ctp.get("lots") or 0)
if ctp_lots > 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: if mark is None or mark <= 0:
mark = entry if entry else None mark = entry if entry else None
close_est = float(mark) if mark and mark > 0 else entry 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( pos_tmp = calc_position_metrics(
direction, entry, sl or entry, tp or entry, lots, mark, capital, sym, direction, entry, sl or entry, tp or entry, lots, mark, capital, sym,
) )
@@ -1676,35 +1674,42 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
if not positions: if not positions:
positions = _ctp_positions(mode, refresh_if_empty=False) positions = _ctp_positions(mode, refresh_if_empty=False)
quotes: list[dict] = [] quotes: list[dict] = []
for p in positions: conn = get_db()
lots = int(p.get("lots") or 0) try:
if lots <= 0: for p in positions:
continue lots = int(p.get("lots") or 0)
ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "") if lots <= 0:
if not ths: continue
continue ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "")
entry = float(p.get("avg_price") or 0) if not ths:
if entry <= 0: continue
continue direction = (p.get("direction") or "long").strip().lower()
direction = (p.get("direction") or "long").strip().lower() entry = float(p.get("avg_price") or 0)
mark = ctp_get_tick_price(mode, ths) mon = _find_active_monitor(conn, ths, direction)
if not mark or mark <= 0: if mon and float(mon.get("entry_price") or 0) > 0:
continue entry = float(mon["entry_price"])
mult = float(get_contract_spec(ths).get("mult") or 10) if entry <= 0:
if direction == "long": continue
float_pnl = round((mark - entry) * mult * lots, 2) mark = ctp_get_tick_price(mode, ths)
else: if not mark or mark <= 0:
float_pnl = round((entry - mark) * mult * lots, 2) continue
row_key = _canonical_position_key( mult = float(get_contract_spec(ths).get("mult") or 10)
ths, direction, (p.get("exchange") or ""), if direction == "long":
) float_pnl = round((mark - entry) * mult * lots, 2)
quotes.append({ else:
"key": row_key, float_pnl = round((entry - mark) * mult * lots, 2)
"position_key": row_key, row_key = _canonical_position_key(
"mark_price": mark, ths, direction, (p.get("exchange") or ""),
"current_price": mark, )
"float_pnl": float_pnl, quotes.append({
}) "key": row_key,
"position_key": row_key,
"mark_price": mark,
"current_price": mark,
"float_pnl": float_pnl,
})
finally:
conn.close()
return {"ok": True, "quotes": quotes} return {"ok": True, "quotes": quotes}
def _push_position_quotes_async() -> None: def _push_position_quotes_async() -> None: