mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 13:01:38 +00:00
* bring dashboards and reports menus one step above * remove checkpoints * add notebooks again
223 lines
7.6 KiB
Plaintext
Vendored
223 lines
7.6 KiB
Plaintext
Vendored
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "e9ad60bc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"from datetime import datetime, timedelta\n",
|
|
"import ipywidgets as widgets\n",
|
|
"import plotly.graph_objs as go\n",
|
|
"import yfinance as yf\n",
|
|
"import pandas as pd\n",
|
|
"from IPython.display import display"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "6c554cd2-462c-4832-b73c-c214aae11ce4",
|
|
"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",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"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=[\"\"]\n",
|
|
" + [\".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",
|
|
"tickers_widget = widgets.Textarea(value=\"TSLA,AAPL,\", layout=w_auto)\n",
|
|
"\n",
|
|
"data_box = widgets.VBox([tickers_widget, data_widget])\n",
|
|
"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",
|
|
" chart.create,\n",
|
|
" {\n",
|
|
" \"data\": data_widget,\n",
|
|
" \"start\": start_widget,\n",
|
|
" \"end\": end_widget,\n",
|
|
" \"interval\": interval_widget,\n",
|
|
" \"tickers\": tickers_widget,\n",
|
|
" },\n",
|
|
")\n",
|
|
"\n",
|
|
"title_html = \"<h1>Correlation Analysis Dashboard</h1>\"\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",
|
|
" widgets.HTML(warning_html),\n",
|
|
" stocks_view,\n",
|
|
"]\n",
|
|
"app = widgets.VBox(app_contents)\n",
|
|
"display(app)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "37f46146-a206-4c25-b969-81c5abec4b1d",
|
|
"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
|
|
}
|