Prerequisite refactor: - Consolidate duplicate _to_dicts into shared obb_utils.to_list - Add fetch_historical and first_or_empty helpers to obb_utils Phase 1 - Local computation (no provider risk): - Group I: 12 technical indicators (ATR, ADX, Stoch, OBV, Ichimoku, Donchian, Aroon, CCI, Keltner, Fibonacci, A/D, Volatility Cones) - Group J: Sortino, Omega ratios + rolling stats (variance, stdev, mean, skew, kurtosis, quantile via generic endpoint) - Group H: ECB currency reference rates Phase 2 - FRED/Federal Reserve providers: - Group C: 10 fixed income endpoints (treasury rates, yield curve, auctions, TIPS, EFFR, SOFR, HQM, commercial paper, spot rates, spreads) - Group D: 11 economy endpoints (CPI, GDP, unemployment, PCE, money measures, CLI, HPI, FRED search, balance of payments, Fed holdings, FOMC documents) - Group E: 5 survey endpoints (Michigan, SLOOS, NFP, Empire State, BLS search) Phase 3 - SEC/stockgrid/FINRA providers: - Group B: 4 equity fundamental endpoints (management, dividends, SEC filings, company search) - Group A: 4 shorts/dark pool endpoints (short volume, FTD, short interest, OTC dark pool) - Group F: 3 index/ETF enhanced (S&P 500 multiples, index constituents, ETF N-PORT) Phase 4 - Regulators: - Group G: 5 regulatory endpoints (COT report, COT search, SEC litigation, institution search, CIK mapping) Security hardening: - Service-layer allowlists for all getattr dynamic dispatch - Regex validation on date, country, security_type, form_type params - Exception handling in fetch_historical - Callable guard on rolling stat dispatch Total: 32 existing + 67 new = 99 endpoints, all free providers.
96 lines
3.3 KiB
Python
96 lines
3.3 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)
|
|
|
|
|
|
# --- Extended Quantitative (Group J) ---
|
|
|
|
|
|
@router.get("/stock/{symbol}/sortino", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_sortino(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
):
|
|
"""Sortino ratio -- risk-adjusted return penalizing only downside deviation."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_sortino(symbol, days=days)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/omega", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_omega(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
):
|
|
"""Omega ratio -- probability-weighted gain vs loss."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_omega(symbol, days=days)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/rolling/{stat}", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_rolling(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
stat: str = Path(..., pattern="^(variance|stdev|mean|skew|kurtosis|quantile)$"),
|
|
days: int = Query(default=365, ge=30, le=3650),
|
|
window: int = Query(default=30, ge=5, le=252),
|
|
):
|
|
"""Rolling statistics: variance, stdev, mean, skew, kurtosis, quantile."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await quantitative_service.get_rolling_stat(symbol, stat=stat, days=days, window=window)
|
|
return ApiResponse(data=data)
|