Harden LIVE opens with slot claim and exchange reconcile.

Prevent duplicate opens by atomically claiming an opening slot, verifying exchange perp is flat before live orders, setting leverage from ledger, and preferring exchange position size when closing perps.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-26 22:39:27 +08:00
parent f48ea5bbcc
commit 0cf3756b09
10 changed files with 390 additions and 21 deletions
+33
View File
@@ -314,6 +314,39 @@ class BinanceTradeClient:
return to_usdt(float(upl), "USDT")
return 0.0
def get_perp_pos_sz(self, symbol: str, *, position_side: str | None = None) -> float | None:
"""当前永续绝对持仓(ETH)。"""
try:
rows = self._signed(
self._fapi, "GET", "/fapi/v2/positionRisk", {"symbol": symbol}
)
except Exception as e:
logger.warning("binance get_perp_pos_sz failed: %s", e)
return None
if isinstance(rows, dict):
rows = [rows]
want = (position_side or "").strip().upper()
for row in rows:
amt = safe_float(row.get("positionAmt")) or 0.0
if abs(amt) < 1e-12:
continue
ps = str(row.get("positionSide") or "").upper()
if want and ps and ps not in ("BOTH",) and ps != want:
continue
return abs(float(amt))
return 0.0
def set_leverage(self, symbol: str, leverage: int | float) -> None:
lev = int(round(float(leverage)))
if lev < 1:
lev = 1
self._signed(
self._fapi,
"POST",
"/fapi/v1/leverage",
{"symbol": symbol, "leverage": lev},
)
def get_funding_usdt(
self, symbol: str, *, begin_ms: int, end_ms: int | None = None
) -> float: