修复关键位现价:模拟盘无密钥时回退 OKX 公开 ticker。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-09-30 23:52:53 +08:00
parent 43951a701b
commit ee635bcd55
+27 -3
View File
@@ -4178,11 +4178,35 @@ def sync_trade_records_from_exchange(conn, force=False):
# 获取实时价格
def get_price(symbol):
"""最新价:优先 ccxt 永续 ticker;失败则走 OKX 公开接口(模拟盘无密钥也能显示)."""
ex_sym = ""
try:
ex_sym = normalize_okx_symbol(symbol)
ensure_markets_loaded()
return exchange.fetch_ticker(normalize_okx_symbol(symbol))["last"]
except:
return None
last = exchange.fetch_ticker(ex_sym).get("last")
if last is not None:
return float(last)
except Exception:
pass
try:
sym = normalize_symbol_input(symbol) or (symbol or "").strip().upper()
base = (sym.split("/")[0] if "/" in sym else sym.split(":")[0]).strip().upper()
if not base:
return None
# SWAP 与现货公开 ticker 均可作为关键位现价
for inst in (f"{base}-USDT-SWAP", f"{base}-USDT"):
if not hasattr(exchange, "public_get_market_ticker"):
break
rows = (exchange.public_get_market_ticker({"instId": inst}) or {}).get("data") or []
if not rows:
continue
row = rows[0] or {}
last = row.get("last") or row.get("lastPx") or row.get("markPx")
if last is not None and float(last) > 0:
return float(last)
except Exception:
pass
return None
# 获取5分钟K线收盘价
def get_5m_close(symbol):