from fastapi import APIRouter, HTTPException, Query from app.schemas import HistoryResponse from app.services.market_data import get_enriched_history, last_n_candles router = APIRouter(prefix="/api") @router.get("/price/history", response_model=HistoryResponse) def get_price_history( ticker: str = Query(..., min_length=1), days: int = Query(60, ge=30, le=365), ): try: df_raw, _df_tech = get_enriched_history(ticker) # Cap by available data, not model window available_days = len(df_raw) n = min(days, available_days) candles = last_n_candles(df_raw, n) return HistoryResponse( ticker=ticker.upper(), historicalPrices=candles, ) except HTTPException: raise except Exception as e: print("🔥 BACKEND ERROR:", repr(e)) raise HTTPException(status_code=500, detail=str(e))