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
+37
View File
@@ -232,6 +232,43 @@ class OkxTradeClient:
return to_usdt(float(upl), ccy)
return 0.0
def get_perp_pos_sz(self, inst_id: str, *, pos_side: str | None = None) -> float | None:
"""当前永续绝对持仓张数。"""
try:
rows = self._request(
"GET", f"/api/v5/account/positions?instId={inst_id}"
)
except Exception as e:
logger.warning("okx get_perp_pos_sz failed: %s", e)
return None
want = (pos_side or "").strip().lower()
for row in rows:
ps = str(row.get("posSide") or "").lower()
pos = safe_float(row.get("pos")) or 0.0
if abs(pos) < 1e-12:
continue
if want and want not in ("net", "") and ps and ps != want and ps != "net":
continue
return abs(float(pos))
return 0.0
def set_leverage(
self,
inst_id: str,
leverage: float,
*,
mgn_mode: str = "cross",
pos_side: str | None = None,
) -> None:
body: dict[str, Any] = {
"instId": inst_id,
"lever": str(leverage),
"mgnMode": mgn_mode,
}
if pos_side:
body["posSide"] = pos_side
self._request("POST", "/api/v5/account/set-leverage", body)
def get_funding_usdt(
self, inst_id: str, *, begin_ms: int, end_ms: int | None = None
) -> float: