diff options
24 files changed, 2120 insertions, 969 deletions
diff --git a/openbb_platform/core/openbb_core/provider/standard_models/consumer_price_index.py b/openbb_platform/core/openbb_core/provider/standard_models/consumer_price_index.py new file mode 100644 index 00000000000..0983d13d9d7 --- /dev/null +++ b/openbb_platform/core/openbb_core/provider/standard_models/consumer_price_index.py @@ -0,0 +1,56 @@ +"""CPI Standard Model.""" + +from datetime import date as dateType +from typing import Literal, Optional + +from pydantic import Field, field_validator + +from openbb_core.provider.abstract.data import Data +from openbb_core.provider.abstract.query_params import QueryParams +from openbb_core.provider.utils.descriptions import ( + DATA_DESCRIPTIONS, + QUERY_DESCRIPTIONS, +) + + +class ConsumerPriceIndexQueryParams(QueryParams): + """CPI Query.""" + + country: str = Field( + description=QUERY_DESCRIPTIONS.get("country"), + default="united_states", + ) + transform: Literal["index", "yoy", "period"] = Field( + description="Transformation of the CPI data. Period represents the change since previous." + + " Defaults to change from one year ago (yoy).", + default="yoy", + json_schema_extra={"choices": ["index", "yoy", "period"]}, + ) + frequency: Literal["annual", "quarter", "monthly"] = Field( + default="monthly", + description=QUERY_DESCRIPTIONS.get("frequency"), + json_schema_extra={"choices": ["annual", "quarter", "monthly"]}, + ) + harmonized: bool = Field( + default=False, description="If true, returns harmonized data." + ) + start_date: Optional[dateType] = Field( + default=None, description=QUERY_DESCRIPTIONS.get("start_date") + ) + end_date: Optional[dateType] = Field( + default=None, description=QUERY_DESCRIPTIONS.get("end_date") + ) + + @field_validator("country", mode="before", check_fields=False) + @classmethod + def to_lower(cls, v): + """Convert country to lower case.""" + return v.replace(" ", "_").lower() + + +class ConsumerPriceIndexData(Data): + """CPI data.""" + + date: dateType = Field(description=DATA_DESCRIPTIONS.get("date")) + country: str = Field(description=DATA_DESCRIPTIONS.get("country")) + value: float = Field(description="CPI index value or period change.") diff --git a/openbb_platform/core/openbb_core/provider/standard_models/cpi.py b/openbb_platform/core/openbb_core/provider/standard_models/cpi.py deleted file mode 100644 index 472a21df233..00000000000 --- a/openbb_platform/core/openbb_core/provider/standard_models/cpi.py +++ /dev/null @@ -1,128 +0,0 @@ -"""CPI Standard Model.""" - -from datetime import date as dateType -from typing import Literal, Optional - -from dateutil import parser -from pydantic import Field, field_validator - -from openbb_core.provider.abstract.data import Data -from openbb_core.provider.abstract.query_params import QueryParams -from openbb_core.provider.utils.descriptions import ( - DATA_DESCRIPTIONS, - QUERY_DESCRIPTIONS, -) -from openbb_core.provider.utils.helpers import check_item - -CPI_COUNTRIES = [ - "australia", - "austria", - "belgium", - "brazil", - "bulgaria", - "canada", - "chile", - "china", - "croatia", - "cyprus", - "czech_republic", - "denmark", - "estonia", - "euro_area", - "finland", - "france", - "germany", - "greece", - "hungary", - "iceland", - "india", - "indonesia", - "ireland", - "israel", - "italy", - "japan", - "korea", - "latvia", - "lithuania", - "luxembourg", - "malta", - "mexico", - "netherlands", - "new_zealand", - "norway", - "poland", - "portugal", - "romania", - "russian_federation", - "slovak_republic", - "slovakia", - "slovenia", - "south_africa", - "spain", - "sweden", - "switzerland", - "turkey", - "united_kingdom", - "united_states", -] - -CPI_UNITS = Literal["growth_previous", "growth_same", "index_2015"] - -CPI_FREQUENCY = Literal["monthly", "quarter", "annual"] - - -class ConsumerPriceIndexQueryParams(QueryParams): - """CPI Query.""" - - country: str = Field( - description=QUERY_DESCRIPTIONS.get("country"), - json_schema_extra={"choices": CPI_COUNTRIES}, # type: ignore[dict-item] - ) - units: CPI_UNITS = Field( - default="growth_same", - description=QUERY_DESCRIPTIONS.get("units", "") - + """ - Options: - - `growth_previous`: Percent growth from the previous period. - If monthly data, this is month-over-month, etc - - `growth_same`: Percent growth from the same period in the previous year. - If looking at monthly data, this would be year-over-year, etc. - - `index_2015`: Rescaled index value, such that the value in 2015 is 100.""", - ) - frequency: CPI_FREQUENCY = Field( - default="monthly", - description=QUERY_DESCRIPTIONS.get("frequency", "") - + """ - Options: `monthly`, `quarter`, and `annual`.""", - ) - harmonized: bool = Field( - default=False, description="Whether you wish to obtain harmonized data." - ) - start_date: Optional[dateType] = Field( - default=None, description=QUERY_DESCRIPTIONS.get("start_date") - ) - end_date: Optional[dateType] = Field( - default=None, description=QUERY_DESCRIPTIONS.get("end_date") - ) - - @field_validator("country", mode="before", check_fields=False) - def validate_country(cls, c: str): # pylint: disable=E0213 - """Validate country.""" - result = [] - values = c.replace(" ", "_").split(",") - for v in values: - check_item(v.lower(), CPI_COUNTRIES) - result.append(v.lower()) - return ",".join(result) - - -class ConsumerPriceIndexData(Data): - """CPI data.""" - - date: dateType = Field(description=DATA_DESCRIPTIONS.get("date")) - - @field_validator("date", mode="before") - @classmethod - def date_validate(cls, v): - """Validate date.""" - return parser.isoparse(v) diff --git a/openbb_platform/core/openbb_core/provider/standard_models/share_price_index.py b/openbb_platform/core/openbb_core/provider/standard_models/share_price_index.py new file mode 100644 index 00000000000..ff2d0681675 --- /dev/null +++ b/openbb_platform/core/openbb_core/provider/standard_models/share_price_index.py @@ -0,0 +1,49 @@ +"""Share Price Index Standard Model.""" + +from datetime import date as dateType +from typing import Literal, Optional + +from pydantic import Field + +from openbb_core.provider.abstract.data import Data +from openbb_core.provider.abstract.query_params import QueryParams +from openbb_core.provider.utils.descriptions import ( + DATA_DESCRIPTIONS, + QUERY_DESCRIPTIONS, +) + + +class SharePriceIndexQueryParams(QueryParams): + """Share Price Index Query.""" + + country: str = Field( + description=QUERY_DESCRIPTIONS.get("country", ""), + default="united_states", + ) + frequency: Literal["monthly", "quarter", "annual"] = Field( + description=QUERY_DESCRIPTIONS.get("frequency", ""), + default="monthly", + json_schema_extra={"choices": ["monthly", "quarter", "annual"]}, + ) + start_date: Optional[dateType] = Field( + default=None, description=QUERY_DESCRIPTIONS.get("start_date") + ) + end_date: Optional[dateType] = Field( + default=None, description=QUERY_DESCRIPTIONS.get("end_date") + ) + + +class SharePriceIndexData(Data): + """Share Price Index Data.""" + + date: Optional[dateType] = Field( + default=None, description=DATA_DESCRIPTIONS.get("date") + ) + country: Optional[str] = Field( + default=None, + description=DATA_DESCRIPTIONS.get("country", ""), + ) + value: Optional[float] = Field( + default=None, + description="Share price index value.", + ) diff --git a/openbb_platform/extensions/economy/integration/test_economy_api.py b/openbb_platform/extensions/economy/integration/test_economy_api.py index b06ac4937e6..556094a492b 100644 --- a/openbb_platform/extensions/economy/integration/test_economy_api.py +++ b/openbb_platform/extensions/economy/integration/test_economy_api.py @@ -71,10 +71,10 @@ def test_economy_calendar(params, headers): ( { "country": "spain", - "units": "growth_same", - "frequency": "monthly", - "harmonized": True, - "start_date": "2023-01-01", + "transform": "yoy", + "frequency": "annual", + "harmonized": False, + "start_date": "2020-01-01", "end_date": "2023-06-06", "provider": "fred", } @@ -82,7 +82,7 @@ def test_economy_calendar(params, headers): ( { "country": "portugal,spain", - "units": "growth_same", + "transform": "period", "frequency": "monthly", "harmonized": True, "start_date": "2023-01-01", @@ -90,6 +90,18 @@ def test_economy_calendar(params, headers): "provider": "fred", } ), + ( + { + "country": "portugal,spain", + "transform": "yoy", + "frequency": "quarter", + "harmonized": False, + "start_date": "2020-01-01", + "end_date": "2023-06-06", + "provider": "oecd", + "expenditure": "transport", + } + ), ], ) @pytest.mark.integration @@ -696,3 +708,29 @@ def test_economy_central_bank_holdings(params, headers): result = requests.get(url, headers=headers, timeout=5) assert isinstance(result, requests.Response) assert result.status_code == 200 + + +@parametrize( + "params", + [ + ( + { + "country": "united_states,united_kingdom", + "frequency": "monthly", + "provider": "oecd", + "start_date": "2022-01-01", + "end_date": "2024-04-01", + } + ), + ], +) +@pytest.mark.integration +def test_economy_share_price_index(params, headers): + """Test the economy share price index.""" + params = {p: v for p, v in params.items() if v} + + query_str = get_querystring(params, []) + url = f"http://0.0.0.0:8000/api/v1/economy/share_price_index?{query_str}" + result = requests.get(url, headers=headers, timeout=10) + assert isinstance(result, requests.Response) + assert result.status_code == 200 diff --git a/openbb_platform/extensions/economy/integration/test_economy_python.py b/openbb_platform/extensions/economy/integration/test_economy_python.py index 4825b504949..9899b9b34c2 100644 --- a/openbb_platform/extensions/economy/integration/test_economy_python.py +++ b/openbb_platform/extensions/economy/integration/test_economy_python.py @@ -65,12 +65,36 @@ def test_economy_calendar(params, obb): [ ( { + "country": "spain", + "transform": "yoy", + "frequency": "annual", + "harmonized": False, + "start_date": "2020-01-01", + "end_date": "2023-06-06", + "provider": "fred", + } + ), + ( + { "country": "portugal,spain", - "units": "growth_same", + "transform": "period", "frequency": "monthly", "harmonized": True, "start_date": "2023-01-01", "end_date": "2023-06-06", + "provider": "fred", + } + ), + ( + { + "country": "portugal,spain", + "transform": "yoy", + "frequency": "quarter", + "harmonized": False, + "start_date": "2020-01-01", + "end_date": "2023-06-06", + "provider": "oecd", + "expenditure": "transport", } ), ], @@ -644,3 +668,28 @@ def test_economy_central_bank_holdings(params, obb): assert result assert isinstance(result, OBBject) assert len(result.results) > 0 + + +@parametrize( + "params", + [ + ( + { + "country": "united_states,united_kingdom", + "frequency": "monthly", + "provider": "oecd", + "start_date": "2022-01-01", + "end_date": "2024-04-01", + } + ), + ], +) +@pytest.mark.integration +def test_economy_share_price_index(params, obb): + """Test economy share price index.""" + params = {p: v for p, v in params.items() if v} + + result = obb.economy.share_price_index(**params) + assert result + assert isinstance(result, OBBject) + assert len(result.results) > 0 diff --git a/openbb_platform/extensions/economy/openbb_economy/economy_router.py b/openbb_platform/extensions/economy/openbb_economy/economy_router.py index b318d1183e6..ade0833bd01 100644 --- a/openbb_platform/extensions/economy/openbb_economy/economy_router.py +++ b/openbb_platform/extensions/economy/openbb_economy/economy_router.py @@ -54,11 +54,12 @@ async def calendar( examples=[ APIEx(parameters={"country": "japan,china,turkey", "provider": "fred"}), APIEx( - description="Use the `units` parameter to define the reference period for the change in values.", + description="Use the `transform` parameter to define the reference period for the change in values." + + " Default is YoY.", parameters={ "country": "united_states,united_kingdom", - "units": "growth_previous", - "provider": "fred", + "transform": "period", + "provider": "oecd", }, ), ], @@ -410,3 +411,27 @@ async def central_bank_holdings( ) -> OBBject: """Get the balance sheet holdings of a central bank.""" return await OBBject.from_query(Query(**locals())) + + +@router.command( + model="SharePriceIndex", + examples=[ + APIEx(parameters={"provider": "oecd"}), + APIEx( + description="Multiple countries can be passed in as a list.", + parameters={ + "country": "united_kingdom,germany", + "frequency": "quarterly", + "provider": "oecd", + }, + ), + ], +) +async def share_price_index( + cc: CommandContext, + provider_choices: ProviderChoices, + standard_params: StandardParams, + extra_params: ExtraParams, +) -> OBBject: + """Get the Share Price Index by country from the OECD Short-Term Economics Statistics.""" + return await OBBject.from_query(Query(**locals())) diff --git a/openbb_platform/openbb/assets/reference.json b/openbb_platform/openbb/assets/reference.json index aae0334f071..664b855d393 100644 --- a/openbb_platform/openbb/assets/reference.json +++ b/openbb_platform/openbb/assets/reference.json @@ -2938,15 +2938,80 @@ "message": null }, "description": "Get Consumer Price Index (CPI).\n\nReturns either the rescaled index value, or a rate of change (inflation).", - "examples": "\nExamples\n--------\n\n```python\nfrom openbb import obb\nobb.economy.cpi(country='japan,china,turkey', provider='fred')\n# Use the `units` parameter to define the reference period for the change in values.\nobb.economy.cpi(country='united_states,united_kingdom', units='growth_previous', provider='fred')\n```\n\n", + "examples": "\nExamples\n--------\n\n```python\nfrom openbb import obb\nobb.economy.cpi(country='japan,china,turkey', provider='fred')\n# Use the `transform` parameter to define the reference period for the change in values. Default is YoY.\nobb.economy.cpi(country='united_states,united_kingdom', transform='period', provider='oecd')\n```\n\n", "parameters": { "standard": [ { "name": "country", "type": "Union[str, List[str]]", - "description": "The country to get data. Multiple items allowed for provider(s): fred.", - "default": "", - "optional": false, + "description": "The country to get data. Multiple items allowed for provider(s): fred, oecd.", + "default": "united_states", + "optional": true, + "choices": null + }, + { + "name": "transform", + "type": "Literal['index', 'yoy', 'period']", + "description": "Transformation of the CPI data. Period represents the change since previous. Defaults to change from one year ago (yoy).", + "default": "yoy", + "optional": true, + "choices": [ + "index", + "yoy", + "period" + ] + }, + { + "name": "frequency", + "type": "Literal['annual', 'quarter', 'monthly']", + "description": "The frequency of the data.", + "default": "monthly", + "optional": true, + "choices": [ + "annual", + "quarter", + "monthly" + ] + }, + { + "name": "harmonized", + "type": "bool", + "description": "If true, returns harmonized data.", + "default": false, + "optional": true, + "choices": null + }, + { + "name": "start_date", + "type": "Union[date, str]", + "description": "Start date of the data, in YYYY-MM-DD format.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "end_date", + "type": "Union[date, str]", + "description": "End date of the data, in YYYY-MM-DD format.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "provider", + "type": "Literal['fred', 'oecd']", + "description": "The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: f, r, e, d.", + "default": null, + "optional": true + } + ], + "fred": [ + { + "name": "country", + "type": "str", + "description": "The country to get data.", + "default": "united_states", + "optional": true, "choices": [ "australia", "austria", @@ -2998,56 +3063,111 @@ "united_kingdom", "united_states" ] - }, - { - "name": "units", - "type": "Literal['growth_previous', 'growth_same', 'index_2015']", - "description": "The unit of measurement for the data. Options: - `growth_previous`: Percent growth from the previous period. If monthly data, this is month-over-month, etc - `growth_same`: Percent growth from the same period in the previous year. If looking at monthly data, this would be year-over-year, etc. - `index_2015`: Rescaled index value, such that the |