修复okx 趋势回调

This commit is contained in:
dekun
2026-05-30 09:43:50 +08:00
parent 81f27765ca
commit 9678cd54ce
2 changed files with 77 additions and 30 deletions
+38 -29
View File
@@ -2672,7 +2672,36 @@ def _position_matches_wanted_contract(exchange_symbol, position):
if not position:
return False
sym = position.get("symbol")
return sym == exchange_symbol
if sym == exchange_symbol:
return True
try:
return normalize_okx_symbol(sym or "") == normalize_okx_symbol(exchange_symbol or "")
except Exception:
return False
def _fetch_okx_swap_position_rows(exchange_symbol=None):
"""OKX 单合约 fetch_positions([sym]) 常返回空;与 /api/prices 一致拉全量 SWAP 再本地匹配。"""
ensure_markets_loaded()
rows = None
for fetcher in (
lambda: exchange.fetch_positions(None, {"instType": OKX_POSITION_INST_TYPE}),
lambda: exchange.fetch_positions(),
):
try:
rows = fetcher() or []
break
except Exception:
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
def _select_live_position_row(rows, exchange_symbol, direction, relax_hedge=False):
@@ -2975,35 +3004,15 @@ def is_no_position_error(err_msg):
def get_live_position_contracts(exchange_symbol, direction):
ensure_markets_loaded()
try:
rows = exchange.fetch_positions([exchange_symbol], params={"instType": OKX_POSITION_INST_TYPE})
except Exception:
rows = _fetch_okx_swap_position_rows(exchange_symbol)
if rows is None:
return None
total = 0.0
for p in rows:
if p.get("symbol") != exchange_symbol:
continue
info = p.get("info", {}) or {}
side = (p.get("side") or info.get("posSide") or "").lower()
contracts = p.get("contracts")
if contracts is None:
raw_pos = info.get("pos")
try:
contracts = abs(float(raw_pos)) if raw_pos is not None else 0.0
except Exception:
contracts = 0.0
try:
contracts = float(contracts)
except Exception:
contracts = 0.0
if contracts <= 0:
continue
if OKX_POS_MODE == "hedge":
if side and side != direction:
continue
total += contracts
return total
if not rows:
return 0.0
prow = _select_live_position_row(rows, exchange_symbol, direction)
if not prow:
return 0.0
return _position_row_effective_contracts(prow)
def opened_at_str_to_ms(opened_at_str):