Added multiselect option (#1232)

This commit is contained in:
Colin Delahunty
2022-01-21 16:09:53 -05:00
committed by GitHub
parent 3a8ce32a24
commit 721208fb45

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 13,
"id": "e9ad60bc",
"metadata": {},
"outputs": [],
@@ -16,11 +16,24 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 18,
"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",
"def clean_str(string):\n",
" new_str = \"\"\n",
" for letter in string:\n",
@@ -32,7 +45,8 @@
"def format_plotly(fig, x, y, ticker, expiry, inst):\n",
" fig.update_yaxes(title=clean_str(x))\n",
" fig.update_xaxes(title=clean_str(y))\n",
" title = f\"{clean_str(y)} vs. {clean_str(x)} for {ticker.upper()} {inst}s on {expiry}\"\n",
" expires = \", \".join(expiry)\n",
" title = f\"{clean_str(y)} vs. {clean_str(x)} for {ticker.upper()} {inst}s on {expires}\"\n",
" fig.update_layout(\n",
" autosize=False,\n",
" width=1000,\n",
@@ -49,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 19,
"id": "f8c2f65c",
"metadata": {
"tags": [
@@ -60,12 +74,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "295abc68725748ad95afc1ffdf7825de",
"model_id": "5906faf5dfa944a6bee1c6c4b84d8ebd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<h1>Option Chain Dashboard</h1>'), VBox(children=(Text(value='', description='Ticke…"
"VBox(children=(HTML(value='<h1>Option Chain Dashboard</h1>'), HBox(children=(VBox(children=(Text(value='', des…"
]
},
"metadata": {},
@@ -73,61 +87,56 @@
}
],
"source": [
"def view_chart(ticker, expiry, inst, x, y):\n",
" global last_ticker, last_exp\n",
" # set_interval_opts(interval_widget,start,end)\n",
" if ticker:\n",
" stock = yf.Ticker(ticker)\n",
" if ticker != last_ticker:\n",
" exps = stock.options\n",
" exps = [\"\"] + list(exps)\n",
" exp_w.options = exps\n",
" last_ticker = ticker\n",
"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",
" chains = stock.option_chain(expiry)\n",
" df = chains.calls if inst == \"Call\" else chains.puts\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",
" fig = go.Figure()\n",
" plot = go.Scatter(\n",
" x=df[x],\n",
" y=df[y],\n",
" mode=\"lines\",\n",
" )\n",
" fig.add_trace(plot)\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",
" 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",
" 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",
"last_ticker = \"\"\n",
"last_exp = \"\"\n",
"opts = [\n",
" \"lastTradeDate\",\n",
" \"strike\",\n",
" \"lastPrice\",\n",
" \"bid\",\n",
" \"ask\",\n",
" \"change\",\n",
" \"percentChange\",\n",
" \"volume\",\n",
" \"openInterest\",\n",
" \"impliedVolatility\",\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.Dropdown(description=\"Expiry\")\n",
"exp_w = widgets.SelectMultiple(description=\"Expiry\")\n",
"inst_w = widgets.Dropdown(options=[\"Put\", \"Call\"], value=\"Call\", description=\"Type\")\n",
"controls = widgets.VBox([tickers_w, exp_w, inst_w, x_w, y_w])\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",
" view_chart,\n",
" chart.update,\n",
" {\"ticker\": tickers_w, \"expiry\": exp_w, \"inst\": inst_w, \"x\": x_w, \"y\": y_w},\n",
")\n",
"\n",