mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 13:32:08 +00:00
* bring dashboards and reports menus one step above * remove checkpoints * add notebooks again
178 lines
5.2 KiB
Plaintext
Vendored
178 lines
5.2 KiB
Plaintext
Vendored
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e9ad60bc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"import ipywidgets as widgets\n",
|
|
"import plotly.graph_objs as go\n",
|
|
"import yfinance as yf\n",
|
|
"from IPython.display import display"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "beb36881",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"opts = [\n",
|
|
" \"lastTradeDate\",\n",
|
|
" \"strike\",\n",
|
|
" \"lastPrice\",\n",
|
|
" \"bid\",\n",
|
|
" \"ask\",\n",
|
|
" \"change\",\n",
|
|
" \"percentChange\",\n",
|
|
" \"volume\",\n",
|
|
" \"openInterest\",\n",
|
|
" \"impliedVolatility\",\n",
|
|
"]\n",
|
|
"\n",
|
|
"\n",
|
|
"def clean_str(string):\n",
|
|
" new_str = \"\"\n",
|
|
" for letter in string:\n",
|
|
" if letter.isupper():\n",
|
|
" new_str += \" \"\n",
|
|
" new_str += letter\n",
|
|
" return new_str.title()\n",
|
|
"\n",
|
|
"\n",
|
|
"def format_plotly(fig, x, y, ticker, expiry, inst):\n",
|
|
" fig.update_yaxes(title=clean_str(y))\n",
|
|
" fig.update_xaxes(title=clean_str(x))\n",
|
|
" expires = \", \".join(expiry)\n",
|
|
" title = (\n",
|
|
" f\"{clean_str(y)} vs. {clean_str(x)} for {ticker.upper()} {inst}s on {expires}\"\n",
|
|
" )\n",
|
|
" fig.update_layout(\n",
|
|
" autosize=False,\n",
|
|
" width=1000,\n",
|
|
" height=500,\n",
|
|
" title={\n",
|
|
" \"text\": title,\n",
|
|
" \"y\": 0.95,\n",
|
|
" \"x\": 0.5,\n",
|
|
" \"xanchor\": \"center\",\n",
|
|
" \"yanchor\": \"top\",\n",
|
|
" },\n",
|
|
" )"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f8c2f65c",
|
|
"metadata": {
|
|
"tags": [
|
|
"parameters"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Chart:\n",
|
|
" def __init__(self):\n",
|
|
" self.last_ticker = \"\"\n",
|
|
" self.dfs = {}\n",
|
|
"\n",
|
|
" def update(self, ticker, expiry, inst, x, y):\n",
|
|
" if ticker:\n",
|
|
" stock = yf.Ticker(ticker)\n",
|
|
" if ticker != self.last_ticker:\n",
|
|
" exp_w.options = stock.options\n",
|
|
" self.last_ticker = ticker\n",
|
|
"\n",
|
|
" if expiry:\n",
|
|
" fig = go.Figure()\n",
|
|
" for expire in expiry:\n",
|
|
" if expire not in self.dfs:\n",
|
|
" self.dfs[expire] = stock.option_chain(expire)\n",
|
|
" group = self.dfs[expire]\n",
|
|
" df = group.calls if inst == \"Call\" else group.puts\n",
|
|
"\n",
|
|
" plot = go.Scatter(\n",
|
|
" x=df[x],\n",
|
|
" y=df[y],\n",
|
|
" mode=\"lines\",\n",
|
|
" connectgaps=True,\n",
|
|
" name=expire,\n",
|
|
" )\n",
|
|
" fig.add_trace(plot)\n",
|
|
"\n",
|
|
" format_plotly(fig, x, y, ticker, expiry, inst)\n",
|
|
"\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",
|
|
"x_w = widgets.Dropdown(options=opts, value=\"strike\", description=\"X\")\n",
|
|
"y_w = widgets.Dropdown(options=opts, value=\"lastPrice\", description=\"Y\")\n",
|
|
"tickers_w = widgets.Text(description=\"Ticker\")\n",
|
|
"exp_w = widgets.SelectMultiple(description=\"Expiry\")\n",
|
|
"inst_w = widgets.Dropdown(options=[\"Put\", \"Call\"], value=\"Call\", description=\"Type\")\n",
|
|
"\n",
|
|
"select_opt = widgets.VBox([tickers_w, exp_w])\n",
|
|
"select_plot = widgets.VBox([inst_w, x_w, y_w])\n",
|
|
"controls = widgets.HBox([select_opt, select_plot])\n",
|
|
"\n",
|
|
"chart = Chart()\n",
|
|
"stocks_view = widgets.interactive_output(\n",
|
|
" chart.update,\n",
|
|
" {\"ticker\": tickers_w, \"expiry\": exp_w, \"inst\": inst_w, \"x\": x_w, \"y\": y_w},\n",
|
|
")\n",
|
|
"\n",
|
|
"title_html = \"<h1>Option Chain 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": "a84c7390-6b43-4a25-8aa2-31aa84f52605",
|
|
"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.12"
|
|
},
|
|
"metadata": {
|
|
"interpreter": {
|
|
"hash": "e896a00ead8b521528d79ac9ef24990696f2b751eb283ab8e0d078c9c4971ffc"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|