from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.ext.asyncio import AsyncSession from app.core.database import get_db from app.services.market_data import MarketDataService router = APIRouter(prefix="/candles", tags=["candles"]) VALID_GRANULARITIES = {"M1", "M5", "M15", "M30", "H1", "H4", "D"} @router.get("") async def get_candles( instrument: str = Query(default="EUR_USD", description="Ex: EUR_USD, GBP_USD, SPX500_USD"), granularity: str = Query(default="H1", description="M1, M5, M15, M30, H1, H4, D"), count: int = Query(default=200, ge=10, le=5000), db: AsyncSession = Depends(get_db), ): if granularity not in VALID_GRANULARITIES: raise HTTPException(400, f"Granularité invalide. Valides: {VALID_GRANULARITIES}") service = MarketDataService(db) df = await service.get_candles(instrument, granularity, count) return { "instrument": instrument, "granularity": granularity, "count": len(df), "candles": df.to_dict(orient="records") if not df.empty else [], }