Skip to content

Commit

Permalink
Merge pull request #4193 from joey-walker/feature/search-return-df
Browse files Browse the repository at this point in the history
Return a dataframe from stocks search, removed export to file system (#3923)
  • Loading branch information
jmaslek authored Feb 10, 2023
2 parents 37d7c87 + 047ee64 commit 7639d46
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 47 deletions.
4 changes: 0 additions & 4 deletions openbb_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,6 @@ def call_search(self, other_args: List[str]):
exchange_country=exchange,
all_exchanges=ns_parser.all_exchanges,
limit=ns_parser.limit,
export=ns_parser.export,
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)

@log_start_end(log=logger)
Expand Down
46 changes: 18 additions & 28 deletions openbb_terminal/stocks/stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from openbb_terminal import config_terminal as cfg
from openbb_terminal.helper_funcs import (
export_data,
lambda_long_number_format_y_axis,
plot_autoscale,
print_rich_table,
Expand Down Expand Up @@ -106,9 +105,7 @@ def search(
exchange_country: str = "",
all_exchanges: bool = False,
limit: int = 0,
export: str = "",
sheet_name: Optional[str] = "",
) -> None:
) -> pd.DataFrame:
"""Search selected query for tickers.
Parameters
Expand All @@ -127,8 +124,12 @@ def search(
Whether to search all exchanges, without this option only the United States market is searched
limit : int
The limit of companies shown.
export : str
Export data
Returns
-------
df: pd.DataFrame
Dataframe of search results.
Empty Dataframe if none are found.
Examples
--------
Expand Down Expand Up @@ -157,10 +158,10 @@ def search(
console.print(
"[red]No companies were found that match the given criteria.[/red]\n"
)
return
return pd.DataFrame()
if not data:
console.print("No companies found.\n")
return
return pd.DataFrame()

if query:
d = fd.search_products(
Expand All @@ -180,23 +181,18 @@ def search(

if not d:
console.print("No companies found.\n")
return
return pd.DataFrame()

df = pd.DataFrame.from_dict(d).T[
["long_name", "short_name", "country", "sector", "industry", "exchange"]
]
if exchange_country:
if exchange_country in market_coverage_suffix:
suffix_tickers = [
ticker.split(".")[1] if "." in ticker else ""
for ticker in list(df.index)
]
df = df[
[
val in market_coverage_suffix[exchange_country]
for val in suffix_tickers
]
]
if exchange_country and exchange_country in market_coverage_suffix:
suffix_tickers = [
ticker.split(".")[1] if "." in ticker else "" for ticker in list(df.index)
]
df = df[
[val in market_coverage_suffix[exchange_country] for val in suffix_tickers]
]

exchange_suffix = {}
for k, v in market_coverage_suffix.items():
Expand Down Expand Up @@ -230,13 +226,7 @@ def search(
title=title,
)

export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"search",
df,
sheet_name,
)
return df


def load(
Expand Down
2 changes: 0 additions & 2 deletions tests/openbb_terminal/stocks/test_stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@ def test_call_func_expect_queue(expected_queue, func, queue):
industry="",
all_exchanges=False,
exchange_country="",
export="csv",
sheet_name=None,
),
),
(
Expand Down
1 change: 0 additions & 1 deletion tests/openbb_terminal/stocks/test_stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def test_search(mocker, use_tab):
exchange_country="",
all_exchanges=False,
limit=5,
export="",
)


Expand Down
25 changes: 13 additions & 12 deletions website/content/sdk/reference/stocks/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ Search selected query for tickers.
Source Code: [[link](https://github.com/OpenBB-finance/OpenBBTerminal/tree/main/openbb_terminal/stocks/stocks_helper.py#L98)]

```python
openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "")
openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0)
```

---

## Parameters

| Name | Type | Description | Default | Optional |
| ---- | ---- | ----------- | ------- | -------- |
| query | str | The search term used to find company tickers | | True |
| country | str | Search by country to find stocks matching the criteria | | True |
| sector | str | Search by sector to find stocks matching the criteria | | True |
| industry | str | Search by industry to find stocks matching the criteria | | True |
| exchange_country | str | Search by exchange country to find stock matching | | True |
| limit | int | The limit of companies shown. | 0 | True |
| export | str | Export data | | True |

| Name | Type | Description | Default | Optional |
|------------------|------|---------------------------------------------------------|---------|----------|
| query | str | The search term used to find company tickers | | True |
| country | str | Search by country to find stocks matching the criteria | | True |
| sector | str | Search by sector to find stocks matching the criteria | | True |
| industry | str | Search by industry to find stocks matching the criteria | | True |
| exchange_country | str | Search by exchange country to find stock matching | | True |
| limit | int | The limit of companies shown. | 0 | True |

---

## Returns

This function does not return anything
| Type | Description |
|--------------|----------------|
| pd.DataFrame | Search results |


---

Expand Down

0 comments on commit 7639d46

Please sign in to comment.