关键位现价改与顶栏同源公开行情,前端无价时回退 nav tickers。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-09-30 23:56:40 +08:00
parent ee635bcd55
commit e7cafd6dbc
5 changed files with 119 additions and 36 deletions
+50 -26
View File
@@ -2591,7 +2591,10 @@ def get_available_trading_usdt():
def get_synced_leverage(exchange_symbol, direction):
ensure_markets_loaded()
try:
ensure_markets_loaded()
except Exception:
return None
# 1) 优先读取交易所杠杆配置
try:
if hasattr(exchange, "fetch_leverage"):
@@ -4178,34 +4181,49 @@ def sync_trade_records_from_exchange(conn, force=False):
# 获取实时价格
def get_price(symbol):
"""最新价:优先 ccxt 永续 ticker;失败则走 OKX 公开接口(模拟盘无密钥也能显示)."""
ex_sym = ""
"""最新价:公开 SWAP/现货 ticker 优先(与顶栏同源),再回退 ccxt."""
sym = normalize_symbol_input(symbol) or (symbol or "").strip().upper()
base = ""
if "/" in sym:
base = sym.split("/", 1)[0].strip().upper()
elif sym:
base = sym.split(":", 1)[0].strip().upper()
# 1) 与顶栏 BTC/ETH 同一公开接口(模拟盘无密钥也可用)
if base in ("BTC", "ETH"):
try:
from lib.market.nav_spot_tickers_lib import _ticker_row
for inst in (f"{base}-USDT-SWAP", f"{base}-USDT"):
row = _ticker_row(exchange, inst)
if not row:
continue
last = _safe_float(row.get("last") or row.get("lastPx") or row.get("markPx"))
if last is not None and last > 0:
return float(last)
except Exception:
pass
# 2) 其它币种:公开 ticker
if base and hasattr(exchange, "public_get_market_ticker"):
try:
for inst in (f"{base}-USDT-SWAP", f"{base}-USDT"):
rows = (exchange.public_get_market_ticker({"instId": inst}) or {}).get("data") or []
if not rows:
continue
row = rows[0] or {}
last = _safe_float(row.get("last") or row.get("lastPx") or row.get("markPx"))
if last is not None and last > 0:
return float(last)
except Exception:
pass
# 3) ccxt 永续
try:
ex_sym = normalize_okx_symbol(symbol)
ensure_markets_loaded()
last = exchange.fetch_ticker(ex_sym).get("last")
if last is not None:
last = exchange.fetch_ticker(normalize_okx_symbol(sym or symbol)).get("last")
last = _safe_float(last)
if last is not None and last > 0:
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线收盘价
@@ -6186,8 +6204,14 @@ def api_order_defaults():
if direction not in ("long", "short"):
direction = "long"
exchange_symbol = normalize_okx_symbol(symbol)
leverage = get_synced_leverage(exchange_symbol, direction) or infer_leverage(symbol)
available = get_available_trading_usdt()
try:
leverage = get_synced_leverage(exchange_symbol, direction) or infer_leverage(symbol)
except Exception:
leverage = infer_leverage(symbol)
try:
available = get_available_trading_usdt()
except Exception:
available = None
last_price = get_price(symbol)
return jsonify({
"ok": True,