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:
@@ -276,9 +276,35 @@ def enrich_trend_plan(cfg: dict, row) -> dict:
|
||||
direction = (d.get("direction") or "long").lower()
|
||||
metrics_fn = getattr(m, "get_live_position_exchange_metrics", None)
|
||||
if callable(metrics_fn):
|
||||
met = metrics_fn(ex_sym, direction)
|
||||
try:
|
||||
lev = int(d.get("leverage") or 0) or None
|
||||
except (TypeError, ValueError):
|
||||
lev = None
|
||||
try:
|
||||
met = metrics_fn(ex_sym, direction, order_leverage=lev)
|
||||
except TypeError:
|
||||
met = metrics_fn(ex_sym, direction)
|
||||
if met and met.get("unrealized_pnl") is not None:
|
||||
d["floating_pnl"] = float(met["unrealized_pnl"])
|
||||
elif (
|
||||
met
|
||||
and met.get("mark_price") is not None
|
||||
and d.get("avg_entry_price") is not None
|
||||
):
|
||||
try:
|
||||
entry = float(d["avg_entry_price"])
|
||||
mark = float(met["mark_price"])
|
||||
margin = float(d.get("plan_margin_capital") or 0)
|
||||
leverage = int(d.get("leverage") or 1)
|
||||
calc_pnl = getattr(m, "calc_pnl", None)
|
||||
if callable(calc_pnl) and entry > 0 and margin > 0:
|
||||
d["floating_pnl"] = float(
|
||||
calc_pnl(direction, entry, mark, margin, leverage)
|
||||
)
|
||||
else:
|
||||
d["floating_pnl"] = None
|
||||
except (TypeError, ValueError):
|
||||
d["floating_pnl"] = None
|
||||
else:
|
||||
d["floating_pnl"] = None
|
||||
if met and met.get("mark_price") is not None:
|
||||
|
||||
Reference in New Issue
Block a user