Hedge options open: require full fill (IOC + wait) before success.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -82,6 +82,13 @@ def build_oo_path_plan(body: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
]
|
||||
|
||||
|
||||
def _option_open_fill_timeout_sec() -> float:
|
||||
try:
|
||||
return max(2.0, float(os.getenv("OKX_OPTIONS_OPEN_FILL_TIMEOUT_SEC") or "12"))
|
||||
except (TypeError, ValueError):
|
||||
return 12.0
|
||||
|
||||
|
||||
def _buy_option(
|
||||
cfg: dict[str, Any],
|
||||
*,
|
||||
@@ -92,6 +99,7 @@ def _buy_option(
|
||||
from lib.exchange.okx_options_lib import (
|
||||
cap_option_buy_sheets_to_ask_depth,
|
||||
option_buy_liquidity_ok,
|
||||
wait_option_order_full_fill,
|
||||
)
|
||||
|
||||
ex = cfg.get("exchange_options")
|
||||
@@ -146,6 +154,7 @@ def _buy_option(
|
||||
td = "isolated"
|
||||
if callable(td_buy):
|
||||
td = td_buy(cfg.get("options_td_mode") or "isolated")
|
||||
# IOC:能成交多少成交多少,剩余立即撤销;再校验是否完全成交
|
||||
order = place_fn(
|
||||
ex,
|
||||
inst_id=inst_id,
|
||||
@@ -154,14 +163,42 @@ def _buy_option(
|
||||
price=float(ask),
|
||||
td_mode=td,
|
||||
tick_sz=q.get("tick_sz"),
|
||||
ord_type="ioc",
|
||||
)
|
||||
if not order.get("ok"):
|
||||
return order
|
||||
ord_id = str((order.get("data") or {}).get("ordId") or "").strip()
|
||||
if not ord_id:
|
||||
return {"ok": False, "msg": "下单成功但未返回订单号", "order": order}
|
||||
fill = wait_option_order_full_fill(
|
||||
ex,
|
||||
inst_id=inst_id,
|
||||
ord_id=ord_id,
|
||||
need_sheets=sheets_i,
|
||||
timeout_sec=_option_open_fill_timeout_sec(),
|
||||
cancel_on_timeout=True,
|
||||
)
|
||||
if not fill.get("ok"):
|
||||
return {
|
||||
"ok": False,
|
||||
"msg": fill.get("msg") or "未完全成交,开仓失败",
|
||||
"inst_id": inst_id,
|
||||
"sheets": sheets_i,
|
||||
"ask": float(ask),
|
||||
"exchange_ord_id": ord_id,
|
||||
"filled_sheets": fill.get("filled_sheets"),
|
||||
"order": order,
|
||||
"fill": fill,
|
||||
"can_open": False,
|
||||
}
|
||||
fill_px = float(fill.get("avg_px") or ask)
|
||||
filled_n = int(fill.get("filled_sheets") or sheets_i)
|
||||
premium = fill_px * filled_n * ct_mult
|
||||
return {
|
||||
"ok": True,
|
||||
"inst_id": inst_id,
|
||||
"sheets": sheets_i,
|
||||
"ask": float(ask),
|
||||
"sheets": filled_n,
|
||||
"ask": fill_px,
|
||||
"ask_sz": float(ask_sz),
|
||||
"premium": premium,
|
||||
"ct_mult": ct_mult,
|
||||
@@ -170,8 +207,9 @@ def _buy_option(
|
||||
"strike": q.get("strike"),
|
||||
"exp_time": q.get("exp_time"),
|
||||
"opt_type": (q.get("meta") or {}).get("optType") or q.get("opt_type"),
|
||||
"exchange_ord_id": (order.get("data") or {}).get("ordId"),
|
||||
"exchange_ord_id": ord_id,
|
||||
"order": order,
|
||||
"fill": fill,
|
||||
"can_open": True,
|
||||
}
|
||||
|
||||
|
||||
@@ -301,4 +301,4 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/hedge_plan.js?v=26"></script>
|
||||
<script src="/static/hedge_plan.js?v=27"></script>
|
||||
|
||||
Reference in New Issue
Block a user