fix模拟盘盈亏:收回×卖回价 − 开仓U权利金 − 手续费,历史按U展示。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -174,6 +174,62 @@ def fee_to_usdt(
|
||||
return round(amt, 4)
|
||||
|
||||
|
||||
def settle_sim_coin_pnl_usdt(
|
||||
*,
|
||||
premium_paid: Any,
|
||||
premium_received: Any,
|
||||
open_fee: Any = 0,
|
||||
close_fee: Any = 0,
|
||||
open_index_px: float | None,
|
||||
sell_back_px: float | None,
|
||||
fee_ccy: str | None = None,
|
||||
premium_ccy: str = "ETH",
|
||||
) -> dict[str, float | None]:
|
||||
"""模拟盘币本位盈亏(U).
|
||||
|
||||
盈利: (权利金btc+盈利btc)×卖回价 − 初始U权利金 − 手续费
|
||||
= 收回币×卖回价 − 已付币×开仓指数 − 手续费U
|
||||
亏损: 剩余权利金U − 手续费U − 初始U权利金(同一式,结果为负)
|
||||
|
||||
不用买币预算当成本(预算含缓冲).
|
||||
"""
|
||||
del fee_ccy # 手续费按权利金币种折 U
|
||||
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))
|
||||
cfee = abs(float(_safe_float(close_fee) or 0.0))
|
||||
open_idx = _safe_float(open_index_px)
|
||||
sell_px = _safe_float(sell_back_px)
|
||||
net_coin = round(recv - paid - ofee - cfee, 8)
|
||||
if open_idx is None or open_idx <= 0 or sell_px is None or sell_px <= 0:
|
||||
return {
|
||||
"realized_pnl": net_coin,
|
||||
"close_index_px": float(sell_px) if sell_px is not None and sell_px > 0 else None,
|
||||
"open_index_px": float(open_idx) if open_idx is not None and open_idx > 0 else None,
|
||||
"realized_pnl_usdt": None,
|
||||
"total_fee": round(ofee + cfee, 8),
|
||||
"initial_premium_usdt": None,
|
||||
"proceeds_usdt": None,
|
||||
}
|
||||
# 权利金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)
|
||||
return {
|
||||
"realized_pnl": net_coin,
|
||||
"close_index_px": float(sell_px),
|
||||
"open_index_px": float(open_idx),
|
||||
"realized_pnl_usdt": pnl_u,
|
||||
"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),
|
||||
"premium_ccy": str(premium_ccy or "").strip().upper() or "ETH",
|
||||
}
|
||||
|
||||
|
||||
def settle_closed_pnl(
|
||||
*,
|
||||
premium_paid: Any,
|
||||
@@ -189,13 +245,16 @@ def settle_closed_pnl(
|
||||
"""
|
||||
由「毛权利金」结算.
|
||||
币数量净盈亏 = 收回 − 已付 − 开/平手续费(只扣一次).
|
||||
冻结 U(币本位) = 净盈亏 × 平仓指数.
|
||||
|
||||
注意:不要用 budget_usdt 当开仓成本——它含现货买入缓冲,多余币会卖回,
|
||||
拿全额预算当成本会把亏损算大. open_index/budget 参数保留兼容,不参与 U 计算.
|
||||
币本位 U:
|
||||
收回×卖回价(index_px) − 已付×开仓指数 − 手续费U
|
||||
(开仓指数缺失时回退为净币×平仓指数)
|
||||
USDC: 净额原样为 U.
|
||||
|
||||
注意:不要用 budget_usdt 当开仓成本——它含现货买入缓冲.
|
||||
不要对 OKX realizedPnl 再调此函数扣费.
|
||||
"""
|
||||
del open_index_px, budget_usdt, fee_ccy # 兼容旧调用,不参与结算
|
||||
del budget_usdt # 买币预算不是权利金成本
|
||||
paid = float(_safe_float(premium_paid) or 0.0)
|
||||
recv = float(_safe_float(premium_received) or 0.0)
|
||||
ofee = float(_safe_float(open_fee) or 0.0)
|
||||
@@ -205,6 +264,29 @@ def settle_closed_pnl(
|
||||
if cfee < 0:
|
||||
cfee = abs(cfee)
|
||||
net = round(recv - paid - ofee - cfee, 8)
|
||||
pc = str(premium_ccy or "").strip().upper()
|
||||
if pc in ("ETH", "BTC"):
|
||||
open_idx = _safe_float(open_index_px)
|
||||
sell_px = _safe_float(index_px)
|
||||
if open_idx is not None and open_idx > 0 and sell_px is not None and sell_px > 0:
|
||||
out = settle_sim_coin_pnl_usdt(
|
||||
premium_paid=paid,
|
||||
premium_received=recv,
|
||||
open_fee=ofee,
|
||||
close_fee=cfee,
|
||||
open_index_px=open_idx,
|
||||
sell_back_px=sell_px,
|
||||
fee_ccy=fee_ccy,
|
||||
premium_ccy=pc,
|
||||
)
|
||||
return out
|
||||
# 无开仓指数:回退净币×平仓价
|
||||
return freeze_pnl_to_usdt(
|
||||
net,
|
||||
premium_ccy=pc,
|
||||
index_px=index_px,
|
||||
total_fee=ofee + cfee,
|
||||
)
|
||||
out = freeze_pnl_to_usdt(
|
||||
net,
|
||||
premium_ccy=premium_ccy,
|
||||
|
||||
Reference in New Issue
Block a user