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
|
|
|
|
from dash.dependencies import Input, Output
|
|
|
|
|
|
|
|
# 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 18:59:35 +02:00
|
|
|
# Initialize the app
|
|
|
|
app = dash.Dash(__name__)
|
|
|
|
app.config.suppress_callback_exceptions = True
|
|
|
|
|
|
|
|
|
|
|
|
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=[
|
|
|
|
html.H2('DASH - STOCK PRICES'),
|
|
|
|
html.P('Visualising time series with Plotly - Dash.'),
|
|
|
|
html.P('Pick one or more stocks from the dropdown below.'),
|
|
|
|
html.Div(
|
|
|
|
className='div-for-dropdown',
|
|
|
|
children=[
|
|
|
|
dcc.Dropdown(id='stockselector',
|
2021-06-20 19:46:12 +02:00
|
|
|
options=get_options(stock_list),
|
|
|
|
multi=True, value=selected_stocks_in_graph,
|
2021-06-20 18:59:35 +02:00
|
|
|
style={'backgroundColor': '#1E1E1E'},
|
|
|
|
className='stockselector'
|
|
|
|
),
|
|
|
|
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'
|
|
|
|
)
|
|
|
|
],
|
|
|
|
style={'color': '#1E1E1E'})
|
|
|
|
]
|
|
|
|
),
|
|
|
|
html.Div(className='eight columns div-for-charts bg-grey',
|
|
|
|
children=[
|
|
|
|
dcc.Graph(id='timeseries', config={'displayModeBar': False}, animate=True),
|
|
|
|
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
|
|
|
)
|
|
|
|
])
|
|
|
|
])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 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 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
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run_server(debug=True)
|