"""按配置创建交易所实例。""" from __future__ import annotations from ..config import Settings, get_settings from .protocol import ExchangeMarket _exchange: ExchangeMarket | None = None def build_exchange(settings: Settings | None = None) -> ExchangeMarket: s = settings or get_settings() name = (s.exchange or "okx").strip().lower() if name == "okx": from .okx.adapter import OkxExchange return OkxExchange(s) if name in ("binance", "bn"): 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