fix: OKX 持仓张数优先读 info.pos,滚仓后同步 order_amount

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-02 22:40:41 +08:00
parent 394793b9d2
commit be7896cc25
4 changed files with 51 additions and 22 deletions
+11 -8
View File
@@ -24,23 +24,26 @@ def _coerce_float(*values: Any) -> float | None:
def position_contracts(p: dict[str, Any]) -> float:
raw = p.get("contracts")
if raw is not None:
try:
return float(raw)
except (TypeError, ValueError):
pass
info = p.get("info") or {}
if not isinstance(info, dict):
info = {}
for k in ("positionAmt", "positionamt", "pos", "size"):
# OKX 等:info.pos 为交易所张数,优先于 ccxt contracts(加仓后后者可能滞后)
for k in ("pos", "positionAmt", "positionamt", "size"):
if k in info:
try:
v = float(info[k])
if v != 0:
return v
return abs(v)
except (TypeError, ValueError):
pass
raw = p.get("contracts")
if raw is not None:
try:
v = float(raw)
if v != 0:
return abs(v)
except (TypeError, ValueError):
pass
return 0.0
+10 -2
View File
@@ -418,9 +418,17 @@ def _execute_pending_roll_leg(
"UPDATE roll_groups SET leg_count=?, current_stop_loss=?, updated_at=? WHERE id=?",
(filled + 1, sl, _now(cfg), gid),
)
live_qty = qty + float(amount)
try:
pos2 = cfg["get_position"](ex_sym, direction) or {}
q2 = float(pos2.get("contracts") or 0)
if q2 > 0:
live_qty = q2
except Exception:
pass
conn.execute(
"UPDATE order_monitors SET stop_loss=? WHERE id=? AND status='active'",
(sl, mon["id"]),
"UPDATE order_monitors SET stop_loss=?, order_amount=? WHERE id=? AND status='active'",
(sl, float(live_qty), mon["id"]),
)
notify = cfg.get("send_wechat")