File size: 17,266 Bytes
4565b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67d9b92
 
 
 
4565b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from multiprocessing import Value
import matplotlib
matplotlib.use('Agg')

import streamlit as st
from yahooquery import Ticker
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import datetime as dt 
from collections import OrderedDict

import style as style



import scrap as scraping

def flatten(d):
    '''
    Flatten an OrderedDict object
    '''
    result = OrderedDict()
    for k, v in d.items():
        if isinstance(v, dict):
            result.update(flatten(v))
        else:
            result[k] = v
    return result


def comparacao_ativos():
        #código para ativar bootstrap css
        st.markdown(
        """
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        """,unsafe_allow_html=True
        )  
        
        col1, col2,col3 = st.columns([0.1,0.4,0.1])   
        with col2:   
                st.title('Comparação de ativos')
                st.subheader('Escolha 4 ativos para comparar')
                codigo_nome = pd.read_excel('data/classification_b3.xlsx')
                nome_do_ativo1 = st.selectbox('Nome do 1º ativo', (codigo_nome['Código']),key=1 )
                nome_do_ativo2 = st.selectbox('Nome do 2º ativo', (codigo_nome['Código']),key=2)
                nome_do_ativo3 = st.selectbox('Nome do 3º ativo', (codigo_nome['Código']),key=3 )
                nome_do_ativo4 = st.selectbox('Nome do 4º ativo', (codigo_nome['Código']),key=4 )
                style.space(1)
                
        if nome_do_ativo4 != "":
                st.subheader('Analisando os dados')
                nome_do_ativo1 = str(nome_do_ativo1 + '.SA').upper()
                nome_do_ativo2 = str(nome_do_ativo2 + '.SA').upper()
                nome_do_ativo3 = str(nome_do_ativo3 + '.SA').upper()
                nome_do_ativo4 = str(nome_do_ativo4 + '.SA').upper()
                
                df = Ticker([nome_do_ativo1,nome_do_ativo2,nome_do_ativo3,nome_do_ativo4],country='Brazil')
                time = df.history( start='2020-01-01', end = (dt.datetime.today() + dt.timedelta(days=1)).strftime(format='20%y-%m-%d'))
                lista = scraping.get_data()
                todos = pd.DataFrame(flatten(lista).keys()).transpose()
                todos.columns = todos.iloc[0]

                for i in range(len(lista)):
                        todos = pd.concat([todos,pd.DataFrame(lista[i]).transpose()])

                todos = todos.iloc[1:]

                
                todos['P/L'] = todos['P/L'].str.replace('.','')
                todos['DY'] = todos['DY'].str.replace('%','')
                todos['Liq.2m.'] = todos['Liq.2m.'].str.replace('.','')
                todos['Pat.Liq'] = todos['Pat.Liq'].str.replace('.','')
                todos = todos.replace(',','.', regex=True)
                todos = todos.apply(pd.to_numeric,errors='ignore').round(2)
                todos.rename(columns={'cotacao': 'Cotação'}, inplace=True)
                comparar = todos.loc[todos.index.isin([nome_do_ativo1[:5],nome_do_ativo2[:5],nome_do_ativo3[:5],nome_do_ativo4[:5]])]
                
                st.dataframe(comparar)
                # st.dataframe(comparar.style.format({"Cotação": "{:.2f}", "P/L": "{:.2f}", "P/VP": "{:.2f}", "P/Ativo": "{:.2f}"
                # , "P/EBIT": "{:.2f}", "P/Ativ.Circ.Liq.": "{:.2f}", "EBITDA": "{:.2f}", "Liq.Corr.": "{:.2f}", "Liq.2m.": "{:.2f}"
                # , "Pat.Liq": "{:.2f}", "Div.Brut/Pat.": "{:.2f}"                
                #       }))


        # ------------------------------ INÍCIO Comparação DY ---------------

                col1, col2 = st.columns([0.5,0.5])   
                with col1:   
                        layout = go.Layout(title="DY",xaxis=dict(title="Ativo"), yaxis=dict(title="DY %"))
                        fig = go.Figure(layout = layout)
                        fig.add_trace(go.Bar(x=comparar.sort_values('DY',ascending=True).index, y=comparar.sort_values('DY',ascending=True)['DY'] ))
                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ INÍCIO Comparação P/L ---------------
                with col2: 
                        layout = go.Layout(title="P/L",xaxis=dict(title="Ativo"), yaxis=dict(title="P/L"))
                        fig = go.Figure(layout = layout)
                        fig.add_trace(go.Bar(x=comparar.sort_values('P/L',ascending=True).index, y=comparar.sort_values('P/L',ascending=True)['P/L'] ))
                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ INÍCIO Comparação P/V---------------
                with col1:  
                        layout = go.Layout(title="P/VP",xaxis=dict(title="Ativo"), yaxis=dict(title="P/VP"))
                        fig = go.Figure(layout = layout)
                        fig.add_trace(go.Bar(x=comparar.sort_values('P/VP',ascending=True).index, y=comparar.sort_values('P/VP',ascending=True)['P/VP'] ))
                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ INÍCIO Comparação P/L * P/VP---------------

                with col2: 
                        layout = go.Layout(title="P/L X P/VP",xaxis=dict(title="Ativo"), yaxis=dict(title="P/L X P/VP"))
                        fig = go.Figure(layout = layout)
                        fig.add_trace(go.Bar(x=comparar.index, y=comparar['P/L'] * comparar['P/VP'] ))
                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE retorno acumulado---------------------------- 

                periodo_inicio = int(st.number_input(label='periodo retorno acumulado',value=360))

                ret = time.reset_index()
                layout = go.Layout(title="Retorno acumulado",xaxis=dict(title="Data"), yaxis=dict(title="Retorno"))
                fig = go.Figure(layout = layout)
                for i in range(len(ret['symbol'].unique())):
                        fig.add_trace(go.Scatter(x=ret.loc[ret['symbol']==ret['symbol'].unique()[i]][-periodo_inicio:]['date'], y=ret.loc[ret['symbol']==ret['symbol'].unique()[i]][-periodo_inicio:]['close'].pct_change().cumsum(),mode='lines',name=ret.reset_index()['symbol'].unique()[i]))

                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE MÉDIAS MÓVEIS 50---------------------------- 

                rolling_50  = time['close'].rolling(window=50)
                rolling_mean_50 = rolling_50.mean()
                rolling_mean_50 = pd.DataFrame(rolling_mean_50.reset_index())
                # mm50 = time.reset_index()


                layout = go.Layout(title="MÉDIAS MÓVEIS 50",xaxis=dict(title="Data"), yaxis=dict(title="Preço R$"))
                fig = go.Figure(layout = layout)
                for i in range(len(rolling_mean_50['symbol'].unique())):
                        fig.add_trace(go.Scatter(x=rolling_mean_50.loc[rolling_mean_50['symbol']==rolling_mean_50['symbol'].unique()[i]]['date'], y=rolling_mean_50.loc[rolling_mean_50['symbol']==rolling_mean_50['symbol'].unique()[i]]['close'],mode='lines',name=time.reset_index()['symbol'].unique()[i]))

                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE MÉDIAS MÓVEIS 20---------------------------- 

                rolling_50  = time['close'].rolling(window=20)
                rolling_mean_50 = rolling_50.mean()
                rolling_mean_50 = pd.DataFrame(rolling_mean_50.reset_index())
                # mm50 = time.reset_index()


                layout = go.Layout(title="MÉDIAS MÓVEIS 20",xaxis=dict(title="Data"), yaxis=dict(title="Preço R$"))
                fig = go.Figure(layout = layout)
                for i in range(len(rolling_mean_50['symbol'].unique())):
                        fig.add_trace(go.Scatter(x=rolling_mean_50.loc[rolling_mean_50['symbol']==rolling_mean_50['symbol'].unique()[i]]['date'], y=rolling_mean_50.loc[rolling_mean_50['symbol']==rolling_mean_50['symbol'].unique()[i]]['close'],mode='lines',name=time.reset_index()['symbol'].unique()[i]))

                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE volatilidade--------------------------- 
                col1, col2 = st.columns([0.5,0.5])   
                with col1:
                        TRADING_DAYS = 360
                        returns = np.log(time['close']/time['close'].shift(1))
                        returns.fillna(0, inplace=True)
                        volatility = returns.rolling(window=TRADING_DAYS).std()*np.sqrt(TRADING_DAYS)
                        vol = pd.DataFrame(volatility).reset_index()
                        vol = vol.dropna()

                        layout = go.Layout(title=f"Volatilidade",xaxis=dict(title="Data"), yaxis=dict(title="Volatilidade"))
                        fig = go.Figure(layout = layout)
                        for i in range(len(vol['symbol'].unique())):
                                fig.add_trace(go.Scatter(x=vol.loc[vol['symbol']==vol['symbol'].unique()[i]]['date'], y=vol.loc[vol['symbol']==vol['symbol'].unique()[i]]['close'],name=vol['symbol'].unique()[i] ))
                                fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                                fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE sharpe_ratio--------------------------- 
                with col2:
                        sharpe_ratio = returns.mean()/volatility
                        sharpe = pd.DataFrame(sharpe_ratio).reset_index()
                        sharpe = sharpe.dropna()

                        layout = go.Layout(title=f"SHARP (Risco / Volatilidade)",xaxis=dict(title="Data"), yaxis=dict(title="Sharp"))
                        fig = go.Figure(layout = layout)
                        for i in range(len(sharpe['symbol'].unique())):
                                fig.add_trace(go.Scatter(x=sharpe.loc[sharpe['symbol']==sharpe['symbol'].unique()[i]]['date'], y=sharpe.loc[sharpe['symbol']==sharpe['symbol'].unique()[i]]['close'],name=sharpe['symbol'].unique()[i] ))
                                fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                                fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                        st.plotly_chart(fig,use_container_width=True)   

        # ------------------------------ GRÁFICOS DE correlação-------------------------- 
                try:

                        time = time.reset_index()
                        time = time[['symbol','date','close']]
                        df_1 = time.loc[time['symbol'] == time['symbol'].unique()[0]]
                        df_1 = df_1.set_index('date')
                        df_1.columns = df_1.columns.values + '-' + df_1.symbol.unique() 
                        df_1.drop(df_1.columns[0],axis=1,inplace=True)
                        df_2 = time.loc[time['symbol'] == time['symbol'].unique()[1]]
                        df_2 = df_2.set_index('date')
                        df_2.columns = df_2.columns.values + '-' + df_2.symbol.unique() 
                        df_2.drop(df_2.columns[0],axis=1,inplace=True)
                        df_3 = time.loc[time['symbol'] == time['symbol'].unique()[2]]
                        df_3 = df_3.set_index('date')
                        df_3.columns = df_3.columns.values + '-' + df_3.symbol.unique() 
                        df_3.drop(df_3.columns[0],axis=1,inplace=True)
                        df_4 = time.loc[time['symbol'] == time['symbol'].unique()[3]]
                        df_4 = df_4.set_index('date')
                        df_4.columns = df_4.columns.values + '-' + df_4.symbol.unique() 
                        df_4.drop(df_4.columns[0],axis=1,inplace=True)

                        merged = pd.merge(pd.merge(pd.merge(df_1,df_2,left_on=df_1.index,right_on=df_2.index,how='left'),df_3,left_on='key_0',right_on=df_3.index,how='left'),df_4,left_on='key_0',right_on=df_4.index,how='left').rename({'key_0':'date'},axis=1).set_index('date')

                        retscomp = merged.pct_change()

                
                        #plt.figure(figsize=(10,8))

                        #sns.heatmap(retscomp.corr(),annot=True)
                        #st.set_option('deprecation.showPyplotGlobalUse', False)
                        #st.pyplot()

                        import plotly.express as px

                        df = px.data.medals_wide(indexed=True)
                        fig = px.imshow(retscomp.corr(),color_continuous_scale='YlOrRd',labels=dict(x="Correlação"))
                        #fig.update_layout(autosize=False,width=1200,height=800, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
                        fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                        fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')
                        fig.update_xaxes(side="top")

                        st.plotly_chart(fig,use_container_width=True)   
                except:
                        exit

        # ------------------------------ GRÁFICOS DE mapa de risco-------------------------- 

                map = returns.reset_index()
                layout = go.Layout(title=f"Mapa de Risco x Retorno",xaxis=dict(title="Retorno esperado"), yaxis=dict(title="Risco"))
                fig = go.Figure(layout = layout)
                for i in range(len(map['symbol'].unique())):
                        fig.add_trace(go.Scatter(x=[map.loc[map['symbol']==map['symbol'].unique()[i]]['close'].mean() * 100], y=[map.loc[map['symbol']==map['symbol'].unique()[i]]['close'].std() * 100],name=map['symbol'].unique()[i],marker=dict(size=30)))
                #fig.add_trace(go.Scatter(x=[map['close'].mean()], y=[map['close'].std()],text=map['symbol'].unique()))
                fig.update_xaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Red')#, range=[-0.005, 0.01])
                fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Red')#, range=[-0.01, 0.1])
                fig.update_traces(textposition='top center')
                #fig.update_layout(autosize=False,width=800,height=600, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')

                fig.update_layout( height=500, showlegend=False, paper_bgcolor='rgba(255,255,255,0.9)', plot_bgcolor='rgba(255,255,255,0.9)') 
                fig.update_yaxes(showgrid=True, gridwidth=0.1, gridcolor = 'rgb(240,238,238)')

                st.plotly_chart(fig,use_container_width=True)