-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
252 lines (195 loc) · 6.06 KB
/
app.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import dash
from dash.dependencies import Output, Input, State, ALL
from dash.exceptions import PreventUpdate
import dash_bootstrap_components as dbc
import flask
import plotly.io as pio
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug import run_simple
from layouts import tickers
from layouts import relatorio
from layouts import ajuda
import app_config as cfg
import report
import utils
server = flask.Flask(__name__)
server.secret_key = cfg.SECRET_KEY
pio.templates.default = cfg.PLOTLY_TEMPLATE
external_stylesheets = [
dbc.themes.BOOTSTRAP,
cfg.MONTSERRAT,
cfg.FONTAWESOME
]
tickers_app = dash.Dash(
__name__,
server = server,
url_base_pathname = '/tickers/',
external_stylesheets = external_stylesheets
)
tickers_app.title = cfg.NAME
tickers_app.layout = tickers.layout
relatorio_app = dash.Dash(
__name__,
server = server,
url_base_pathname = '/relatorio/',
external_stylesheets = external_stylesheets
)
relatorio_app.title = cfg.NAME
relatorio_app.layout = relatorio.layout
ajuda_app = dash.Dash(
__name__,
server = server,
url_base_pathname = '/ajuda/',
external_stylesheets = external_stylesheets
)
ajuda_app.title = cfg.NAME
ajuda_app.layout = ajuda.layout
@server.route('/')
def redirect_menu():
return flask.redirect('/tickers/')
@server.route('/tickers/')
def render_tickers():
return flask.redirect('/PyTickers')
@server.route('/relatorio/')
def render_relatorio():
return flask.redirect('/PyRelatorio')
@server.route('/ajuda/')
def render_ajuda():
return flask.redirect('/PyAjuda')
@tickers_app.callback(
Output('ticker_data', 'data'),
Output('ticker_toast', 'children'),
Output('ticker_toast', 'header'),
Output('ticker_toast', 'icon'),
Output('ticker_toast', 'is_open'),
Output('ticker_input', 'value'),
Input('ticker_add', 'n_clicks'),
Input({'ticker_remove': ALL}, 'n_clicks_timestamp'),
State('ticker_input', 'value'),
State('ticker_checkbox', 'checked'),
State('ticker_data', 'data'),
prevent_initial_call = True)
def add_ticker_to_data(_, clicks, ticker, b3, data):
# Remover ticker
cc = dash.callback_context.triggered[0]['prop_id']
if cc != 'ticker_add.n_clicks':
ticker_to_remove = cc.split('"')[3]
data.pop(clicks.index(max(clicks)))
return (
data,
f'Ticker "{ticker_to_remove}" removido da carteira.',
'Ticker removido',
'danger',
True,
dash.no_update
)
# Formatar ticker
ticker = ticker.upper()
if b3 and not ticker.endswith('.SA'):
ticker = f'{ticker}.SA'
# Adicionar ticker
if ticker in data:
full_name = f'Ticker "{ticker}" já está na carteira.'
color = 'danger'
header = 'Erro! Tente novamente.'
else:
full_name, color, header = utils.get_ticker_name(ticker)
if color == 'primary':
data.append(ticker)
value = None
else:
data = dash.no_update
value = dash.no_update
return data, full_name, header, color, True, value
@tickers_app.callback(
Output('ticker_tags', 'children'),
Output('ticker_analyse', 'disabled'),
Input('ticker_data', 'data'))
def generate_tags(data):
tags = [
dbc.Col(
utils.tag(ticker),
width = 'auto',
className = 'mb-3'
) for ticker in data
]
return tags, len(data)<2
@tickers_app.callback(
Output('location', 'href'),
Input('ticker_analyse', 'n_clicks'),
State('ticker_data', 'data'),
prevent_initial_call = True)
def go_to_report(_, data):
hashs = ''.join([f'#{d}' for d in data])
return f'https://diversificador.herokuapp.com/relatorio/{hashs}'
@relatorio_app.callback(
Output('corr_table', 'children'),
Output('monthly_returns', 'data'),
Output('portfolios_data', 'data'),
Input('location', 'hash'))
def load_relatorio(hashtags):
r = report.Markowitz(hashtags)
return (
r.corr_table(),
r.df.to_json(),
r.portfolios.to_json()
)
@relatorio_app.callback(
Output('portfolios_returns', 'children'),
Output('portfolios_chart', 'figure'),
Output('selected_portfolio', 'data'),
Output('efficiency_frontier', 'figure'),
Input('efficiency_frontier', 'clickData'),
Input('portfolios_data', 'data'))
def select_portfolio_risk(click, data):
portfolio = 0 if click is None else click['points'][0]['pointNumber']
r = report.MarkowitzAllocation(data, portfolio)
return (
r.expected_returns(),
r.pie(),
r.portfolio.to_json(),
r.efficiency_frontier()
)
@relatorio_app.callback(
Output('capital_allocation_line', 'figure'),
Output('risk_free_returns', 'children'),
Output('risk_free_table', 'children'),
Input('selected_portfolio', 'data'),
Input('capital_allocation_line', 'clickData'),
prevent_inital_call = True)
def update_capital_allocation_line(data, click):
p = 0 if click is None else click['points'][0]['pointNumber']
r = report.CapitalAllocation(data)
return (
r.capital_allocation_line(p),
r.expected_returns(p/20),
r.final_table(p/20)
)
@relatorio_app.callback(
Output('corr_timeline_modal', 'is_open'),
Output('corr_timeline_title', 'children'),
Output('corr_timeline_chart', 'figure'),
Input({'ticker_a': ALL, 'ticker_b': ALL}, 'n_clicks'),
State('monthly_returns', 'data'),
prevent_initial_call = True)
def load_corr_timeline(tickers, data):
if all(click is None for click in tickers):
raise PreventUpdate
cc = dash.callback_context.triggered[0]['prop_id'].split('"')
fig = report.CorrelationTimeline(cc[3], cc[7])
return (
True,
fig.title(),
fig.plot(data)
)
app = DispatcherMiddleware(server, {
'/PyTickers': tickers_app.server,
'/PyRelatorio': relatorio_app.server,
'/PyAjuda': ajuda_app.server
})
if __name__ == '__main__':
run_simple(
hostname = '0.0.0.0',
port = cfg.PORT,
application = app
)