修复币本位平仓/到期后未自动卖回,并消除模拟盘卖币时数据库锁定。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -475,3 +475,65 @@ def maybe_sell_spot_after_close(
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def sweep_spot_after_options_flat(
|
||||
cfg: dict[str, Any],
|
||||
ex: Any | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""币本位:某标的已无期权持仓时,把交易户该标的残留币卖回 USDT.
|
||||
|
||||
覆盖到期结算、限价平仓后同步关仓等未走买一平仓卖回路径的情况。
|
||||
"""
|
||||
if not is_coin_margin_mode():
|
||||
return {"ok": True, "skipped": True, "msg": "非币本位"}
|
||||
exchange = ex if ex is not None else cfg.get("exchange_options")
|
||||
if exchange is None:
|
||||
return {"ok": False, "msg": "exchange 未就绪"}
|
||||
live_uly: set[str] = set()
|
||||
try:
|
||||
rows = cfg["fetch_option_positions"](exchange) or []
|
||||
for p in rows:
|
||||
try:
|
||||
if abs(float(p.get("pos") or 0)) <= 1e-12:
|
||||
continue
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
inst = str(p.get("instId") or p.get("inst_id") or "").strip()
|
||||
if not inst:
|
||||
continue
|
||||
live_uly.add(inst.split("-")[0].upper())
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": f"持仓查询失败: {e}"}
|
||||
|
||||
from lib.options.options_db import init_options_tables
|
||||
|
||||
sold: list[dict[str, Any]] = []
|
||||
for uly in ("BTC", "ETH"):
|
||||
if uly in live_uly:
|
||||
continue
|
||||
try:
|
||||
avail = _spot_bridge.fetch_trading_coin_available(exchange, uly)
|
||||
if avail is None or float(avail) <= 1e-8:
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
conn = cfg["get_db"]()
|
||||
try:
|
||||
init_options_tables(conn)
|
||||
out = _spot_bridge.sell_residual_after_option_flat(
|
||||
conn, exchange, underlying=uly, inst_id=None
|
||||
)
|
||||
sold.append({"underlying": uly, **(out or {})})
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
sold.append({"underlying": uly, "ok": False, "msg": str(e)})
|
||||
finally:
|
||||
try:
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
return {"ok": True, "results": sold, "skipped": not sold}
|
||||
|
||||
Reference in New Issue
Block a user