"""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 []