Show option/perp PnL and fee-deducted net in trade details.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-26 16:14:48 +08:00
parent 21b8f587a2
commit 96e8e5cb70
4 changed files with 206 additions and 38 deletions
+20 -12
View File
@@ -53,10 +53,6 @@ old_base = float(o_close["base_px"] or o_close["fill_px"])
# 到期对齐实盘:严格内在价值,无盘口滑点
new_base = intrinsic
new_fill = intrinsic
if abs(new_base - old_base) < 1e-9 and abs(float(o_close["fill_px"]) - new_fill) < 1e-9:
print("already ok", old_base, intrinsic)
raise SystemExit(0)
qty = float(o_close["qty_eth"])
new_notional = new_fill * qty
new_fee = new_notional * FEE
@@ -64,6 +60,7 @@ new_slip = 0.0
old_cash = float(o_close["notional"]) - float(o_close["fee"])
new_cash = new_notional - new_fee
cash_delta = new_cash - old_cash
# 即使价已修好,也继续同步 realized_pnl(扣完全部手续费)
opt_entry = float(o_open["fill_px"])
opt_pnl = (new_fill - opt_entry) * qty
@@ -73,7 +70,14 @@ if perp_side == "long":
perp_pnl = (float(p_close["fill_px"]) - perp_entry) * float(p_open["qty_eth"])
else:
perp_pnl = (perp_entry - float(p_close["fill_px"])) * float(p_open["qty_eth"])
net = perp_pnl + opt_pnl - float(p_close["fee"]) - new_fee
fees_all = (
float(o_open["fee"])
+ float(p_open["fee"])
+ float(p_close["fee"])
+ new_fee
)
net_after_all_fees = perp_pnl + opt_pnl - fees_all
# fees on group: replace option close fee contribution
old_opt_fee = float(o_close["fee"])
@@ -93,17 +97,18 @@ con.execute(
)
con.execute(
"UPDATE groups SET realized_pnl=?, fees=?, slip_cost=?, note=? WHERE group_id=?",
(net, fees, slip, f"repaired_intrinsic:{intrinsic:.4f}", GROUP),
(net_after_all_fees, fees, slip, f"repaired_intrinsic:{intrinsic:.4f}", GROUP),
)
con.execute(
"UPDATE ledger_meta SET equity=?, available=?, updated_at_ms=? WHERE id=1",
(eq, av, now),
)
con.execute(
"INSERT INTO ledger_entries(group_id, kind, amount, balance_after, note, ts_ms) VALUES (?,?,?,?,?,?)",
(GROUP, "repair_option_intrinsic", cash_delta, eq,
f"repair {GROUP}: option close {old_base:.4f}->{new_base:.4f} intrinsic={intrinsic:.4f}", now),
)
if abs(cash_delta) > 1e-12:
con.execute(
"INSERT INTO ledger_entries(group_id, kind, amount, balance_after, note, ts_ms) VALUES (?,?,?,?,?,?)",
(GROUP, "repair_option_intrinsic", cash_delta, eq,
f"repair {GROUP}: option close {old_base:.4f}->{new_base:.4f} intrinsic={intrinsic:.4f}", now),
)
# also fix close_option entry amount if present
row = con.execute(
"SELECT id, amount FROM ledger_entries WHERE group_id=? AND kind='close_option' ORDER BY id DESC LIMIT 1",
@@ -121,9 +126,12 @@ print({
"intrinsic": intrinsic,
"old_base": old_base,
"new_base": new_base,
"option_pnl": opt_pnl,
"perp_pnl": perp_pnl,
"fees_all": fees_all,
"cash_delta": cash_delta,
"new_equity": eq,
"new_realized_pnl": net,
"new_realized_pnl": net_after_all_fees,
})
''' % {"db": DB, "group": GROUP}