Files
OpenBB/openbb_platform/providers/fred/openbb_fred/models/moody.py
Danglewood 78445ce007 [Feature] Delay/Optimize Imports (#6532)
* round 1

* black

* more black

* providers a-thru-fmp

* charting settings

* charting backend

* black

* charting_settings

* missing nasdaq import

* nasdaq mypy

* fed money measure

* some linting

* black

* more linting

* more linting

* charting imports

* black

* pylint

* refactor fred

* more linting

* rest of the providers

* didn't want that file

* more cleanup

* missed

* ta_helpers

* router views

* chart ta_class

* forgot a file

* forgot another file

* biztoc

* package builder

* technical router

* fred params

* bump finviz

* fix tmx

* more cleanup

* try again

* currency historical

* equity search

* missed files in commit

* fix some tests

* black

* fix more test cassettes

* more patchy

* black

* linters

* more tests

* black

* mypy

* delete unused model

* more linting

* forgot files

* more linting

* make up your mind, black, and stop moving pylint for me

* more black

* back to where we were before black destroyed it

* ..........

* ..................

* unused fetcher test

* remove standard model validator

* commodity router

* treasury prices

* oecd tests

* finra

* container cleanup

* black

* forgotten file

* linter

* remove wrapped ""

* fix merge

* patches

* ecb test

* finra cassette

* finviz cassettes

* nasdaq cassettes

* oecd cassettes

* yfinance cassettes

* more test cassettes

* black

* merge cleanup

* cboe helpers

* black

* fix typo

---------

Co-authored-by: Henrique Joaquim <henriquecjoaquim@gmail.com>
Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
2024-07-09 16:28:15 +00:00

117 lines
3.4 KiB
Python

"""FRED Moody Corporate Bond Index Model."""
# pylint: disable=unused-argument
from typing import Any, Dict, List, Literal, Optional
from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.moody import (
MoodyCorporateBondIndexData,
MoodyCorporateBondIndexQueryParams,
)
from pydantic import Field, field_validator
MOODY_TO_OPTIONS = {
"aaa": {
"index": {
"id": "DAAA",
"name": "Moody's Seasoned Aaa Corporate Bond Yield",
},
"treasury": {
"id": "AAA10Y",
"name": "Moody's Seasoned Aaa Corporate Bond Yield Relative to Yield "
"on 10-Year Treasury Constant Maturity",
},
"fed_funds": {
"id": "AAAFF",
"name": "Moody's Seasoned Aaa Corporate Bond Minus Federal Funds Rate",
},
},
"baa": {
"index": {
"id": "DBAA",
"name": "Moody's Seasoned Baa Corporate Bond Yield",
},
"treasury": {
"id": "BAA10Y",
"name": "Moody's Seasoned Baa Corporate Bond Yield Relative "
"to Yield on 10-Year Treasury Constant Maturity",
},
"fed_funds": {
"id": "BAAFF",
"name": "Moody's Seasoned Baa Corporate Bond Minus Federal Funds Rate",
},
},
}
class FREDMoodyCorporateBondIndexQueryParams(MoodyCorporateBondIndexQueryParams):
"""FRED Moody Corporate Bond Index Query."""
spread: Optional[Literal["treasury", "fed_funds"]] = Field(
default=None, description="The type of spread."
)
class FREDMoodyCorporateBondIndexData(MoodyCorporateBondIndexData):
"""FRED Moody Corporate Bond Index Data."""
__alias_dict__ = {"rate": "value"}
@field_validator("rate", mode="before", check_fields=False)
@classmethod
def value_validate(cls, v):
"""Validate rate."""
try:
return float(v)
except ValueError:
return None
class FREDMoodyCorporateBondIndexFetcher(
Fetcher[
FREDMoodyCorporateBondIndexQueryParams,
List[FREDMoodyCorporateBondIndexData],
]
):
"""FRED Moody Corporate Bond Index Fetcher."""
@staticmethod
def transform_query(
params: Dict[str, Any]
) -> FREDMoodyCorporateBondIndexQueryParams:
"""Transform query."""
return FREDMoodyCorporateBondIndexQueryParams(**params)
@staticmethod
def extract_data(
query: FREDMoodyCorporateBondIndexQueryParams,
credentials: Optional[Dict[str, str]],
**kwargs: Any
) -> List:
"""Extract data."""
# pylint: disable=import-outside-toplevel
from openbb_fred.utils.fred_base import Fred
key = credentials.get("fred_api_key") if credentials else ""
fred = Fred(key)
opt_key = query.spread if query.spread else "index"
id_ = MOODY_TO_OPTIONS[query.index_type][opt_key]["id"]
data = fred.get_series(
series_id=id_,
start_date=query.start_date,
end_date=query.end_date,
**kwargs,
)
return data
@staticmethod
def transform_data(
query: FREDMoodyCorporateBondIndexQueryParams, data: List, **kwargs: Any
) -> List[FREDMoodyCorporateBondIndexData]:
"""Transform data."""
return [FREDMoodyCorporateBondIndexData.model_validate(d) for d in data]