修复已实现盈亏重复扣手续费,并自动纠正偏亏冻结值。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -114,6 +114,40 @@ def sum_option_order_fees_in_premium(
|
||||
return round(total, 8)
|
||||
|
||||
|
||||
def freeze_pnl_to_usdt(
|
||||
net_pnl: float,
|
||||
*,
|
||||
premium_ccy: str,
|
||||
index_px: float | None,
|
||||
total_fee: float = 0.0,
|
||||
) -> dict[str, float | None]:
|
||||
"""把已是「净额」的权利金盈亏冻结为 U(不再扣费)."""
|
||||
net = round(float(net_pnl), 8)
|
||||
pc = str(premium_ccy or "").strip().upper()
|
||||
idx = _safe_float(index_px)
|
||||
fee_total = abs(float(total_fee or 0.0))
|
||||
if pc in ("ETH", "BTC"):
|
||||
if idx is None or idx <= 0:
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": None,
|
||||
"realized_pnl_usdt": None,
|
||||
"total_fee": round(fee_total, 8),
|
||||
}
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": float(idx),
|
||||
"realized_pnl_usdt": round(net * float(idx), 4),
|
||||
"total_fee": round(fee_total, 8),
|
||||
}
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": float(idx) if idx is not None and idx > 0 else None,
|
||||
"realized_pnl_usdt": round(net, 4),
|
||||
"total_fee": round(fee_total, 8),
|
||||
}
|
||||
|
||||
|
||||
def settle_closed_pnl(
|
||||
*,
|
||||
premium_paid: Any,
|
||||
@@ -124,8 +158,9 @@ def settle_closed_pnl(
|
||||
index_px: float | None,
|
||||
) -> dict[str, float | None]:
|
||||
"""
|
||||
净盈亏 = 收回 − 已付 − 开/平手续费(权利金单位).
|
||||
币本位再乘平仓指数得到冻结 U;USDC/USDT 直接冻结.
|
||||
由「毛权利金」结算:净盈亏 = 收回 − 已付 − 开/平手续费(权利金单位).
|
||||
仅用于本地开平仓权利金口径;不要对 OKX realizedPnl 再调此函数扣费
|
||||
(交易所 realizedPnl = pnl + fee + ..., 已含手续费).
|
||||
"""
|
||||
paid = float(_safe_float(premium_paid) or 0.0)
|
||||
recv = float(_safe_float(premium_received) or 0.0)
|
||||
@@ -136,29 +171,39 @@ def settle_closed_pnl(
|
||||
if cfee < 0:
|
||||
cfee = abs(cfee)
|
||||
net = round(recv - paid - ofee - cfee, 8)
|
||||
pc = str(premium_ccy or "").strip().upper()
|
||||
idx = _safe_float(index_px)
|
||||
if pc in ("ETH", "BTC"):
|
||||
if idx is None or idx <= 0:
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": None,
|
||||
"realized_pnl_usdt": None,
|
||||
"total_fee": round(ofee + cfee, 8),
|
||||
}
|
||||
return freeze_pnl_to_usdt(
|
||||
net,
|
||||
premium_ccy=premium_ccy,
|
||||
index_px=index_px,
|
||||
total_fee=ofee + cfee,
|
||||
)
|
||||
|
||||
|
||||
def settle_exchange_realized_pnl(
|
||||
*,
|
||||
realized_pnl: Any,
|
||||
premium_ccy: str,
|
||||
index_px: float | None,
|
||||
fee_hint: Any = None,
|
||||
) -> dict[str, float | None]:
|
||||
"""
|
||||
冻结交易所 positions-history 的 realizedPnl.
|
||||
该字段已含手续费,此处只折 U,不再减 fee.
|
||||
"""
|
||||
net = _safe_float(realized_pnl)
|
||||
if net is None:
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": float(idx),
|
||||
"realized_pnl_usdt": round(net * float(idx), 4),
|
||||
"total_fee": round(ofee + cfee, 8),
|
||||
"realized_pnl": None,
|
||||
"close_index_px": None,
|
||||
"realized_pnl_usdt": None,
|
||||
"total_fee": fee_amount_abs(fee_hint),
|
||||
}
|
||||
# USDC / USDT 权利金
|
||||
return {
|
||||
"realized_pnl": net,
|
||||
"close_index_px": float(idx) if idx is not None and idx > 0 else None,
|
||||
"realized_pnl_usdt": round(net, 4),
|
||||
"total_fee": round(ofee + cfee, 8),
|
||||
}
|
||||
return freeze_pnl_to_usdt(
|
||||
float(net),
|
||||
premium_ccy=premium_ccy,
|
||||
index_px=index_px,
|
||||
total_fee=fee_amount_abs(fee_hint),
|
||||
)
|
||||
|
||||
|
||||
def resolve_close_index_px(
|
||||
|
||||
Reference in New Issue
Block a user