From e1dc01add739ac66bf145bab660797d9739950eb Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 9 Dec 2022 11:47:24 +0000 Subject: [PATCH 1/5] handle empty dfs --- openbb_terminal/stocks/due_diligence/finviz_model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openbb_terminal/stocks/due_diligence/finviz_model.py b/openbb_terminal/stocks/due_diligence/finviz_model.py index 9ef4f4be0210..0429c93426f8 100644 --- a/openbb_terminal/stocks/due_diligence/finviz_model.py +++ b/openbb_terminal/stocks/due_diligence/finviz_model.py @@ -45,6 +45,7 @@ def get_analyst_data(symbol: str) -> pd.DataFrame: """ d_finviz_analyst_price = finviz.get_analyst_price_targets(symbol) df_fa = pd.DataFrame.from_dict(d_finviz_analyst_price) - df_fa.set_index("date", inplace=True) + if not df_fa.empty: + df_fa.set_index("date", inplace=True) return df_fa From a2c302d5faf9ad0aefe12b96ebccde3113c4ba0e Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 9 Dec 2022 11:47:33 +0000 Subject: [PATCH 2/5] handle empty dfs --- .../reports/templates/equity.ipynb | 77 +++++++++++-------- .../due_diligence/business_insider_model.py | 44 ++++++----- .../yahoo_finance_model.py | 1 + .../stocks/insider/finviz_model.py | 27 +++---- 4 files changed, 82 insertions(+), 67 deletions(-) diff --git a/openbb_terminal/reports/templates/equity.ipynb b/openbb_terminal/reports/templates/equity.ipynb index f3a21c511440..dad8ade79143 100644 --- a/openbb_terminal/reports/templates/equity.ipynb +++ b/openbb_terminal/reports/templates/equity.ipynb @@ -803,7 +803,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 123, "id": "6272e34b-9511-4b48-9eea-ea882ce9846a", "metadata": {}, "outputs": [], @@ -811,16 +811,17 @@ "import pandas as pd\n", "\n", "df_insider = pd.DataFrame.from_dict(openbb.stocks.ins.lins(symbol=symbol)).head(10)\n", - "df_insider[\"Val ($)\"] = df_insider[\"Value ($)\"].replace({\",\": \"\"}, regex=True)\n", - "df_insider[\"Trade\"] = df_insider.apply(\n", - " lambda row: (-1 * float(row[\"Val ($)\"]))\n", - " if row[\"Transaction\"] == \"Sale\"\n", - " else (float(row[\"Val ($)\"]) if row[\"Transaction\"] == \"Buy\" else 0),\n", - " axis=1,\n", - ")\n", - "last_10_insider_trading = round(sum(df_insider[\"Trade\"]) / 1_000_000, 2)\n", - "df_insider = df_insider.drop(columns=[\"Val ($)\", \"Trade\"])\n", - "df_insider" + "if not df_insider.empty:\n", + " df_insider[\"Val ($)\"] = df_insider[\"Value ($)\"].replace({\",\": \"\"}, regex=True)\n", + " df_insider[\"Trade\"] = df_insider.apply(\n", + " lambda row: (-1 * float(row[\"Val ($)\"]))\n", + " if row[\"Transaction\"] == \"Sale\"\n", + " else (float(row[\"Val ($)\"]) if row[\"Transaction\"] == \"Buy\" else 0),\n", + " axis=1,\n", + " )\n", + " last_10_insider_trading = round(sum(df_insider[\"Trade\"]) / 1_000_000, 2)\n", + " df_insider = df_insider.drop(columns=[\"Val ($)\", \"Trade\"])\n", + " df_insider" ] }, { @@ -953,10 +954,11 @@ "income_df = openbb.stocks.fa.income(symbol, source=\"YahooFinance\")\n", "data_df = openbb.stocks.fa.data(symbol)\n", "mgmt_df = openbb.stocks.fa.mgmt(symbol)\n", - "mgmt_df[\"Info\"] = mgmt_df[\"Info\"].apply(lambda x: f'{x}')\n", - "mgmt_df[\"Insider Activity\"] = mgmt_df[\"Insider Activity\"].apply(\n", - " lambda x: f'{x}' if x != \"-\" else x\n", - ")\n", + "if not mgmt_df.empty:\n", + " mgmt_df[\"Info\"] = mgmt_df[\"Info\"].apply(lambda x: f'{x}')\n", + " mgmt_df[\"Insider Activity\"] = mgmt_df[\"Insider Activity\"].apply(\n", + " lambda x: f'{x}' if x != \"-\" else x\n", + " )\n", "try:\n", " hist_dcf = openbb.stocks.fa.dcf(symbol)\n", "except:\n", @@ -1152,14 +1154,17 @@ " [\"RSI level is oversold\", \"RSI level is normal\", \"RSI level is overbought\"],\n", " rsi_value,\n", ")\n", - "htmlcode += widgets.kpi(\n", - " [0],\n", - " [\n", - " \"The sum of last 10 insider trading (in millions) was negative\",\n", - " \"The sum of last 10 insider trading (in millions) was positive\",\n", - " ],\n", - " last_10_insider_trading,\n", - ")\n", + "try:\n", + " htmlcode += widgets.kpi(\n", + " [0],\n", + " [\n", + " \"The sum of last 10 insider trading (in millions) was negative\",\n", + " \"The sum of last 10 insider trading (in millions) was positive\",\n", + " ],\n", + " last_10_insider_trading,\n", + " )\n", + "except:\n", + " pass\n", "htmlcode += widgets.kpi(\n", " [-0.1, 0.1],\n", " [\n", @@ -1201,9 +1206,11 @@ " )\n", "except:\n", " pass\n", - "htmlcode += widgets.row(\n", - " [widgets.h(3, f\"Management team of {symbol}\") + mgmt_df.to_html(escape=False)]\n", - ")\n", + "\n", + "if not mgmt_df.empty:\n", + " htmlcode += widgets.row(\n", + " [widgets.h(3, f\"Management team of {symbol}\") + mgmt_df.to_html(escape=False)]\n", + " )\n", "body += widgets.add_tab(\"Overview\", htmlcode)\n", "\n", "htmlcode = widgets.row([widgets.h(3, \"Price Target Chart\") + price_target_chart])\n", @@ -1229,12 +1236,15 @@ " )\n", "except:\n", " pass\n", - "htmlcode += widgets.row(\n", - " [widgets.h(3, \"Earnings\") + df_quarter_earnings.head().to_html()]\n", - ")\n", - "htmlcode += widgets.row(\n", - " [widgets.h(3, \"Revenues\") + df_quarter_revenues.head().to_html()]\n", - ")\n", + "\n", + "if not df_quarter_earnings.empty:\n", + " htmlcode += widgets.row(\n", + " [widgets.h(3, \"Earnings\") + df_quarter_earnings.head().to_html()]\n", + " )\n", + "if not df_quarter_revenues.empty:\n", + " htmlcode += widgets.row(\n", + " [widgets.h(3, \"Revenues\") + df_quarter_revenues.head().to_html()]\n", + " )\n", "try:\n", " htmlcode += widgets.row(\n", " [\n", @@ -1369,9 +1379,10 @@ " htmlcode = widgets.row(\n", " [widgets.h(3, \"Last Activity\") + df_insider.head(10).to_html(col_space=\"75px\")]\n", " )\n", + " if htmlcode:\n", + " body += widgets.add_tab(\"Insider Trading\", htmlcode)\n", "except:\n", " pass\n", - "body += widgets.add_tab(\"Insider Trading\", htmlcode)\n", "\n", "htmlcode = widgets.row([bw_year])\n", "htmlcode += widgets.row([bw_month])\n", diff --git a/openbb_terminal/stocks/due_diligence/business_insider_model.py b/openbb_terminal/stocks/due_diligence/business_insider_model.py index 2c49584aef52..f7591385ab2e 100644 --- a/openbb_terminal/stocks/due_diligence/business_insider_model.py +++ b/openbb_terminal/stocks/due_diligence/business_insider_model.py @@ -194,27 +194,29 @@ def get_estimates(symbol: str) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame ) # df_quarter_revenues.index.name = 'Revenues' - l_quarter = list() - l_date = list() - for quarter_title in df_quarter_earnings.columns: - l_quarter.append(re.split(" ending", quarter_title)[0]) - if len(re.split(" ending", quarter_title)) == 2: - l_date.append("ending " + re.split(" ending", quarter_title)[1].strip()) - else: - l_date.append("-") - - df_quarter_earnings.index.name = "QUARTER EARNINGS ESTIMATES" - df_quarter_earnings.columns = l_quarter - df_quarter_earnings.loc["Date"] = l_date - df_quarter_earnings = df_quarter_earnings.reindex( - ["Date", "No. of Analysts", "Average Estimate", "Year Ago", "Publish Date"] - ) + if not df_quarter_earnings.empty: + l_quarter = list() + l_date = list() + for quarter_title in df_quarter_earnings.columns: + l_quarter.append(re.split(" ending", quarter_title)[0]) + if len(re.split(" ending", quarter_title)) == 2: + l_date.append("ending " + re.split(" ending", quarter_title)[1].strip()) + else: + l_date.append("-") + + df_quarter_earnings.index.name = "QUARTER EARNINGS ESTIMATES" + df_quarter_earnings.columns = l_quarter + df_quarter_earnings.loc["Date"] = l_date + df_quarter_earnings = df_quarter_earnings.reindex( + ["Date", "No. of Analysts", "Average Estimate", "Year Ago", "Publish Date"] + ) - df_quarter_revenues.index.name = "QUARTER REVENUES ESTIMATES" - df_quarter_revenues.columns = l_quarter - df_quarter_revenues.loc["Date"] = l_date - df_quarter_revenues = df_quarter_revenues.reindex( - ["Date", "No. of Analysts", "Average Estimate", "Year Ago", "Publish Date"] - ) + if not df_quarter_revenues.empty: + df_quarter_revenues.index.name = "QUARTER REVENUES ESTIMATES" + df_quarter_revenues.columns = l_quarter + df_quarter_revenues.loc["Date"] = l_date + df_quarter_revenues = df_quarter_revenues.reindex( + ["Date", "No. of Analysts", "Average Estimate", "Year Ago", "Publish Date"] + ) return df_year_estimates, df_quarter_earnings, df_quarter_revenues diff --git a/openbb_terminal/stocks/fundamental_analysis/yahoo_finance_model.py b/openbb_terminal/stocks/fundamental_analysis/yahoo_finance_model.py index a45e61a7833b..47067ec475e8 100644 --- a/openbb_terminal/stocks/fundamental_analysis/yahoo_finance_model.py +++ b/openbb_terminal/stocks/fundamental_analysis/yahoo_finance_model.py @@ -436,6 +436,7 @@ def get_financials(symbol: str, statement: str, ratios: bool = False) -> pd.Data df.replace("-", np.nan, inplace=True) df = df.dropna(how="all") df = df.replace(",", "", regex=True) + df = df.replace("k", "", regex=True) df = df.astype("float") # Data except EPS is returned in thousands, convert it diff --git a/openbb_terminal/stocks/insider/finviz_model.py b/openbb_terminal/stocks/insider/finviz_model.py index 934b7dc7e386..ee263e624fe9 100644 --- a/openbb_terminal/stocks/insider/finviz_model.py +++ b/openbb_terminal/stocks/insider/finviz_model.py @@ -28,21 +28,22 @@ def get_last_insider_activity(symbol: str) -> pd.DataFrame: """ activity_dict = finviz.get_insider(symbol) df = pd.DataFrame.from_dict(activity_dict) - df.set_index("Date", inplace=True) - df = df[ - [ - "Relationship", - "Transaction", - "#Shares", - "Cost", - "Value ($)", - "#Shares Total", - "Insider Trading", - "SEC Form 4", - ] - ] if df.empty: console.print(f"[red]No insider information found for {symbol}.\n[/red]") + else: + df.set_index("Date", inplace=True) + df = df[ + [ + "Relationship", + "Transaction", + "#Shares", + "Cost", + "Value ($)", + "#Shares Total", + "Insider Trading", + "SEC Form 4", + ] + ] return df From 6039e4f681e723376807e7572a95988c52437f1e Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 9 Dec 2022 11:52:37 +0000 Subject: [PATCH 3/5] black --- .../stocks/due_diligence/business_insider_model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openbb_terminal/stocks/due_diligence/business_insider_model.py b/openbb_terminal/stocks/due_diligence/business_insider_model.py index f7591385ab2e..f6a6d42ac814 100644 --- a/openbb_terminal/stocks/due_diligence/business_insider_model.py +++ b/openbb_terminal/stocks/due_diligence/business_insider_model.py @@ -200,7 +200,9 @@ def get_estimates(symbol: str) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame for quarter_title in df_quarter_earnings.columns: l_quarter.append(re.split(" ending", quarter_title)[0]) if len(re.split(" ending", quarter_title)) == 2: - l_date.append("ending " + re.split(" ending", quarter_title)[1].strip()) + l_date.append( + "ending " + re.split(" ending", quarter_title)[1].strip() + ) else: l_date.append("-") From 3fd1127703681e43b91ec0721edf2532addadcf7 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Mon, 12 Dec 2022 11:02:50 +0000 Subject: [PATCH 4/5] fix equity report --- .../reports/templates/equity.ipynb | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/openbb_terminal/reports/templates/equity.ipynb b/openbb_terminal/reports/templates/equity.ipynb index dad8ade79143..091e6972b06a 100644 --- a/openbb_terminal/reports/templates/equity.ipynb +++ b/openbb_terminal/reports/templates/equity.ipynb @@ -87,7 +87,7 @@ "source": [ "# Parameters that will be replaced when calling this notebook\n", "# Do not leave parameters blank as notebook will not run otherwise\n", - "symbol = \"msft\"\n", + "symbol = \"TM\"\n", "report_name = f\"Equity report for {symbol}\"" ] }, @@ -257,23 +257,23 @@ "outputs": [], "source": [ "df_analyst = openbb.stocks.dd.analyst(symbol=symbol)\n", - "print(df_analyst.columns)\n", + "\n", "if not df_analyst.empty:\n", " if \"target\" in df_analyst.columns:\n", " if \"target_to\" in df_analyst.columns:\n", " df_analyst[\"target\"] = df_analyst[\"target_to\"].combine_first(\n", " df_analyst[\"target\"]\n", " )\n", - " else:\n", - " df_analyst[\"target\"] = df_analyst[\"target_to\"]\n", - " df_analyst = df_analyst[[\"category\", \"analyst\", \"rating\", \"target\"]].rename(\n", - " columns={\n", - " \"category\": \"Category\",\n", - " \"analyst\": \"Analyst\",\n", - " \"rating\": \"Rating\",\n", - " \"target\": \"Price Target\",\n", - " }\n", - " )\n", + " else:\n", + " df_analyst[\"target\"] = df_analyst[\"target_to\"]\n", + " df_analyst = df_analyst[[\"category\", \"analyst\", \"rating\", \"target\"]].rename(\n", + " columns={\n", + " \"category\": \"Category\",\n", + " \"analyst\": \"Analyst\",\n", + " \"rating\": \"Rating\",\n", + " \"target\": \"Price Target\",\n", + " }\n", + " )\n", "df_analyst" ] }, @@ -803,7 +803,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": null, "id": "6272e34b-9511-4b48-9eea-ea882ce9846a", "metadata": {}, "outputs": [], @@ -1375,14 +1375,15 @@ "htmlcode += widgets.row([adx_chart])\n", "body += widgets.add_tab(\"Technical Analysis\", htmlcode)\n", "\n", - "try:\n", + "if not df_insider.empty:\n", " htmlcode = widgets.row(\n", " [widgets.h(3, \"Last Activity\") + df_insider.head(10).to_html(col_space=\"75px\")]\n", " )\n", - " if htmlcode:\n", - " body += widgets.add_tab(\"Insider Trading\", htmlcode)\n", - "except:\n", - " pass\n", + "else:\n", + " htmlcode = widgets.row([widgets.h(3, \"Last Activity\") + widgets.p(\"No insider trading data found.\")])\n", + "\n", + "body += widgets.add_tab(\"Insider Trading\", htmlcode)\n", + "\n", "\n", "htmlcode = widgets.row([bw_year])\n", "htmlcode += widgets.row([bw_month])\n", @@ -1467,7 +1468,7 @@ }, "vscode": { "interpreter": { - "hash": "cb66ba39c97f15743fbb79e204a841ea03600987d49579cfb0ffc8e8dd934c69" + "hash": "e784374cc4f9a84ed6af2983247aaf351b155c82bcc2188134a65f3c99031000" } } }, From c0fc16c3e16cb2c08ac2f372d82cbd2a81e1e24e Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Mon, 12 Dec 2022 11:05:23 +0000 Subject: [PATCH 5/5] msft --- openbb_terminal/reports/templates/equity.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_terminal/reports/templates/equity.ipynb b/openbb_terminal/reports/templates/equity.ipynb index 091e6972b06a..c3ff7630c8e3 100644 --- a/openbb_terminal/reports/templates/equity.ipynb +++ b/openbb_terminal/reports/templates/equity.ipynb @@ -87,7 +87,7 @@ "source": [ "# Parameters that will be replaced when calling this notebook\n", "# Do not leave parameters blank as notebook will not run otherwise\n", - "symbol = \"TM\"\n", + "symbol = \"msft\"\n", "report_name = f\"Equity report for {symbol}\"" ] },