- Extract shared route_utils.py (validate_symbol, safe decorator)
removing duplication from 6 route files
- Extract shared obb_utils.py (to_list, extract_single, safe_last)
removing duplication from calendar_service and market_service
- Fix _to_list dict mutation during iteration (use comprehension)
- Fix double vars() call and live __dict__ mutation risk
- Fix route ordering: /etf/search and /crypto/search now registered
before /{symbol} path params to prevent shadowing
- Add date format validation (YYYY-MM-DD pattern) on calendar routes
- Use timezone-aware datetime.now(tz=timezone.utc) in all services
- Add explicit type annotation for asyncio.gather results
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""Routes for quantitative analysis: risk metrics, CAPM, normality, unit root."""
|
|
|
|
from fastapi import APIRouter, Path, Query
|
|
|
|
from models import ApiResponse
|
|
from route_utils import safe, validate_symbol
|
|
import quantitative_service
|
|
|
|
router = APIRouter(prefix="/api/v1")
|
|
|
|
|
|
@router.get("/stock/{symbol}/performance", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_performance(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
):
|
|
"""Performance metrics: Sharpe, Sortino, max drawdown, volatility."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_performance_metrics(symbol, days=days)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/capm", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_capm(symbol: str = Path(..., min_length=1, max_length=20)):
|
|
"""CAPM: beta, alpha, systematic and idiosyncratic risk."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_capm(symbol)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/normality", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_normality(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
):
|
|
"""Normality tests: Jarque-Bera, Shapiro-Wilk on returns."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_normality_test(symbol, days=days)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/unitroot", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_unitroot(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
):
|
|
"""Unit root tests: ADF, KPSS for stationarity."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_unitroot_test(symbol, days=days)
|
|
return ApiResponse(data=data)
|