Update swing trading dashboard to use newly generated signals

This commit is contained in:
moshferatu 2024-10-28 10:52:46 -07:00
parent c2b2d00beb
commit d8870e0c62

View File

@ -3,35 +3,52 @@ import dash_bootstrap_components as dbc
from dash import Dash, dcc, html
from dash_ag_grid import AgGrid
from dash_bootstrap_templates import load_figure_template
from datetime import date, datetime, timedelta
from dotenv import load_dotenv
from pandas import DataFrame
from datetime import datetime, timedelta
from pandas import DataFrame, Series
from typing import Callable, List, Dict
from ohlc import ohlc
from plotting import CandlestickChart, figure_with_subplots
load_dotenv()
from strategies import internal_bar_strength
from strategies import end_of_month
from stylesheets import grid_stylesheet, theme_stylesheet
app = Dash(__name__, external_stylesheets = [theme_stylesheet, grid_stylesheet])
app.title = 'Swing Trading Dashboard'
load_figure_template('lux_dark')
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
app = Dash(__name__, external_stylesheets = [dbc.themes.LUX, dbc_css])
app.title = 'Swing Trading Dashboard'
SignalFunction = Callable[[DataFrame], Series]
today = date.today()
signal_data = [
{'Strategy': '2-Period RSI', '10/14': 'N', '10/15': 'N', '10/16': 'N', '10/17': 'N', '10/18': 'L', '10/19': 'N', '10/20': 'L', '10/21': 'N', '10/22': 'N', '10/23': 'N'},
{'Strategy': 'Cumulative RSI', '10/14': 'N', '10/15': 'N', '10/16': 'N', '10/17': 'N', '10/18': 'L', '10/19': 'N', '10/20': 'L', '10/21': 'N', '10/22': 'N', '10/23': 'N'},
{'Strategy': 'Internal Bar Strength', '10/14': 'N', '10/15': 'N', '10/16': 'N', '10/17': 'N', '10/18': 'L', '10/19': 'N', '10/20': 'L', '10/21': 'N', '10/22': 'N', '10/23': 'N'},
{'Strategy': 'End of Month', '10/14': 'N', '10/15': 'N', '10/16': 'N', '10/17': 'N', '10/18': 'L', '10/19': 'N', '10/20': 'L', '10/21': 'N', '10/22': 'N', '10/23': 'N'}
signal_functions: List[Dict[str, SignalFunction]] = [
{'strategy': 'End of Month', 'function': end_of_month.signals},
{'strategy': 'Internal Bar Strength', 'function': internal_bar_strength.signals}
]
data = DataFrame(signal_data)
def load_chart():
symbol = 'SPY'
symbol = 'SPY'
def calculate_signals(data: DataFrame, days: int = 12) -> DataFrame:
end_date = datetime.today()
start_date = end_date - timedelta(days = days + 10)
data = ohlc(symbol = symbol, start_date = start_date, end_date = end_date)
recent_signals = data.tail(days)
signal_data = []
for signal_info in signal_functions:
signals = signal_info['function'](recent_signals)
signal_dict = {'Strategy': signal_info['strategy']}
dates = [datetime.strptime(str(date), '%Y-%m-%d').strftime('%m/%d') for date in recent_signals['Date']]
for date, signal in zip(dates, signals):
signal_dict[date] = signal
signal_data.append(signal_dict)
return DataFrame(signal_data)
def load_chart() -> dict:
today = datetime.today()
data = ohlc(symbol = symbol, start_date = today - timedelta(days = 180), end_date = today)
candlestick_chart = CandlestickChart(
@ -44,6 +61,8 @@ def load_chart():
return figure_with_subplots([[candlestick_chart]])
signal_data = calculate_signals(DataFrame(), days = 12)
app.layout = dbc.Container(
[
dcc.Graph(
@ -54,10 +73,10 @@ app.layout = dbc.Container(
html.Div(
AgGrid(
columnDefs = [
{'field': col, 'flex': 2} if col == 'Strategy' else {'field': col, 'flex': 1, 'cellRenderer': 'SignalRenderer'}
for col in data.columns
{'field': col, 'flex': 3} if col == 'Strategy' else {'field': col, 'flex': 1, 'cellRenderer': 'SignalRenderer'}
for col in signal_data.columns
],
rowData = data.to_dict(orient = 'records'),
rowData = signal_data.to_dict(orient = 'records'),
defaultColDef = {'flex': 1, 'sortable': False, 'resizable': False},
dashGridOptions = {'domLayout': 'autoHeight'},
style = {'height': None}
@ -65,10 +84,7 @@ app.layout = dbc.Container(
className = 'dbc dbc-ag-grid'
)
],
style = {
'maxWidth': '1200px',
'margin': '0 auto', # For centering the page content.
},
style = {'maxWidth': '1200px', 'margin': '0 auto'},
fluid = True
)