Added exchange filter in stocks\search (#4801)

* Added `exchange` filter in `stocks\search`

* Fixed arguments

* test

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
This commit is contained in:
Pratyush Shukla
2023-04-19 20:32:04 +05:30
committed by GitHub
parent e27a40ab09
commit df5d5a0047
3 changed files with 35 additions and 9 deletions

View File

@@ -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,
)

View File

@@ -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()}"

View File

@@ -273,6 +273,7 @@ def test_call_func_expect_queue(expected_queue, func, queue):
industry="",
all_exchanges=False,
exchange_country="",
exchange="",
),
),
(