2024-10-23 18:58:31 +00:00
|
|
|
import dash_bootstrap_components as dbc
|
|
|
|
|
2024-11-12 15:22:32 +00:00
|
|
|
from dash import Dash, dcc, html, Input, Output
|
2024-10-23 18:58:31 +00:00
|
|
|
from dash_ag_grid import AgGrid
|
|
|
|
from dash_bootstrap_templates import load_figure_template
|
2024-11-11 17:38:43 +00:00
|
|
|
from datetime import datetime
|
2024-11-12 15:22:32 +00:00
|
|
|
from flask_caching import Cache
|
2024-10-28 17:52:46 +00:00
|
|
|
from pandas import DataFrame, Series
|
2024-10-23 18:58:31 +00:00
|
|
|
|
|
|
|
from plotting import CandlestickChart, figure_with_subplots
|
|
|
|
|
2024-11-11 17:38:43 +00:00
|
|
|
from daily_data import get_daily_data
|
2024-11-27 20:41:20 +00:00
|
|
|
from signals import calculate_signals
|
2024-10-28 17:52:46 +00:00
|
|
|
from stylesheets import grid_stylesheet, theme_stylesheet
|
2024-10-23 18:58:31 +00:00
|
|
|
|
2024-10-28 17:52:46 +00:00
|
|
|
app = Dash(__name__, external_stylesheets = [theme_stylesheet, grid_stylesheet])
|
2024-10-23 18:58:31 +00:00
|
|
|
app.title = 'Swing Trading Dashboard'
|
|
|
|
|
2024-11-12 15:22:32 +00:00
|
|
|
cache = Cache(app.server, config = {
|
|
|
|
'CACHE_TYPE': 'SimpleCache',
|
|
|
|
'CACHE_DEFAULT_TIMEOUT': 60 # Seconds.
|
|
|
|
})
|
|
|
|
|
2024-10-28 17:52:46 +00:00
|
|
|
load_figure_template('lux_dark')
|
2024-10-23 18:58:31 +00:00
|
|
|
|
2024-11-12 15:22:32 +00:00
|
|
|
def load_chart(data: DataFrame) -> dict:
|
2024-10-28 18:25:04 +00:00
|
|
|
chart_data = data.tail(180)
|
2024-10-23 18:58:31 +00:00
|
|
|
candlestick_chart = CandlestickChart(
|
2024-10-28 18:25:04 +00:00
|
|
|
x = chart_data['Date'],
|
|
|
|
opens = chart_data['Open'],
|
|
|
|
highs = chart_data['High'],
|
|
|
|
lows = chart_data['Low'],
|
|
|
|
closes = chart_data['Close']
|
2024-10-23 18:58:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return figure_with_subplots([[candlestick_chart]])
|
|
|
|
|
2024-11-12 15:22:32 +00:00
|
|
|
@cache.cached(timeout = 60, key_prefix = 'get_daily_data')
|
|
|
|
def get_cached_data():
|
|
|
|
return get_daily_data('SPY')
|
|
|
|
|
|
|
|
@cache.cached(timeout = 60, key_prefix = 'calculate_signals')
|
|
|
|
def get_cached_signals():
|
|
|
|
data = get_cached_data()
|
|
|
|
return calculate_signals(data)
|
|
|
|
|
|
|
|
@cache.cached(timeout = 60, key_prefix = 'load_chart')
|
|
|
|
def get_cached_chart():
|
|
|
|
data = get_cached_data()
|
|
|
|
return load_chart(data)
|
2024-10-28 17:52:46 +00:00
|
|
|
|
2024-10-23 18:58:31 +00:00
|
|
|
app.layout = dbc.Container(
|
|
|
|
[
|
2024-11-12 15:22:32 +00:00
|
|
|
dcc.Interval(
|
|
|
|
id = 'refresh-interval',
|
|
|
|
interval = 60 * 1000, # Milliseconds.
|
|
|
|
n_intervals = 0
|
|
|
|
),
|
2024-10-23 18:58:31 +00:00
|
|
|
dcc.Graph(
|
|
|
|
id = 'candlestick-chart',
|
|
|
|
config = {'displayModeBar': False},
|
2024-11-12 15:22:32 +00:00
|
|
|
figure = figure_with_subplots([[CandlestickChart(
|
|
|
|
x = Series(datetime.today()),
|
|
|
|
opens = Series(),
|
|
|
|
highs = Series(),
|
|
|
|
lows = Series(),
|
|
|
|
closes = Series(),
|
|
|
|
title = ''
|
2024-11-13 19:11:20 +00:00
|
|
|
)]])
|
2024-10-23 18:58:31 +00:00
|
|
|
),
|
|
|
|
html.Div(
|
|
|
|
AgGrid(
|
2024-11-12 15:22:32 +00:00
|
|
|
id = 'signal-table',
|
|
|
|
columnDefs = [{'field': 'Strategy', 'flex': 3}],
|
|
|
|
rowData = [],
|
2024-10-23 18:58:31 +00:00
|
|
|
defaultColDef = {'flex': 1, 'sortable': False, 'resizable': False},
|
2024-11-16 14:25:41 +00:00
|
|
|
dashGridOptions = {
|
|
|
|
'domLayout': 'autoHeight',
|
|
|
|
'pagination': True,
|
|
|
|
'paginationPageSizeSelector': False,
|
|
|
|
'paginationPageSize': 10,
|
|
|
|
},
|
2024-10-23 18:58:31 +00:00
|
|
|
style = {'height': None}
|
|
|
|
),
|
|
|
|
className = 'dbc dbc-ag-grid'
|
|
|
|
)
|
|
|
|
],
|
2024-10-28 17:52:46 +00:00
|
|
|
style = {'maxWidth': '1200px', 'margin': '0 auto'},
|
2024-10-23 18:58:31 +00:00
|
|
|
fluid = True
|
|
|
|
)
|
|
|
|
|
2024-11-12 15:22:32 +00:00
|
|
|
@app.callback(
|
|
|
|
[
|
|
|
|
Output('candlestick-chart', 'figure'),
|
|
|
|
Output('signal-table', 'columnDefs'),
|
|
|
|
Output('signal-table', 'rowData')
|
|
|
|
],
|
|
|
|
Input('refresh-interval', 'n_intervals')
|
|
|
|
)
|
|
|
|
def update_dashboard(_):
|
|
|
|
signal_data = get_cached_signals()
|
|
|
|
chart_figure = get_cached_chart()
|
|
|
|
|
|
|
|
column_defs = [
|
|
|
|
{'field': col, 'flex': 3} if col == 'Strategy' else {'field': col, 'flex': 1, 'cellRenderer': 'SignalRenderer'}
|
|
|
|
for col in signal_data.columns
|
|
|
|
]
|
|
|
|
|
|
|
|
return chart_figure, column_defs, signal_data.to_dict(orient = 'records')
|
|
|
|
|
2024-10-23 18:58:31 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run_server(debug = False)
|