Recover stuck opening and harden LIVE open/close reconcile.

Stamp open intent, recover opening from exchange option/perp state, skip resell/reopen when already flat, and persist Binance margin mode.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-29 20:32:45 +08:00
parent 44fd0371b9
commit e2a19a1614
8 changed files with 893 additions and 104 deletions
+63 -3
View File
@@ -162,7 +162,7 @@ class BinanceTradeClient:
sz = safe_float(q.get("executedQty")) or sz
data = q
if not avg or avg <= 0:
raise RuntimeError(f"币安永续无成交均价: {data}")
raise RuntimeError(f"币安永续无成交均价 orderId={ord_id} last={data}")
from .money import abs_fee_usdt
fee = abs(safe_float(data.get("cumCommission")) or 0.0)
@@ -226,10 +226,12 @@ class BinanceTradeClient:
if st == "PARTIALLY_FILLED":
continue
if not avg or avg <= 0:
raise RuntimeError(f"币安期权无成交均价: {data}")
raise RuntimeError(f"币安期权无成交均价 orderId={ord_id} last={data}")
st_final = str(data.get("status") or "").upper()
if st_final and st_final != "FILLED":
raise RuntimeError(f"币安期权未完全成交 status={st_final} {data}")
raise RuntimeError(
f"币安期权未完全成交 status={st_final} orderId={ord_id} last={data}"
)
from .money import abs_fee_usdt
fee = abs(safe_float(data.get("fee")) or 0.0)
@@ -336,6 +338,64 @@ class BinanceTradeClient:
return abs(float(amt))
return 0.0
def get_option_pos_sz(self, symbol: str) -> float | None:
"""期权持仓绝对张数;查不到接口时返回 None。"""
try:
rows = self._signed(self._eapi, "GET", "/eapi/v1/position", {"symbol": symbol})
except Exception as e:
logger.warning("binance get_option_pos_sz failed: %s", e)
return None
if isinstance(rows, dict):
rows = [rows]
total = 0.0
hit = False
for row in rows:
if not isinstance(row, dict):
continue
if str(row.get("symbol") or "") and str(row.get("symbol")) != symbol:
continue
qty = safe_float(row.get("quantity")) or safe_float(row.get("positionAmt")) or 0.0
hit = True
total += abs(float(qty))
return total if hit else 0.0
def any_option_pos_abs(self) -> float | None:
"""账户任意期权绝对持仓合计(ETH 期权)。"""
try:
rows = self._signed(self._eapi, "GET", "/eapi/v1/position", {})
except Exception as e:
logger.warning("binance any_option_pos_abs failed: %s", e)
return None
if isinstance(rows, dict):
rows = [rows]
total = 0.0
for row in rows:
if not isinstance(row, dict):
continue
sym = str(row.get("symbol") or "")
if sym and not sym.upper().startswith("ETH"):
continue
qty = safe_float(row.get("quantity")) or safe_float(row.get("positionAmt")) or 0.0
total += abs(float(qty))
return total
def set_margin_type(self, symbol: str, margin_type: str) -> None:
"""ISOLATED | CROSSED。"""
mt = "ISOLATED" if str(margin_type).lower() == "isolated" else "CROSSED"
try:
self._signed(
self._fapi,
"POST",
"/fapi/v1/marginType",
{"symbol": symbol, "marginType": mt},
)
except Exception as e:
# 已是目标模式时币安常报错,忽略
msg = str(e).lower()
if "no need to change" in msg or "-4046" in msg:
return
raise
def set_leverage(self, symbol: str, leverage: int | float) -> None:
lev = int(round(float(leverage)))
if lev < 1: