diff options
author | Danglewood <85772166+deeleeramone@users.noreply.github.com> | 2024-06-02 12:51:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-02 19:51:49 +0000 |
commit | 0b3e2860357373846e21225b10ea4ec5900ccfa9 (patch) | |
tree | 3453952240f90b6f4122d574328aff38c833a0fb | |
parent | d80a5ed3920a6bd4504d20585139d4386b360c9d (diff) |
[Feature] Add Retail Prices to Economy (#6474)
* add retail prices
* static files
* recapture test
* import lib path instead of file..?
* add missing init file
* need a docstring
* no tea
17 files changed, 1000 insertions, 2 deletions
diff --git a/openbb_platform/core/openbb_core/provider/standard_models/retail_prices.py b/openbb_platform/core/openbb_core/provider/standard_models/retail_prices.py new file mode 100644 index 00000000000..3f0996e411d --- /dev/null +++ b/openbb_platform/core/openbb_core/provider/standard_models/retail_prices.py @@ -0,0 +1,56 @@ +"""Retail Prices Standard Model.""" + +from datetime import date as dateType +from typing import 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 RetailPricesQueryParams(QueryParams): + """Retail Prices Query.""" + + item: Optional[str] = Field( + default=None, + description="The item or basket of items to query.", + ) + country: str = Field( + description=QUERY_DESCRIPTIONS.get("country", ""), + default="united_states", + ) + 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 RetailPricesData(Data): + """Retail Prices Data.""" + + date: Optional[dateType] = Field( + default=None, description=DATA_DESCRIPTIONS.get("date") + ) + symbol: Optional[str] = Field( + default=None, + description=DATA_DESCRIPTIONS.get("symbol", ""), + ) + country: Optional[str] = Field( + default=None, + description=DATA_DESCRIPTIONS.get("country", ""), + ) + description: str = Field( + default=None, + description="Description of the item.", + ) + value: Optional[float] = Field( + default=None, + description="Price, or change in price, per unit.", + ) diff --git a/openbb_platform/extensions/economy/integration/test_economy_api.py b/openbb_platform/extensions/economy/integration/test_economy_api.py index db2ada070bb..118c16c3cee 100644 --- a/openbb_platform/extensions/economy/integration/test_economy_api.py +++ b/openbb_platform/extensions/economy/integration/test_economy_api.py @@ -786,3 +786,32 @@ def test_economy_immediate_interest_rate(params, headers): result = requests.get(url, headers=headers, timeout=10) assert isinstance(result, requests.Response) assert result.status_code == 200 + + +@parametrize( + "params", + [ + ( + { + "country": "united_states", + "item": "meats", + "region": "all_city", + "frequency": "annual", + "provider": "fred", + "start_date": "2022-01-01", + "end_date": "2024-04-01", + "transform": "pc1", + } + ), + ], +) +@pytest.mark.integration +def test_economy_retail_prices(params, headers): + """Test the economy retail_prices.""" + 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/retail_prices?{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 f31297561c3..0ab1441af5b 100644 --- a/openbb_platform/extensions/economy/integration/test_economy_python.py +++ b/openbb_platform/extensions/economy/integration/test_economy_python.py @@ -743,3 +743,31 @@ def test_economy_immediate_interest_rate(params, obb): assert result assert isinstance(result, OBBject) assert len(result.results) > 0 + + +@parametrize( + "params", + [ + ( + { + "country": "united_states", + "item": "meats", + "region": "all_city", + "frequency": "annual", + "provider": "fred", + "start_date": "2022-01-01", + "end_date": "2024-04-01", + "transform": "pc1", + } + ), + ], +) +@pytest.mark.integration +def test_economy_retail_prices(params, obb): + """Test economy retail prices.""" + params = {p: v for p, v in params.items() if v} + + result = obb.economy.retail_prices(**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 43f86910491..57b3cd55c97 100644 --- a/openbb_platform/extensions/economy/openbb_economy/economy_router.py +++ b/openbb_platform/extensions/economy/openbb_economy/economy_router.py @@ -483,3 +483,35 @@ async def immediate_interest_rate( ) -> OBBject: """Get immediate interest rates by country.""" return await OBBject.from_query(Query(**locals())) + + +@router.command( + model="RetailPrices", + examples=[ + APIEx(parameters={"provider": "fred"}), + APIEx( + description="The price of eggs in the northeast census region.", + parameters={ + "item": "eggs", + "region": "northeast", + "provider": "fred", + }, + ), + APIEx( + description="The percentage change in price, from one-year ago, of various meats, US City Average.", + parameters={ + "item": "meats", + "transform": "pc1", + "provider": "fred", + }, + ), + ], +) +async def retail_prices( + cc: CommandContext, + provider_choices: ProviderChoices, + standard_params: StandardParams, + extra_params: ExtraParams, +) -> OBBject: + """Get retail prices for common items.""" + return await OBBject.from_query(Query(**locals())) diff --git a/openbb_platform/openbb/assets/reference.json b/openbb_platform/openbb/assets/reference.json index 5f7900d0901..afb50083b18 100644 --- a/openbb_platform/openbb/assets/reference.json +++ b/openbb_platform/openbb/assets/reference.json @@ -6270,6 +6270,245 @@ }, "model": "ImmediateInterestRate" }, + "/economy/retail_prices": { + "deprecated": { + "flag": null, + "message": null + }, + "description": "Get retail prices for common items.", + "examples": "\nExamples\n--------\n\n```python\nfrom openbb import obb\nobb.economy.retail_prices(provider='fred')\n# The price of eggs in the northeast census region.\nobb.economy.retail_prices(item='eggs', region=northeast, provider='fred')\n# The percentage change in price, from one-year ago, of various meats, US City Average.\nobb.economy.retail_prices(item='meats', transform=pc1, provider='fred')\n```\n\n", + "parameters": { + "standard": [ + { + "name": "item", + "type": "str", + "description": "The item or basket of items to query.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "country", + "type": "str", + "description": "The country to get data.", + "default": "united_states", + "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']", + "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": "item", + "type": "Literal['beverages', 'cereals', 'dairy', 'fuel', 'produce', 'meats', 'bacon', 'bananas', 'beans', 'beef', 'beer', 'bread', 'butter', 'cheese', 'chicken', 'chops', 'coffee', 'cookies', 'corn', 'diesel', 'eggs', 'electricity', 'flour', 'gas', 'gasoline', 'grapefruit', 'ground_beef', 'ham', 'ice_cream', 'lemons', 'lettuce', 'malt_beverages', 'milk', 'oil', 'orange_juice', 'oranges', 'pork', 'potato_chips', 'potatoes', 'rice', 'soft_drinks', 'spaghetti', 'steak', 'strawberries', 'sugar', 'tomatoes', 'unleaded', 'usda', 'vodka', 'wine', 'yogurt']", + "description": "The item or basket of items to query.", + "default": "fuel", + "optional": true, + "choices": [ + "beverages", + "cereals", + "dairy", + "fuel", + "meats", + "produce", + "bacon", + "bananas", + "beans", + "beef", + "beer", + "bread", + "butter", + "cheese", + "chicken", + "chops", + "coffee", + "cookies", + "corn", + "diesel", + "eggs", + "electricity", + "flour", + "gas", + "gasoline", + "grapefruit", + "groud_beef", + "ham", + "ice_cream", + "lemons", + "lettuce", + "malt_beverages", + "milk", + "oil", + "orange_juice", + "oranges", + "potato_chips", + "potatoes", + "pork", + "rice", + "soft_drinks", + "spaghetti", + "steak", + "strawberries", + "sugar", + "tomatoes", + "unleaded", + "usda", + "vodka", + "wine", + "yogurt" + ] + }, + { + "name": "country", + "type": "Literal['united_states']", + "description": "The country to get data.", + "default": "united_states", + "optional": true, + "choices": "united_states" + }, + { + "name": "region", + "type": "Literal['all_city', 'northeast', 'midwest', 'south', 'west']", + "description": "The region to get average price levels for.", + "default": "all_city", + "optional": true, + "choices": [ + "all_city", + "northeast", + "midwest", + "south", + "west" + ] + }, + { + "name": "frequency", + "type": "Literal['annual', 'quarter', 'monthly']", + "description": "The frequency of the data.", + "default": "monthly", + "optional": true, + "choices": [ + "annual", + "quarter", + "monthly" + ] + }, + { + "name": "transform", + "type": "Literal['chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log']", + "description": "Transformation type None = No transformation chg = Change ch1 = Change from Year Ago pch = Percent Change pc1 = Percent Change from Year Ago pca = Compounded Annual Rate of Change cch = Continuously Compounded Rate of Change cca = Continuously Compounded Annual Rate of Change log = Natural Log", + "default": null, + "optional": true, + "choices": [ + "chg", + "ch1", + "pch", + "pc1", + "pca", + "cch", + "cca", + "log" + ] + } + ] + }, + "returns": { + "OBBject": [ + { + "name": "results", + "type": "List[RetailPrices]", + "description": "Serializable results." + }, + { + "name": "provider", + "type": "Optional[Literal['fred']]", + "description": "Provider name." + }, + { + "name": "warnings", + "type": "Optional[List[Warning_]]", + "description": "List of warnings." + }, + { + "name": "chart", + "type": "Optional[Chart]", + "description": "Chart object." + }, + { + "name": "extra", + "type": "Dict[str, Any]", + "description": "Extra info." + } + ] + }, + "data": { + "standard": [ + { + "name": "date", + "type": "date", + "description": "The date of the data.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "symbol", + "type": "str", + "description": "Symbol representing the entity requested in the data.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "country", + "type": "str", + "description": "", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "description", + "type": "str", + "description": "Description of the item.", + "default": null, + "optional": true, + "choices": null + }, + { + "name": "value", + "type": "float", + "description": "Price, or change in price, per unit.", + "default": null, + "optional": true, + "choices": null + } + ], + "fred": [] + }, + "model": "RetailPrices" + }, "/equity/calendar/ipo": { "deprecated": { "flag": null, diff --git a/openbb_platform/openbb/package/economy.py b/openbb_platform/openbb/package/economy.py index ec82f700614..7051e898719 100644 --- a/openbb_platform/openbb/package/economy.py +++ b/openbb_platform/openbb/package/economy.py @@ -29,6 +29,7 @@ class ROUTER_economy(Container): indicators long_term_interest_rate money_measures + retail_prices risk_premium share_price_index short_term_interest_rate @@ -1759,6 +1760,122 @@ class ROUTER_economy(Container): @exception_handler @validate + def retail_prices( + self, + item: Annotated[ + Optional[str], + OpenBBField(description="The item or basket of items to query."), + ] = None, + country: Annotated[ + str, OpenBBField(description="The country to get data.") + ] = "united_states", + start_date: Annotated[ + Union[datetime.date, None, str], + OpenBBField(description="Start date of the data, in YYYY-MM-DD format."), + ] = None, + end_date: Annotated[ + Union[datetime.date, None, str], + OpenBBField(description="End date of the data, in YYYY-MM-DD format."), + ] = None, + provider: Annotated[ + Optional[Literal["fred"]], + OpenBBField( + description="The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: fred." + ), + ] = None, + **kwargs + ) -> OBBject: + """Get retail prices for common items. + + Parameters + ---------- + item : Optional[str] + The item or basket of items to query. + country : str + The country to get data. + start_date : Union[datetime.date, None, str] + Start date of the data, in YYYY-MM-DD format. + end_date : Union[datetime.date, None, str] + End date of the data, in YYYY-MM-DD format. + provider : Optional[Literal['fred']] + The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: fred. + region : Literal['all_city', 'northeast', 'midwest', 'south', 'west'] + The region to get average price levels for. (provider: fred) + frequency : Literal['annual', 'quarter', 'monthly'] + The frequency of the data. (provider: fred) + transform : Optional[Literal['chg', 'ch1', 'pch', 'pc1', 'pca', 'cch', 'cca', 'log']] + + Transformation type + None = No transformation + chg = Change + ch1 = Change from Year Ago + pch = Percent Change + pc1 = Percent Change from Year Ago + pca = Compounded Annual Rate of Change + cch = Continuously Compounded Rate of Change + cca = Continuously Compounded Annual Rate of Change + log = Natural Log + (provider: fred) + + Returns + ------- + OBBject + results : List[RetailPrices] + Serializable results. + provider : Optional[Literal['fred']] + Provider name. + warnings : Optional[List[Warning_]] + List of warnings. + chart : Optional[Chart] + Chart object. + extra : Dict[str, Any] + Extra info. + + RetailPrices + ------------ + date : Optional[date] + The date of the data. + symbol : Optional[str] + Symbol representing the entity requested in the data. + country : Optional[str] + + description : Optional[str] + Description of the item. + value : Optional[float] + Price, or change in price, per unit. + + Examples + -------- + >>> from openbb import obb + >>> obb.economy.retail_prices(provider='fred') + >>> # The price of eggs in the northeast census region. + >>> obb.economy.retail_prices(item='eggs', region='northeast', provider='fred') + >>> # The percentage change in price, from one-year ago, of various meats, US City Average. + >>> obb.economy.retail_prices(item='meats', transform='pc1', provider='fred') + """ # noqa: E501 + + return self._run( + "/economy/retail_prices", + **filter_inputs( + provider_choices={ + "provider": self._get_provider( + provider, + "economy.retail_prices", + ("fred",), + ) + }, + standard_params={ + "item": item, + "country": country, + "start_date": start_date, + "end_date": end_date, + }, + extra_params=kwargs, + ) + ) + + @exception_handler + @validate def risk_premium( self, provider: Annotated[ diff --git a/openbb_platform/providers/fred/openbb_fred/__init__.py b/openbb_platform/providers/fred/openbb_fred/__init__.py index 92323baabfb..d4a86ac40f8 100644 --- a/openbb_platform/providers/fred/openbb_fred/__init__.py +++ b/openbb_platform/providers/fred/openbb_fred/__init__.py @@ -18,6 +18,7 @@ from openbb_fred.models.ice_bofa import FREDICEBofAFetcher from openbb_fred.models.iorb_rates import FREDIORBFetcher from openbb_fred.models.moody import FREDMoodyCorporateBondIndexFetcher from openbb_fred.models.regional import FredRegionalDataFetcher +from openbb_fred.models.retail_prices import FredRetailPricesFetcher from openbb_fred.models.search import ( FredSearchFetcher, ) @@ -58,6 +59,7 @@ Research division of the Federal Reserve Bank of St. Louis that has more than "FredSearch": FredSearchFetcher, "FredSeries": FredSeriesFetcher, "FredRegional": FredRegionalDataFetcher, + "RetailPrices": FredRetailPricesFetcher, "SpotRate": FREDSpotRateFetcher, "HighQualityMarketCorporateBond": FREDHighQualityMarketCorporateBondFetcher, "TreasuryConstantMaturity": FREDTreasuryConstantMaturityFetcher, diff --git a/openbb_platform/providers/fred/openbb_fred/models/consumer_price_index.py b/openbb_platform/providers/fred/openbb_fred/models/consumer_price_index.py index 9efd080f0a8..75479e8a154 100644 --- a/openbb_platform/providers/fred/openbb_fred/models/consumer_price_index.py +++ b/openbb_platform/providers/fred/openbb_fred/models/consumer_price_index.py @@ -72,8 +72,7 @@ class FREDConsumerPriceIndexFetcher( } units = ( "growth_same" - if query.transform == "period" - and frequency == "annual" + if query.transform == "period" and frequency == "annual" else units_dict.get(query.transform) ) step_1 = [x for x in all_options if x["country"] in query.country] |