2021-06-20 18:59:35 +02:00
|
|
|
import dash
|
|
|
|
import dash_html_components as html
|
|
|
|
import dash_core_components as dcc
|
|
|
|
import dash_table
|
|
|
|
import plotly.express as px
|
|
|
|
import yfinance as yf
|
2021-06-21 15:42:00 +02:00
|
|
|
from dash.dependencies import Input, Output, State
|
2021-06-20 22:02:32 +02:00
|
|
|
import plotly.graph_objects as go
|
2021-06-21 11:37:44 +02:00
|
|
|
import numpy as np
|
2021-06-20 22:02:32 +02:00
|
|
|
import pandas as pd
|
2021-06-21 11:37:44 +02:00
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
2021-06-20 18:59:35 +02:00
|
|
|
# Load data
|
|
|
|
stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD', 'AAP', 'AES', 'AFL', 'A', 'APD', 'AKAM',
|
|
|
|
'ALK', 'ALB', 'ARE', 'ALXN', 'ALGN', 'ALLE', 'LNT', 'ALL', 'GOOGL', 'GOOG', 'MO', 'AMZN', 'AMCR', 'AEE',
|
|
|
|
'AAL', 'AEP', 'AXP', 'AIG', 'AMT', 'AWK', 'AMP', 'ABC', 'AME', 'AMGN', 'APH', 'ADI', 'ANSS', 'ANTM',
|
|
|
|
'AON', 'AOS', 'APA', 'AAPL', 'AMAT', 'APTV', 'ADM', 'ANET', 'AJG', 'AIZ', 'T', 'ATO', 'ADSK', 'ADP',
|
|
|
|
'AZO', 'AVB', 'AVY', 'BKR', 'BLL', 'BAC', 'BK', 'BAX', 'BDX', 'BRK.B', 'BBY', 'BIO', 'BIIB', 'BLK', 'BA',
|
|
|
|
'BKNG', 'BWA', 'BXP', 'BSX', 'BMY', 'AVGO', 'BR', 'BF.B', 'CHRW', 'COG', 'CDNS', 'CZR', 'CPB', 'COF',
|
|
|
|
'CAH', 'KMX', 'CCL', 'CARR', 'CTLT', 'CAT', 'CBOE', 'CBRE', 'CDW', 'CE', 'CNC', 'CNP', 'CERN', 'CF',
|
|
|
|
'CRL', 'SCHW', 'CHTR', 'CVX', 'CMG', 'CB', 'CHD', 'CI', 'CINF', 'CTAS', 'CSCO', 'C', 'CFG', 'CTXS', 'CLX',
|
|
|
|
'CME', 'CMS', 'KO', 'CTSH', 'CL', 'CMCSA', 'CMA', 'CAG', 'COP', 'ED', 'STZ', 'COO', 'CPRT', 'GLW', 'CTVA',
|
|
|
|
'COST', 'CCI', 'CSX', 'CMI', 'CVS', 'DHI', 'DHR', 'DRI', 'DVA', 'DE', 'DAL', 'XRAY', 'DVN', 'DXCM',
|
|
|
|
'FANG', 'DLR', 'DFS', 'DISCA', 'DISCK', 'DISH', 'DG', 'DLTR', 'D', 'DPZ', 'DOV', 'DOW', 'DTE', 'DUK',
|
|
|
|
'DRE', 'DD', 'DXC', 'EMN', 'ETN', 'EBAY', 'ECL', 'EIX', 'EW', 'EA', 'EMR', 'ENPH', 'ETR', 'EOG', 'EFX',
|
|
|
|
'EQIX', 'EQR', 'ESS', 'EL', 'ETSY', 'EVRG', 'ES', 'RE', 'EXC', 'EXPE', 'EXPD', 'EXR', 'XOM', 'FFIV', 'FB',
|
|
|
|
'FAST', 'FRT', 'FDX', 'FIS', 'FITB', 'FE', 'FRC', 'FISV', 'FLT', 'FMC', 'F', 'FTNT', 'FTV', 'FBHS',
|
|
|
|
'FOXA', 'FOX', 'BEN', 'FCX', 'GPS', 'GRMN', 'IT', 'GNRC', 'GD', 'GE', 'GIS', 'GM', 'GPC', 'GILD', 'GL',
|
|
|
|
'GPN', 'GS', 'GWW', 'HAL', 'HBI', 'HIG', 'HAS', 'HCA', 'PEAK', 'HSIC', 'HSY', 'HES', 'HPE', 'HLT', 'HOLX',
|
|
|
|
'HD', 'HON', 'HRL', 'HST', 'HWM', 'HPQ', 'HUM', 'HBAN', 'HII', 'IEX', 'IDXX', 'INFO', 'ITW', 'ILMN',
|
|
|
|
'INCY', 'IR', 'INTC', 'ICE', 'IBM', 'IP', 'IPG', 'IFF', 'INTU', 'ISRG', 'IVZ', 'IPGP', 'IQV', 'IRM',
|
|
|
|
'JKHY', 'J', 'JBHT', 'SJM', 'JNJ', 'JCI', 'JPM', 'JNPR', 'KSU', 'K', 'KEY', 'KEYS', 'KMB', 'KIM', 'KMI',
|
|
|
|
'KLAC', 'KHC', 'KR', 'LB', 'LHX', 'LH', 'LRCX', 'LW', 'LVS', 'LEG', 'LDOS', 'LEN', 'LLY', 'LNC', 'LIN',
|
|
|
|
'LYV', 'LKQ', 'LMT', 'L', 'LOW', 'LUMN', 'LYB', 'MTB', 'MRO', 'MPC', 'MKTX', 'MAR', 'MMC', 'MLM', 'MAS',
|
|
|
|
'MA', 'MKC', 'MXIM', 'MCD', 'MCK', 'MDT', 'MRK', 'MET', 'MTD', 'MGM', 'MCHP', 'MU', 'MSFT', 'MAA', 'MHK',
|
|
|
|
'TAP', 'MDLZ', 'MPWR', 'MNST', 'MCO', 'MS', 'MOS', 'MSI', 'MSCI', 'NDAQ', 'NTAP', 'NFLX', 'NWL', 'NEM',
|
|
|
|
'NWSA', 'NWS', 'NEE', 'NLSN', 'NKE', 'NI', 'NSC', 'NTRS', 'NOC', 'NLOK', 'NCLH', 'NOV', 'NRG', 'NUE',
|
|
|
|
'NVDA', 'NVR', 'NXPI', 'ORLY', 'OXY', 'ODFL', 'OMC', 'OKE', 'ORCL', 'OGN', 'OTIS', 'PCAR', 'PKG', 'PH',
|
|
|
|
'PAYX', 'PAYC', 'PYPL', 'PENN', 'PNR', 'PBCT', 'PEP', 'PKI', 'PRGO', 'PFE', 'PM', 'PSX', 'PNW', 'PXD',
|
|
|
|
'PNC', 'POOL', 'PPG', 'PPL', 'PFG', 'PG', 'PGR', 'PLD', 'PRU', 'PTC', 'PEG', 'PSA', 'PHM', 'PVH', 'QRVO',
|
|
|
|
'PWR', 'QCOM', 'DGX', 'RL', 'RJF', 'RTX', 'O', 'REG', 'REGN', 'RF', 'RSG', 'RMD', 'RHI', 'ROK', 'ROL',
|
|
|
|
'ROP', 'ROST', 'RCL', 'SPGI', 'CRM', 'SBAC', 'SLB', 'STX', 'SEE', 'SRE', 'NOW', 'SHW', 'SPG', 'SWKS',
|
|
|
|
'SNA', 'SO', 'LUV', 'SWK', 'SBUX', 'STT', 'STE', 'SYK', 'SIVB', 'SYF', 'SNPS', 'SYY', 'TMUS', 'TROW',
|
|
|
|
'TTWO', 'TPR', 'TGT', 'TEL', 'TDY', 'TFX', 'TER', 'TSLA', 'TXN', 'TXT', 'TMO', 'TJX', 'TSCO', 'TT', 'TDG',
|
|
|
|
'TRV', 'TRMB', 'TFC', 'TWTR', 'TYL', 'TSN', 'UDR', 'ULTA', 'USB', 'UAA', 'UA', 'UNP', 'UAL', 'UNH', 'UPS',
|
|
|
|
'URI', 'UHS', 'UNM', 'VLO', 'VTR', 'VRSN', 'VRSK', 'VZ', 'VRTX', 'VFC', 'VIAC', 'VTRS', 'V', 'VNO', 'VMC',
|
|
|
|
'WRB', 'WAB', 'WMT', 'WBA', 'DIS', 'WM', 'WAT', 'WEC', 'WFC', 'WELL', 'WST', 'WDC', 'WU', 'WRK', 'WY',
|
|
|
|
'WHR', 'WMB', 'WLTW', 'WYNN', 'XEL', 'XLNX', 'XYL', 'YUM', 'ZBRA', 'ZBH', 'ZION', 'ZTS']
|
|
|
|
stock_list = ['WHR', 'WMB', 'WLTW', 'WYNN', 'XEL', 'XLNX', 'XYL', 'YUM', 'ZBRA', 'ZBH', 'ZION', 'ZTS']
|
|
|
|
|
|
|
|
tickerData = yf.Tickers(stock_list)
|
|
|
|
fullTableDf = tickerData.history(period='1d', start='2019-1-1', end='2020-1-25')
|
2021-06-20 19:46:12 +02:00
|
|
|
closePrices = fullTableDf['Close']
|
|
|
|
selected_stocks_in_graph = [stock_list[0]]
|
|
|
|
selected_stocks_in_graph_df = closePrices[selected_stocks_in_graph]
|
|
|
|
selected_stock_in_table = stock_list[0]
|
|
|
|
selected_stock_in_table_df = fullTableDf.xs(selected_stock_in_table, axis=1, level=1)
|
2021-06-20 22:02:32 +02:00
|
|
|
from_time = None
|
|
|
|
to_time = None
|
2021-06-20 18:59:35 +02:00
|
|
|
# Initialize the app
|
|
|
|
app = dash.Dash(__name__)
|
|
|
|
app.config.suppress_callback_exceptions = True
|
2021-06-21 11:37:44 +02:00
|
|
|
stock_info_in_time_period_df = pd.DataFrame(columns=stock_list, index=["średnia", "cena", "dywidenda", "wolatylność"])
|
2021-06-20 18:59:35 +02:00
|
|
|
|
|
|
|
|
2021-06-20 22:02:32 +02:00
|
|
|
def make_gauge(title, min_v, value, max_v):
|
|
|
|
gauge_size = 180
|
|
|
|
fig = go.Figure(go.Indicator(
|
|
|
|
mode="gauge+number",
|
|
|
|
value=value,
|
|
|
|
domain={'x': [0, 1], 'y': [0, 1]},
|
|
|
|
title={'text': title}))
|
|
|
|
fig.layout.height = gauge_size
|
|
|
|
fig.layout.width = gauge_size
|
|
|
|
fig.layout.margin = dict(l=2, r=2, t=10, b=2)
|
|
|
|
fig.layout.paper_bgcolor = "#1E1E1E"
|
|
|
|
fig.layout.font.color = "white"
|
|
|
|
fig.layout.font.family = "Arial"
|
|
|
|
return fig
|
|
|
|
|
|
|
|
|
2021-06-20 18:59:35 +02:00
|
|
|
def get_options(list_stocks):
|
|
|
|
dict_list = []
|
|
|
|
for i in list_stocks:
|
|
|
|
dict_list.append({'label': i, 'value': i})
|
|
|
|
|
|
|
|
return dict_list
|
|
|
|
|
|
|
|
|
|
|
|
app.layout = html.Div(
|
|
|
|
children=[
|
|
|
|
html.Div(className='row',
|
|
|
|
children=[
|
|
|
|
html.Div(className='four columns div-user-controls',
|
|
|
|
children=[
|
2021-06-20 19:51:50 +02:00
|
|
|
html.P('Wybierz akcje (jedną lub więcej) do porównania na wykresie'),
|
2021-06-20 18:59:35 +02:00
|
|
|
html.Div(
|
|
|
|
className='div-for-dropdown',
|
|
|
|
children=[
|
|
|
|
dcc.Dropdown(id='stockselector',
|
2021-06-20 19:46:12 +02:00
|
|
|
options=get_options(stock_list),
|
2021-06-20 22:02:32 +02:00
|
|
|
multi=True,
|
|
|
|
value=selected_stocks_in_graph,
|
2021-06-20 18:59:35 +02:00
|
|
|
style={'backgroundColor': '#1E1E1E'},
|
|
|
|
className='stockselector'
|
2021-06-20 19:51:50 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
style={'color': '#1E1E1E'}),
|
|
|
|
html.P('Wybierz jedną akcję aby zobaczyć szczegóły w tabeli'),
|
|
|
|
html.Div(
|
|
|
|
className='div-for-dropdown',
|
|
|
|
children=[
|
2021-06-21 15:42:00 +02:00
|
|
|
dcc.Dropdown(id='table_selector',
|
2021-06-20 19:46:12 +02:00
|
|
|
options=get_options(stock_list),
|
|
|
|
multi=False, value=selected_stock_in_table,
|
2021-06-20 18:59:35 +02:00
|
|
|
style={'backgroundColor': '#1E1E1E'},
|
|
|
|
className='stockselector'
|
2021-06-21 15:42:00 +02:00
|
|
|
),
|
2021-06-21 16:00:58 +02:00
|
|
|
html.Button("Pobierz dane", id="btn_data",style={'margin-top': '3rem'}),
|
2021-06-21 15:42:00 +02:00
|
|
|
dcc.Download(id="download-data")
|
|
|
|
|
2021-06-20 18:59:35 +02:00
|
|
|
],
|
2021-06-20 22:02:32 +02:00
|
|
|
style={'color': '#1E1E1E'}),
|
|
|
|
html.P('Wybierz przedział czasu by policzyć średnie i wachania'),
|
2021-06-21 16:00:58 +02:00
|
|
|
html.Div(
|
|
|
|
className='gauges',
|
|
|
|
children=[
|
|
|
|
dcc.Graph(id='average_gauge'),
|
|
|
|
dcc.Graph(id='volatility_gauge')
|
|
|
|
]),
|
2021-06-21 11:37:44 +02:00
|
|
|
dash_table.DataTable(
|
|
|
|
id='info_in_time_period',
|
|
|
|
style_header={'backgroundColor': 'rgb(30, 30, 30)'},
|
|
|
|
style_cell={
|
|
|
|
'backgroundColor': 'rgb(50, 50, 50)',
|
|
|
|
'color': 'white'
|
|
|
|
},
|
|
|
|
)
|
2021-06-20 22:02:32 +02:00
|
|
|
]),
|
2021-06-20 18:59:35 +02:00
|
|
|
html.Div(className='eight columns div-for-charts bg-grey',
|
|
|
|
children=[
|
2021-06-21 16:35:32 +02:00
|
|
|
dcc.Tabs(id='tabs', value='tab-1', children=[
|
|
|
|
dcc.Tab(id='tab-1', label='Chart 1', value='tab-1', children=[
|
|
|
|
dcc.Graph(id='timeseries', config={'displayModeBar': False}, animate=True),
|
|
|
|
]),
|
|
|
|
dcc.Tab(id='tab-2', label='Chart 2', value='tab-2',children=[
|
|
|
|
dcc.Graph(id='price-dividends', config={'displayModeBar': False}, animate=True),
|
|
|
|
]),
|
|
|
|
]),
|
2021-06-20 18:59:35 +02:00
|
|
|
dash_table.DataTable(
|
2021-06-20 19:46:12 +02:00
|
|
|
id='stock_price_table',
|
|
|
|
style_header={'backgroundColor': 'rgb(30, 30, 30)'},
|
|
|
|
style_cell={
|
|
|
|
'backgroundColor': 'rgb(50, 50, 50)',
|
|
|
|
'color': 'white'
|
|
|
|
},
|
2021-06-20 18:59:35 +02:00
|
|
|
)
|
|
|
|
])
|
|
|
|
])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-06-21 15:44:53 +02:00
|
|
|
# Callback for downloading file
|
2021-06-21 15:42:00 +02:00
|
|
|
@app.callback(
|
|
|
|
Output("download-data", "data"),
|
2021-06-21 16:00:58 +02:00
|
|
|
Input("btn_data", "n_clicks"),
|
|
|
|
State('table_selector', 'value'),
|
2021-06-21 15:42:00 +02:00
|
|
|
prevent_initial_call=True,
|
|
|
|
)
|
2021-06-21 15:44:53 +02:00
|
|
|
def create_download_file(n_clicks, selected_table):
|
2021-06-21 15:42:00 +02:00
|
|
|
global selected_stock_in_table_df
|
2021-06-21 15:44:53 +02:00
|
|
|
return dcc.send_data_frame(selected_stock_in_table_df.to_csv, "data-{table}.csv".format(table = selected_table))
|
2021-06-20 18:59:35 +02:00
|
|
|
|
|
|
|
# Callback for timeseries price
|
|
|
|
@app.callback(Output('timeseries', 'figure'), [Input('stockselector', 'value')])
|
|
|
|
def update_graph(selected_dropdown_value):
|
2021-06-20 19:46:12 +02:00
|
|
|
global selected_stocks_in_graph
|
|
|
|
global selected_stocks_in_graph_df
|
|
|
|
selected_stocks_in_graph = selected_dropdown_value
|
|
|
|
selected_stocks_in_graph_df = closePrices[selected_stocks_in_graph]
|
|
|
|
figure = px.line(selected_stocks_in_graph_df, template='plotly_dark', title='Stock Prices')
|
2021-06-20 22:02:32 +02:00
|
|
|
# figure.update_layout(clickmode='event+select')
|
2021-06-20 18:59:35 +02:00
|
|
|
return figure
|
|
|
|
|
|
|
|
|
2021-06-20 19:46:12 +02:00
|
|
|
@app.callback([Output("stock_price_table", "data"), Output('stock_price_table', 'columns')],
|
|
|
|
[Input('table_selector', 'value')])
|
|
|
|
def update_table(selected_dropdown_value):
|
|
|
|
global selected_stock_in_table
|
|
|
|
global selected_stock_in_table_df
|
|
|
|
selected_stock_in_table = selected_dropdown_value
|
|
|
|
selected_stock_in_table_df = fullTableDf.xs(selected_stock_in_table, axis=1, level=1)
|
|
|
|
data = selected_stock_in_table_df.to_dict('records')
|
|
|
|
columns = [{"name": i, "id": i} for i in selected_stock_in_table_df.columns]
|
|
|
|
return data, columns
|
2021-06-20 18:59:35 +02:00
|
|
|
|
|
|
|
|
2021-06-21 11:37:44 +02:00
|
|
|
average_gauge = make_gauge('średnia', 0, 0, 400)
|
|
|
|
volatility_gauge = make_gauge('wolatylność', 0, 0, 400)
|
|
|
|
|
|
|
|
|
|
|
|
def round_to_nearest_weekday(date):
|
|
|
|
if isinstance(date,pd.Timestamp):
|
|
|
|
if date.dayofweek > 5:
|
|
|
|
date += datetime.timedelta(days=8-date.dayofweek)
|
|
|
|
assert date.dayofweek <= 5
|
|
|
|
return date
|
|
|
|
elif isinstance(date, str):
|
|
|
|
if '.' in date:
|
|
|
|
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f")
|
|
|
|
elif ':' in date:
|
|
|
|
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
else:
|
|
|
|
date = datetime.datetime.strptime(date, "%Y-%m-%d")
|
|
|
|
if date.isoweekday() > 5:
|
|
|
|
date += datetime.timedelta(days=8-date.isoweekday())
|
|
|
|
assert date.isoweekday() <= 5
|
|
|
|
return date
|
|
|
|
|
|
|
|
|
|
|
|
@app.callback([Output('average_gauge', 'figure'),
|
|
|
|
Output('volatility_gauge', 'figure'),
|
|
|
|
Output('info_in_time_period', 'data')],
|
2021-06-20 22:02:32 +02:00
|
|
|
Input('timeseries', 'relayoutData'))
|
|
|
|
def change_time_period(selectedData):
|
|
|
|
global from_time
|
|
|
|
global to_time
|
2021-06-21 11:37:44 +02:00
|
|
|
global average_gauge
|
|
|
|
global volatility_gauge
|
|
|
|
global stock_info_in_time_period_df
|
2021-06-20 22:03:46 +02:00
|
|
|
if selectedData is not None:
|
2021-06-21 11:37:44 +02:00
|
|
|
if "xaxis.range[0]" in selectedData and "xaxis.range[1]" in selectedData:
|
2021-06-20 22:03:46 +02:00
|
|
|
from_time = selectedData["xaxis.range[0]"]
|
|
|
|
to_time = selectedData["xaxis.range[1]"]
|
2021-06-21 11:37:44 +02:00
|
|
|
else:
|
|
|
|
from_time = selected_stock_in_table_df.index.min()
|
|
|
|
to_time = selected_stock_in_table_df.index.max()
|
|
|
|
from_time = round_to_nearest_weekday(from_time)
|
|
|
|
to_time = round_to_nearest_weekday(to_time)
|
|
|
|
time_period = selected_stock_in_table_df.loc[from_time:to_time]
|
|
|
|
mean = time_period.mean(axis=0)
|
|
|
|
std = time_period.std(axis=0)
|
|
|
|
# TODO: oblicz stock_info_in_time_period_df tutuaj !!!
|
|
|
|
average_gauge = make_gauge('średnia', 0, mean['Close'], 400)
|
|
|
|
volatility_gauge = make_gauge('wolatylność', 0, std['Close'], 400)
|
|
|
|
return average_gauge, volatility_gauge, stock_info_in_time_period_df.T.to_dict('records')
|
2021-06-20 22:02:32 +02:00
|
|
|
|
2021-06-21 16:35:32 +02:00
|
|
|
@app.callback(Output('price-dividends', 'figure'),
|
|
|
|
[Input('table_selector', 'value')])
|
|
|
|
def update_point_chart(selected_dropdown_value):
|
|
|
|
selected_stock_in_table_df = fullTableDf.xs(selected_dropdown_value, axis=1, level=1)
|
|
|
|
figure = px.scatter(selected_stock_in_table_df, x='Volume', y='Dividends', template='plotly_dark')
|
|
|
|
return figure
|
2021-06-20 22:02:32 +02:00
|
|
|
|
2021-06-20 18:59:35 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run_server(debug=True)
|