Fix hedge market quotes via public HTTP and default cloud-server deploy settings.
Avoid load_markets hangs on weak networks; enable login-required sim defaults and document Baota Nginx reverse proxy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from typing import Any, Optional
|
||||
|
||||
from flask import Flask, jsonify, request
|
||||
@@ -10,16 +11,10 @@ from jinja2 import ChoiceLoader, FileSystemLoader
|
||||
from lib.hedge_plan.hedge_plan_calc_lib import (
|
||||
build_options_options_preview,
|
||||
build_perp_options_preview,
|
||||
floor_contracts_to_precision,
|
||||
gate_status,
|
||||
option_premium_total,
|
||||
suggest_contracts_from_notional,
|
||||
)
|
||||
from lib.hub.hub_calculator_market_lib import amount_decimals_from_exchange
|
||||
from lib.trade.position_sizing_lib import (
|
||||
compute_full_margin_sizing,
|
||||
load_position_sizing_mode,
|
||||
)
|
||||
from lib.trade.position_sizing_lib import load_position_sizing_mode
|
||||
|
||||
|
||||
def _env_bool(key: str, default: bool = False) -> bool:
|
||||
@@ -1311,15 +1306,8 @@ def _preview_oo(body: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any], str | None]:
|
||||
"""对冲页行情:只走公共 HTTP/curl,绝不触发 ccxt load_markets(弱网会卡死整页)."""
|
||||
ex = cfg.get("exchange")
|
||||
if ex is None:
|
||||
return {}, "永续交易所未初始化"
|
||||
ensure = cfg.get("ensure_markets_loaded")
|
||||
if callable(ensure):
|
||||
try:
|
||||
ensure()
|
||||
except Exception as e:
|
||||
return {}, f"加载市场失败: {e}"
|
||||
norm = cfg.get("normalize_exchange_symbol")
|
||||
sym = f"{base}/USDT:USDT"
|
||||
if callable(norm):
|
||||
@@ -1327,67 +1315,43 @@ def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any],
|
||||
sym = norm(f"{base}/USDT")
|
||||
except Exception:
|
||||
sym = f"{base}/USDT:USDT"
|
||||
mark = bid = ask = last = None
|
||||
|
||||
mark = bid = ask = last = index_px = None
|
||||
last_err: Exception | None = None
|
||||
try:
|
||||
t = ex.fetch_ticker(sym)
|
||||
last = _sf(t.get("last"))
|
||||
mark = _sf(t.get("info", {}).get("markPx")) if isinstance(t.get("info"), dict) else None
|
||||
if mark is None:
|
||||
mark = _sf(t.get("mark")) or last
|
||||
bid = _sf(t.get("bid"))
|
||||
ask = _sf(t.get("ask"))
|
||||
from lib.exchange.okx_public_http_lib import fetch_index_px, fetch_swap_ticker
|
||||
|
||||
row = fetch_swap_ticker(base)
|
||||
last = _sf(row.get("last"))
|
||||
mark = _sf(row.get("markPx")) or last
|
||||
bid = _sf(row.get("bidPx"))
|
||||
ask = _sf(row.get("askPx"))
|
||||
index_px = fetch_index_px(base)
|
||||
except Exception as e:
|
||||
return {}, f"拉永续行情失败: {e}"
|
||||
|
||||
cs = 0.01
|
||||
get_cs = cfg.get("get_contract_size")
|
||||
if callable(get_cs):
|
||||
try:
|
||||
cs = float(get_cs(sym) or 0.01)
|
||||
except Exception:
|
||||
cs = 0.01
|
||||
last_err = e
|
||||
if ex is not None:
|
||||
try:
|
||||
inst = f"{base}-USDT-SWAP"
|
||||
rows = (ex.public_get_market_ticker({"instId": inst}) or {}).get("data") or []
|
||||
row = rows[0] if rows else {}
|
||||
last = _sf(row.get("last"))
|
||||
mark = _sf(row.get("markPx")) or last
|
||||
bid = _sf(row.get("bidPx"))
|
||||
ask = _sf(row.get("askPx"))
|
||||
except Exception as e2:
|
||||
last_err = e2
|
||||
if last is None and mark is None:
|
||||
return {}, f"拉永续行情失败: {last_err or '无报价'}"
|
||||
if index_px is None:
|
||||
index_px = mark or last
|
||||
|
||||
# 面值/可用/精度:展示用默认值,避免 get_contract_size→load_markets 阻塞
|
||||
cs = 0.1 if base == "ETH" else 0.01
|
||||
available = None
|
||||
get_av = cfg.get("get_available_trading_usdt")
|
||||
if callable(get_av):
|
||||
try:
|
||||
available = get_av()
|
||||
except Exception:
|
||||
available = None
|
||||
|
||||
entry = float(mark or last or 0)
|
||||
amount_precision = 2 if base == "ETH" else 4
|
||||
sizing = None
|
||||
suggest_contracts = None
|
||||
amount_precision = 4
|
||||
try:
|
||||
amount_precision = int(amount_decimals_from_exchange(ex, sym))
|
||||
except Exception:
|
||||
amount_precision = 4
|
||||
if available is not None and entry > 0:
|
||||
sizing, _serr = compute_full_margin_sizing(
|
||||
symbol=sym,
|
||||
available_usdt=float(available),
|
||||
capital_base=float(available),
|
||||
buffer_ratio=float(cfg.get("full_margin_buffer") or 0.98),
|
||||
btc_leverage=int(cfg.get("btc_leverage") or 10),
|
||||
alt_leverage=int(cfg.get("alt_leverage") or 5),
|
||||
funds_decimals=int(cfg.get("funds_decimals") or 2),
|
||||
)
|
||||
if sizing:
|
||||
raw_contracts = suggest_contracts_from_notional(
|
||||
notional=float(sizing["notional_value"]),
|
||||
entry=entry,
|
||||
contract_size=cs,
|
||||
)
|
||||
# 优先走交易所 amount_to_precision;失败则按精度位数向下取整
|
||||
suggest_contracts = None
|
||||
try:
|
||||
precise = float(ex.amount_to_precision(sym, raw_contracts))
|
||||
if precise > raw_contracts + 1e-12:
|
||||
precise = floor_contracts_to_precision(raw_contracts, amount_precision)
|
||||
suggest_contracts = precise
|
||||
except Exception:
|
||||
suggest_contracts = floor_contracts_to_precision(raw_contracts, amount_precision)
|
||||
entry = float(mark or last or 0)
|
||||
|
||||
return {
|
||||
"exchange_symbol": sym,
|
||||
@@ -1395,6 +1359,7 @@ def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any],
|
||||
"last": last,
|
||||
"bid": bid,
|
||||
"ask": ask,
|
||||
"index_px": index_px,
|
||||
"contract_size": cs,
|
||||
"available_usdt": available,
|
||||
"full_margin_sizing": sizing,
|
||||
|
||||
Reference in New Issue
Block a user