From 4186317a00d1e2141f4671b834d52b887d96ef23 Mon Sep 17 00:00:00 2001 From: colin99d Date: Fri, 4 Nov 2022 11:45:55 +0100 Subject: [PATCH 1/3] Refactored quote command --- openbb_terminal/sdk.py | 2 +- openbb_terminal/stocks/stocks_controller.py | 3 +- openbb_terminal/stocks/stocks_helper.py | 60 ----------------- openbb_terminal/stocks/stocks_models.py | 64 ++++++++++++++++++- openbb_terminal/stocks/stocks_views.py | 13 ++++ .../stocks/test_stocks_helper.py | 3 +- 6 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 openbb_terminal/stocks/stocks_views.py diff --git a/openbb_terminal/sdk.py b/openbb_terminal/sdk.py index bf51e71f936f..7226b528d8ce 100644 --- a/openbb_terminal/sdk.py +++ b/openbb_terminal/sdk.py @@ -1896,7 +1896,7 @@ "model": "openbb_terminal.stocks.stocks_helper.process_candle" }, "stocks.search": {"model": "openbb_terminal.stocks.stocks_helper.search"}, - "stocks.quote": {"model": "openbb_terminal.stocks.stocks_helper.quote"}, + "stocks.quote": {"model": "openbb_terminal.stocks.stocks_models.load_quote"}, "stocks.tob": {"model": "openbb_terminal.stocks.cboe_model.get_top_of_book"}, "stocks.candle": {"model": "openbb_terminal.stocks.stocks_helper.display_candle"}, "crypto.load": { diff --git a/openbb_terminal/stocks/stocks_controller.py b/openbb_terminal/stocks/stocks_controller.py index 6cef8807528d..13ccae959e5a 100644 --- a/openbb_terminal/stocks/stocks_controller.py +++ b/openbb_terminal/stocks/stocks_controller.py @@ -33,6 +33,7 @@ translate, ) from openbb_terminal.stocks import stocks_helper +from openbb_terminal.stocks import stocks_views # pylint: disable=R1710,import-outside-toplevel,R0913,R1702,no-member @@ -368,7 +369,7 @@ def call_quote(self, other_args: List[str]): other_args.insert(0, "-t") ns_parser = self.parse_known_args_and_warn(parser, other_args) if ns_parser: - stocks_helper.quote(ns_parser.s_ticker) + stocks_views.display_quote(ns_parser.s_ticker) @log_start_end(log=logger) def call_codes(self, _): diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index 6947da86f11e..2ff75114250f 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -23,7 +23,6 @@ from requests.exceptions import ReadTimeout import yfinance as yf -from numpy.core.fromnumeric import transpose from plotly.subplots import make_subplots from scipy import stats @@ -721,65 +720,6 @@ def display_candle( return data -def quote(symbol: str) -> pd.DataFrame: - """Ticker quote. - - Parameters - ---------- - symbol : str - Ticker - """ - ticker = yf.Ticker(symbol) - - try: - quote_df = pd.DataFrame( - [ - { - "Symbol": ticker.info["symbol"], - "Name": ticker.info["shortName"], - "Price": ticker.info["regularMarketPrice"], - "Open": ticker.info["regularMarketOpen"], - "High": ticker.info["dayHigh"], - "Low": ticker.info["dayLow"], - "Previous Close": ticker.info["previousClose"], - "Volume": ticker.info["volume"], - "52 Week High": ticker.info["fiftyTwoWeekHigh"], - "52 Week Low": ticker.info["fiftyTwoWeekLow"], - } - ] - ) - - quote_df["Change"] = quote_df["Price"] - quote_df["Previous Close"] - quote_df["Change %"] = quote_df.apply( - lambda x: f'{((x["Change"] / x["Previous Close"]) * 100):.2f}%', - axis="columns", - ) - for c in [ - "Price", - "Open", - "High", - "Low", - "Previous Close", - "52 Week High", - "52 Week Low", - "Change", - ]: - quote_df[c] = quote_df[c].apply(lambda x: f"{x:.2f}") - quote_df["Volume"] = quote_df["Volume"].apply(lambda x: f"{x:,}") - - quote_df = quote_df.set_index("Symbol") - - quote_data = transpose(quote_df) - - print_rich_table(quote_data, title="Ticker Quote", show_index=True) - return quote_data - - except KeyError: - logger.exception("Invalid stock ticker") - console.print(f"Invalid stock ticker: {symbol}") - return "" - - def load_ticker( ticker: str, start_date: Union[str, datetime], end_date: Union[str, datetime] = "" ) -> pd.DataFrame: diff --git a/openbb_terminal/stocks/stocks_models.py b/openbb_terminal/stocks/stocks_models.py index 3ae983e0b8ce..3df088af9fe3 100644 --- a/openbb_terminal/stocks/stocks_models.py +++ b/openbb_terminal/stocks/stocks_models.py @@ -1,18 +1,22 @@ import os from datetime import datetime +import logging import pyEX import requests import pandas as pd import yfinance as yf from alpha_vantage.timeseries import TimeSeries -from openbb_terminal.decorators import check_api_key +from numpy.core.fromnumeric import transpose +from openbb_terminal.decorators import check_api_key from openbb_terminal.rich_config import console from openbb_terminal import config_terminal as cfg # pylint: disable=unsupported-assignment-operation,no-member +logger = logging.getLogger(__name__) + def load_stock_av( symbol: str, start_date: datetime, end_date: datetime @@ -225,3 +229,61 @@ def load_stock_polygon( df_stock_candidate = df_stock_candidate.sort_values(by="date") df_stock_candidate = df_stock_candidate.set_index("date") return df_stock_candidate + + +def load_quote(symbol: str) -> pd.DataFrame: + """Ticker quote. + + Parameters + ---------- + symbol : str + Ticker + """ + ticker = yf.Ticker(symbol) + + try: + quote_df = pd.DataFrame( + [ + { + "Symbol": ticker.info["symbol"], + "Name": ticker.info["shortName"], + "Price": ticker.info["regularMarketPrice"], + "Open": ticker.info["regularMarketOpen"], + "High": ticker.info["dayHigh"], + "Low": ticker.info["dayLow"], + "Previous Close": ticker.info["previousClose"], + "Volume": ticker.info["volume"], + "52 Week High": ticker.info["fiftyTwoWeekHigh"], + "52 Week Low": ticker.info["fiftyTwoWeekLow"], + } + ] + ) + + quote_df["Change"] = quote_df["Price"] - quote_df["Previous Close"] + quote_df["Change %"] = quote_df.apply( + lambda x: f'{((x["Change"] / x["Previous Close"]) * 100):.2f}%', + axis="columns", + ) + for c in [ + "Price", + "Open", + "High", + "Low", + "Previous Close", + "52 Week High", + "52 Week Low", + "Change", + ]: + quote_df[c] = quote_df[c].apply(lambda x: f"{x:.2f}") + quote_df["Volume"] = quote_df["Volume"].apply(lambda x: f"{x:,}") + + quote_df = quote_df.set_index("Symbol") + + quote_data = transpose(quote_df) + + return quote_data + + except KeyError: + logger.exception("Invalid stock ticker") + console.print(f"Invalid stock ticker: {symbol}") + return pd.DataFrame() diff --git a/openbb_terminal/stocks/stocks_views.py b/openbb_terminal/stocks/stocks_views.py new file mode 100644 index 000000000000..985666e47ec9 --- /dev/null +++ b/openbb_terminal/stocks/stocks_views.py @@ -0,0 +1,13 @@ +import pandas as pd +from openbb_terminal.stocks import stocks_models +from openbb_terminal.helper_funcs import print_rich_table + + +def display_quote(symbol: str) -> pd.DataFrame: + quote_data = stocks_models.load_quote(symbol) + if quote_data is None: + return pd.DataFrame() + if quote_data.empty: + return pd.DataFrame() + print_rich_table(quote_data, title="Ticker Quote", show_index=True) + return quote_data diff --git a/tests/openbb_terminal/stocks/test_stocks_helper.py b/tests/openbb_terminal/stocks/test_stocks_helper.py index ed405dc60b78..57604fb76f29 100644 --- a/tests/openbb_terminal/stocks/test_stocks_helper.py +++ b/tests/openbb_terminal/stocks/test_stocks_helper.py @@ -7,6 +7,7 @@ # IMPORTATION INTERNAL from openbb_terminal.stocks import stocks_helper +from openbb_terminal.stocks import stocks_views from openbb_terminal import helper_funcs @@ -26,7 +27,7 @@ def vcr_config(): @pytest.mark.vcr def test_quote(): - stocks_helper.quote("GME") + stocks_views.display_quote("GME") @pytest.mark.default_cassette("test_search") From 9a1abc0be9f55f09af699dea3952508713ba1200 Mon Sep 17 00:00:00 2001 From: james Date: Fri, 4 Nov 2022 10:36:32 -0400 Subject: [PATCH 2/3] gotchu colin --- openbb_terminal/stocks/stocks_models.py | 5 ++--- openbb_terminal/stocks/stocks_views.py | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openbb_terminal/stocks/stocks_models.py b/openbb_terminal/stocks/stocks_models.py index 3df088af9fe3..3155eeaf8c97 100644 --- a/openbb_terminal/stocks/stocks_models.py +++ b/openbb_terminal/stocks/stocks_models.py @@ -7,7 +7,6 @@ import pandas as pd import yfinance as yf from alpha_vantage.timeseries import TimeSeries -from numpy.core.fromnumeric import transpose from openbb_terminal.decorators import check_api_key from openbb_terminal.rich_config import console @@ -232,7 +231,7 @@ def load_stock_polygon( def load_quote(symbol: str) -> pd.DataFrame: - """Ticker quote. + """Ticker quote. [Source: YahooFinance] Parameters ---------- @@ -279,7 +278,7 @@ def load_quote(symbol: str) -> pd.DataFrame: quote_df = quote_df.set_index("Symbol") - quote_data = transpose(quote_df) + quote_data = quote_df.T return quote_data diff --git a/openbb_terminal/stocks/stocks_views.py b/openbb_terminal/stocks/stocks_views.py index 985666e47ec9..4cb5ba9de7e6 100644 --- a/openbb_terminal/stocks/stocks_views.py +++ b/openbb_terminal/stocks/stocks_views.py @@ -4,6 +4,7 @@ def display_quote(symbol: str) -> pd.DataFrame: + """Display quote from YahooFinance""" quote_data = stocks_models.load_quote(symbol) if quote_data is None: return pd.DataFrame() From d697ca4edc5e7615f0a8bea8ed0319bbc25f81c5 Mon Sep 17 00:00:00 2001 From: james Date: Fri, 4 Nov 2022 12:32:12 -0400 Subject: [PATCH 3/3] Test --- tests/openbb_terminal/stocks/test_stocks_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/openbb_terminal/stocks/test_stocks_controller.py b/tests/openbb_terminal/stocks/test_stocks_controller.py index 3c716bfdb00b..e57cb3a21d02 100644 --- a/tests/openbb_terminal/stocks/test_stocks_controller.py +++ b/tests/openbb_terminal/stocks/test_stocks_controller.py @@ -268,7 +268,7 @@ def test_call_func_expect_queue(expected_queue, func, queue): ( "call_quote", [], - "stocks_helper.quote", + "stocks_views.display_quote", [], dict(), ),