Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exchange filter in stocks\search #4801

Merged
merged 7 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions openbb_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -197,7 +198,7 @@ def call_search(self, other_args: List[str]):
)
parser.add_argument(
"-g",
"--industrygroup",
"--industry-group",
default="",
choices=stocks_helper.format_parse_choices(self.industry_group),
type=str.lower,
Expand All @@ -219,15 +220,24 @@ def call_search(self, other_args: List[str]):
"-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",
Expand All @@ -253,7 +263,10 @@ def call_search(self, other_args: List[str]):
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]

Expand All @@ -263,7 +276,8 @@ def call_search(self, other_args: List[str]):
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,
)
Expand Down
15 changes: 13 additions & 2 deletions openbb_terminal/stocks/stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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()}"
Expand Down
1 change: 1 addition & 0 deletions tests/openbb_terminal/stocks/test_stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def test_call_func_expect_queue(expected_queue, func, queue):
industry="",
all_exchanges=False,
exchange_country="",
exchange="",
),
),
(
Expand Down