From df5d5a0047ae35ec348538e983f1de077f9bc46a Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Wed, 19 Apr 2023 20:32:04 +0530 Subject: [PATCH] Added `exchange` filter in `stocks\search` (#4801) * Added `exchange` filter in `stocks\search` * Fixed arguments * test --------- Co-authored-by: James Maslek --- openbb_terminal/stocks/stocks_controller.py | 28 ++++++++++++++----- openbb_terminal/stocks/stocks_helper.py | 15 ++++++++-- .../stocks/test_stocks_controller.py | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/openbb_terminal/stocks/stocks_controller.py b/openbb_terminal/stocks/stocks_controller.py index 77c58131b2d..6572379ccf4 100644 --- a/openbb_terminal/stocks/stocks_controller.py +++ b/openbb_terminal/stocks/stocks_controller.py @@ -77,8 +77,9 @@ class StocksController(StockBaseController): sector = stocks_options["sector"].tolist() industry_group = stocks_options["industry_group"].tolist() industry = stocks_options["industry"].tolist() + exchange = stocks_options["exchange"].tolist() except Exception: - country, sector, industry_group, industry = {}, {}, {}, {} + country, sector, industry_group, industry, exchange = {}, {}, {}, {}, {} console.print( "[red]Note: Some datasets from GitHub failed to load. This means that the `search` command will not work. " "If other commands are failing please check your internet connection or communicate with your " @@ -197,7 +198,7 @@ class StocksController(StockBaseController): ) parser.add_argument( "-g", - "--industrygroup", + "--industry-group", default="", choices=stocks_helper.format_parse_choices(self.industry_group), type=str.lower, @@ -219,15 +220,24 @@ class StocksController(StockBaseController): "-e", "--exchange", default="", + choices=stocks_helper.format_parse_choices(self.exchange), + type=str.lower, + metavar="exchange", + dest="exchange", + help="Search by a specific exchange to find stocks matching the criteria", + ) + parser.add_argument( + "-m", + "--exchange-country", + default="", choices=stocks_helper.format_parse_choices( list(stocks_helper.market_coverage_suffix.keys()) ), type=str.lower, - metavar="exchange", + metavar="exchange_country", dest="exchange_country", - help="Search by a specific exchange country to find stocks matching the criteria", + help="Search by a specific country and all its exchanges to find stocks matching the criteria", ) - parser.add_argument( "-a", "--all-exchanges", @@ -253,7 +263,10 @@ class StocksController(StockBaseController): industry_group = stocks_helper.map_parse_choices(self.industry_group)[ ns_parser.industry_group ] - exchange = stocks_helper.map_parse_choices( + exchange = stocks_helper.map_parse_choices(self.exchange)[ + ns_parser.exchange + ] + exchange_country = stocks_helper.map_parse_choices( list(stocks_helper.market_coverage_suffix.keys()) )[ns_parser.exchange_country] @@ -263,7 +276,8 @@ class StocksController(StockBaseController): sector=sector, industry_group=industry_group, industry=industry, - exchange_country=exchange, + exchange=exchange, + exchange_country=exchange_country, all_exchanges=ns_parser.all_exchanges, limit=ns_parser.limit, ) diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index 7cb3360be18..047ccadfd98 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -116,6 +116,7 @@ def search( sector: str = "", industry_group: str = "", industry: str = "", + exchange: str = "", exchange_country: str = "", all_exchanges: bool = False, limit: int = 0, @@ -130,8 +131,12 @@ def search( Search by country to find stocks matching the criteria sector : str Search by sector to find stocks matching the criteria + industry_group : str + Search by industry group to find stocks matching the criteria industry : str Search by industry to find stocks matching the criteria + exchange: str + Search by exchange to find stock matching the criteria exchange_country: str Search by exchange country to find stock matching all_exchanges: bool @@ -148,7 +153,7 @@ def search( Examples -------- >>> from openbb_terminal.sdk import openbb - >>> openbb.stocks.search(country="united states", exchange_country="Germany") + >>> openbb.stocks.search(country="United States", exchange_country="Germany") """ kwargs: Dict[str, Any] = {"exclude_exchanges": False} if country: @@ -159,6 +164,8 @@ def search( kwargs["industry"] = industry if industry_group: kwargs["industry_group"] = industry_group + if exchange: + kwargs["exchange"] = exchange kwargs["exclude_exchanges"] = False if exchange_country else not all_exchanges try: @@ -220,7 +227,11 @@ def search( title = "Companies found" if query: title += f" on term {query}" - if exchange_country: + if exchange_country and exchange: + title += f" on the exchange {exchange} in {exchange_country.replace('_', ' ').title()}" + if exchange and not exchange_country: + title += f" on the exchange {exchange}" + if exchange_country and not exchange: title += f" on an exchange in {exchange_country.replace('_', ' ').title()}" if country: title += f" in {country.replace('_', ' ').title()}" diff --git a/tests/openbb_terminal/stocks/test_stocks_controller.py b/tests/openbb_terminal/stocks/test_stocks_controller.py index f42cee41a38..10509a33649 100644 --- a/tests/openbb_terminal/stocks/test_stocks_controller.py +++ b/tests/openbb_terminal/stocks/test_stocks_controller.py @@ -273,6 +273,7 @@ def test_call_func_expect_queue(expected_queue, func, queue): industry="", all_exchanges=False, exchange_country="", + exchange="", ), ), (