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

Fixed search command issues #4845

Merged
merged 18 commits into from
May 5, 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
19 changes: 7 additions & 12 deletions openbb_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ def call_search(self, other_args: List[str]):
help="Search by sector to find stocks matching the criteria",
)
parser.add_argument(
"-g",
"--industry-group",
"--industrygroup",
default="",
choices=stocks_helper.format_parse_choices(self.industry_group),
type=str.lower,
Expand Down Expand Up @@ -227,8 +226,7 @@ def call_search(self, other_args: List[str]):
help="Search by a specific exchange to find stocks matching the criteria",
)
parser.add_argument(
"-m",
"--exchange-country",
"--exchangecountry",
default="",
choices=stocks_helper.format_parse_choices(
list(stocks_helper.market_coverage_suffix.keys())
Expand All @@ -248,13 +246,12 @@ def call_search(self, other_args: List[str]):
)
if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-q")
ns_parser = self.parse_known_args_and_warn(
if ns_parser := self.parse_known_args_and_warn(
parser,
other_args,
EXPORT_ONLY_RAW_DATA_ALLOWED,
limit=10,
)
if ns_parser:
):
# Mapping
sector = stocks_helper.map_parse_choices(self.sector)[ns_parser.sector]
industry = stocks_helper.map_parse_choices(self.industry)[
Expand Down Expand Up @@ -296,7 +293,7 @@ def call_tob(self, other_args: List[str]):
"--ticker",
action="store",
dest="s_ticker",
required=not any(x in other_args for x in ["-h", "--help"])
required=all(x not in other_args for x in ["-h", "--help"])
and not self.ticker,
help="Ticker to get data for",
)
Expand All @@ -311,10 +308,8 @@ def call_tob(self, other_args: List[str]):

if not self.ticker and other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-t")
ns_parser = self.parse_known_args_and_warn(parser, other_args)

if ns_parser:
ticker = ns_parser.s_ticker if ns_parser.s_ticker else self.ticker
if ns_parser := self.parse_known_args_and_warn(parser, other_args):
ticker = ns_parser.s_ticker or self.ticker
cboe_view.display_top_of_book(ticker, ns_parser.exchange)

@log_start_end(log=logger)
Expand Down
15 changes: 10 additions & 5 deletions openbb_terminal/stocks/stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ def search(
exchange: str
Search by exchange to find stock matching the criteria
exchange_country: str
Search by exchange country to find stock matching
Search by exchange country to find stock matching the criteria
all_exchanges: bool
Whether to search all exchanges, without this option only the United States market is searched
limit : int
The limit of companies shown.
The limit of results shown, where 0 means all the results

Returns
-------
Expand All @@ -166,7 +166,9 @@ def search(
kwargs["industry_group"] = industry_group
if exchange:
kwargs["exchange"] = exchange
kwargs["exclude_exchanges"] = False if exchange_country else not all_exchanges
kwargs["exclude_exchanges"] = (
False if (exchange_country or exchange) else not all_exchanges
)

try:
equities_database = fd.Equities()
Expand All @@ -187,7 +189,7 @@ def search(
" capabilities. This tends to be due to access restrictions for GitHub.com,"
" please check if you can access this website without a VPN.[/red]\n"
)
data = {}
data = pd.DataFrame()
except ValueError:
console.print(
"[red]No companies were found that match the given criteria.[/red]\n"
Expand Down Expand Up @@ -223,6 +225,8 @@ def search(
exchange_suffix[x] = k

df = df[["name", "country", "sector", "industry_group", "industry", "exchange"]]
# To automate renaming columns
headers = [col.replace("_", " ") for col in df.columns.tolist()]

title = "Companies found"
if query:
Expand Down Expand Up @@ -252,7 +256,8 @@ def search(
print_rich_table(
df,
show_index=True,
headers=["Name", "Country", "Sector", "Industry Group", "Industry", "Exchange"],
headers=headers,
index_name="Symbol",
title=title,
limit=limit,
)
Expand Down
1 change: 1 addition & 0 deletions tests/openbb_terminal/stocks/test_stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_search(mocker, use_tab):
sector="",
industry="",
industry_group="",
exchange="",
exchange_country="",
all_exchanges=False,
limit=5,
Expand Down