fix(okx): show negative unrealized PnL on strategy page

Parse signed upl/unrealizedPnl from CCXT positions and fall back to calc_pnl when exchange metrics are missing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-02 16:02:24 +08:00
parent 724b4a3dd8
commit a5f4ad8e97
3 changed files with 83 additions and 2 deletions
+18 -1
View File
@@ -2789,10 +2789,12 @@ def parse_ccxt_position_metrics(position, order_leverage=None):
initial = approx
except (TypeError, ValueError):
pass
unrealized = _coerce_float(
unrealized = _coerce_float_signed(
p.get("unrealizedPnl"),
info.get("upl"),
info.get("uplLast"),
info.get("unrealized_pnl"),
info.get("unrealisedPnl"),
)
mark = _coerce_float(p.get("markPrice"), p.get("mark_price"), info.get("markPx"))
out = {}
@@ -3996,6 +3998,7 @@ def calc_price_diff_pct(current_price, target_price):
def _coerce_float(*values):
"""取第一个可解析且 > 0 的数(用于价格、保证金等)。"""
for v in values:
if v is None:
continue
@@ -4008,6 +4011,20 @@ def _coerce_float(*values):
return None
def _coerce_float_signed(*values):
"""取第一个有限浮点数(含 0 与负数),用于未实现盈亏等。"""
for v in values:
if v is None or v == "":
continue
try:
f = float(v)
if math.isfinite(f):
return f
except (TypeError, ValueError):
continue
return None
def _sqlite_row_val(row, key, default=None):
try:
v = row[key]