fix真盈亏收口为币本位×卖回价减初始权利金U,禁指数冒充平仓价。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -178,29 +178,32 @@ def true_pnl_usdt_premium_move(
|
||||
*,
|
||||
coin_pnl: Any,
|
||||
premium_paid: Any,
|
||||
open_index_px: Any,
|
||||
sell_back_px: Any,
|
||||
initial_premium_usdt: Any = None,
|
||||
open_index_px: Any = None,
|
||||
) -> float | None:
|
||||
"""复盘真盈亏(U):计入权利金 USDT 波动;现货多买数量不进公式.
|
||||
"""复盘真盈亏(U):币本位×卖回价 − 初始权利金U.
|
||||
|
||||
真盈亏 = 期权历史盈亏(币)×平仓卖回价
|
||||
− (权利金×开仓价 − 权利金×平仓卖回价)
|
||||
= 历史币盈亏×卖回价 − 权利金×(开仓价 − 卖回价)
|
||||
|
||||
币量=期权腿(realized_pnl / premium_paid);价=买卖 VWAP.
|
||||
无手续费时 ≡ 期权收回权利金×卖回价 − 权利金×开仓价
|
||||
(收回≠账户卖掉的全部现货;多余仓只影响均价).
|
||||
币本位 = 权利金 + 币盈亏
|
||||
真盈亏U = 币本位 × 卖回VWAP − 初始权利金U
|
||||
初始权利金U 优先用开仓冻结值;缺失时才用 权利金×开仓价 补算.
|
||||
现货多买数量不进公式(只影响均价).
|
||||
"""
|
||||
pnl = _safe_float(coin_pnl)
|
||||
paid = _safe_float(premium_paid)
|
||||
open_px = _safe_float(open_index_px)
|
||||
close_px = _safe_float(sell_back_px)
|
||||
if pnl is None or paid is None:
|
||||
return None
|
||||
if open_px is None or open_px <= 0 or close_px is None or close_px <= 0:
|
||||
if close_px is None or close_px <= 0:
|
||||
return None
|
||||
prem_move = float(paid) * float(open_px) - float(paid) * float(close_px)
|
||||
return round(float(pnl) * float(close_px) - prem_move, 4)
|
||||
initial = _safe_float(initial_premium_usdt)
|
||||
if initial is None:
|
||||
open_px = _safe_float(open_index_px)
|
||||
if open_px is None or open_px <= 0:
|
||||
return None
|
||||
initial = float(paid) * float(open_px)
|
||||
coin_basis = float(paid) + float(pnl)
|
||||
return round(coin_basis * float(close_px) - float(initial), 4)
|
||||
|
||||
|
||||
def resolve_spot_vwap_prices(
|
||||
@@ -210,13 +213,18 @@ def resolve_spot_vwap_prices(
|
||||
open_index_px: Any = None,
|
||||
close_index_px: Any = None,
|
||||
) -> tuple[float | None, float | None, str]:
|
||||
"""开仓价/卖回价:优先桥上多笔 VWAP,否则冻结指数.
|
||||
"""开仓价/卖回价解析.
|
||||
|
||||
开仓价:优先已冻结 open_index_px,缺失才补桥买 VWAP(不覆盖已冻值).
|
||||
卖回价:有桥时只认 close_sell VWAP;桥未关→pending_sell;已关无卖回→missing.
|
||||
无桥时 close_index_px 视为调用方传入的卖回价(测试/无现货桥).
|
||||
|
||||
返回 (open_px, close_px, gate):
|
||||
gate='' 可算; 'pending_sell' 卖回未完成; 'missing' 缺价
|
||||
"""
|
||||
open_px = _safe_float(open_index_px)
|
||||
close_px = _safe_float(close_index_px)
|
||||
close_hint = _safe_float(close_index_px)
|
||||
close_px: float | None = None
|
||||
bid = None
|
||||
try:
|
||||
if bridge_id is not None and str(bridge_id).strip() != "":
|
||||
@@ -233,26 +241,34 @@ def resolve_spot_vwap_prices(
|
||||
)
|
||||
|
||||
ensure_bridge_table(conn)
|
||||
bv = bridge_buy_vwap(conn, bid)
|
||||
if bv is not None and bv > 0:
|
||||
open_px = bv
|
||||
row = conn.execute(
|
||||
"SELECT status FROM options_spot_bridge WHERE id=?",
|
||||
(bid,),
|
||||
).fetchone()
|
||||
st = str(
|
||||
row["status"] if row and hasattr(row, "keys") else (row[0] if row else "") or ""
|
||||
)
|
||||
# 桥未关闭(=卖回流程未结束):禁止用平仓指数冒充卖回 VWAP 提前结算
|
||||
if st and st != BRIDGE_CLOSED:
|
||||
return open_px, None, "pending_sell"
|
||||
sv = bridge_sell_vwap(conn, bid)
|
||||
if sv is not None and sv > 0:
|
||||
close_px = sv
|
||||
# 已关桥但无卖回 VWAP:允许回退 close_index_px;两者都无则 missing
|
||||
if row is None:
|
||||
# 桥记录不存在:按无桥处理,close_index 视为调用方卖回价
|
||||
close_px = close_hint
|
||||
else:
|
||||
if open_px is None or open_px <= 0:
|
||||
bv = bridge_buy_vwap(conn, bid)
|
||||
if bv is not None and bv > 0:
|
||||
open_px = bv
|
||||
st = str(
|
||||
row["status"] if hasattr(row, "keys") else (row[0] if row else "") or ""
|
||||
)
|
||||
if st and st != BRIDGE_CLOSED:
|
||||
return open_px, None, "pending_sell"
|
||||
sv = bridge_sell_vwap(conn, bid)
|
||||
if sv is not None and sv > 0:
|
||||
close_px = sv
|
||||
else:
|
||||
# 有桥却无卖回成交:禁止用指数/旧 close_index 冒充
|
||||
return open_px, None, "missing"
|
||||
except Exception:
|
||||
pass
|
||||
if open_px is None or open_px <= 0 or close_px is None or close_px <= 0:
|
||||
else:
|
||||
close_px = close_hint
|
||||
if close_px is None or close_px <= 0:
|
||||
return open_px, close_px, "missing"
|
||||
return open_px, close_px, ""
|
||||
|
||||
@@ -264,6 +280,7 @@ def resolve_true_pnl_usdt_from_trade(
|
||||
realized_pnl: Any = None,
|
||||
open_index_px: Any = None,
|
||||
close_index_px: Any = None,
|
||||
initial_premium_usdt: Any = None,
|
||||
open_fee: Any = 0,
|
||||
close_fee: Any = 0,
|
||||
premium_ccy: str | None = None,
|
||||
@@ -271,7 +288,7 @@ def resolve_true_pnl_usdt_from_trade(
|
||||
conn: Any = None,
|
||||
bridge_id: Any = None,
|
||||
) -> tuple[float | None, str]:
|
||||
"""由成交字段算复盘真盈亏;开/卖回价优先多笔 VWAP.
|
||||
"""由成交字段算复盘真盈亏: (权利金+币盈亏)×卖回价 − 初始权利金U.
|
||||
|
||||
返回 (pnl_u, source): premium_move | stable | pending_sell | none
|
||||
"""
|
||||
@@ -299,12 +316,12 @@ 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,
|
||||
open_index_px=open_px,
|
||||
sell_back_px=close_px,
|
||||
initial_premium_usdt=initial_premium_usdt,
|
||||
open_index_px=open_px,
|
||||
)
|
||||
if usdt is not None:
|
||||
return usdt, "premium_move"
|
||||
@@ -325,8 +342,7 @@ def settle_sim_coin_pnl_usdt(
|
||||
"""由毛权利金生成净币盈亏,再按用户式折 U(费只扣进币盈亏一次).
|
||||
|
||||
币盈亏 = 收回 − 已付 − 开/平手续费
|
||||
真盈亏U = 币盈亏×卖回价 − 权利金×(开仓价−卖回价)
|
||||
不再用「毛收回折U后再减手续费U」(避免与历史币盈亏口径两套算法).
|
||||
真盈亏U = (权利金+币盈亏)×卖回价 − 初始权利金U
|
||||
"""
|
||||
del fee_ccy
|
||||
paid = float(_safe_float(premium_paid) or 0.0)
|
||||
@@ -358,8 +374,8 @@ def settle_sim_coin_pnl_usdt(
|
||||
pnl_u = true_pnl_usdt_premium_move(
|
||||
coin_pnl=net_coin,
|
||||
premium_paid=paid,
|
||||
open_index_px=open_idx,
|
||||
sell_back_px=sell_px,
|
||||
initial_premium_usdt=initial_u,
|
||||
)
|
||||
return {
|
||||
"realized_pnl": net_coin,
|
||||
@@ -391,7 +407,7 @@ def settle_closed_pnl(
|
||||
币数量净盈亏 = 收回 − 已付 − 开/平手续费(只扣一次).
|
||||
|
||||
币本位 U:
|
||||
期权收回权利金×卖回价 − 权利金×开仓价 − 手续费U
|
||||
(权利金+币盈亏)×卖回价 − 初始权利金U
|
||||
(开仓价缺失时回退为净币×卖回价;币量不含现货多余仓)
|
||||
USDC: 净额原样为 U.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user