Spaces:
Sleeping
Sleeping
Update app/api/routes_price.py
Browse files- app/api/routes_price.py +30 -21
app/api/routes_price.py
CHANGED
|
@@ -1,21 +1,30 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException, Query
|
| 2 |
-
from app.schemas import HistoryResponse
|
| 3 |
-
from app.services.market_data import get_enriched_history, last_n_candles
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Query
|
| 2 |
+
from app.schemas import HistoryResponse
|
| 3 |
+
from app.services.market_data import get_enriched_history, last_n_candles
|
| 4 |
+
|
| 5 |
+
router = APIRouter(prefix="/api")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@router.get("/price/history", response_model=HistoryResponse)
|
| 9 |
+
def get_price_history(
|
| 10 |
+
ticker: str = Query(..., min_length=1),
|
| 11 |
+
days: int = Query(60, ge=30, le=365),
|
| 12 |
+
):
|
| 13 |
+
try:
|
| 14 |
+
df_raw, _df_tech = get_enriched_history(ticker)
|
| 15 |
+
|
| 16 |
+
# Cap by available data, not model window
|
| 17 |
+
available_days = len(df_raw)
|
| 18 |
+
n = min(days, available_days)
|
| 19 |
+
|
| 20 |
+
candles = last_n_candles(df_raw, n)
|
| 21 |
+
|
| 22 |
+
return HistoryResponse(
|
| 23 |
+
ticker=ticker.upper(),
|
| 24 |
+
historicalPrices=candles,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
except HTTPException:
|
| 28 |
+
raise
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise HTTPException(status_code=500, detail=str(e))
|