Files
OpenBB/openbb_terminal/reports/templates/forecast.ipynb
James Maslek cfe41ef7a9 More sdk enhancements (#3480)
* Combine CA peers to smiliar command

* crypto/disc/top into one sdk function

* Exchanges into sdk function

* delete wrong platforms trail

* #3478 sdk crypto load

* Options hist 1 sdk function

* SDK functions

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
2022-11-21 21:58:32 +00:00

294 lines
7.7 KiB
Plaintext
Vendored

{
"cells": [
{
"cell_type": "markdown",
"id": "70ba2c8a",
"metadata": {},
"source": [
"## Notebook setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "driven-billion",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import io\n",
"import warnings\n",
"import datetime\n",
"import numpy as np\n",
"import base64\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib_inline.backend_inline\n",
"\n",
"from IPython.display import HTML\n",
"\n",
"from openbb_terminal.reports import widget_helpers as widgets\n",
"from openbb_terminal.sdk import openbb\n",
"from openbb_terminal.helper_classes import TerminalStyle\n",
"from openbb_terminal.core.config.paths import REPOSITORY_DIRECTORY\n",
"\n",
"%matplotlib inline\n",
"matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3fe7db5-ec6a-42cf-9e66-52dc1de22370",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" theme = TerminalStyle(\"light\", \"light\", \"light\")\n",
"except:\n",
" pass\n",
"stylesheet = widgets.html_report_stylesheet()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c3ae3e3-2b91-43a6-aa44-7b10986fd369",
"metadata": {},
"outputs": [],
"source": [
"def get_image(func, title, *args, **kwargs):\n",
" local_fig, ax = plt.subplots(dpi=20)\n",
" # kwargs[\"chart\"] = True\n",
" kwargs[\"external_axes\"] = [ax]\n",
" if title:\n",
" ax.set_title(title)\n",
" ax.set_xlabel(\"Lag\")\n",
" ax.set_ylabel(\"Autocorrelation\")\n",
" func(*args, **kwargs)\n",
" local_fig.tight_layout()\n",
" f = io.BytesIO()\n",
" local_fig.savefig(f, format=\"svg\")\n",
" return f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "markdown",
"id": "807fda4e",
"metadata": {},
"source": [
"## Select Ticker"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "intended-sherman",
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"# Parameters that will be replaced when calling this notebook\n",
"# Do not leave parameters blank as notebook will not run otherwise\n",
"symbol = \"VTI\"\n",
"report_name = \"Forecast Report for VTI\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1f87190",
"metadata": {},
"outputs": [],
"source": [
"ticker = symbol"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a441ddbc-d75b-4a18-84d4-e01b677674d3",
"metadata": {},
"outputs": [],
"source": [
"if \".\" in ticker:\n",
" import sys\n",
"\n",
" sys.exit(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1be26dae-cafe-4a22-80aa-eff296fc1a9b",
"metadata": {},
"outputs": [],
"source": [
"ticker_data = openbb.stocks.load(\n",
" symbol=ticker, start_date=datetime.datetime.now() - datetime.timedelta(days=400)\n",
")\n",
"ticker_data = ticker_data.reset_index()\n",
"ticker_data.columns = [x.lower() for x in ticker_data.columns]\n",
"\n",
"author = \"OpenBB\"\n",
"report_title = f\"FORECAST RESEARCH REPORT ON {ticker.upper()}\"\n",
"report_date = datetime.datetime.now().strftime(\"%d %B, %Y\")\n",
"report_time = datetime.datetime.now().strftime(\"%H:%M\")\n",
"report_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo\n",
"report_title, report_date, report_time, report_timezone"
]
},
{
"cell_type": "markdown",
"id": "f8a0d747",
"metadata": {},
"source": [
"## Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb31e9ee-e7ae-4e1b-89b4-bb6f571cfdfa",
"metadata": {},
"outputs": [],
"source": [
"local_fig, ax = plt.subplots(dpi=20)\n",
"column = \"close\"\n",
"ticker_data.plot(x=\"date\", y=column, ax=ax)\n",
"ax.set_title(f\"{column.title()} for {ticker}\")\n",
"ax.set_xlabel(\"Date\")\n",
"ax.set_ylabel(\"Price\")\n",
"local_fig.tight_layout()\n",
"f = io.BytesIO()\n",
"local_fig.savefig(f, format=\"svg\")\n",
"plot_chart = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8bb53c1-147e-4342-af61-0d0a9cc56e59",
"metadata": {},
"outputs": [],
"source": [
"corr_chart = get_image(openbb.forecast.corr_chart, None, ticker_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74b914a1-ed32-4a17-a943-7879bc8f00df",
"metadata": {},
"outputs": [],
"source": [
"season_chart = get_image(openbb.forecast.season_chart, \"Seasonality Data\", ticker_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53568c9b-aa06-4ba0-ad86-ede6732e3187",
"metadata": {},
"outputs": [],
"source": [
"expo_chart = get_image(openbb.forecast.expo_chart, None, ticker_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a919f67-ac9f-4b75-a3b3-102e0fbe5862",
"metadata": {},
"outputs": [],
"source": [
"rnn_chart = get_image(openbb.forecast.rnn_chart, None, ticker_data)"
]
},
{
"cell_type": "markdown",
"id": "ac501a32-124d-4d21-aae1-a8d147bb1071",
"metadata": {},
"source": [
"## Render the report template to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abfe37e4-ea38-42c3-8fb5-7cceb233fb12",
"metadata": {},
"outputs": [],
"source": [
"body = \"\"\n",
"\n",
"img = (\n",
" str(REPOSITORY_DIRECTORY)\n",
" + \"/openbb_terminal/reports/templates/OpenBB_reports_logo.png\"\n",
")\n",
"floppy_disk_img = (\n",
" str(REPOSITORY_DIRECTORY) + \"/openbb_terminal/reports/templates/floppy-disc.png\"\n",
")\n",
"body += widgets.header(\n",
" img,\n",
" floppy_disk_img,\n",
" author,\n",
" report_date,\n",
" report_time,\n",
" report_timezone,\n",
" f\"<b>FORECAST REPORT ON:</b> {ticker.upper()}\",\n",
")\n",
"\n",
"body += widgets.tablinks([\"SUMMARY\"])\n",
"\n",
"htmlcode = widgets.h(3, \"Summary\")\n",
"htmlcode += widgets.row([widgets.h(3, \"Understand the data\")])\n",
"htmlcode += widgets.row([ticker_data.head(10).to_html(), plot_chart])\n",
"htmlcode += widgets.row([widgets.h(3, \"Analyzing the data\")])\n",
"htmlcode += widgets.row([corr_chart, season_chart])\n",
"htmlcode += widgets.row([widgets.h(3, \"Creating predictions\")])\n",
"htmlcode += widgets.row([expo_chart, rnn_chart])\n",
"\n",
"body += widgets.add_tab(\"SUMMARY\", htmlcode)\n",
"body += widgets.tab_clickable_and_save_evt()\n",
"report = widgets.html_report(title=report_name, stylesheet=stylesheet, body=body)\n",
"\n",
"# to save the results\n",
"with open(report_name + \".html\", \"w\", encoding=\"utf-8\") as fh:\n",
" fh.write(report)"
]
}
],
"metadata": {
"celltoolbar": "Tags",
"kernelspec": {
"display_name": "Python 3.9.6 ('obb')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"vscode": {
"interpreter": {
"hash": "bd3088e2ce83c7406a082a5b650cfc74fd1480320510d276c9fa5780b96e5fae"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}