feat期权盈亏按交易账户快照结算:一次一仓、持仓禁划转、卖回后冻结。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -303,7 +303,11 @@ _CLOSED_STATS_SELECT = """
|
||||
created_at, closed_at, inst_id, underlying, premium_ccy
|
||||
FROM options_trades
|
||||
WHERE status = 'closed'
|
||||
AND (realized_pnl IS NOT NULL OR realized_pnl_usdt IS NOT NULL)
|
||||
AND (
|
||||
(COALESCE(pnl_settled, 0) = 1 AND realized_pnl_usdt IS NOT NULL)
|
||||
OR (trading_usdt_before IS NULL
|
||||
AND (realized_pnl IS NOT NULL OR realized_pnl_usdt IS NOT NULL))
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
@@ -322,6 +326,7 @@ def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
WHERE status = 'closed'
|
||||
AND realized_pnl_usdt IS NULL
|
||||
AND realized_pnl IS NOT NULL
|
||||
AND trading_usdt_before IS NULL
|
||||
ORDER BY id DESC
|
||||
LIMIT 300
|
||||
"""
|
||||
@@ -512,96 +517,15 @@ def _sim_net_usdt_converts_for_trade(
|
||||
|
||||
|
||||
def sync_sim_cash_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
"""模拟盘:收回×卖回价 − 初始U权利金 − 手续费U.
|
||||
"""兼容入口:已按交易账户结算(pnl_settled=1)的永不改写.
|
||||
|
||||
盈利=(权利金币+盈利币)×卖回价−开仓U权利金−费;
|
||||
亏损=剩余权利金U−费−开仓U(同一式).不用买币预算差额.
|
||||
有 trading_usdt_before 的待结算单留给卖回结算;
|
||||
仅无账户快照的旧单才回退权利金×指数冻结.
|
||||
"""
|
||||
del ex
|
||||
init_options_tables(conn)
|
||||
from lib.options.options_pnl_settle_lib import resolve_close_index_px, settle_closed_pnl
|
||||
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, inst_id, underlying, premium_ccy, premium_paid, premium_received,
|
||||
open_fee, close_fee, realized_pnl, realized_pnl_usdt,
|
||||
close_index_px, open_index_px, close_quote, eth_amount
|
||||
FROM options_trades
|
||||
WHERE status = 'closed'
|
||||
ORDER BY id DESC
|
||||
LIMIT 300
|
||||
"""
|
||||
).fetchall()
|
||||
updated = 0
|
||||
for row in rows:
|
||||
ccy = str(_row_get(row, "premium_ccy") or "").strip().upper()
|
||||
underly = str(_row_get(row, "underlying") or "").strip().upper()
|
||||
if ccy not in ("ETH", "BTC") and underly not in ("ETH", "BTC"):
|
||||
continue
|
||||
underly = underly or ccy
|
||||
paid = _safe_float(_row_get(row, "premium_paid"))
|
||||
recv = _safe_float(_row_get(row, "premium_received"))
|
||||
if recv is None:
|
||||
cq = _safe_float(_row_get(row, "close_quote"))
|
||||
eth = _safe_float(_row_get(row, "eth_amount"))
|
||||
if cq is not None and eth is not None and eth > 0:
|
||||
try:
|
||||
from lib.options.options_pricing_lib import total_premium
|
||||
|
||||
recv = float(total_premium(cq, eth))
|
||||
except Exception:
|
||||
recv = None
|
||||
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 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(sell_px), 4)
|
||||
close_idx = float(sell_px)
|
||||
new_pnl = float(net)
|
||||
open_write = open_idx
|
||||
else:
|
||||
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=sell_px,
|
||||
open_index_px=open_idx,
|
||||
)
|
||||
usdt = settled.get("realized_pnl_usdt")
|
||||
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"))
|
||||
if old is not None and abs(float(old) - float(usdt)) < 1e-6:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
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, open_write, new_pnl, int(row["id"])),
|
||||
)
|
||||
updated += 1
|
||||
return updated
|
||||
# 账户快照路径由 sell_residual / maybe_sell 结算,此处不覆盖
|
||||
return 0
|
||||
|
||||
|
||||
def load_local_options_stats_history(conn, ex: Any = None) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user