修复okx 趋势回调

This commit is contained in:
dekun
2026-05-30 10:06:54 +08:00
parent 9678cd54ce
commit 8501f7fe0e
2 changed files with 103 additions and 22 deletions
+48 -15
View File
@@ -2675,12 +2675,46 @@ def _position_matches_wanted_contract(exchange_symbol, position):
if sym == exchange_symbol:
return True
try:
return normalize_okx_symbol(sym or "") == normalize_okx_symbol(exchange_symbol or "")
if normalize_okx_symbol(sym or "") == normalize_okx_symbol(exchange_symbol or ""):
return True
except Exception:
pass
info = position.get("info") or {}
inst = (info.get("instId") or "").strip().upper()
if not inst:
return False
try:
ensure_markets_loaded()
want = exchange.market(exchange_symbol)
mid = (want.get("id") or "").strip().upper()
if mid and inst == mid:
return True
base = (want.get("base") or "").strip().upper()
quote = (want.get("quote") or "").strip().upper()
if base and quote and inst == f"{base}-{quote}-SWAP":
return True
except Exception:
pass
return False
def _fetch_okx_swap_position_rows(exchange_symbol=None):
def _okx_position_direction(position):
info = position.get("info") or {}
side = (position.get("side") or info.get("posSide") or "").strip().lower()
if side in ("long", "short"):
return side
try:
raw = float(info.get("pos") or position.get("contracts") or 0)
except (TypeError, ValueError):
raw = 0.0
if raw > 0:
return "long"
if raw < 0:
return "short"
return ""
def _fetch_okx_swap_position_rows():
"""OKX 单合约 fetch_positions([sym]) 常返回空;与 /api/prices 一致拉全量 SWAP 再本地匹配。"""
ensure_markets_loaded()
rows = None
@@ -2695,16 +2729,11 @@ def _fetch_okx_swap_position_rows(exchange_symbol=None):
continue
if rows is None:
return None
if not exchange_symbol:
return rows
out = []
for p in rows:
if _position_matches_wanted_contract(exchange_symbol, p):
out.append(p)
return out
return rows
def _select_live_position_row(rows, exchange_symbol, direction, relax_hedge=False):
exchange_symbol = normalize_okx_symbol(exchange_symbol or "")
if not rows:
return None
candidates = []
@@ -2716,8 +2745,13 @@ def _select_live_position_row(rows, exchange_symbol, direction, relax_hedge=Fals
contracts = _position_row_effective_contracts(p)
if contracts <= 0:
continue
if (not relax_hedge) and OKX_POS_MODE == "hedge":
if side and side != (direction or "").lower():
want_dir = (direction or "").lower()
if OKX_POS_MODE == "net" or side == "net":
pos_dir = _okx_position_direction(p)
if pos_dir and pos_dir != want_dir:
continue
elif (not relax_hedge) and OKX_POS_MODE == "hedge":
if side and side != want_dir:
continue
candidates.append((contracts, p))
if not candidates and (not relax_hedge) and OKX_POS_MODE == "hedge":
@@ -3004,12 +3038,11 @@ def is_no_position_error(err_msg):
def get_live_position_contracts(exchange_symbol, direction):
rows = _fetch_okx_swap_position_rows(exchange_symbol)
ex_sym = normalize_okx_symbol(exchange_symbol or "")
rows = _fetch_okx_swap_position_rows()
if rows is None:
return None
if not rows:
return 0.0
prow = _select_live_position_row(rows, exchange_symbol, direction)
prow = _select_live_position_row(rows, ex_sym, direction)
if not prow:
return 0.0
return _position_row_effective_contracts(prow)