Files
openbb-invest-api/routes_market.py
Yaojia Wang 87260f4b10 feat: add 67 new endpoints across 10 feature groups
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.
2026-03-19 17:28:31 +01:00

180 lines
5.6 KiB
Python

"""Routes for ETF, index, crypto, currency, and derivatives data."""
from fastapi import APIRouter, Path, Query
from models import ApiResponse
from route_utils import safe, validate_symbol
import market_service
router = APIRouter(prefix="/api/v1")
# --- ETF ---
# NOTE: /etf/search MUST be registered before /etf/{symbol} to avoid shadowing.
@router.get("/etf/search", response_model=ApiResponse)
@safe
async def etf_search(query: str = Query(..., min_length=1, max_length=100)):
"""Search for ETFs by name or keyword."""
data = await market_service.search_etf(query)
return ApiResponse(data=data)
@router.get("/etf/{symbol}/info", response_model=ApiResponse)
@safe
async def etf_info(symbol: str = Path(..., min_length=1, max_length=20)):
"""Get ETF profile and info."""
symbol = validate_symbol(symbol)
data = await market_service.get_etf_info(symbol)
return ApiResponse(data=data)
@router.get("/etf/{symbol}/historical", response_model=ApiResponse)
@safe
async def etf_historical(
symbol: str = Path(..., min_length=1, max_length=20),
days: int = Query(default=365, ge=1, le=3650),
):
"""Get ETF price history."""
symbol = validate_symbol(symbol)
data = await market_service.get_etf_historical(symbol, days=days)
return ApiResponse(data=data)
# --- Index ---
@router.get("/index/available", response_model=ApiResponse)
@safe
async def index_available():
"""List available market indices."""
data = await market_service.get_available_indices()
return ApiResponse(data=data)
@router.get("/index/{symbol}/historical", response_model=ApiResponse)
@safe
async def index_historical(
symbol: str = Path(..., min_length=1, max_length=20),
days: int = Query(default=365, ge=1, le=3650),
):
"""Get index price history (e.g., ^GSPC, ^DJI, ^IXIC)."""
symbol = validate_symbol(symbol)
data = await market_service.get_index_historical(symbol, days=days)
return ApiResponse(data=data)
# --- Crypto ---
# NOTE: /crypto/search MUST be registered before /crypto/{symbol} to avoid shadowing.
@router.get("/crypto/search", response_model=ApiResponse)
@safe
async def crypto_search(query: str = Query(..., min_length=1, max_length=100)):
"""Search for cryptocurrencies."""
data = await market_service.search_crypto(query)
return ApiResponse(data=data)
@router.get("/crypto/{symbol}/historical", response_model=ApiResponse)
@safe
async def crypto_historical(
symbol: str = Path(..., min_length=1, max_length=20),
days: int = Query(default=365, ge=1, le=3650),
):
"""Get cryptocurrency price history (e.g., BTC-USD)."""
symbol = validate_symbol(symbol)
data = await market_service.get_crypto_historical(symbol, days=days)
return ApiResponse(data=data)
# --- Currency ---
@router.get("/currency/{symbol}/historical", response_model=ApiResponse)
@safe
async def currency_historical(
symbol: str = Path(..., min_length=1, max_length=20),
days: int = Query(default=365, ge=1, le=3650),
):
"""Get forex price history (e.g., EURUSD, USDSEK)."""
symbol = validate_symbol(symbol)
data = await market_service.get_currency_historical(symbol, days=days)
return ApiResponse(data=data)
# --- Derivatives ---
@router.get("/options/{symbol}/chains", response_model=ApiResponse)
@safe
async def options_chains(symbol: str = Path(..., min_length=1, max_length=20)):
"""Get options chain data."""
symbol = validate_symbol(symbol)
data = await market_service.get_options_chains(symbol)
return ApiResponse(data=data)
@router.get("/futures/{symbol}/historical", response_model=ApiResponse)
@safe
async def futures_historical(
symbol: str = Path(..., min_length=1, max_length=20),
days: int = Query(default=365, ge=1, le=3650),
):
"""Get futures price history."""
symbol = validate_symbol(symbol)
data = await market_service.get_futures_historical(symbol, days=days)
return ApiResponse(data=data)
@router.get("/futures/{symbol}/curve", response_model=ApiResponse)
@safe
async def futures_curve(symbol: str = Path(..., min_length=1, max_length=20)):
"""Get futures term structure/curve."""
symbol = validate_symbol(symbol)
data = await market_service.get_futures_curve(symbol)
return ApiResponse(data=data)
# --- Currency Reference Rates (Group H) ---
@router.get("/currency/reference-rates", response_model=ApiResponse)
@safe
async def currency_reference_rates():
"""Get ECB reference exchange rates for 28 major currencies."""
data = await market_service.get_currency_reference_rates()
return ApiResponse(data=data)
# --- Index Enhanced (Group F) ---
@router.get("/index/sp500-multiples", response_model=ApiResponse)
@safe
async def sp500_multiples(
series: str = Query(default="pe_ratio", pattern="^[a-z_]+$"),
):
"""Historical S&P 500 valuation: pe_ratio, shiller_pe_ratio, dividend_yield, etc."""
data = await market_service.get_sp500_multiples(series)
return ApiResponse(data=data)
@router.get("/index/{symbol}/constituents", response_model=ApiResponse)
@safe
async def index_constituents(symbol: str = Path(..., min_length=1, max_length=20)):
"""Get index member stocks with sector and price data."""
symbol = validate_symbol(symbol)
data = await market_service.get_index_constituents(symbol)
return ApiResponse(data=data)
@router.get("/etf/{symbol}/nport", response_model=ApiResponse)
@safe
async def etf_nport(symbol: str = Path(..., min_length=1, max_length=20)):
"""Detailed ETF holdings from SEC N-PORT filings."""
symbol = validate_symbol(symbol)
data = await market_service.get_etf_nport(symbol)
return ApiResponse(data=data)