fix真盈亏只认卖光收回减买币,禁止权利金指数冒充。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -429,8 +429,7 @@ def sync_options_from_local_trades(
|
||||
) -> dict[str, Any]:
|
||||
"""从本地 options_trades 已平仓记录导入复盘快照(不访问交易所).
|
||||
|
||||
盈亏优先用已冻结 realized_pnl_usdt(模拟盘现金口径 / 平仓时指数),
|
||||
禁止每次同步用实时指数重算,否则复盘数字会跟盘跳动.
|
||||
真盈亏=卖光收回−买币(after−before / 已关桥);禁止权利金×指数冒充.
|
||||
"""
|
||||
init_options_review_tables(conn)
|
||||
from lib.options.options_db import init_options_tables
|
||||
@@ -462,8 +461,8 @@ def sync_options_from_local_trades(
|
||||
eth_amount, open_quote, close_quote, premium_paid, premium_received,
|
||||
realized_pnl, realized_pnl_usdt, close_index_px, open_index_px,
|
||||
budget_usdt, premium_ccy, created_at, closed_at, signal_note, status,
|
||||
trading_usdt_before, pnl_settled, initial_premium_usdt,
|
||||
open_fee, close_fee, exchange_ord_id, close_ord_id
|
||||
trading_usdt_before, trading_usdt_after, bridge_id, pnl_settled,
|
||||
initial_premium_usdt, open_fee, close_fee, exchange_ord_id, close_ord_id
|
||||
FROM options_trades
|
||||
WHERE status = 'closed'
|
||||
ORDER BY id DESC
|
||||
@@ -560,8 +559,6 @@ def sync_options_from_local_trades(
|
||||
settled = int(r["pnl_settled"] or 0) == 1
|
||||
except Exception:
|
||||
settled = False
|
||||
# 有账户快照未结算:交易记录显示待结算(真盈亏=卖回后 after−before)
|
||||
acct_pending = _safe_float(r["trading_usdt_before"]) is not None and not settled
|
||||
close_idx = _safe_float(r["close_index_px"])
|
||||
open_idx = _safe_float(r["open_index_px"])
|
||||
# 折算指数:仅用于初始权利金/期权手续费展示,不用于真盈亏
|
||||
@@ -573,14 +570,60 @@ def sync_options_from_local_trades(
|
||||
else:
|
||||
idx = idx_cache.get(underly)
|
||||
prem = _safe_float(r["premium_paid"])
|
||||
# 交易记录真盈亏=口径 A(卖光币后账户差分);未结算不写假 U
|
||||
if acct_pending:
|
||||
pnl_u = None
|
||||
elif frozen_usdt is not None and (settled or _safe_float(r["trading_usdt_before"]) is None):
|
||||
pnl_u = round(float(frozen_usdt), 4)
|
||||
else:
|
||||
# 旧单无账户快照:不回退币×价冒充真盈亏
|
||||
pnl_u = None
|
||||
# 真盈亏=卖光收回−买币(账户 after−before 或已关桥);禁止权利金×指数冒充
|
||||
from lib.options.options_account_pnl_lib import resolve_true_pnl_usdt
|
||||
|
||||
pnl_u, pnl_src = resolve_true_pnl_usdt(
|
||||
conn,
|
||||
trading_usdt_before=r["trading_usdt_before"],
|
||||
trading_usdt_after=r["trading_usdt_after"],
|
||||
bridge_id=r["bridge_id"],
|
||||
realized_pnl_usdt=frozen_usdt,
|
||||
pnl_settled=settled,
|
||||
)
|
||||
# 快照能算真盈亏但冻结值偏离:纠偏写回(仅纠正错账,不改口径)
|
||||
before_f = _safe_float(r["trading_usdt_before"])
|
||||
after_f = _safe_float(r["trading_usdt_after"])
|
||||
if (
|
||||
pnl_src == "account"
|
||||
and pnl_u is not None
|
||||
and before_f is not None
|
||||
and after_f is not None
|
||||
and (not settled or frozen_usdt is None or abs(float(frozen_usdt) - float(pnl_u)) > 0.05)
|
||||
):
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE options_trades
|
||||
SET realized_pnl_usdt = ?, pnl_settled = 1
|
||||
WHERE id = ?
|
||||
""",
|
||||
(pnl_u, trade_id),
|
||||
)
|
||||
settled = True
|
||||
except Exception:
|
||||
pass
|
||||
elif pnl_src == "bridge" and pnl_u is not None and not settled:
|
||||
try:
|
||||
after_heal = (
|
||||
round(float(before_f) + float(pnl_u), 8) if before_f is not None else None
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE options_trades
|
||||
SET realized_pnl_usdt = ?,
|
||||
trading_usdt_after = COALESCE(trading_usdt_after, ?),
|
||||
pnl_settled = 1
|
||||
WHERE id = ? AND COALESCE(pnl_settled, 0)=0
|
||||
""",
|
||||
(pnl_u, after_heal, trade_id),
|
||||
)
|
||||
settled = True
|
||||
except Exception:
|
||||
pass
|
||||
acct_pending = pnl_u is None and (
|
||||
before_f is not None or r["bridge_id"] is not None
|
||||
)
|
||||
# 初始权利金(U)=开仓指数×权利金币量;不含多买
|
||||
stored_initial = _safe_float(r["initial_premium_usdt"])
|
||||
open_idx_for_prem = open_idx if (open_idx is not None and open_idx > 0) else idx
|
||||
|
||||
Reference in New Issue
Block a user