"""按配置创建交易所实例。""" from __future__ import annotations from ..config import Settings from .protocol import ExchangeMarket _exchange: ExchangeMarket | None = None def build_exchange(settings: Settings | None = None) -> ExchangeMarket: from .runtime import load_runtime_settings, normalize_exchange_name s = settings or load_runtime_settings() name = normalize_exchange_name(s.exchange) if name == "okx": from .okx.adapter import OkxExchange return OkxExchange(s) if name == "binance": from .binance.adapter import BinanceExchange return BinanceExchange(s) raise ValueError(f"未知交易所 EXCHANGE={s.exchange!r},支持 okx / binance") def get_exchange() -> ExchangeMarket: global _exchange if _exchange is None: _exchange = build_exchange() return _exchange def set_exchange(ex: ExchangeMarket | None) -> None: global _exchange _exchange = ex