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.
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
"""Shared OpenBB result conversion utilities."""
|
|
|
|
import asyncio
|
|
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import Any
|
|
|
|
from openbb import obb
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
PROVIDER = "yfinance"
|
|
|
|
|
|
def to_list(result: Any) -> list[dict[str, Any]]:
|
|
"""Convert OBBject result to list of dicts with serialized dates."""
|
|
if result is None or result.results is None:
|
|
return []
|
|
items = result.results
|
|
if not isinstance(items, list):
|
|
items = [items]
|
|
out = []
|
|
for item in items:
|
|
if hasattr(item, "model_dump"):
|
|
d = item.model_dump()
|
|
else:
|
|
raw = vars(item)
|
|
d = dict(raw) if raw else {}
|
|
d = {
|
|
k: v.isoformat() if hasattr(v, "isoformat") else v
|
|
for k, v in d.items()
|
|
}
|
|
out.append(d)
|
|
return out
|
|
|
|
|
|
def extract_single(result: Any) -> dict[str, Any]:
|
|
"""Extract data from an OBBject result (single model or list)."""
|
|
if result is None:
|
|
return {}
|
|
items = getattr(result, "results", None)
|
|
if items is None:
|
|
return {}
|
|
if hasattr(items, "model_dump"):
|
|
return items.model_dump()
|
|
if isinstance(items, list) and items:
|
|
last = items[-1]
|
|
return last.model_dump() if hasattr(last, "model_dump") else {}
|
|
return {}
|
|
|
|
|
|
def safe_last(result: Any) -> dict[str, Any] | None:
|
|
"""Get the last item from a list result, or None."""
|
|
if result is None:
|
|
return None
|
|
items = getattr(result, "results", None)
|
|
if items is None or not isinstance(items, list) or not items:
|
|
return None
|
|
last = items[-1]
|
|
return last.model_dump() if hasattr(last, "model_dump") else None
|
|
|
|
|
|
def first_or_empty(result: Any) -> dict[str, Any]:
|
|
"""Get first result as dict, or empty dict."""
|
|
items = to_list(result)
|
|
return items[0] if items else {}
|
|
|
|
|
|
async def fetch_historical(
|
|
symbol: str, days: int = 365, provider: str = PROVIDER,
|
|
) -> Any | None:
|
|
"""Fetch historical price data, returning the OBBject result or None."""
|
|
start = (datetime.now(tz=timezone.utc) - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.equity.price.historical, symbol, start_date=start, provider=provider,
|
|
)
|
|
except Exception:
|
|
logger.warning("Historical fetch failed for %s", symbol, exc_info=True)
|
|
return None
|
|
if result is None or result.results is None:
|
|
return None
|
|
return result
|