Use bid-only limit closes when option index targets hit.

Target price is only for monitoring; auto flat waits for 买一 instead of falling back to mark.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-15 15:53:08 +08:00
parent 280e59eb25
commit 61f63c456b
4 changed files with 18 additions and 21 deletions
+12 -17
View File
@@ -1,4 +1,4 @@
"""期权目标位委托:指数达价后限价平仓(无止损,到期结算收口)."""
"""期权目标位委托:指数目标价仅用于监控触发;触发后按买一限价平仓(无止损,到期结算)."""
from __future__ import annotations
import sqlite3
@@ -214,7 +214,7 @@ def close_option_by_bid_depth(
*,
sheets: int | None = None,
) -> dict[str, Any]:
"""按买盘拆分限价卖出(最多5档),供目标位自动平仓复用."""
"""目标触发后仅用买一/买盘限价卖出(最多5档);无买一则等待下次轮询,不用标记价."""
from lib.exchange.okx_options_lib import (
_pos_side_from_position,
invalidate_option_positions_cache,
@@ -301,22 +301,13 @@ def close_option_by_bid_depth(
preview = estimate_close_by_bids(book.get("bids") or [], remaining, ct_mult=ct_mult)
levels = preview.get("levels") or []
if not levels:
# 无买盘时回退:报价买一 → 报价标记价 → 持仓标记价
# 无买盘深度时仅允许真实买一价,不用标记价挂单
q2 = cfg["quote_option_contract"](ex, inst_id)
cur_mark = _safe_float(cur_pos.get("markPx")) if cur_pos else None
pos_mark = _safe_float(pos.get("markPx"))
fallback_px = (
_safe_float(q2.get("bid"))
or _safe_float(q2.get("mark_px"))
or _safe_float(q.get("bid"))
or _safe_float(q.get("mark_px"))
or cur_mark
or pos_mark
)
if fallback_px is None or fallback_px <= 0:
stopped_reason = "no_bid_depth"
bid_px = _safe_float(q2.get("bid")) or _safe_float(q.get("bid"))
if bid_px is None or bid_px <= 0:
stopped_reason = "no_bid"
break
levels = [{"sheets": remaining, "px": fallback_px, "fallback": True}]
levels = [{"sheets": remaining, "px": bid_px}]
level = levels[0]
level_sheets = int(level.get("sheets") or 0)
level_px = float(level.get("px") or 0)
@@ -386,7 +377,11 @@ def close_option_by_bid_depth(
"fully_closed": True,
}
return {"ok": False, "msg": mkt.get("msg") or "市价平仓失败", "stopped_reason": stopped_reason}
return {"ok": False, "msg": "暂无可用买盘深度,无法限价平仓", "stopped_reason": stopped_reason}
return {
"ok": False,
"msg": "暂无买一,等待盘口后按买一限价平仓",
"stopped_reason": stopped_reason or "no_bid",
}
avg_bid = (total_received / (submitted_sheets * ct_mult)) if submitted_sheets > 0 and ct_mult > 0 else 0
prem_recv = round(total_received, 4)