Files
OpenBB/openbb_terminal/reports/templates/etf.ipynb
2022-11-22 10:03:26 +00:00

314 lines
7.9 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",
"# import sys\n",
"# sys.path.append('../../')\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\")\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",
"\n",
"# TODO Fix predictions virtual path on api refactored\n",
"\n",
"predictions = False"
]
},
{
"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": "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 = \"Equity 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=4 * 30)\n",
")\n",
"ticker_data = openbb.stocks.process_candle(data=ticker_data)\n",
"\n",
"author = \"OpenBB\"\n",
"report_title = f\"ETF 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": "code",
"execution_count": null,
"id": "2ecfcf81",
"metadata": {},
"outputs": [],
"source": [
"info = openbb.etf.summary(name=ticker)\n",
"info"
]
},
{
"cell_type": "markdown",
"id": "f8a0d747",
"metadata": {},
"source": [
"## Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39b8e77d",
"metadata": {},
"outputs": [],
"source": [
"data = openbb.stocks.load(ticker)\n",
"\n",
"fig, (candles, volume) = plt.subplots(nrows=2, ncols=1, figsize=(11, 5), dpi=150)\n",
"openbb.etf.candle(\n",
" symbol=ticker,\n",
" data=data,\n",
" use_matplotlib=True,\n",
" external_axes=[candles, volume],\n",
")\n",
"\n",
"candles.set_xticklabels(\"\")\n",
"fig.tight_layout()\n",
"\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"candle_chart = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f751f57c",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"weights = openbb.etf.weights(ticker)\n",
"weights_df = pd.DataFrame.from_dict([weights]).T\n",
"weights_df.columns = [\"Percentage\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "324e2d4b-b2f6-4da3-bac4-ef785ad4e436",
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(dpi=150)\n",
"ax.pie(\n",
" weights_df[weights_df[\"Percentage\"] > 10][\"Percentage\"],\n",
" labels=weights_df[weights_df[\"Percentage\"] > 10].index,\n",
" autopct=\"%1.1f%%\",\n",
" shadow=True,\n",
" startangle=90,\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"weights_chart = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8bb53c1-147e-4342-af61-0d0a9cc56e59",
"metadata": {},
"outputs": [],
"source": [
"holdings = openbb.etf.holdings(ticker)\n",
"holdings"
]
},
{
"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>ETF RESEARCH REPORT:</b> {ticker.upper()}\",\n",
")\n",
"\n",
"body += widgets.tablinks([\"SUMMARY\"])\n",
"\n",
"htmlcode = widgets.h(3, \"Summary\")\n",
"htmlcode += widgets.p(info)\n",
"htmlcode += widgets.row([candle_chart])\n",
"\n",
"htmlcode += widgets.p(\"The ETF has the following weights distribution:\")\n",
"\n",
"htmlcode += widgets.row([weights_df.to_html()])\n",
"\n",
"htmlcode += widgets.p(\"And has funds in the following assets:\")\n",
"htmlcode += widgets.row([holdings.to_html()])\n",
"\n",
"body += widgets.add_tab(\"SUMMARY\", htmlcode)\n",
"\n",
"body += widgets.tab_clickable_and_save_evt()\n",
"\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": "cb66ba39c97f15743fbb79e204a841ea03600987d49579cfb0ffc8e8dd934c69"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}