Run OKX auto USDC convert before option pick when trading USDC is short.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-30 00:28:11 +08:00
parent a9939bbbdd
commit cc0f0ffd0d
3 changed files with 106 additions and 3 deletions
+37 -2
View File
@@ -807,9 +807,44 @@ class StrategyEngine:
self._set_state(phase="open", last_error="有未平仓,禁止开下一组")
return
# OKX:先按盘口刷新名义并检测 USDC;不够则交易账户市价兑换(不依赖已选中合格期权)
try:
from .auto_usdc import prepare_okx_trading_usdc
prep = prepare_okx_trading_usdc(self.db)
conv = prep.get("convert") or {}
if conv.get("acted"):
self._set_state(
last_error=None,
phase="wait_signal",
)
logger.info("auto_usdc prepared: %s", conv.get("detail"))
elif conv.get("ok") is False and "不足" in str(conv.get("detail") or ""):
# 保留资金提示,但仍继续尝试选约(可能只是冷却/短暂失败)
logger.warning("auto_usdc: %s", conv.get("detail"))
except Exception:
logger.exception("prepare OKX USDC failed")
self._set_state(phase="wait_signal")
pick = await get_session().pick_for_open_async()
if pick is None:
# 若期权仍不可开且刚才未兑成功,把资金状态写进错误,便于排查「为何没自动兑」
try:
from .open_capacity import assess_open_capacity
cap = assess_open_capacity(self.db)
if cap.get("option_can_open") is False:
self._set_state(
phase="wait_funds",
last_error=(
"无合格期权;且交易账户 USDC 不够开仓"
f"(需≈{cap.get('option_need_usdc')}U / 有{cap.get('option_have_usdc')}U)。"
"系统会在冷却后自动市价兑 USDC。"
),
)
return
except Exception:
pass
self._set_state(
last_error="无合格期权:需剩余时长、杠杆(及已开启的ATM偏差)同时满足"
)
@@ -842,11 +877,11 @@ class StrategyEngine:
self._set_state(phase="idle", last_error="以损定仓计算异常,暂不开仓")
return
# OKX:交易账户 USDC 不够开期权时,市价 USDT→USDC(目标=所需×2);够则跳过
# 选约后名义可能变化,再检一次 USDC(force 跳过冷却,避免刚选完仍差一截)
try:
from .auto_usdc import ensure_okx_trading_usdc
ensure_okx_trading_usdc(self.db)
ensure_okx_trading_usdc(self.db, force=True)
except Exception:
logger.exception("auto USDC top-up failed")