-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
292 lines (205 loc) · 10.3 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import concurrent.futures
import io
import pandas as pd
import streamlit as st
from matplotlib import pyplot as plt
from config import Logger
from py_invest_analyser.models import RealEstateFunds, Stock
from py_invest_analyser.services import ExtractInfoFromREF, ExtractInfoFromStock
def generate_data_fiis(actives):
result_actives = []
logger = Logger().logger
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(ExtractInfoFromREF().get_info_active, active) for active in actives]
for future in concurrent.futures.as_completed(futures):
try:
active = future.result()
if isinstance(active, str):
active = ExtractInfoFromREF().get_active_keys_indicators(active)
result_actives.append(active)
except Exception as e:
logger.error(f"Error to get information for active {active.name}")
logger.error(e)
return pd.DataFrame(result_actives)
def generate_data_stock(actives):
result_actives = []
logger = Logger().logger
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(ExtractInfoFromStock().get_info_active, active) for active in actives]
for future in concurrent.futures.as_completed(futures):
try:
active = future.result()
if isinstance(active, str):
active = ExtractInfoFromStock().get_active_keys_indicators(active)
result_actives.append(active)
except Exception as e:
logger.error(f"Error to get information for active {active.name}")
logger.error(e)
return pd.DataFrame(result_actives)
st.set_page_config(page_title='PyInvestAnalyser', page_icon='📊', layout='wide')
st.set_option('deprecation.showPyplotGlobalUse', False)
st.title('PyInvestAnalyser')
data = st.file_uploader('Escolha o arquivo para analise de dados', type=['csv'])
if data is None:
st.warning(
"""
Por favor, escolha um arquivo para analise de dados."):
""")
st.stop()
content = data.read().decode('latin-1')
content_lines = content.split('\n')
content_without_first_line = '\n'.join(content_lines[1:])
df = pd.read_csv(io.StringIO(content_without_first_line), sep=';')
with st.expander("Visualizar dados brutos"):
st.dataframe(df)
st.title('Fundos Imobiliários')
df = df[['TIPO DE INVESTIMENTO', 'DESCRIÇÃO', 'VALOR BRUTO', "QUANTIDADE"]]
fiis = df[df['TIPO DE INVESTIMENTO'] == 'FII'].copy()
with st.spinner('Loading...'):
df_infos_fiis = generate_data_fiis(fiis['DESCRIÇÃO'].unique())
df_infos_fiis.columns = RealEstateFunds.get_meaning_of_fields().values()
with st.expander("Visualizar dados brutos"):
st.dataframe(df_infos_fiis)
df_infos_fiis = df_infos_fiis[
["Nome", "Cotação", "Dividend Yield", "P/VP", "VAL. PATRIMONIAL P/ COTA", 'SEGMENTO', "Valorização",
"ÚLTIMO RENDIMENTO"]]
fiis["ULT. RENDIMENTO"] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["ÚLTIMO RENDIMENTO"])
fiis['VALORIZAÇÃO'] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["Valorização"])
fiis["SEGMENTO"] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["SEGMENTO"])
fiis["COTAÇÃO"] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["Cotação"])
fiis["P/VP"] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["P/VP"])
fiis["DIVIDEND YIELD"] = fiis["DESCRIÇÃO"].map(df_infos_fiis.set_index("Nome")["Dividend Yield"])
fiis["P/VP"] = fiis["P/VP"].str.replace(',', '.')
fiis['VALOR BRUTO'] = fiis['VALOR BRUTO'].str.replace('.', '')
fiis['VALOR BRUTO'] = fiis['VALOR BRUTO'].str.replace('R$ ', '')
fiis['VALOR BRUTO'] = fiis['VALOR BRUTO'].str.replace(',', '.')
fiis['VALOR BRUTO'] = fiis['VALOR BRUTO'].astype(float)
fiis["COTAÇÃO"] = fiis["COTAÇÃO"].str.replace(',', '.')
fiis["COTAÇÃO"] = fiis["COTAÇÃO"].str.replace('R$ ', '')
fiis['COTAÇÃO'] = fiis['COTAÇÃO'].astype(float)
fiis["VALORIZAÇÃO"] = fiis["VALORIZAÇÃO"].str.replace(',', '.')
fiis["VALORIZAÇÃO"] = fiis["VALORIZAÇÃO"].str.replace('%', '')
fiis["VALORIZAÇÃO"] = fiis["VALORIZAÇÃO"].astype(float)
fiis['QUANTIDADE'] = fiis['QUANTIDADE'].str.replace(',0', '')
fiis['QUANTIDADE'] = fiis['QUANTIDADE'].astype(int)
fiis["DIVIDEND YIELD"] = fiis["DIVIDEND YIELD"].str.replace(',', '.')
fiis["DIVIDEND YIELD"] = fiis["DIVIDEND YIELD"].str.replace('%', '')
fiis["DIVIDEND YIELD"] = fiis["DIVIDEND YIELD"].astype(float)
fiis['ULT. RENDIMENTO'] = fiis['ULT. RENDIMENTO'].str.replace('.', '')
fiis['ULT. RENDIMENTO'] = fiis['ULT. RENDIMENTO'].str.replace('R$ ', '')
fiis['ULT. RENDIMENTO'] = fiis['ULT. RENDIMENTO'].str.replace(',', '.')
fiis['ULT. RENDIMENTO'] = fiis['ULT. RENDIMENTO'].astype(float)
fiis.drop(columns=['TIPO DE INVESTIMENTO'], inplace=True)
# Metrics
col1, col2, col3, col4, col5, col6, col7 = st.columns(7)
with col1:
st.metric(label="Total de Investimento", value=f"R$ {fiis['VALOR BRUTO'].sum():,.2f}")
with col2:
total_rendimento = fiis['ULT. RENDIMENTO'] * fiis['QUANTIDADE']
st.metric(label="Total de Rendimento(último mês)", value=f"R$ {total_rendimento.sum():,.2f}")
with col3:
porcentagem_mes = (total_rendimento.sum() / fiis['VALOR BRUTO'].sum()) * 100
st.metric(label=f"Rendimento Mensal(%)", value=f"{porcentagem_mes:,.2f}%")
with col4:
maior = fiis['VALOR BRUTO'].values.max()
nome = fiis[fiis['VALOR BRUTO'] == maior]['DESCRIÇÃO'].values[0]
st.metric(label=f"Maior posição ({nome})", value=f"R$ {maior:,.2f}")
with col5:
media_dividend_yield = fiis['DIVIDEND YIELD'].mean()
st.metric(label=f"Dividend Yield Médio (12M)", value=f"{media_dividend_yield:,.2f}%")
with col6:
media_valorizacao = fiis['VALORIZAÇÃO'].mean()
st.metric(label=f"Valorização Média (12M)", value=f"{media_valorizacao:,.2f}%")
with col7:
st.metric(label="Total de Ativos", value=f"{len(fiis)}")
def bigger_than_pvp(value):
if float(value) < 1.0:
return 'background-color: green'
if float(value) > 1.0:
return 'background-color: Firebrick'
def bigger_than_val(value):
if float(value) > 0:
return 'background-color: green'
if float(value) < 0:
return 'background-color: Firebrick'
styled_df_fiis = fiis.style
styled_df_fiis = styled_df_fiis.format({
'VALOR BRUTO': 'R${:,.2f}',
'VALORIZAÇÃO': '{:.2f}%',
'COTAÇÃO': 'R$ {:,.2f}',
'DIVIDEND YIELD': '{:.2f}%',
'ULT. RENDIMENTO': 'R$ {:,.2f}',
})
styled_df_fiis = styled_df_fiis.applymap(bigger_than_pvp, subset=['P/VP'])
styled_df_fiis = styled_df_fiis.applymap(bigger_than_val, subset=['VALORIZAÇÃO'])
st.dataframe(styled_df_fiis, use_container_width=True)
st.title('Gráficos Por:')
col1, col2, col3 = st.columns(3)
# Tamanho fixo para as figuras
fig_width = 300
fig_height = 300
with col1:
fig1, ax1 = plt.subplots(figsize=(fig_width / 100, 370 / 100))
# fig1, ax1 = plt.subplots(figsize=(fig_width / 100, fig_height - 30 / 100))
ax1.pie(fiis['VALOR BRUTO'], labels=fiis['DESCRIÇÃO'], autopct='%1.1f%%')
ax1.axis('equal')
st.pyplot(fig1)
with col2:
segmentos = fiis.groupby('SEGMENTO').sum().reset_index()
segmentos.sort_values(by=['VALOR BRUTO'], inplace=True, ascending=False)
fig2, ax2 = plt.subplots(figsize=(fig_width / 100, fig_height / 60.5))
ax2.pie(segmentos["VALOR BRUTO"], labels=segmentos["SEGMENTO"], autopct='%1.1f%%', startangle=90)
ax2.axis('equal')
st.pyplot(fig2)
with col3:
seg_valor = fiis.groupby('SEGMENTO').sum().reset_index().sort_values(by=['VALOR BRUTO'], ascending=False)
st.dataframe(seg_valor[["SEGMENTO", "VALOR BRUTO"]], use_container_width=True)
st.title('Ações')
# filtrando apenas ações
acao = df[df['TIPO DE INVESTIMENTO'] == 'Ação'].copy()
with st.spinner('Loading...'):
df_infos_acao = generate_data_stock(acao['DESCRIÇÃO'].unique())
# dropando colunas que não serão utilizadas
df_infos_acao = df_infos_acao.drop(columns=["p_vp", "dividend_yield_stock"])
# renomeando colunas
df_infos_acao.columns = Stock.get_meaning_of_fields().values()
with st.expander("Visualizar dados brutos"):
st.dataframe(df_infos_acao, use_container_width=True)
# reordenando colunas
df_infos_acao = df_infos_acao[["Nome", "Cotação", "Dividend Yield", "P/VP", "Valorização"]]
# dropando colunas que não serão utilizadas
acao.drop(columns=['TIPO DE INVESTIMENTO'], inplace=True)
# adicionando colunas com informações das ações
acao['VALORIZAÇÃO'] = acao["DESCRIÇÃO"].map(df_infos_acao.set_index("Nome")["Valorização"])
acao["COTAÇÃO"] = acao["DESCRIÇÃO"].map(df_infos_acao.set_index("Nome")["Cotação"])
acao["P/VP"] = acao["DESCRIÇÃO"].map(df_infos_acao.set_index("Nome")["P/VP"])
acao["DIVIDEND YIELD"] = acao["DESCRIÇÃO"].map(df_infos_acao.set_index("Nome")["Dividend Yield"])
# formatando colunas
acao["P/VP"] = acao["P/VP"].str.replace(',', '.')
acao['VALOR BRUTO'] = acao['VALOR BRUTO'].str.replace('.', '')
acao['VALOR BRUTO'] = acao['VALOR BRUTO'].str.replace('R$ ', '')
acao['VALOR BRUTO'] = acao['VALOR BRUTO'].str.replace(',', '.')
acao['VALOR BRUTO'] = acao['VALOR BRUTO'].astype(float)
acao["COTAÇÃO"] = acao["COTAÇÃO"].str.replace(',', '.')
acao["COTAÇÃO"] = acao["COTAÇÃO"].str.replace('R$ ', '')
acao['COTAÇÃO'] = acao['COTAÇÃO'].astype(float)
acao["VALORIZAÇÃO"] = acao["VALORIZAÇÃO"].str.replace(',', '.')
acao["VALORIZAÇÃO"] = acao["VALORIZAÇÃO"].str.replace('%', '')
acao["VALORIZAÇÃO"] = acao["VALORIZAÇÃO"].astype(float)
acao['QUANTIDADE'] = acao['QUANTIDADE'].str.replace(',0', '')
acao['QUANTIDADE'] = acao['QUANTIDADE'].astype(int)
# acao["DIVIDEND YIELD"] = acao["DIVIDEND YIELD"].str.replace(',', '.')
# acao["DIVIDEND YIELD"] = acao["DIVIDEND YIELD"].str.replace('%', '')
# acao["DIVIDEND YIELD"] = acao["DIVIDEND YIELD"].str.replace('-', None)
# acao["DIVIDEND YIELD"] = acao["DIVIDEND YIELD"].astype(float)
# aplicando formatação
styled_df_acao = acao.style
styled_df_acao = styled_df_acao.format({
'VALOR BRUTO': 'R${:,.2f}',
'VALORIZAÇÃO': '{:.2f}%',
'COTAÇÃO': 'R$ {:,.2f}',
# 'DIVIDEND YIELD': '{:.2f}%'
})
styled_df_acao = styled_df_acao.applymap(bigger_than_pvp, subset=['P/VP'])
styled_df_acao = styled_df_acao.applymap(bigger_than_val, subset=['VALORIZAÇÃO'])
st.dataframe(styled_df_acao, use_container_width=True)