Auto-cancel stale option close limits after pending TTL.

Default 10m via OKX_OPTIONS_PENDING_TTL_SECONDS; show age/countdown in pending panel; monitor loop cancels sell closes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-15 22:06:24 +08:00
parent ee7be3e7f3
commit cbb7f954f5
12 changed files with 306 additions and 15 deletions
+52 -1
View File
@@ -107,6 +107,8 @@ def _build_cfg(app_module: Any) -> dict[str, Any]:
"td_mode": (os.getenv("OKX_OPTIONS_TD_MODE") or "isolated").strip(),
# 市价平仓已硬关闭(忽略 env),仅买一限价
"allow_market_close": False,
# 平仓限价挂单超时自动撤单(秒);默认 600=10 分钟,联调可设 60
"pending_ttl_seconds": _env_float("OKX_OPTIONS_PENDING_TTL_SECONDS", 600.0),
"profit_ratio": _env_float("OKX_OPTIONS_PROFIT_ALERT_RATIO", 1.0),
"poll_seconds": _env_float("OKX_OPTIONS_POLL_SECONDS", 15.0),
"account_label": (os.getenv("OKX_OPTIONS_ACCOUNT_LABEL") or "OKX期权").strip(),
@@ -586,7 +588,18 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
orders = cfg["fetch_option_pending_orders"](ex, inst_id)
except Exception as e:
return jsonify({"ok": False, "msg": f"获取委托失败: {e}"})
return jsonify({"ok": True, "orders": orders, "count": len(orders)})
from lib.options.options_pending_lib import enrich_pending_orders
ttl = float(cfg.get("pending_ttl_seconds") or 600.0)
enriched = enrich_pending_orders(orders, ttl_seconds=ttl)
return jsonify(
{
"ok": True,
"orders": enriched,
"count": len(enriched),
"pending_ttl_seconds": ttl,
}
)
@app.route("/api/options/orders/cancel", methods=["POST"])
@lr
@@ -1079,6 +1092,43 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
pass
return result
def _stale_pending() -> dict[str, Any]:
from lib.exchange.okx_options_lib import invalidate_option_positions_cache
from lib.options.options_pending_lib import cancel_stale_close_pending_orders
ex = cfg.get("exchange_options")
if ex is None:
return {"ok": False, "msg": "期权 exchange 未就绪"}
ttl = float(cfg.get("pending_ttl_seconds") or 600.0)
out = cancel_stale_close_pending_orders(
fetch_pending=lambda _ex: cfg["fetch_option_pending_orders"](_ex),
cancel_order=lambda _ex, inst_id, ord_id: cfg["cancel_option_order"](
_ex, inst_id=inst_id, ord_id=ord_id
),
ttl_seconds=ttl,
ex=ex,
)
if out.get("cancelled"):
try:
invalidate_option_positions_cache()
except Exception:
pass
try:
send = cfg.get("send_wechat")
if callable(send):
parts = [
"【OKX期权·挂单超时撤销】",
f"账户:{cfg.get('account_label') or 'OKX期权'}",
f"超时:{ttl:g}s",
f"撤销:{out.get('cancelled')}",
]
for o in out.get("orders") or []:
parts.append(f"- {o.get('inst_id')} #{o.get('ord_id')}")
send("\n".join(parts))
except Exception:
pass
return out
t = threading.Thread(
target=options_monitor_loop,
kwargs={
@@ -1092,6 +1142,7 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
"profit_ratio": cfg["profit_ratio"],
"sync_trades_fn": _sync,
"target_close_fn": _target_close,
"stale_pending_fn": _stale_pending,
},
daemon=True,
name="options-monitor",