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.
This commit is contained in:
156
economy_service.py
Normal file
156
economy_service.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""Economy data: FRED search, regional data, Fed holdings, FOMC documents."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from openbb import obb
|
||||
|
||||
from obb_utils import to_list
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_cpi(country: str = "united_states") -> list[dict[str, Any]]:
|
||||
"""Get Consumer Price Index data."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.cpi, country=country, provider="fred"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("CPI failed for %s", country, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
_VALID_GDP_TYPES = {"nominal", "real", "forecast"}
|
||||
|
||||
|
||||
async def get_gdp(gdp_type: str = "real") -> list[dict[str, Any]]:
|
||||
"""Get GDP data (nominal, real, or forecast)."""
|
||||
if gdp_type not in _VALID_GDP_TYPES:
|
||||
return []
|
||||
try:
|
||||
fn = getattr(obb.economy.gdp, gdp_type, None)
|
||||
if fn is None:
|
||||
return []
|
||||
result = await asyncio.to_thread(fn, provider="oecd")
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("GDP %s failed", gdp_type, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_unemployment(country: str = "united_states") -> list[dict[str, Any]]:
|
||||
"""Get unemployment rate data."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.unemployment, country=country, provider="oecd"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("Unemployment failed for %s", country, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_composite_leading_indicator(
|
||||
country: str = "united_states",
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get Composite Leading Indicator (recession predictor)."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.composite_leading_indicator, country=country, provider="oecd"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("CLI failed for %s", country, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_house_price_index(
|
||||
country: str = "united_states",
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get housing price index."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.house_price_index, country=country, provider="oecd"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("HPI failed for %s", country, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_pce() -> list[dict[str, Any]]:
|
||||
"""Get Personal Consumption Expenditures (Fed preferred inflation)."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.pce, provider="fred"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("PCE failed", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_money_measures() -> list[dict[str, Any]]:
|
||||
"""Get M1/M2 money supply data."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.money_measures, provider="federal_reserve"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("Money measures failed", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def fred_search(query: str) -> list[dict[str, Any]]:
|
||||
"""Search FRED series by keyword."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.fred_search, query, provider="fred"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("FRED search failed for %s", query, exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_balance_of_payments() -> list[dict[str, Any]]:
|
||||
"""Get balance of payments (current/capital/financial account)."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.balance_of_payments, provider="fred"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("Balance of payments failed", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_central_bank_holdings() -> list[dict[str, Any]]:
|
||||
"""Get Fed SOMA portfolio holdings."""
|
||||
try:
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.central_bank_holdings, provider="federal_reserve"
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("Central bank holdings failed", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
async def get_fomc_documents(year: int | None = None) -> list[dict[str, Any]]:
|
||||
"""Get FOMC meeting documents (minutes, projections, etc.)."""
|
||||
try:
|
||||
kwargs: dict[str, Any] = {"provider": "federal_reserve"}
|
||||
if year:
|
||||
kwargs["year"] = year
|
||||
result = await asyncio.to_thread(
|
||||
obb.economy.fomc_documents, **kwargs
|
||||
)
|
||||
return to_list(result)
|
||||
except Exception:
|
||||
logger.warning("FOMC documents failed", exc_info=True)
|
||||
return []
|
||||
Reference in New Issue
Block a user