fix模拟盘盈亏:收回×卖回价 − 开仓U权利金 − 手续费,历史按U展示。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -512,11 +512,10 @@ def _sim_net_usdt_converts_for_trade(
|
||||
|
||||
|
||||
def sync_sim_cash_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
"""模拟盘:按权利金净盈亏冻结 U(收回−已付−费)×平仓指数.
|
||||
"""模拟盘:收回×卖回价 − 初始U权利金 − 手续费U.
|
||||
|
||||
禁止再用「卖回 USDT − 买币预算」:买币预算含权利金本金与缓冲,卖回差额会把
|
||||
权利金本金也算进盈利,导致合计盈亏虚高(约翻倍).
|
||||
函数名保留兼容旧调用.
|
||||
盈利=(权利金币+盈利币)×卖回价−开仓U权利金−费;
|
||||
亏损=剩余权利金U−费−开仓U(同一式).不用买币预算差额.
|
||||
"""
|
||||
init_options_tables(conn)
|
||||
from lib.options.options_pnl_settle_lib import resolve_close_index_px, settle_closed_pnl
|
||||
@@ -551,45 +550,40 @@ def sync_sim_cash_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
recv = float(total_premium(cq, eth))
|
||||
except Exception:
|
||||
recv = None
|
||||
if paid is None or recv is None:
|
||||
# 无毛权利金时回退:已有币数量净盈亏 × 冻结指数
|
||||
net = _safe_float(_row_get(row, "realized_pnl"))
|
||||
if net is None:
|
||||
continue
|
||||
idx = _safe_float(_row_get(row, "close_index_px")) or _safe_float(
|
||||
_row_get(row, "open_index_px")
|
||||
sell_px = _safe_float(_row_get(row, "close_index_px"))
|
||||
open_idx = _safe_float(_row_get(row, "open_index_px"))
|
||||
if sell_px is None or sell_px <= 0:
|
||||
sell_px = resolve_close_index_px(
|
||||
ex,
|
||||
inst_id=str(_row_get(row, "inst_id") or ""),
|
||||
underlying=underly,
|
||||
hint=open_idx,
|
||||
)
|
||||
if idx is None or idx <= 0:
|
||||
idx = resolve_close_index_px(
|
||||
ex,
|
||||
inst_id=str(_row_get(row, "inst_id") or ""),
|
||||
underlying=underly,
|
||||
)
|
||||
if idx is None or idx <= 0:
|
||||
if open_idx is None or open_idx <= 0:
|
||||
# 无开仓指数时用卖回价兜底(等价旧净币×价)
|
||||
open_idx = sell_px
|
||||
if paid is None or recv is None:
|
||||
net = _safe_float(_row_get(row, "realized_pnl"))
|
||||
if net is None or sell_px is None or sell_px <= 0:
|
||||
continue
|
||||
usdt = round(float(net) * float(idx), 4)
|
||||
close_idx = float(idx)
|
||||
usdt = round(float(net) * float(sell_px), 4)
|
||||
close_idx = float(sell_px)
|
||||
new_pnl = float(net)
|
||||
open_write = open_idx
|
||||
else:
|
||||
idx = _safe_float(_row_get(row, "close_index_px"))
|
||||
if idx is None or idx <= 0:
|
||||
idx = resolve_close_index_px(
|
||||
ex,
|
||||
inst_id=str(_row_get(row, "inst_id") or ""),
|
||||
underlying=underly,
|
||||
hint=_row_get(row, "open_index_px"),
|
||||
)
|
||||
settled = settle_closed_pnl(
|
||||
premium_paid=paid,
|
||||
premium_received=recv,
|
||||
open_fee=_row_get(row, "open_fee"),
|
||||
close_fee=_row_get(row, "close_fee"),
|
||||
premium_ccy=ccy or underly or "ETH",
|
||||
index_px=idx,
|
||||
index_px=sell_px,
|
||||
open_index_px=open_idx,
|
||||
)
|
||||
usdt = settled.get("realized_pnl_usdt")
|
||||
close_idx = settled.get("close_index_px")
|
||||
close_idx = settled.get("close_index_px") or sell_px
|
||||
new_pnl = settled.get("realized_pnl")
|
||||
open_write = settled.get("open_index_px") or open_idx
|
||||
if usdt is None:
|
||||
continue
|
||||
old = _safe_float(_row_get(row, "realized_pnl_usdt"))
|
||||
@@ -600,10 +594,11 @@ def sync_sim_cash_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
UPDATE options_trades
|
||||
SET realized_pnl_usdt = ?,
|
||||
close_index_px = COALESCE(close_index_px, ?),
|
||||
open_index_px = COALESCE(open_index_px, ?),
|
||||
realized_pnl = COALESCE(?, realized_pnl)
|
||||
WHERE id = ?
|
||||
""",
|
||||
(float(usdt), close_idx, new_pnl, int(row["id"])),
|
||||
(float(usdt), close_idx, open_write, new_pnl, int(row["id"])),
|
||||
)
|
||||
updated += 1
|
||||
return updated
|
||||
|
||||
Reference in New Issue
Block a user