-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
211 lines (191 loc) · 7.4 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import dash
from dash import html, dcc, ALL
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from quantstats import stats
import numpy as np
import warnings
warnings.filterwarnings("ignore")
import os
# Import our modular components
from src.config import COLORS, DATA_PATH
from src.data.loader import PortfolioDataLoader
from src.components.charts import PortfolioCharts
from src.components.metrics import PortfolioMetrics
from src.layouts.dashboard import DashboardLayout
# Initialize components
data_loader = PortfolioDataLoader(DATA_PATH)
returns, port_ret = data_loader.calculate_returns()
charts = PortfolioCharts(COLORS)
metrics = PortfolioMetrics(COLORS)
layout = DashboardLayout()
# Initialize Dash app with callback exception suppression
app = dash.Dash(
__name__,
external_stylesheets=[
dbc.themes.FLATLY,
{
'href': 'https://use.fontawesome.com/releases/v5.15.4/css/all.css',
'rel': 'stylesheet',
'integrity': 'sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm',
'crossorigin': 'anonymous'
},
{
'href': 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
'rel': 'stylesheet',
'integrity': 'sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3',
'crossorigin': 'anonymous'
},
{'href': '/assets/styles.css', 'rel': 'stylesheet'}
],
suppress_callback_exceptions=True,
assets_folder='assets'
)
# Create initial charts and metrics
initial_metric_cards = metrics.create_all_metric_cards(port_ret)
initial_chart_figures = {
'Cumulative Returns': charts.create_cumulative_returns_chart(port_ret),
'Rolling Statistics': charts.create_rolling_stats_chart(port_ret),
'Drawdown Analysis': charts.create_drawdown_chart(port_ret),
'Risk Metrics': charts.create_risk_metrics_chart({
'Monthly VaR': stats.var(port_ret),
'Monthly CVaR': stats.cvar(port_ret),
'Max Drawdown': stats.max_drawdown(port_ret)
})
}
# Create weight input components
def create_weight_inputs():
asset_names = data_loader.asset_names
current_weights = data_loader.weights
return dbc.Card([
dbc.CardHeader("Portfolio Weights", className="text-center"),
dbc.CardBody([
dbc.Row([
dbc.Col([
dbc.Label(f"{asset} Weight"),
dbc.Input(
id={'type': 'weight-input', 'index': i},
type='number',
min=0,
max=1,
step=0.1,
value=weight
)
], width=12, md=4, className="mb-3")
for i, (asset, weight) in enumerate(zip(asset_names, current_weights))
]),
dbc.Row([
dbc.Col([
dbc.Button("Update Portfolio", id='update-portfolio', color="primary", className="w-100"),
html.Div(id="weight-error", className="text-danger small mt-2")
], width=12)
])
])
], className="mb-4")
# Create file upload component
def create_upload_section():
return dbc.Card([
dbc.CardHeader("Data Upload", className="text-center"),
dbc.CardBody([
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select CSV File')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px 0'
},
multiple=False
),
html.Div(id='upload-error', className="text-danger small mt-2"),
html.Div(id='upload-success', className="text-success small mt-2")
])
], className="mb-4")
# Create app layout with initial content
app.layout = dbc.Container([
dbc.Row([
dbc.Col(html.H1("Portfolio Statistics", className="text-center my-4"), width=12)
]),
create_upload_section(),
html.Div(create_weight_inputs(), id='weight-inputs-container'),
html.Div(layout.create_layout(initial_metric_cards, initial_chart_figures), id='charts-container')
], fluid=True)
# Callback to update the dashboard
@app.callback(
[
Output('weight-inputs-container', 'children'),
Output('charts-container', 'children'),
Output('upload-error', 'children'),
Output('upload-success', 'children')
],
[
Input('upload-data', 'contents'),
Input('update-portfolio', 'n_clicks')
],
[
State('upload-data', 'filename'),
State({'type': 'weight-input', 'index': ALL}, 'value')
],
prevent_initial_call=True
)
def update_dashboard(contents, n_clicks, filename, weights):
ctx = dash.callback_context
trigger_id = ctx.triggered[0]['prop_id'] if ctx.triggered else None
try:
# Handle file upload
if trigger_id == 'upload-data.contents' and contents is not None:
# Validate the uploaded file
is_valid, error_message = data_loader.validate_csv(contents, filename)
if not is_valid:
return dash.no_update, dash.no_update, error_message, ""
# Load new data
data_loader.load_data(contents, filename)
success_msg = f"Successfully loaded {filename}"
else:
success_msg = ""
# Handle weight updates
if trigger_id == 'update-portfolio.n_clicks' and weights:
try:
weights = [float(w) if w is not None else 0 for w in weights]
data_loader.set_weights(weights)
except ValueError as e:
return dash.no_update, dash.no_update, str(e), ""
# Calculate portfolio returns
returns, port_ret = data_loader.calculate_returns()
# Create weight inputs section
weight_inputs = create_weight_inputs()
# Create charts and metrics
metric_cards = metrics.create_all_metric_cards(port_ret)
chart_figures = {
'Cumulative Returns': charts.create_cumulative_returns_chart(port_ret),
'Rolling Statistics': charts.create_rolling_stats_chart(port_ret),
'Drawdown Analysis': charts.create_drawdown_chart(port_ret),
'Risk Metrics': charts.create_risk_metrics_chart({
'Monthly VaR': stats.var(port_ret),
'Monthly CVaR': stats.cvar(port_ret),
'Max Drawdown': stats.max_drawdown(port_ret)
})
}
# Create layout components
charts_layout = layout.create_layout(metric_cards, chart_figures)
return weight_inputs, charts_layout, "", success_msg
except Exception as e:
return dash.no_update, dash.no_update, f"Error: {str(e)}", ""
if __name__ == '__main__':
# Get host and port from environment variables with defaults
host = os.getenv('HOST', '0.0.0.0')
port = int(os.getenv('PORT', 8050))
debug = os.getenv('DASH_DEBUG_MODE', 'False').lower() == 'true'
app.run_server(
host=host,
port=port,
debug=debug
)