"""Strategy endpoints.""" import logging import os import sys from pathlib import Path from fastapi import APIRouter, HTTPException # Resolve strategies directory from env or relative paths _STRATEGIES_DIR = Path( os.environ.get( "STRATEGIES_DIR", str(Path(__file__).resolve().parents[5] / "services" / "strategy-engine" / "strategies"), ) ) # Add parent so `from strategies.base import BaseStrategy` works if str(_STRATEGIES_DIR.parent) not in sys.path: sys.path.insert(0, str(_STRATEGIES_DIR.parent)) # Add strategy-engine src for plugin_loader _SE_SRC = Path(__file__).resolve().parents[5] / "services" / "strategy-engine" / "src" if str(_SE_SRC) not in sys.path: sys.path.insert(0, str(_SE_SRC)) logger = logging.getLogger(__name__) router = APIRouter() @router.get("/") async def list_strategies(): """List available strategies.""" try: from strategy_engine.plugin_loader import load_strategies strategies = load_strategies(_STRATEGIES_DIR) return [ { "name": s.name, "warmup_period": s.warmup_period, "class": type(s).__name__, } for s in strategies ] except (ImportError, FileNotFoundError) as exc: logger.error("Strategy loading error: %s", exc) raise HTTPException(status_code=503, detail="Strategy engine unavailable") from exc except Exception as exc: logger.error("Failed to list strategies: %s", exc, exc_info=True) raise HTTPException(status_code=500, detail="Failed to list strategies") from exc