From 335b89c8e2a4d36f09252a5c7f2e2320cb7c3556 Mon Sep 17 00:00:00 2001 From: Sam Kerr Date: Mon, 6 Jun 2022 10:15:09 -0400 Subject: [PATCH] Skip predictions in reports if prediction package is missing. (#1900) * Add error handling if prediction package is missing. * More error handlers * Use try/except blocks * Another error handler * Convert try/except blocks to a variable with if statements * Add string in final report about no predictions. Make file encoding explicit. * Add a comment describing the flag * Update openbb_terminal/reports/equity.ipynb Co-authored-by: James Maslek Co-authored-by: James Maslek --- openbb_terminal/reports/equity.ipynb | 75 +++++++++++++++++----------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/openbb_terminal/reports/equity.ipynb b/openbb_terminal/reports/equity.ipynb index 7b29bec5eed..9fc6e0dabef 100644 --- a/openbb_terminal/reports/equity.ipynb +++ b/openbb_terminal/reports/equity.ipynb @@ -33,7 +33,15 @@ "\n", "%matplotlib inline\n", "matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")\n", - "warnings.filterwarnings(\"ignore\")" + "warnings.filterwarnings(\"ignore\")\n", + "\n", + "# Detect if prediction capabilities are present. If they are not, disable prediction in the rest of the script\n", + "# so that the report can still be generated without prediction results.\n", + "predictions = True\n", + "try:\n", + " openbb.stocks.pred.models\n", + "except Exception as e:\n", + " predictions = False\n", ] }, { @@ -532,8 +540,9 @@ "metadata": {}, "outputs": [], "source": [ - "regression_val = round(openbb.stocks.pred.models.regression.get_regression_model(ticker_data_all[\"Close\"], 1, 80, 20, 1)[0][-1], 2)\n", - "regression_val" + "if predictions:\n", + " regression_val = round(openbb.stocks.pred.models.regression.get_regression_model(ticker_data_all[\"Close\"], 1, 80, 20, 1)[0][-1], 2)\n", + " regression_val" ] }, { @@ -543,12 +552,13 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n", - "openbb.stocks.pred.regression(ticker, ticker_data_all[\"Close\"], 1, 80, 20, 1, external_axes=[ax])\n", - "fig.tight_layout()\n", - "f = io.BytesIO()\n", - "fig.savefig(f, format=\"svg\")\n", - "regression = f.getvalue().decode(\"utf-8\")" + "if predictions:\n", + " fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n", + " openbb.stocks.pred.regression(ticker, ticker_data_all[\"Close\"], 1, 80, 20, 1, external_axes=[ax])\n", + " fig.tight_layout()\n", + " f = io.BytesIO()\n", + " fig.savefig(f, format=\"svg\")\n", + " regression = f.getvalue().decode(\"utf-8\")" ] }, { @@ -558,12 +568,13 @@ "metadata": {}, "outputs": [], "source": [ - "fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(11, 3), dpi=150)\n", - "openbb.stocks.pred.mc(ticker_data[\"Close\"], 30, 100, external_axes=[ax1, ax2])\n", - "fig.tight_layout()\n", - "f = io.BytesIO()\n", - "fig.savefig(f, format=\"svg\")\n", - "mc = f.getvalue().decode(\"utf-8\")" + "if predictions:\n", + " fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(11, 3), dpi=150)\n", + " openbb.stocks.pred.mc(ticker_data[\"Close\"], 30, 100, external_axes=[ax1, ax2])\n", + " fig.tight_layout()\n", + " f = io.BytesIO()\n", + " fig.savefig(f, format=\"svg\")\n", + " mc = f.getvalue().decode(\"utf-8\")" ] }, { @@ -573,12 +584,13 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n", - "openbb.stocks.pred.regression(ticker, ticker_data_all[\"Close\"], 1, 80, 20, 1, external_axes=[ax])\n", - "fig.tight_layout()\n", - "f = io.BytesIO()\n", - "fig.savefig(f, format=\"svg\")\n", - "regression = f.getvalue().decode(\"utf-8\")" + "if predictions:\n", + " fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n", + " openbb.stocks.pred.regression(ticker, ticker_data_all[\"Close\"], 1, 80, 20, 1, external_axes=[ax])\n", + " fig.tight_layout()\n", + " f = io.BytesIO()\n", + " fig.savefig(f, format=\"svg\")\n", + " regression = f.getvalue().decode(\"utf-8\")" ] }, { @@ -605,11 +617,13 @@ "\n", "htmlcode = openbb.widgets.h(3, \"KPIs\")\n", "htmlcode += openbb.widgets.kpi([last_price], [\"Last closing price is above the average price ratings of last 30 days\", \"Average price ratings of last 30 day is above last closing price\"], avg_ratings_last_30_days)\n", - "htmlcode += openbb.widgets.kpi([0], [\"Regression (dollars per market day) on last 30 market days is negative\", \"Regression (dollars per market day) on last 30 market days is positive\"], regression_slope)\n", + "if predictions:\n", + " htmlcode += openbb.widgets.kpi([0], [\"Regression (dollars per market day) on last 30 market days is negative\", \"Regression (dollars per market day) on last 30 market days is positive\"], regression_slope)\n", "htmlcode += openbb.widgets.kpi([30, 70], [\"RSI level is oversold\", \"RSI level is normal\", \"RSI level is overbought\"], rsi_value) \n", "htmlcode += openbb.widgets.kpi([0], [\"The sum of last 10 insider trading (in millions) was negative\", \"The sum of last 10 insider trading (in millions) was positive\"], last_10_insider_trading)\n", "htmlcode += openbb.widgets.kpi([-.1, .1], [\"Last FinBrain sentiment is bearish\", \" Last FinBrain sentiment is neutral\", \"Last FinBrain sentiment is bullish\"], finbrain_sentiment_val) \n", - "htmlcode += openbb.widgets.kpi([0], [\"The regression for the next 20th market price is below closing price\", \"The regression for the next 20th market price is above closing price\"], round(regression_val - last_price, 2)) \n", + "if predictions:\n", + " htmlcode += openbb.widgets.kpi([0], [\"The regression for the next 20th market price is below closing price\", \"The regression for the next 20th market price is above closing price\"], round(regression_val - last_price, 2)) \n", "body += openbb.widgets.add_tab(\"SUMMARY\", htmlcode)\n", "\n", "htmlcode = openbb.widgets.row([price_chart])\n", @@ -647,8 +661,11 @@ "htmlcode += openbb.widgets.row([bw_year])\n", "body += openbb.widgets.add_tab(\"Quantitative Analysis\", htmlcode)\n", "\n", - "htmlcode = openbb.widgets.row([regression])\n", - "htmlcode += openbb.widgets.row([mc])\n", + "if predictions:\n", + " htmlcode = openbb.widgets.row([regression])\n", + " htmlcode += openbb.widgets.row([mc])\n", + "else:\n", + " htmlcode = openbb.widgets.row([\"Prediction features not enabled.\"])\n", "body += openbb.widgets.add_tab(\"Prediction Techniques\", htmlcode)\n", "\n", "body += openbb.widgets.tab_clickable_evt()\n", @@ -656,7 +673,7 @@ "report = openbb.widgets.html_report(title=report_name, stylesheet=stylesheet, body=body)\n", "\n", "# to save the results\n", - "with open(report_name + \".html\", \"w\") as fh:\n", + "with open(report_name + \".html\", \"w\", encoding='utf-8') as fh:\n", " fh.write(report)" ] }, @@ -672,10 +689,10 @@ "metadata": { "celltoolbar": "Tags", "interpreter": { - "hash": "aa1c4d436101635411b2afa2ce061827695b6e3d0efa7f05338cbf11ad26165e" + "hash": "81794d4967e6c3204c66dcd87b604927b115b27c00565d3d43f05ba2f3a2cb0d" }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.1 64-bit", "language": "python", "name": "python3" }, @@ -689,7 +706,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.6" + "version": "3.9.1" } }, "nbformat": 4,