Files
OpenBB/openbb_terminal/reports/templates/forecast.ipynb
jose-donato c5d5b511ac Adds crypto fees revenue command (#4067)
* fix: initial commit

* fix: extra stuff

* fix: lint

* fix: ruff

* fix: linting

* fix: ruff

* fix: pylint

* fix: maybe fixed tests maybe not

* fix: pr issues

* fix: tests

* fix: updated test locally

* fix: generate sdk cmds

* fix: added source

* fix: tests maybe

* fix: lint

* fix: tests

* test

* test

* test

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: DidierRLopes <dro.lopes@campus.fct.unl.pt>
2023-06-13 13:50:17 +00:00

320 lines
8.1 KiB
Plaintext
Vendored

{
"cells": [
{
"attachments": {},
"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",
"from IPython.display import HTML\n",
"\n",
"import openbb_terminal.config_terminal as cfg\n",
"from openbb_terminal.helper_classes import TerminalStyle\n",
"from openbb_terminal import OpenBBFigure, theme\n",
"\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "632bdfbc",
"metadata": {},
"outputs": [],
"source": [
"# Suppressing sdk logs\n",
"from openbb_terminal.core.session.current_system import set_system_variable\n",
"\n",
"set_system_variable(\"LOGGING_SUPPRESS\", True)\n",
"\n",
"# Import the OpenBB SDK\n",
"from openbb_terminal.sdk import openbb, widgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3fe7db5-ec6a-42cf-9e66-52dc1de22370",
"metadata": {},
"outputs": [],
"source": [
"from openbb_terminal.core.session.current_user import get_current_user\n",
"\n",
"user = get_current_user()\n",
"\n",
"cfg.theme = TerminalStyle(\n",
" user.preferences.MPL_STYLE,\n",
" user.preferences.PMF_STYLE,\n",
" user.preferences.RICH_STYLE,\n",
")\n",
"theme.apply_style(\"light\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6d22d4f",
"metadata": {},
"outputs": [],
"source": [
"stylesheet = widgets.html_report_stylesheet()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c3ae3e3-2b91-43a6-aa44-7b10986fd369",
"metadata": {},
"outputs": [],
"source": [
"def get_html(func, title, *args, legend=False, **kwargs):\n",
" kwargs[\"external_axes\"] = True\n",
" local_fig = func(*args, **kwargs)\n",
" # kwargs[\"chart\"] = True\n",
" if title:\n",
" local_fig.set_title(title)\n",
" local_fig.set_xaxis_title(\"Lag\")\n",
" local_fig.set_yaxis_title(\"Autocorrelation\")\n",
" if legend:\n",
" local_fig.update_layout(\n",
" legend=dict(\n",
" x=0.01,\n",
" y=0.99,\n",
" xanchor=\"left\",\n",
" yanchor=\"top\",\n",
" xref=\"paper\",\n",
" yref=\"paper\",\n",
" )\n",
" )\n",
" return local_fig.to_html()"
]
},
{
"attachments": {},
"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",
"\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"
]
},
{
"attachments": {},
"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 = openbb.stocks.candle(ticker, ticker_data, external_axes=True)\n",
"plot_chart = local_fig.to_html()\n",
"\n",
"# Prepare date for forecast\n",
"ticker_data = ticker_data.reset_index()\n",
"ticker_data.columns = [x.lower() for x in ticker_data.columns]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8bb53c1-147e-4342-af61-0d0a9cc56e59",
"metadata": {},
"outputs": [],
"source": [
"corr_chart = get_html(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_html(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_html(openbb.forecast.expo_chart, None, ticker_data, legend=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a919f67-ac9f-4b75-a3b3-102e0fbe5862",
"metadata": {},
"outputs": [],
"source": [
"rnn_chart = get_html(openbb.forecast.rnn_chart, None, ticker_data, legend=True)"
]
},
{
"attachments": {},
"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",
"\n",
"body += widgets.header(\n",
" author,\n",
" report_date,\n",
" report_time,\n",
" report_timezone,\n",
" f\"<b>FORECAST REPORT ON:</b> {ticker.upper()}\",\n",
" plotly_js=True,\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()])\n",
"htmlcode += widgets.row([plot_chart])\n",
"htmlcode += widgets.row([widgets.h(3, \"Analyzing the data\")])\n",
"htmlcode += widgets.row([corr_chart])\n",
"htmlcode += widgets.row([season_chart])\n",
"htmlcode += widgets.row([widgets.h(3, \"Creating predictions\")])\n",
"htmlcode += widgets.row([expo_chart])\n",
"htmlcode += widgets.row([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": "venv",
"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.10.8"
},
"vscode": {
"interpreter": {
"hash": "381a209f752e9104bff95ec5e80679ea2e476866b80e4326f1ee6b7658af6049"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}