Sell residual options at latest bid via IOC limit, not market.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1150,7 +1150,7 @@ class BinanceLiveExecutor(Matcher):
|
||||
)
|
||||
|
||||
def try_close_one_residual(self, row: dict) -> dict | None:
|
||||
"""LIVE-BN:权利金达标后交易所市价卖出归档期权。"""
|
||||
"""LIVE-BN:权利金达标后按最新买一 IOC 限价卖出归档期权(不扫市价)。"""
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
logger.warning("residual premium close blocked: %s", err)
|
||||
@@ -1175,17 +1175,25 @@ class BinanceLiveExecutor(Matcher):
|
||||
"residual premium close skip %s: bad contracts", row.get("group_id")
|
||||
)
|
||||
return None
|
||||
oq2 = self._quote_held_option(option_inst_id)
|
||||
bid_px = float(oq2.bid) if oq2 is not None and oq2.bid is not None else float(close_bid)
|
||||
if bid_px <= 0:
|
||||
logger.debug(
|
||||
"residual premium close skip %s: bid vanished", row.get("group_id")
|
||||
)
|
||||
return None
|
||||
client = self._client()
|
||||
try:
|
||||
opt_live = client.place_option_market(
|
||||
opt_live = client.place_option_ioc(
|
||||
symbol=option_inst_id,
|
||||
side="SELL",
|
||||
quantity=max(1.0, opt_contracts),
|
||||
price=bid_px,
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"residual premium close exchange sell failed %s: %s",
|
||||
"residual premium close bid-ioc sell failed %s: %s",
|
||||
row.get("group_id"),
|
||||
e,
|
||||
)
|
||||
@@ -1208,7 +1216,7 @@ class BinanceLiveExecutor(Matcher):
|
||||
notional=of_notional,
|
||||
slip=0.0,
|
||||
now_ms=now_ms,
|
||||
note="LIVE-BN residual mid-close by premium recovery",
|
||||
note=f"LIVE-BN residual mid-close at bid IOC px={bid_px}",
|
||||
exec_mode="LIVE",
|
||||
)
|
||||
|
||||
|
||||
@@ -201,6 +201,35 @@ class BinanceTradeClient:
|
||||
data = self._signed(self._eapi, "POST", "/eapi/v1/order", params)
|
||||
return self._fill_from_eapi(symbol, data)
|
||||
|
||||
def place_option_ioc(
|
||||
self,
|
||||
*,
|
||||
symbol: str,
|
||||
side: str, # BUY|SELL
|
||||
quantity: float,
|
||||
price: float,
|
||||
reduce_only: bool = False,
|
||||
) -> LiveFill:
|
||||
"""期权限价 IOC:按买一/卖一价吃单,未成交部分取消。"""
|
||||
qty = str(int(round(quantity)))
|
||||
if qty == "0":
|
||||
qty = "1"
|
||||
px = f"{float(price):.8f}".rstrip("0").rstrip(".")
|
||||
if not px or px == "0":
|
||||
raise RuntimeError("币安期权 IOC 价格无效")
|
||||
params: dict[str, Any] = {
|
||||
"symbol": symbol,
|
||||
"side": side.upper(),
|
||||
"type": "LIMIT",
|
||||
"timeInForce": "IOC",
|
||||
"quantity": qty,
|
||||
"price": px,
|
||||
}
|
||||
if reduce_only:
|
||||
params["reduceOnly"] = "true"
|
||||
data = self._signed(self._eapi, "POST", "/eapi/v1/order", params)
|
||||
return self._fill_from_eapi(symbol, data)
|
||||
|
||||
def _fill_from_eapi(self, symbol: str, data: dict[str, Any]) -> LiveFill:
|
||||
ord_id = str(data.get("orderId") or data.get("id") or "")
|
||||
avg = safe_float(data.get("avgPrice")) or safe_float(data.get("price"))
|
||||
|
||||
@@ -1193,7 +1193,7 @@ class OkxLiveExecutor(Matcher):
|
||||
)
|
||||
|
||||
def try_close_one_residual(self, row: dict) -> dict | None:
|
||||
"""LIVE:权利金达标后交易所市价卖出归档期权。"""
|
||||
"""LIVE:权利金达标后按最新买一 IOC 限价卖出归档期权(不扫市价)。"""
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
logger.warning("residual premium close blocked: %s", err)
|
||||
@@ -1218,18 +1218,27 @@ class OkxLiveExecutor(Matcher):
|
||||
"residual premium close skip %s: bad contracts", row.get("group_id")
|
||||
)
|
||||
return None
|
||||
# 下单前再刷一次买一,按最新盘口挂 IOC
|
||||
oq2 = self._quote_held_option(option_inst_id)
|
||||
bid_px = float(oq2.bid) if oq2 is not None and oq2.bid is not None else float(close_bid)
|
||||
if bid_px <= 0:
|
||||
logger.debug(
|
||||
"residual premium close skip %s: bid vanished", row.get("group_id")
|
||||
)
|
||||
return None
|
||||
client = self._client()
|
||||
try:
|
||||
opt_live = client.place_market(
|
||||
opt_live = client.place_ioc(
|
||||
inst_id=option_inst_id,
|
||||
side="sell",
|
||||
sz=str(max(1, int(round(opt_contracts)))),
|
||||
px=bid_px,
|
||||
td_mode="cash",
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"residual premium close exchange sell failed %s: %s",
|
||||
"residual premium close bid-ioc sell failed %s: %s",
|
||||
row.get("group_id"),
|
||||
e,
|
||||
)
|
||||
@@ -1252,7 +1261,7 @@ class OkxLiveExecutor(Matcher):
|
||||
notional=of_notional,
|
||||
slip=0.0,
|
||||
now_ms=now_ms,
|
||||
note="LIVE residual mid-close by premium recovery",
|
||||
note=f"LIVE residual mid-close at bid IOC px={bid_px}",
|
||||
exec_mode="LIVE",
|
||||
)
|
||||
|
||||
|
||||
@@ -157,6 +157,36 @@ class OkxTradeClient:
|
||||
fill = self._wait_fill(inst_id, ord_id)
|
||||
return fill
|
||||
|
||||
def place_ioc(
|
||||
self,
|
||||
*,
|
||||
inst_id: str,
|
||||
side: str, # buy|sell
|
||||
sz: str,
|
||||
px: float | str,
|
||||
td_mode: str,
|
||||
pos_side: str | None = None,
|
||||
reduce_only: bool = False,
|
||||
) -> LiveFill:
|
||||
"""限价 IOC:残留回收等场景按指定买一/卖一吃单,不成交部分立即取消。"""
|
||||
body: dict[str, Any] = {
|
||||
"instId": inst_id,
|
||||
"tdMode": td_mode,
|
||||
"side": side,
|
||||
"ordType": "ioc",
|
||||
"sz": str(sz),
|
||||
"px": str(px),
|
||||
}
|
||||
if pos_side:
|
||||
body["posSide"] = pos_side
|
||||
if reduce_only:
|
||||
body["reduceOnly"] = True
|
||||
rows = self._request("POST", "/api/v5/trade/order", body)
|
||||
if not rows:
|
||||
raise RuntimeError("OKX IOC 下单无返回")
|
||||
ord_id = str(rows[0].get("ordId") or "")
|
||||
return self._wait_fill(inst_id, ord_id)
|
||||
|
||||
def _fill_from_order_row(self, inst_id: str, ord_id: str, row: dict[str, Any]) -> LiveFill:
|
||||
avg = safe_float(row.get("avgPx")) or 0.0
|
||||
sz = safe_float(row.get("accFillSz")) or safe_float(row.get("sz")) or 0.0
|
||||
|
||||
@@ -1037,7 +1037,7 @@ class Matcher:
|
||||
notional=of.notional,
|
||||
slip=of.slip,
|
||||
now_ms=now_ms,
|
||||
note="residual mid-close by premium recovery",
|
||||
note=f"residual mid-close at bid px={close_bid}",
|
||||
)
|
||||
|
||||
def try_close_pending_residuals(self) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user