Files
OpenBB/openbb_terminal/dashboards/vsurf.ipynb
didierlopes.eth a7c148026f Re-add notebooks removed by #1805 (#1812)
* bring dashboards and reports menus one step above

* remove checkpoints

* add notebooks again
2022-05-12 22:47:04 +01:00

163 lines
5.2 KiB
Plaintext
Vendored

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "68b664c7-c52d-42c1-b170-575370323d22",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import warnings\n",
"import sys\n",
"import ipywidgets as widgets\n",
"import plotly.graph_objects as go\n",
"import numpy as np\n",
"import pandas as pd\n",
"from IPython.display import display\n",
"\n",
"warnings.filterwarnings(\"ignore\")\n",
"module_path = os.path.abspath(os.path.join(\"../../..\"))\n",
"\n",
"if module_path not in sys.path:\n",
" sys.path.append(module_path)\n",
"\n",
"from openbb_terminal import api as openbb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c98dc75-6325-43da-93a2-1b8ec96bd368",
"metadata": {},
"outputs": [],
"source": [
"z_dict = {\n",
" \"IV\": \"Implied Volatility\",\n",
" \"OI\": \"Open Interest\",\n",
" \"LP\": \"Last Price\",\n",
"}\n",
"strings = {\n",
" \"IV\": \"DTE: %{x} <br>Strike: %{y} <br>IV: %{z}<extra></extra>\",\n",
" \"OI\": \"DTE: %{x} <br>Strike: %{y} <br>OI: %{z}<extra></extra>\",\n",
" \"LP\": \"DTE: %{x} <br>Strike: %{y} <br>LP: %{z}<extra></extra>\",\n",
"}\n",
"\n",
"\n",
"def create_layout(fig, width, height, z, ticker):\n",
" fig.update_layout(\n",
" title=f\"{z.upper()} Surface for {ticker.upper()}\",\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" scene=dict(\n",
" xaxis_title=\"Days to Expiration\",\n",
" yaxis_title=\"Strike\",\n",
" zaxis_title=z_dict[z],\n",
" ),\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1bc5c6a-ccb2-4ef5-9c79-1f619fbf95a6",
"metadata": {},
"outputs": [],
"source": [
"class Chart:\n",
" def __init__(self):\n",
" self.last_ticker = \"\"\n",
" self.df = pd.DataFrame()\n",
"\n",
" def create(self, z, ticker, height, width):\n",
" if ticker:\n",
" if ticker != self.last_ticker:\n",
" self.df = openbb.stocks.options.models.yfinance.get_iv_surface(ticker)\n",
" self.last_ticker = ticker\n",
"\n",
" if not self.df.empty:\n",
" z_dict = {\n",
" \"IV\": self.df.impliedVolatility,\n",
" \"OI\": self.df.openInterest,\n",
" \"LP\": self.df.lastPrice,\n",
" }\n",
" fig = go.Figure(\n",
" data=[\n",
" go.Mesh3d(\n",
" z=z_dict[z],\n",
" x=self.df.dte,\n",
" y=self.df.strike,\n",
" intensity=np.sqrt(z_dict[z]),\n",
" colorscale=\"Jet\",\n",
" hovertemplate=strings[z],\n",
" showscale=False,\n",
" )\n",
" ]\n",
" )\n",
"\n",
" create_layout(fig, height, width, z, ticker)\n",
" if os.environ.get(\"SERVER_SOFTWARE\", \"jupyter\").startswith(\"voila\"):\n",
" fig.show(config={\"showTips\": False}, renderer=\"notebook\")\n",
" else:\n",
" fig.show(config={\"showTips\": False})\n",
"\n",
"\n",
"opts = [\"IV\", \"OI\", \"LP\"]\n",
"z_widget = widgets.Dropdown(options=opts, value=\"IV\", description=\"Data\")\n",
"tickers_widget = widgets.Text(description=\"Ticker\", value=\"TSLA\")\n",
"h_widget = widgets.IntText(value=1000, description=\"Height\")\n",
"w_widget = widgets.IntText(value=1000, description=\"Width\")\n",
"\n",
"controls = widgets.VBox([tickers_widget, z_widget])\n",
"size = widgets.VBox([h_widget, w_widget])\n",
"controls = widgets.HBox([controls, size])\n",
"\n",
"chart = Chart()\n",
"stocks_view = widgets.interactive_output(\n",
" chart.create,\n",
" {\"z\": z_widget, \"ticker\": tickers_widget, \"height\": h_widget, \"width\": w_widget},\n",
")\n",
"\n",
"title_html = \"<h1>Option Surface Dashboard</h1>\"\n",
"app_contents = [\n",
" widgets.HTML(title_html),\n",
" controls,\n",
" stocks_view,\n",
"]\n",
"app = widgets.VBox(app_contents)\n",
"display(app)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "014f267a-92ba-4f23-afb1-9720d8a68cbb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}