mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-27 11:41:32 +00:00
Refactored Jupyter lab to class based views (#1239)
Co-authored-by: jmaslek <jmaslek11@gmail.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 10,
|
||||
"id": "e9ad60bc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -13,112 +13,16 @@
|
||||
"import plotly.graph_objs as go\n",
|
||||
"import yfinance as yf\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"from IPython.display import display\n",
|
||||
"\n",
|
||||
"last_tickers = \"\"\n",
|
||||
"last_interval = \"id\""
|
||||
"from IPython.display import display"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "f8c2f65c",
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"parameters"
|
||||
]
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "5200fc1590894f5296bb577d9c58363d",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"VBox(children=(HTML(value='<h1>Correlation Analysis Dashboard</h1>'), HBox(children=(VBox(children=(Textarea(v…"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"execution_count": 11,
|
||||
"id": "6c554cd2-462c-4832-b73c-c214aae11ce4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def view_chart(data, start, end, interval, tickers):\n",
|
||||
" global last_tickers, df, last_interval\n",
|
||||
" # set_interval_opts(interval_widget,start,end)\n",
|
||||
" if tickers and tickers[-1] == \",\":\n",
|
||||
" if tickers != last_tickers or interval != last_interval:\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" df = yf.download(\n",
|
||||
" tickers, period=\"max\", interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" df = yf.download(\n",
|
||||
" tickers, start=start, end=end, interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" last_tickers = tickers\n",
|
||||
"\n",
|
||||
" start_n = datetime(start.year, start.month, start.day)\n",
|
||||
" end_n = datetime(end.year, end.month, end.day)\n",
|
||||
"\n",
|
||||
" df = df[data]\n",
|
||||
"\n",
|
||||
" if not isinstance(df, pd.Series):\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" result = df.loc[(df.index >= start_n) & (df.index <= end_n)].corr()\n",
|
||||
" else:\n",
|
||||
" result = df.corr()\n",
|
||||
"\n",
|
||||
" base = [\n",
|
||||
" [\n",
|
||||
" \"black\" if x == 1 else \"lightgreen\" if x > 0 else \"lightpink\"\n",
|
||||
" for x in result[y].tolist()\n",
|
||||
" ]\n",
|
||||
" for y in result.columns\n",
|
||||
" ]\n",
|
||||
" base = [[\"lightgray\" for _ in range(result.shape[0])]] + base\n",
|
||||
" result = result.reset_index()\n",
|
||||
" result.rename(columns={\"index\": \"\"}, inplace=True)\n",
|
||||
"\n",
|
||||
" fig = go.Figure(\n",
|
||||
" data=[\n",
|
||||
" go.Table(\n",
|
||||
" header=dict(\n",
|
||||
" values=list(result.columns),\n",
|
||||
" fill_color=\"lightgray\",\n",
|
||||
" font=dict(color=\"black\"),\n",
|
||||
" align=\"left\",\n",
|
||||
" ),\n",
|
||||
" cells=dict(\n",
|
||||
" values=[result[x] for x in result.columns],\n",
|
||||
" fill_color=base,\n",
|
||||
" format=[\"\"] + [\".2f\" for _ in range(len(df.columns) - 1)],\n",
|
||||
" font=dict(color=\"black\"),\n",
|
||||
" align=\"left\",\n",
|
||||
" ),\n",
|
||||
" )\n",
|
||||
" ]\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",
|
||||
"w_auto = widgets.Layout(width=\"auto\")\n",
|
||||
"data_opts = [\"Open\", \"Close\", \"High\", \"Low\", \"Volume\"]\n",
|
||||
"data_widget = widgets.Dropdown(\n",
|
||||
" options=data_opts, value=\"Close\", layout=w_auto, description=\"Data\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"base_date = (datetime.today() - timedelta(days=365)).date()\n",
|
||||
"start_widget = widgets.DatePicker(value=base_date, layout=w_auto, description=\"Start\")\n",
|
||||
"end_widget = widgets.DatePicker(\n",
|
||||
" value=datetime.today().date(), layout=w_auto, description=\"End\"\n",
|
||||
")\n",
|
||||
"interval_opts = [\n",
|
||||
" \"1m\",\n",
|
||||
" \"2m\",\n",
|
||||
@@ -133,7 +37,119 @@
|
||||
" \"1wk\",\n",
|
||||
" \"1mo\",\n",
|
||||
" \"3mo\",\n",
|
||||
"]\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "f8c2f65c",
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"parameters"
|
||||
]
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "b462032b043b47a8899f8366f43ab28e",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"VBox(children=(HTML(value='<h1>Correlation Analysis Dashboard</h1>'), HBox(children=(VBox(children=(Textarea(v…"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"class Chart:\n",
|
||||
" def __init__(self):\n",
|
||||
" self.last_tickers = \"\"\n",
|
||||
" self.last_interval = \"1d\"\n",
|
||||
" self.df = pd.DataFrame()\n",
|
||||
"\n",
|
||||
" def create(self, data, start, end, interval, tickers):\n",
|
||||
" global last_tickers, df, last_interval\n",
|
||||
" if tickers and tickers[-1] == \",\":\n",
|
||||
" if tickers != self.last_tickers or interval != self.last_interval:\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" self.df = yf.download(\n",
|
||||
" tickers, period=\"max\", interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" self.df = yf.download(\n",
|
||||
" tickers, start=start, end=end, interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" self.last_tickers = tickers\n",
|
||||
" self.last_interval = interval\n",
|
||||
"\n",
|
||||
" start_n = datetime(start.year, start.month, start.day)\n",
|
||||
" end_n = datetime(end.year, end.month, end.day)\n",
|
||||
"\n",
|
||||
" df = self.df[data]\n",
|
||||
"\n",
|
||||
" if not isinstance(df, pd.Series):\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" result = df.loc[(df.index >= start_n) & (df.index <= end_n)].corr()\n",
|
||||
" else:\n",
|
||||
" result = df.corr()\n",
|
||||
"\n",
|
||||
" base = [\n",
|
||||
" [\n",
|
||||
" \"black\" if x == 1 else \"lightgreen\" if x > 0 else \"lightpink\"\n",
|
||||
" for x in result[y].tolist()\n",
|
||||
" ]\n",
|
||||
" for y in result.columns\n",
|
||||
" ]\n",
|
||||
" base = [[\"lightgray\" for _ in range(result.shape[0])]] + base\n",
|
||||
" result = result.reset_index()\n",
|
||||
" result.rename(columns={\"index\": \"\"}, inplace=True)\n",
|
||||
"\n",
|
||||
" fig = go.Figure(\n",
|
||||
" data=[\n",
|
||||
" go.Table(\n",
|
||||
" header=dict(\n",
|
||||
" values=list(result.columns),\n",
|
||||
" fill_color=\"lightgray\",\n",
|
||||
" font=dict(color=\"black\"),\n",
|
||||
" align=\"left\",\n",
|
||||
" ),\n",
|
||||
" cells=dict(\n",
|
||||
" values=[result[x] for x in result.columns],\n",
|
||||
" fill_color=base,\n",
|
||||
" format=[\"\"] + [\".2f\" for _ in range(len(df.columns) - 1)],\n",
|
||||
" font=dict(color=\"black\"),\n",
|
||||
" align=\"left\",\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" fig.update_layout(\n",
|
||||
" autosize=True,\n",
|
||||
" height=600,\n",
|
||||
" showlegend=False,\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",
|
||||
"w_auto = widgets.Layout(width=\"auto\")\n",
|
||||
"data_opts = [\"Open\", \"Close\", \"High\", \"Low\", \"Volume\"]\n",
|
||||
"data_widget = widgets.Dropdown(\n",
|
||||
" options=data_opts, value=\"Close\", layout=w_auto, description=\"Data\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"base_date = (datetime.today() - timedelta(days=365)).date()\n",
|
||||
"start_widget = widgets.DatePicker(value=base_date, layout=w_auto, description=\"Start\")\n",
|
||||
"end_widget = widgets.DatePicker(\n",
|
||||
" value=datetime.today().date(), layout=w_auto, description=\"End\"\n",
|
||||
")\n",
|
||||
"interval_widget = widgets.Dropdown(\n",
|
||||
" options=interval_opts, value=\"1d\", layout=w_auto, description=\"Interval\"\n",
|
||||
")\n",
|
||||
@@ -143,8 +159,9 @@
|
||||
"date_box = widgets.VBox([start_widget, end_widget, interval_widget])\n",
|
||||
"controls = widgets.HBox([data_box, date_box], layout=widgets.Layout(width=\"60%\"))\n",
|
||||
"\n",
|
||||
"chart = Chart()\n",
|
||||
"stocks_view = widgets.interactive_output(\n",
|
||||
" view_chart,\n",
|
||||
" chart.create,\n",
|
||||
" {\n",
|
||||
" \"data\": data_widget,\n",
|
||||
" \"start\": start_widget,\n",
|
||||
@@ -155,9 +172,7 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"title_html = \"<h1>Correlation Analysis Dashboard</h1>\"\n",
|
||||
"warning_html = (\n",
|
||||
" '<p style=\"color:red\"=>You MUST use a comma after EVERY stock you type in.</p>'\n",
|
||||
")\n",
|
||||
"warning_html = ('<p style=\"color:red\">Use a comma after EVERY stock typed.</p>')\n",
|
||||
"app_contents = [\n",
|
||||
" widgets.HTML(title_html),\n",
|
||||
" controls,\n",
|
||||
@@ -171,7 +186,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "621ab2c1-be11-4f41-9bc6-121c7a357618",
|
||||
"id": "37f46146-a206-4c25-b969-81c5abec4b1d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
|
||||
146
gamestonk_terminal/jupyter/dashboards/stocks.ipynb
vendored
146
gamestonk_terminal/jupyter/dashboards/stocks.ipynb
vendored
@@ -23,6 +23,22 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"interval_opts = [\n",
|
||||
" \"1m\",\n",
|
||||
" \"2m\",\n",
|
||||
" \"5m\",\n",
|
||||
" \"15m\",\n",
|
||||
" \"30m\",\n",
|
||||
" \"60m\",\n",
|
||||
" \"90m\",\n",
|
||||
" \"1h\",\n",
|
||||
" \"1d\",\n",
|
||||
" \"5d\",\n",
|
||||
" \"1wk\",\n",
|
||||
" \"1mo\",\n",
|
||||
" \"3mo\",\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"views = {\n",
|
||||
" \"Raw Data\": lambda x, y: x,\n",
|
||||
" \"Percent Change\": lambda x, y: x.pct_change(),\n",
|
||||
@@ -55,11 +71,7 @@
|
||||
" \"xanchor\": \"center\",\n",
|
||||
" \"yanchor\": \"top\",\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"last_tickers = \"\"\n",
|
||||
"last_interval = \"id\""
|
||||
" )\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -73,60 +85,58 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def view_chart(calculation, data, rolling, start, end, interval, tickers):\n",
|
||||
" global last_tickers, df, last_interval\n",
|
||||
" # set_interval_opts(interval_widget,start,end)\n",
|
||||
" if tickers and tickers[-1] == \",\":\n",
|
||||
" if tickers != last_tickers or interval != last_interval:\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" df = yf.download(\n",
|
||||
" tickers, period=\"max\", interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" df = yf.download(\n",
|
||||
" tickers, start=start, end=end, interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" last_tickers = tickers\n",
|
||||
"class Chart:\n",
|
||||
" def __init__(self):\n",
|
||||
" self.last_tickers = \"\"\n",
|
||||
" self.last_interval = \"1d\"\n",
|
||||
" self.df = pd.DataFrame()\n",
|
||||
"\n",
|
||||
" start_n = datetime(start.year, start.month, start.day)\n",
|
||||
" end_n = datetime(end.year, end.month, end.day)\n",
|
||||
" fig = go.Figure()\n",
|
||||
" for item in calculation:\n",
|
||||
" calcs = views[item](df[data], rolling)\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" result = calcs.loc[(calcs.index >= start_n) & (calcs.index <= end_n)]\n",
|
||||
" else:\n",
|
||||
" result = calcs\n",
|
||||
"\n",
|
||||
" if isinstance(result, pd.Series):\n",
|
||||
" name = f\"{tickers.split(',')[0]} {item}\"\n",
|
||||
" plot = go.Scatter(x=result.index, y=result, mode=\"lines\", name=name)\n",
|
||||
" fig.add_trace(plot)\n",
|
||||
" else:\n",
|
||||
" for val in result:\n",
|
||||
" name = f\"{val.upper()} {item}\"\n",
|
||||
" plot = go.Scatter(\n",
|
||||
" x=result.index,\n",
|
||||
" y=result[val],\n",
|
||||
" mode=\"lines\",\n",
|
||||
" name=name,\n",
|
||||
" connectgaps=True,\n",
|
||||
" def create(self, calculation, data, rolling, start, end, interval, tickers):\n",
|
||||
" if tickers and tickers[-1] == \",\":\n",
|
||||
" if tickers != self.last_tickers or interval != self.last_interval:\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" self.df = yf.download(\n",
|
||||
" tickers, period=\"max\", interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" self.df = yf.download(\n",
|
||||
" tickers, start=start, end=end, interval=interval, progress=False\n",
|
||||
" )\n",
|
||||
" self.last_tickers = tickers\n",
|
||||
" self.last_interval = interval\n",
|
||||
"\n",
|
||||
" start_n = datetime(start.year, start.month, start.day)\n",
|
||||
" end_n = datetime(end.year, end.month, end.day)\n",
|
||||
" fig = go.Figure()\n",
|
||||
" for item in calculation:\n",
|
||||
" calcs = views[item](self.df[data], rolling)\n",
|
||||
" if interval in [\"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"]:\n",
|
||||
" result = calcs.loc[(calcs.index >= start_n) & (calcs.index <= end_n)]\n",
|
||||
" else:\n",
|
||||
" result = calcs\n",
|
||||
"\n",
|
||||
" if isinstance(result, pd.Series):\n",
|
||||
" name = f\"{tickers.split(',')[0]} {item}\"\n",
|
||||
" plot = go.Scatter(x=result.index, y=result, mode=\"lines\", name=name)\n",
|
||||
" fig.add_trace(plot)\n",
|
||||
" else:\n",
|
||||
" for val in result:\n",
|
||||
" name = f\"{val.upper()} {item}\"\n",
|
||||
" plot = go.Scatter(\n",
|
||||
" x=result.index,\n",
|
||||
" y=result[val],\n",
|
||||
" mode=\"lines\",\n",
|
||||
" name=name,\n",
|
||||
" connectgaps=True,\n",
|
||||
" )\n",
|
||||
" fig.add_trace(plot)\n",
|
||||
"\n",
|
||||
" format_plotly(fig, calculation, data, start, end)\n",
|
||||
" format_plotly(fig, calculation, data, start, end)\n",
|
||||
"\n",
|
||||
" \"\"\"fig.update_layout(legend=dict(\n",
|
||||
" yanchor=\"top\",\n",
|
||||
" y=0.99,\n",
|
||||
" xanchor=\"left\",\n",
|
||||
" x=0.01\n",
|
||||
" ))\"\"\"\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",
|
||||
"w_auto = widgets.Layout(width=\"auto\")\n",
|
||||
@@ -147,21 +157,7 @@
|
||||
"end_widget = widgets.DatePicker(\n",
|
||||
" value=datetime.today().date(), layout=w_auto, description=\"End\"\n",
|
||||
")\n",
|
||||
"interval_opts = [\n",
|
||||
" \"1m\",\n",
|
||||
" \"2m\",\n",
|
||||
" \"5m\",\n",
|
||||
" \"15m\",\n",
|
||||
" \"30m\",\n",
|
||||
" \"60m\",\n",
|
||||
" \"90m\",\n",
|
||||
" \"1h\",\n",
|
||||
" \"1d\",\n",
|
||||
" \"5d\",\n",
|
||||
" \"1wk\",\n",
|
||||
" \"1mo\",\n",
|
||||
" \"3mo\",\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"interval_widget = widgets.Dropdown(\n",
|
||||
" options=interval_opts, value=\"1d\", layout=w_auto, description=\"Interval\"\n",
|
||||
")\n",
|
||||
@@ -175,8 +171,9 @@
|
||||
" layout=widgets.Layout(width=\"90%\"),\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"chart = Chart()\n",
|
||||
"stocks_view = widgets.interactive_output(\n",
|
||||
" view_chart,\n",
|
||||
" chart.create,\n",
|
||||
" {\n",
|
||||
" \"calculation\": calc_widget,\n",
|
||||
" \"data\": data_widget,\n",
|
||||
@@ -189,9 +186,8 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"title_html = \"<h1>Stock Analysis Dashboard</h1>\"\n",
|
||||
"warning_html = (\n",
|
||||
" '<p style=\"color:red\"=>You MUST use a comma after EVERY stock you type in.</p>'\n",
|
||||
")\n",
|
||||
"warning_html = ('<p style=\"color:red\"=>Use a comma after EVERY stock typed.</p>')\n",
|
||||
"\n",
|
||||
"app_contents = [\n",
|
||||
" widgets.HTML(title_html),\n",
|
||||
" controls,\n",
|
||||
@@ -213,9 +209,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": "",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
"name": ""
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
|
||||
113
gamestonk_terminal/jupyter/dashboards/vsurf.ipynb
vendored
113
gamestonk_terminal/jupyter/dashboards/vsurf.ipynb
vendored
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"id": "68b664c7-c52d-42c1-b170-575370323d22",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -14,6 +14,7 @@
|
||||
"import plotly.graph_objs as go\n",
|
||||
"import plotly.express as px\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"from IPython.display import display\n",
|
||||
"from gamestonk_terminal import api as gst\n",
|
||||
"\n",
|
||||
@@ -26,25 +27,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "c1bc5c6a-ccb2-4ef5-9c79-1f619fbf95a6",
|
||||
"execution_count": null,
|
||||
"id": "6c98dc75-6325-43da-93a2-1b8ec96bd368",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "1a98edffff1b42c18732e8659f425579",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"VBox(children=(HTML(value='<h1>Option Surface Dashboard</h1>'), HBox(children=(VBox(children=(Text(value='TSLA…"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"z_dict = {\n",
|
||||
" \"IV\": \"Implied Volatility\",\n",
|
||||
@@ -68,44 +54,54 @@
|
||||
" yaxis_title=\"Strike\",\n",
|
||||
" zaxis_title=z_dict[z],\n",
|
||||
" ),\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 = gst.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=px.colors.sequential.Plasma,\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",
|
||||
"def view_chart(z, ticker, height, width):\n",
|
||||
" global last_ticker, df\n",
|
||||
" if ticker:\n",
|
||||
" if ticker != last_ticker:\n",
|
||||
" df = gst.stocks.options.models.yfinance.get_iv_surface(ticker)\n",
|
||||
" last_ticker = ticker\n",
|
||||
"\n",
|
||||
" if not df.empty:\n",
|
||||
" z_dict = {\n",
|
||||
" \"IV\": df.impliedVolatility,\n",
|
||||
" \"OI\": df.openInterest,\n",
|
||||
" \"LP\": df.lastPrice,\n",
|
||||
" }\n",
|
||||
" fig = go.Figure(\n",
|
||||
" data=[\n",
|
||||
" go.Mesh3d(\n",
|
||||
" z=z_dict[z],\n",
|
||||
" x=df.dte,\n",
|
||||
" y=df.strike,\n",
|
||||
" intensity=np.sqrt(z_dict[z]),\n",
|
||||
" colorscale=px.colors.sequential.Plasma,\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",
|
||||
"last_ticker = \"\"\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",
|
||||
@@ -116,8 +112,9 @@
|
||||
"size = widgets.VBox([h_widget, w_widget])\n",
|
||||
"controls = widgets.HBox([controls,size])\n",
|
||||
"\n",
|
||||
"chart = Chart()\n",
|
||||
"stocks_view = widgets.interactive_output(\n",
|
||||
" view_chart,\n",
|
||||
" chart.create,\n",
|
||||
" {\n",
|
||||
" \"z\": z_widget,\n",
|
||||
" \"ticker\": tickers_widget,\n",
|
||||
@@ -147,9 +144,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": "",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
"name": ""
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
|
||||
Reference in New Issue
Block a user