from fastapi import APIRouter, Query from services.database import ( get_portfolio_exposure, get_pnl_timeline, get_risk_clusters, get_pattern_correlations, compute_kelly_sizing, get_risk_dashboard, ) router = APIRouter(prefix="/api/risk", tags=["risk"]) @router.get("/exposure") def portfolio_exposure(): """Exposure by asset class + risk factor for open positions.""" return get_portfolio_exposure() @router.get("/timeline") def pnl_timeline(days: int = Query(default=90, ge=7, le=365)): """Daily aggregated P&L timeline for equity curve.""" return {"timeline": get_pnl_timeline(days=days)} @router.get("/clusters") def risk_clusters(): """Risk factor clustering + saturation detection + prompt context.""" return get_risk_clusters() @router.get("/correlations") def pattern_correlations(): """Pearson correlation matrix between patterns (mature trades only).""" return get_pattern_correlations() @router.get("/kelly/{pattern_id}") def kelly_sizing( pattern_id: str, capital: float = Query(default=10000.0, ge=100, le=10_000_000), fractional: float = Query(default=0.33, ge=0.1, le=1.0), ): """Fractional Kelly position sizing for a pattern.""" return compute_kelly_sizing(pattern_id=pattern_id, capital_available=capital, fractional=fractional) @router.get("/dashboard") def risk_dashboard(): """Full portfolio risk snapshot: concentration, diversification, expected drawdown, recommendation.""" return get_risk_dashboard()