fix真盈亏只认历史币盈亏不再另扣费,刷新按固定量价重算;修模拟到期币盈亏。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 11:30:07 +08:00
parent aa8b16ce27
commit dd979d9dd0
7 changed files with 278 additions and 128 deletions
+29 -41
View File
@@ -181,14 +181,15 @@ def true_pnl_usdt_premium_move(
open_index_px: Any,
sell_back_px: Any,
) -> float | None:
"""复盘真盈亏(U):计入权利金 USDT 波动,忽略现货多买.
"""复盘真盈亏(U):计入权利金 USDT 波动;现货多买数量不进公式.
真盈亏 = 期权历史盈亏(币)×平仓卖回价
− (权利金×开仓价 − 权利金×平仓卖回价)
= 历史币盈亏×卖回价 − 权利金×(开仓价 − 卖回价)
无手续费时等价于: 收回×卖回价 − 已付×开仓价.
币盈亏须已含期权开/平手续费(与期权历史同口径).
币量=期权腿(realized_pnl / premium_paid);价=买卖 VWAP.
无手续费时 ≡ 期权收回权利金×卖回价 − 权利金×开仓价
(收回≠账户卖掉的全部现货;多余仓只影响均价).
"""
pnl = _safe_float(coin_pnl)
paid = _safe_float(premium_paid)
@@ -274,12 +275,13 @@ def resolve_true_pnl_usdt_from_trade(
返回 (pnl_u, source): premium_move | stable | pending_sell | none
"""
del fee_ccy # 费率折 U 已体现在历史币盈亏里;本函数不重扣
del fee_ccy
pc = str(premium_ccy or "").strip().upper()
paid = _safe_float(premium_paid)
recv = _safe_float(premium_received)
ofee = abs(float(_safe_float(open_fee) or 0.0))
cfee = abs(float(_safe_float(close_fee) or 0.0))
# 期权历史币盈亏已含开/平手续费;真盈亏不再另扣费
coin = _safe_float(realized_pnl)
if coin is None and paid is not None and recv is not None:
coin = round(float(recv) - float(paid) - ofee - cfee, 8)
@@ -297,7 +299,7 @@ def resolve_true_pnl_usdt_from_trade(
)
if gate == "pending_sell":
return None, "pending_sell"
# 优先严格按用户式:历史币盈亏×卖回价 − 权利金×(开仓价−卖回价)
# 只用:历史币盈亏×卖回价 − 权利金×(开仓价−卖回价)
usdt = true_pnl_usdt_premium_move(
coin_pnl=coin,
premium_paid=paid,
@@ -306,28 +308,6 @@ def resolve_true_pnl_usdt_from_trade(
)
if usdt is not None:
return usdt, "premium_move"
# 无历史币盈亏时回退收回/已付精确折 U
if (
paid is not None
and recv is not None
and open_px is not None
and open_px > 0
and close_px is not None
and close_px > 0
):
settled = settle_sim_coin_pnl_usdt(
premium_paid=paid,
premium_received=recv,
open_fee=ofee,
close_fee=cfee,
open_index_px=open_px,
sell_back_px=close_px,
premium_ccy=pc,
)
usdt2 = _safe_float(settled.get("realized_pnl_usdt"))
if usdt2 is not None:
return usdt2, "premium_move"
return None, "none"
@@ -342,14 +322,13 @@ def settle_sim_coin_pnl_usdt(
fee_ccy: str | None = None,
premium_ccy: str = "ETH",
) -> dict[str, float | None]:
"""币本位真盈亏(U):含权利金 USDT 波动,不含现货多买.
"""由毛权利金生成净币盈亏,再按用户式折 U(费只扣进币盈亏一次).
= 历史币盈亏×卖回价 − (权利金×开仓价 − 权利金×卖回价)
= 收回币×卖回价 − 已付币×开仓指数 − 手续费U
不用买币预算当成本(预算含缓冲).
币盈亏 = 收回 − 已付 − 开/平手续费
真盈亏U = 币盈亏×卖回价 − 权利金×(开仓价−卖回价)
不再用「毛收回折U后再减手续费U」(避免与历史币盈亏口径两套算法).
"""
del fee_ccy # 手续费按权利金币种折 U
del fee_ccy
paid = float(_safe_float(premium_paid) or 0.0)
recv = float(_safe_float(premium_received) or 0.0)
ofee = abs(float(_safe_float(open_fee) or 0.0))
@@ -357,6 +336,11 @@ def settle_sim_coin_pnl_usdt(
open_idx = _safe_float(open_index_px)
sell_px = _safe_float(sell_back_px)
net_coin = round(recv - paid - ofee - cfee, 8)
fee_u = None
if open_idx is not None and open_idx > 0 and sell_px is not None and sell_px > 0:
fee_u = round(ofee * float(open_idx) + cfee * float(sell_px), 4)
elif sell_px is not None and sell_px > 0 and (ofee > 0 or cfee > 0):
fee_u = round((ofee + cfee) * float(sell_px), 4)
if open_idx is None or open_idx <= 0 or sell_px is None or sell_px <= 0:
return {
"realized_pnl": net_coin,
@@ -366,13 +350,17 @@ def settle_sim_coin_pnl_usdt(
"total_fee": round(ofee + cfee, 8),
"initial_premium_usdt": None,
"proceeds_usdt": None,
"fee_usdt": fee_u,
"premium_ccy": str(premium_ccy or "").strip().upper() or "ETH",
}
# 权利金btc+盈利btc = 平仓收回币
proceeds_u = float(recv) * float(sell_px)
initial_u = float(paid) * float(open_idx)
# 开仓费按开仓指数、平仓费按卖回价折 U
fee_u = ofee * float(open_idx) + cfee * float(sell_px)
pnl_u = round(proceeds_u - initial_u - fee_u, 4)
proceeds_u = float(recv) * float(sell_px)
pnl_u = true_pnl_usdt_premium_move(
coin_pnl=net_coin,
premium_paid=paid,
open_index_px=open_idx,
sell_back_px=sell_px,
)
return {
"realized_pnl": net_coin,
"close_index_px": float(sell_px),
@@ -381,7 +369,7 @@ def settle_sim_coin_pnl_usdt(
"total_fee": round(ofee + cfee, 8),
"initial_premium_usdt": round(initial_u, 4),
"proceeds_usdt": round(proceeds_u, 4),
"fee_usdt": round(fee_u, 4),
"fee_usdt": fee_u,
"premium_ccy": str(premium_ccy or "").strip().upper() or "ETH",
}
@@ -403,8 +391,8 @@ def settle_closed_pnl(
币数量净盈亏 = 收回 − 已付 − 开/平手续费(只扣一次).
币本位 U:
收回×卖回价(index_px) − 已付×开仓指数 − 手续费U
(开仓指数缺失时回退为净币×平仓指数)
期权收回权利金×卖回价 − 权利金×开仓价 − 手续费U
(开仓价缺失时回退为净币×卖回价;币量不含现货多余仓)
USDC: 净额原样为 U.
注意:不要用 budget_usdt 当开仓成本——它含现货买入缓冲.
+51 -48
View File
@@ -555,11 +555,6 @@ def sync_options_from_local_trades(
ccy = str(r["premium_ccy"] or "").strip().upper()
if not ccy:
ccy = premium_ccy_for_mode(margin_mode_from_inst_id(inst), underly)
frozen_usdt = _safe_float(r["realized_pnl_usdt"])
try:
settled = int(r["pnl_settled"] or 0) == 1
except Exception:
settled = False
close_idx = _safe_float(r["close_index_px"])
open_idx = _safe_float(r["open_index_px"])
idx = None
@@ -580,60 +575,67 @@ def sync_options_from_local_trades(
close_fee=cfee,
premium_ccy=ccy,
)
# 权利金币量/币盈亏固定;开仓价与卖回价各自冻一次后真盈亏只读
# 权利金币量/币盈亏/已冻均价不变;刷新时按公式重算真盈亏U
open_idx_for_prem = open_idx if (open_idx is not None and open_idx > 0) else idx
sell_idx = close_idx if (close_idx is not None and close_idx > 0) else None
from lib.options.options_pnl_settle_lib import resolve_true_pnl_usdt_from_trade
from lib.options.options_pnl_settle_lib import (
resolve_spot_vwap_prices,
resolve_true_pnl_usdt_from_trade,
)
status_label = "已平"
pnl_u = None
if settled and frozen_usdt is not None:
# 已冻结:只读展示,禁止同步反复改写
pnl_u = round(float(frozen_usdt), 4)
else:
pnl_u, pnl_src = resolve_true_pnl_usdt_from_trade(
premium_paid=prem,
premium_received=r["premium_received"],
realized_pnl=r["realized_pnl"],
open_index_px=open_idx if (open_idx is not None and open_idx > 0) else None,
close_index_px=sell_idx,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
fee_ccy=str(r["premium_ccy"] or ccy or ""),
conn=conn,
bridge_id=r["bridge_id"],
)
if pnl_src == "pending_sell":
status_label = "待卖回"
pnl_u = None
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
# 未冻结且可算:只写一次
try:
from lib.options.options_pnl_settle_lib import resolve_spot_vwap_prices
# 价已冻则只用库内价;缺价才问桥 VWAP(避免刷新改均价)
prices_frozen = (
open_idx is not None
and open_idx > 0
and sell_idx is not None
and sell_idx > 0
)
use_bridge = None if prices_frozen else r["bridge_id"]
pnl_u, pnl_src = resolve_true_pnl_usdt_from_trade(
premium_paid=prem,
premium_received=r["premium_received"],
realized_pnl=r["realized_pnl"],
open_index_px=open_idx if (open_idx is not None and open_idx > 0) else None,
close_index_px=sell_idx,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
fee_ccy=str(r["premium_ccy"] or ccy or ""),
conn=conn if use_bridge is not None else None,
bridge_id=use_bridge,
)
if pnl_src == "pending_sell":
status_label = "待卖回"
pnl_u = None
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
try:
opx, cpx = open_idx, sell_idx
if not prices_frozen:
opx, cpx, _g = resolve_spot_vwap_prices(
conn,
bridge_id=r["bridge_id"],
open_index_px=open_idx,
close_index_px=sell_idx,
)
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = ?,
open_index_px = COALESCE(open_index_px, ?),
close_index_px = COALESCE(?, close_index_px),
pnl_settled = 1
WHERE id = ? AND COALESCE(pnl_settled, 0)=0
""",
(pnl_u, opx, cpx, trade_id),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 真盈亏U每次刷新按固定量+价重算;均价只补缺不覆盖
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = ?,
open_index_px = COALESCE(open_index_px, ?),
close_index_px = COALESCE(close_index_px, ?),
pnl_settled = 1
WHERE id = ?
""",
(pnl_u, opx, cpx, trade_id),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 初始权利金(U)=开仓指数×权利金币量;不含多买
stored_initial = _safe_float(r["initial_premium_usdt"])
prem_u = stored_initial
@@ -641,6 +643,7 @@ def sync_options_from_local_trades(
prem_u = _to_usdt(prem, ccy=ccy, idx=open_idx_for_prem)
if ccy in ("ETH", "BTC") and prem_u is None:
prem_u = prem
# 手续费列仅展示(已含在历史币盈亏内,真盈亏不再扣)
fee_u = None
if ccy in ("ETH", "BTC"):
fee_parts = 0.0