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.
144 lines
4.3 KiB
Python
144 lines
4.3 KiB
Python
"""Fixed income data: treasury rates, yield curve, auctions, corporate bonds."""
|
|
|
|
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_treasury_rates() -> list[dict[str, Any]]:
|
|
"""Get full treasury yield curve rates (4W-30Y)."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.government.treasury_rates, provider="federal_reserve"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Treasury rates failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_yield_curve(date: str | None = None) -> list[dict[str, Any]]:
|
|
"""Get yield curve with maturity/rate pairs."""
|
|
try:
|
|
kwargs: dict[str, Any] = {"provider": "federal_reserve"}
|
|
if date:
|
|
kwargs["date"] = date
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.government.yield_curve, **kwargs
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Yield curve failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_treasury_auctions(security_type: str | None = None) -> list[dict[str, Any]]:
|
|
"""Get treasury auction data (bid-to-cover, yields)."""
|
|
try:
|
|
kwargs: dict[str, Any] = {"provider": "government_us"}
|
|
if security_type:
|
|
kwargs["security_type"] = security_type
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.government.treasury_auctions, **kwargs
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Treasury auctions failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_tips_yields() -> list[dict[str, Any]]:
|
|
"""Get TIPS real yields by maturity."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.government.tips_yields, provider="fred"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("TIPS yields failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_effr() -> list[dict[str, Any]]:
|
|
"""Get Effective Federal Funds Rate with percentiles."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.rate.effr, provider="federal_reserve"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("EFFR failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_sofr() -> list[dict[str, Any]]:
|
|
"""Get SOFR rate with moving averages."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.rate.sofr, provider="federal_reserve"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("SOFR failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_hqm() -> list[dict[str, Any]]:
|
|
"""Get High Quality Market corporate bond yields."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.corporate.hqm, provider="fred"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("HQM failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_commercial_paper() -> list[dict[str, Any]]:
|
|
"""Get commercial paper rates by maturity and type."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.corporate.commercial_paper, provider="fred"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Commercial paper failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
async def get_spot_rates() -> list[dict[str, Any]]:
|
|
"""Get corporate bond spot rates."""
|
|
try:
|
|
result = await asyncio.to_thread(
|
|
obb.fixedincome.corporate.spot_rates, provider="fred"
|
|
)
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Spot rates failed", exc_info=True)
|
|
return []
|
|
|
|
|
|
_VALID_SPREAD_SERIES = {"tcm", "tcm_effr", "treasury_effr"}
|
|
|
|
|
|
async def get_spreads(series: str = "tcm") -> list[dict[str, Any]]:
|
|
"""Get treasury/corporate spreads (tcm, tcm_effr, treasury_effr)."""
|
|
if series not in _VALID_SPREAD_SERIES:
|
|
return []
|
|
try:
|
|
fn = getattr(obb.fixedincome.spreads, series, None)
|
|
if fn is None:
|
|
return []
|
|
result = await asyncio.to_thread(fn, provider="fred")
|
|
return to_list(result)
|
|
except Exception:
|
|
logger.warning("Spreads %s failed", series, exc_info=True)
|
|
return []
|