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

Fix empty equity report #3739

Merged
merged 8 commits into from
Dec 13, 2022
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
104 changes: 58 additions & 46 deletions openbb_terminal/reports/templates/equity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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'<a href=\"{x}\">{x}</a>')\n",
"mgmt_df[\"Insider Activity\"] = mgmt_df[\"Insider Activity\"].apply(\n",
" lambda x: f'<a href=\"{x}\">{x}</a>' if x != \"-\" else x\n",
")\n",
"if not mgmt_df.empty:\n",
" mgmt_df[\"Info\"] = mgmt_df[\"Info\"].apply(lambda x: f'<a href=\"{x}\">{x}</a>')\n",
" mgmt_df[\"Insider Activity\"] = mgmt_df[\"Insider Activity\"].apply(\n",
" lambda x: f'<a href=\"{x}\">{x}</a>' if x != \"-\" else x\n",
" )\n",
"try:\n",
" hist_dcf = openbb.stocks.fa.dcf(symbol)\n",
"except:\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -1365,14 +1375,16 @@
"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",
"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",
"body += widgets.add_tab(\"Quantitative Analysis\", htmlcode)\n",
Expand Down Expand Up @@ -1456,7 +1468,7 @@
},
"vscode": {
"interpreter": {
"hash": "cb66ba39c97f15743fbb79e204a841ea03600987d49579cfb0ffc8e8dd934c69"
"hash": "e784374cc4f9a84ed6af2983247aaf351b155c82bcc2188134a65f3c99031000"
}
}
},
Expand Down
46 changes: 25 additions & 21 deletions openbb_terminal/stocks/due_diligence/business_insider_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,31 @@ 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
3 changes: 2 additions & 1 deletion openbb_terminal/stocks/due_diligence/finviz_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def get_analyst_data(symbol: str) -> pd.DataFrame:
"""
d_finviz_analyst_price = get_analyst_price_targets_workaround(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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions openbb_terminal/stocks/insider/finviz_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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