Close residual options when premium recovers above configurable threshold.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-02 14:19:21 +08:00
parent 60b3743e8d
commit 62c91d4bd0
12 changed files with 603 additions and 11 deletions
+18
View File
@@ -46,6 +46,8 @@ KEYS = (
"fixed_direction_enabled",
"fixed_perp_side",
"close_bid_mark_max_pct",
"residual_min_premium_pct",
"residual_close_check_sec",
"perp_qty_eth",
"option_qty_eth",
"show_manual_trade_buttons",
@@ -82,6 +84,8 @@ class StrategySettingsBody(BaseModel):
fixed_direction_enabled: bool | None = None
fixed_perp_side: str | None = Field(default=None, pattern="^(long|short)$")
close_bid_mark_max_pct: float | None = Field(default=None, ge=1, le=100)
residual_min_premium_pct: float | None = Field(default=None, ge=1, le=100)
residual_close_check_sec: int | None = Field(default=None, ge=30, le=86400)
perp_qty_eth: float | None = Field(default=None, ge=0.01, le=100)
option_qty_eth: float | None = Field(default=None, ge=0.01, le=100)
show_manual_trade_buttons: bool | None = None
@@ -213,6 +217,20 @@ def _read_settings() -> dict:
db.get_setting("close_bid_mark_max_pct", str(s.close_bid_mark_max_pct))
or s.close_bid_mark_max_pct
),
"residual_min_premium_pct": float(
db.get_setting(
"residual_min_premium_pct", str(s.residual_min_premium_pct)
)
or s.residual_min_premium_pct
),
"residual_close_check_sec": int(
float(
db.get_setting(
"residual_close_check_sec", str(s.residual_close_check_sec)
)
or s.residual_close_check_sec
)
),
"perp_qty_eth": float(
db.get_setting("perp_qty_eth", str(s.perp_qty_eth)) or s.perp_qty_eth
),
+3
View File
@@ -82,6 +82,9 @@ class Settings(BaseSettings):
fixed_direction_enabled: bool = False
fixed_perp_side: str = "long" # long|shortlong→买Putshort→买Call
close_bid_mark_max_pct: float = 30.0 # 平仓:买一相对标记最大偏差%
# 残留期权中途平:当前买一权利金 ≥ 初始权利金 × 该% 才尝试卖出
residual_min_premium_pct: float = 20.0
residual_close_check_sec: int = 300 # 残留巡检间隔(秒)
perp_qty_eth: float = 1.0
option_qty_eth: float = 2.0
db_path: str = "" # empty -> backend/data/hedge.db
+63
View File
@@ -1149,6 +1149,69 @@ class BinanceLiveExecutor(Matcher):
},
)
def try_close_one_residual(self, row: dict) -> dict | None:
"""LIVE-BN:权利金达标后交易所市价卖出归档期权。"""
err = self._guard_live()
if err:
logger.warning("residual premium close blocked: %s", err)
return None
skip, close_bid, _oq = self._evaluate_residual_premium_close(row)
if skip or close_bid is None:
if skip:
logger.debug(
"residual premium close skip %s: %s",
row.get("group_id"),
skip,
)
return None
option_inst_id = str(row.get("option_inst_id") or "")
opt_contracts = float(row.get("option_qty_contracts") or 0)
opt_qty = float(row.get("option_qty_eth") or 0)
if opt_contracts <= 0:
ct = self._ct_mult(option_inst_id)
opt_contracts = float(contracts_for_eth(opt_qty, ct) or 0)
if opt_contracts <= 0:
logger.warning(
"residual premium close skip %s: bad contracts", row.get("group_id")
)
return None
client = self._client()
try:
opt_live = client.place_option_market(
symbol=option_inst_id,
side="SELL",
quantity=max(1.0, opt_contracts),
reduce_only=True,
)
except Exception as e:
logger.warning(
"residual premium close exchange sell failed %s: %s",
row.get("group_id"),
e,
)
return None
of_px = float(opt_live.avg_px)
of_fee = float(opt_live.fee)
filled_c = (
float(opt_live.sz)
if opt_live.sz and float(opt_live.sz) > 0
else opt_contracts
)
opt_qty = eth_from_contracts(filled_c, self._ct_mult(option_inst_id))
row = {**row, "option_qty_eth": opt_qty, "option_qty_contracts": filled_c}
of_notional = of_px * opt_qty
now_ms = int(time.time() * 1000)
return self._book_residual_market_close(
row,
fill_px=of_px,
fee=of_fee,
notional=of_notional,
slip=0.0,
now_ms=now_ms,
note="LIVE-BN residual mid-close by premium recovery",
exec_mode="LIVE",
)
def close_perp_abandon_option(
self, *, reason: str = "target_perp_only", require_deep_otm: bool = True
) -> CloseResult:
+64
View File
@@ -1192,6 +1192,70 @@ class OkxLiveExecutor(Matcher):
},
)
def try_close_one_residual(self, row: dict) -> dict | None:
"""LIVE:权利金达标后交易所市价卖出归档期权。"""
err = self._guard_live()
if err:
logger.warning("residual premium close blocked: %s", err)
return None
skip, close_bid, _oq = self._evaluate_residual_premium_close(row)
if skip or close_bid is None:
if skip:
logger.debug(
"residual premium close skip %s: %s",
row.get("group_id"),
skip,
)
return None
option_inst_id = str(row.get("option_inst_id") or "")
opt_contracts = float(row.get("option_qty_contracts") or 0)
opt_qty = float(row.get("option_qty_eth") or 0)
if opt_contracts <= 0:
ct = self._ct_mult(option_inst_id)
opt_contracts = float(contracts_for_eth(opt_qty, ct) or 0)
if opt_contracts <= 0:
logger.warning(
"residual premium close skip %s: bad contracts", row.get("group_id")
)
return None
client = self._client()
try:
opt_live = client.place_market(
inst_id=option_inst_id,
side="sell",
sz=str(max(1, int(round(opt_contracts)))),
td_mode="cash",
reduce_only=True,
)
except Exception as e:
logger.warning(
"residual premium close exchange sell failed %s: %s",
row.get("group_id"),
e,
)
return None
of_px = float(opt_live.avg_px)
of_fee = float(opt_live.fee)
filled_c = (
float(opt_live.sz)
if opt_live.sz and float(opt_live.sz) > 0
else opt_contracts
)
opt_qty = eth_from_contracts(filled_c, self._ct_mult(option_inst_id))
row = {**row, "option_qty_eth": opt_qty, "option_qty_contracts": filled_c}
of_notional = of_px * opt_qty
now_ms = int(time.time() * 1000)
return self._book_residual_market_close(
row,
fill_px=of_px,
fee=of_fee,
notional=of_notional,
slip=0.0,
now_ms=now_ms,
note="LIVE residual mid-close by premium recovery",
exec_mode="LIVE",
)
def close_perp_abandon_option(
self, *, reason: str = "target_perp_only", require_deep_otm: bool = True
) -> CloseResult:
+1
View File
@@ -37,6 +37,7 @@ CLOSE_REASON_ZH: dict[str, str] = {
"manual": "手动全平",
"perp_pending_retry": "续平永续",
"liquidity_retry": "等待流动性后全平",
"residual_premium_close": "残留期权·权利金回收中途平",
"unknown": "未知原因",
}
+226
View File
@@ -829,6 +829,232 @@ class Matcher:
)
return [dict(r) for r in rows]
def _residual_min_premium_pct(self) -> float:
s = get_settings()
return float(
self.ledger.get_setting_float(
"residual_min_premium_pct", s.residual_min_premium_pct
)
)
def _evaluate_residual_premium_close(
self, row: dict[str, Any]
) -> tuple[str | None, float | None, Any]:
"""
残留中途平前置:权利金比例 + 买一流动性。
返回 (skip_reason, close_bid, option_quote)skip_reason 非空则本轮不卖。
"""
s = get_settings()
initial_premium = float(row.get("initial_premium") or 0)
opt_qty = float(row.get("option_qty_eth") or 0)
if initial_premium <= 0 or opt_qty <= 0:
return ("invalid_initial_premium_or_qty", None, None)
option_inst_id = str(row.get("option_inst_id") or "")
if not option_inst_id:
return ("missing_option_inst", None, None)
oq = self._quote_held_option(option_inst_id)
if oq is None or oq.bid is None:
return ("option_bid_unavailable", None, None)
close_bid = float(oq.bid)
current_premium = close_bid * opt_qty
min_pct = self._residual_min_premium_pct()
threshold = initial_premium * (min_pct / 100.0)
if current_premium + 1e-12 < threshold:
return (
f"premium_below_threshold curr={current_premium:.4f} "
f"need>={threshold:.4f} ({min_pct:g}%)",
None,
None,
)
ct_mult = self._ct_mult(option_inst_id)
if not bid_covers_eth(
bid_sz_contracts=oq.bid_sz,
ct_mult=ct_mult,
need_eth=opt_qty,
):
return ("option_bid_liquidity_insufficient", None, None)
max_dev = self.ledger.get_setting_float(
"close_bid_mark_max_pct", s.close_bid_mark_max_pct
)
ok_dev, why = bid_mark_ok(bid=close_bid, mark=oq.mark_px, max_dev_pct=max_dev)
if not ok_dev:
return (why or "bid_mark_deviation", None, None)
strike = row.get("strike")
spot = self._close_spot_px(get_session().snapshot())
intrinsic: float | None = None
if strike is not None and spot is not None:
intrinsic = option_intrinsic(
option_side=str(row["option_side"]),
strike=float(strike),
spot=float(spot),
)
resolved = resolve_option_close_bid(
bid=close_bid,
mark=oq.mark_px,
intrinsic=intrinsic,
bypass_liquidity=False,
)
if resolved is None:
return ("option_close_px_unavailable", None, None)
return (None, float(resolved), oq)
def _book_residual_market_close(
self,
row: dict[str, Any],
*,
fill_px: float,
fee: float,
notional: float,
slip: float,
now_ms: int,
note: str,
exec_mode: str | None = None,
) -> dict[str, Any]:
"""买一卖出残留后的入账与结清(SIM/LIVE 共用)。"""
group_id = str(row["group_id"])
opt_qty = float(row["option_qty_eth"])
opt_entry = float(row["option_entry_px"])
opt_pnl = (float(fill_px) - opt_entry) * opt_qty
opt_cash = float(notional) - float(fee)
self.ledger.apply_cash(
opt_cash,
kind="close_option",
group_id=group_id,
note=note,
allow_negative=not get_settings().is_sim,
)
fill_cols = (
"group_id, leg, action, side, inst_id, qty_eth, qty_contracts, "
"base_px, fill_px, fee, slip, notional, ts_ms"
)
fill_vals: list[Any] = [
group_id,
"option",
"close",
"flat",
str(row["option_inst_id"]),
opt_qty,
float(row["option_qty_contracts"] or 0),
float(fill_px),
float(fill_px),
float(fee),
float(slip),
float(notional),
now_ms,
]
if exec_mode:
fill_cols += ", exec_mode"
fill_vals.append(exec_mode)
with self.db._lock:
self.db._conn.execute(
f"""INSERT INTO fills({fill_cols})
VALUES ({",".join("?" for _ in fill_vals)})""",
tuple(fill_vals),
)
fills = self.db._conn.execute(
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC", (group_id,)
).fetchall()
from ..sim.pnl import summarize_fills_pnl
summary = summarize_fills_pnl(list(fills))
net = summary.get("net_pnl")
if net is None:
net = opt_pnl - float(fee)
g = self.db._conn.execute(
"SELECT fees, slip_cost FROM groups WHERE group_id=?", (group_id,)
).fetchone()
fees = float(g["fees"] or 0) + float(fee) if g else float(fee)
slip_total = float(g["slip_cost"] or 0) + float(slip) if g else float(slip)
self.db._conn.execute(
"""UPDATE residual_options SET status=?, settled_at_ms=?, settle_px=?, settle_pnl=?, note=?
WHERE group_id=?""",
(
"settled",
now_ms,
float(fill_px),
opt_pnl,
note,
group_id,
),
)
self.db._conn.execute(
"""UPDATE groups SET status=?, close_at_ms=COALESCE(close_at_ms, ?),
close_reason=COALESCE(close_reason, ?), realized_pnl=?, fees=?, slip_cost=?
WHERE group_id=?""",
(
"closed",
now_ms,
"residual_premium_close",
float(net),
fees,
slip_total,
group_id,
),
)
self.db._conn.commit()
return {
"group_id": group_id,
"option_pnl": opt_pnl,
"settle_px": float(fill_px),
"net_pnl": float(net),
"reason": "residual_premium_close",
"current_premium": float(fill_px) * opt_qty,
"initial_premium": float(row.get("initial_premium") or 0),
}
def try_close_one_residual(self, row: dict[str, Any]) -> dict[str, Any] | None:
"""SIM:权利金达标且流动性通过则本地吃买一平残留。"""
skip, close_bid, oq = self._evaluate_residual_premium_close(row)
if skip or close_bid is None or oq is None:
if skip:
logger.debug(
"residual premium close skip %s: %s",
row.get("group_id"),
skip,
)
return None
of = option_fill(
action="close",
bid=float(close_bid),
ask=float(oq.ask or close_bid),
qty_eth=float(row["option_qty_eth"]),
fee_rate=self._fee_rate(),
)
now_ms = int(time.time() * 1000)
return self._book_residual_market_close(
row,
fill_px=of.fill_px,
fee=of.fee,
notional=of.notional,
slip=of.slip,
now_ms=now_ms,
note="residual mid-close by premium recovery",
)
def try_close_pending_residuals(self) -> list[dict[str, Any]]:
"""巡检全部 pending 残留,尝试权利金回收平仓。"""
out: list[dict[str, Any]] = []
for row in self.list_residual_options(pending_only=True):
try:
r = self.try_close_one_residual(row)
except Exception:
logger.exception(
"try_close_one_residual failed group=%s", row.get("group_id")
)
continue
if r:
out.append(r)
return out
def settle_due_residuals(self, *, now_ms: int | None = None) -> list[dict[str, Any]]:
"""到期结算所有 pending 残留期权(不扫描进活跃组平仓)。"""
now = int(now_ms if now_ms is not None else time.time() * 1000)
+37
View File
@@ -31,6 +31,7 @@ class StrategyEngine:
self._lock = asyncio.Lock()
self._retry_gate = LiveRetryGate()
self._extra_sleep_sec = 0.0
self._last_residual_premium_check_ms = 0
def refresh_executor(self) -> None:
"""MODE 变更后刷新执行器。"""
@@ -545,6 +546,41 @@ class StrategyEngine:
async def _settle_residuals(self) -> None:
await asyncio.to_thread(self.matcher.settle_due_residuals)
async def _maybe_close_residuals_by_premium(self) -> None:
"""残留期权:权利金回升达标时周期性尝试中途平仓。"""
s = get_settings()
interval_sec = int(
self.ledger.get_setting_int(
"residual_close_check_sec", s.residual_close_check_sec
)
)
interval_sec = max(30, interval_sec)
now_ms = int(time.time() * 1000)
if now_ms - self._last_residual_premium_check_ms < interval_sec * 1000:
return
self._last_residual_premium_check_ms = now_ms
fn = getattr(self.matcher, "try_close_pending_residuals", None)
if not callable(fn):
return
try:
closed = await asyncio.to_thread(fn)
except Exception:
logger.exception("try_close_pending_residuals failed")
return
if not closed:
return
for item in closed:
try:
from ..notify import wecom
wecom.notify_close(
reason="residual_premium_close",
detail="残留期权权利金回收中途平",
data=item if isinstance(item, dict) else {},
)
except Exception:
logger.exception("wecom notify residual premium close failed")
async def _maybe_expiry_close(self) -> bool:
"""若持仓已到期则强制全平。返回是否触发到期平仓。"""
await self._settle_residuals()
@@ -613,6 +649,7 @@ class StrategyEngine:
async def _tick_manage_positions(self) -> None:
"""有仓时的盯盘:残留结算 / 半仓修复 / 目标平 / 到期平。不新开仓。"""
await self._settle_residuals()
await self._maybe_close_residuals_by_premium()
s = get_settings()
st = self.db.fetchone("SELECT * FROM strategy_state WHERE id=1")