Split exchange and strategy modules for future Binance support.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-25 08:43:09 +08:00
parent 3957a83761
commit e19bb452f9
27 changed files with 1504 additions and 1048 deletions
+18 -11
View File
@@ -11,9 +11,9 @@ from fastapi.staticfiles import StaticFiles
from .api import router as api_router
from .config import get_settings
from .market import MarketGateway, set_gateway
from .models.db import Database, set_db
from .strategy import StrategyEngine, set_engine
from .strategy.session import bootstrap_session
logging.basicConfig(
level=logging.INFO,
@@ -36,13 +36,15 @@ async def lifespan(app: FastAPI):
engine = StrategyEngine()
set_engine(engine)
gw = MarketGateway(settings)
set_gateway(gw)
session = bootstrap_session(settings)
try:
await gw.start()
logger.info("market gateway started (SIM)")
await session.start()
logger.info(
"exchange=%s strategy session started (SIM)",
settings.exchange,
)
except Exception:
logger.exception("market gateway failed to start")
logger.exception("strategy session failed to start")
yield
@@ -53,8 +55,12 @@ async def lifespan(app: FastAPI):
await engine._task
except Exception:
pass
await gw.stop()
set_gateway(None)
await session.stop()
from .exchange import set_exchange
from .strategy.session import set_session
set_session(None)
set_exchange(None)
set_engine(None)
db.close()
set_db(None)
@@ -78,12 +84,12 @@ app.include_router(api_router)
@app.get("/health")
async def health() -> dict:
from .market import get_gateway
from .strategy import get_engine
from .strategy.session import get_session
settings = get_settings()
gw = get_gateway()
snap = gw.snapshot()
sess = get_session()
snap = sess.snapshot()
try:
st = get_engine().state()
except Exception:
@@ -92,6 +98,7 @@ async def health() -> dict:
"ok": True,
"mode": settings.mode,
"env_name": settings.env_name,
"exchange": settings.exchange,
"sim": settings.is_sim,
"market_connected": snap.connected,
"pair": snap.pair.to_dict() if snap.pair else None,