Mirror option expiry settlements from exchange bills; debit OO LIVE open premiums.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -140,6 +140,47 @@ def reconcile_closed_group_pnl(
|
||||
float(local_perp) if local_perp is not None else 0.0
|
||||
)
|
||||
opt = float(option_pnl) if option_pnl is not None else 0.0
|
||||
|
||||
# 期权平仓 fill 为 0 价镜像时:尝试用交易所交割账单补期权腿
|
||||
try:
|
||||
zero_opt_close = False
|
||||
for fr in fills:
|
||||
if str(fr["leg"] or "") in ("option", "option2") and str(
|
||||
fr["action"] or ""
|
||||
) == "close":
|
||||
if abs(float(fr["notional"] or 0)) < 1e-12 and abs(
|
||||
float(fr["fill_px"] or 0)
|
||||
) < 1e-12:
|
||||
zero_opt_close = True
|
||||
break
|
||||
if zero_opt_close:
|
||||
from .option_settle import fetch_option_settlement
|
||||
|
||||
g = db.fetchone(
|
||||
"SELECT option_inst_id, option2_inst_id FROM groups WHERE group_id=?",
|
||||
(group_id,),
|
||||
)
|
||||
settle_cash = 0.0
|
||||
for inst_key in ("option_inst_id", "option2_inst_id"):
|
||||
inst = str((g[inst_key] if g else None) or "")
|
||||
if not inst:
|
||||
continue
|
||||
st = fetch_option_settlement(
|
||||
client,
|
||||
exchange=ex,
|
||||
option_inst_id=inst,
|
||||
qty_eth=1.0,
|
||||
begin_ms=begin,
|
||||
end_ms=end,
|
||||
)
|
||||
if st.found:
|
||||
settle_cash += float(st.cash)
|
||||
if abs(settle_cash) > 1e-12:
|
||||
# 用交割净现金替换本地 0 价期权盈亏近似:仍减 fees(交割费若已在 cash 内则可能双计,保守保留)
|
||||
opt = float(settle_cash)
|
||||
except Exception as e:
|
||||
logger.warning("reconcile option settlement overlay failed: %s", e)
|
||||
|
||||
net = perp_pnl + opt - fees + funding
|
||||
|
||||
if local_net is not None and exch_perp is None and abs(funding) < 1e-12:
|
||||
|
||||
Reference in New Issue
Block a user