Пример #1
0
 def Financial_Info(self, asset_id):
     ticker = Ticker(asset_id)
     data = ticker.income_statement()
     data = data.transpose()
     data.columns = data.iloc[0, :]
     data = data.iloc[2:, :-1]
     data = data.iloc[:, ::-1]
     return data
Пример #2
0
def update_earnings(ticker):
    # print("ticker: ", type(ticker))
    # print("ticker: ", ticker.values[0])
    ticker = Ticker(ticker.values[0])
    try:
        earnings = ticker.income_statement().totalRevenue[0]
        return earnings
    except Exception as e:
        print(e)
Пример #3
0
    def fetch(self, dataset: AnyStr, ticker: Ticker, **kwargs):
        if dataset == "income_statement":
            output = ticker.income_statement(frequency=kwargs.get("period"),
                                             trailing=False)
        elif dataset == "cash_flow":
            output = ticker.cash_flow(frequency=kwargs.get("period"),
                                      trailing=False)
        elif dataset == "balance_sheet":
            output = ticker.balance_sheet(frequency=kwargs.get("period"),
                                          trailing=False)
        else:
            return DataFrame()

        if type(output) == DataFrame:
            return output
        else:
            self.logger.warning(output)
            return DataFrame()
Пример #4
0
# https://gist.github.com/rodrigobercini/8bbee7fc735ad7d696f7a2ec31df9610
from yahooquery import Ticker

# Período máximo
petr = Ticker("PETR4.SA")
petr.history(period='max')

# Datas específicas
petr.history(start='2005-05-01', end='2013-12-31')

# Intraday - 30 minutos
abev = Ticker('ABEV3.SA')
abev.history(period='60d', interval="30m")

# Intraday - 1 minuto
abev = abev.history(period='7d', interval="1m")
abev

# Informações financeiras
petr = Ticker("PETR4.SA")  # Coleta dados
petr = petr.income_statement()  # Chama função de Demonstração de resultados
petr = petr.transpose()  # Transpõe a matriz
petr.columns = petr.iloc[0, :]  # Renomeia colunas
petr = petr.iloc[2:, :-1]  # Seleciona dados
petr = petr.iloc[:, ::-1]  # Inverte colunas
petr
Пример #5
0
from yahooquery import Ticker

# financial report
petr = Ticker("PETR4.SA")  # Pick up data
petr = petr.income_statement()  # Call income statement function
petr = petr.transpose()  # Transpor matrix
petr.columns = petr.iloc[0, :]  # Rename columns
petr = petr.iloc[2:, :-1]  # Select data
petr = petr.iloc[:, ::-1]  # Invert columns
print(petr)
Пример #6
0
# Add CSV File Implementation 

df_stocks = pd.read_csv(r"/Users/matthew/Documents/UNI/Coding/Stock.csv")
df_stocks.columns = ["Stocks"]

stocklist = df_stocks["Stocks"].values.tolist()
tickers = Ticker(stocklist)


# Key Stats Data 
keystats = tickers.key_stats
esg_scores = tickers.summary_detail

# Storing the income statement data into a dataframe
df_balance = tickers.balance_sheet().transpose()
df_income = tickers.income_statement().transpose()
df_cashflow = tickers.cash_flow().transpose()
df_keystats = pd.DataFrame.from_dict(keystats)
df_summary = pd.DataFrame(tickers.summary_detail)


# Defining all the empty directories 
all_stat_income = {}
all_stat_balance = {}
all_stat_cashflow = {}
all_stat_keystat = {}
all_stat_summary = {}


# All the data directories 
stats_income = ["asOfDate", "EBIT", "NetIncome", "NetIncomeCommonStockholders"]
Пример #7
0
def main():

    st.sidebar.header("Explorador de ativos")
    n_sprites = st.sidebar.radio("Escolha uma opção",
                                 options=[
                                     "Análise técnica e fundamentalista",
                                     "Comparação de ativos",
                                     "Descobrir novos ativos"
                                 ],
                                 index=0)

    st.sidebar.markdown(
        'É preciso ter paciência e disciplina para se manter firme em suas convicções quando o mercado insiste que você está errado.!'
    )
    st.sidebar.markdown('Benjamin Graham')
    st.sidebar.markdown('Email para contato: [email protected]')
    st.sidebar.markdown('Portfólio: https://github.com/lucasvascrocha')

    # ------------------------------ INÍCIO ANÁLISE TÉCNICA E FUNDAMENTALISTA ----------------------------

    if n_sprites == "Análise técnica e fundamentalista":
        st.image('https://media.giphy.com/media/rM0wxzvwsv5g4/giphy.gif',
                 width=400)
        #image = Image.open('imagens/logo.jpg')
        #st.image(image, use_column_width=True)
        st.title('Análise Técnica e fundamentalista')
        st.subheader('Escolha o ativo que deseja analisar e pressione enter')
        nome_do_ativo = st.text_input('Nome do ativo')

        st.write(
            'Este explorador funciona melhor para ações, porém também suporta alguns fundos imobiliários'
        )
        st.write(
            'Os parâmetros utilizados em grande maioria foram seguindo as teorias de Benjamin Graham'
        )

        if nome_do_ativo != "":
            nome_do_ativo = str(nome_do_ativo + '.SA')
            st.subheader('Analisando os dados')
            df = Ticker(nome_do_ativo, country='Brazil')
            time = df.history(period='max')
            st.dataframe(time.tail())

            # ------------------------------ RESUMO ----------------------------

            resumo = pd.DataFrame(df.summary_detail)
            resumo = resumo.transpose()
            if len(nome_do_ativo) == 8:
                fundamentus = get_specific_data(nome_do_ativo[:5])
                fundamentus = pd.DataFrame([fundamentus])

                pfizer = yf.Ticker(nome_do_ativo)
                info = pfizer.info
                st.title('PERFIL DA EMPRESA')
                st.subheader(info['longName'])
                st.markdown('** Setor **: ' + info['sector'])
                st.markdown('** Atividade **: ' + info['industry'])
                st.markdown('** Website **: ' + info['website'])

                try:
                    fundInfo = {
                        'Dividend Yield (%) -12 meses':
                        round(info['dividendYield'] * 100, 2),
                        'P/L':
                        fundamentus['P/L'][0],
                        'P/VP':
                        fundamentus['P/VP'][0],
                        'Próximo pagamento de dividendo:':
                        (pfizer.calendar.transpose()
                         ['Earnings Date'].dt.strftime('%d/%m/%Y')[0])
                    }
                    fundDF = pd.DataFrame.from_dict(fundInfo, orient='index')
                    fundDF = fundDF.rename(columns={0: 'Valores'})
                    st.subheader('Informações fundamentalistas')
                    st.table(fundDF)
                except:
                    exit

            else:
                st.write(
                    '---------------------------------------------------------------------'
                )
                st.dataframe(resumo)
                pfizer = yf.Ticker(nome_do_ativo)
                info = pfizer.info
                st.title('Company Profile')
                st.subheader(info['longName'])
                try:
                    st.markdown('** Sector **: ' + info['sector'])
                    st.markdown('** Industry **: ' + info['industry'])
                    st.markdown('** Website **: ' + info['website'])
                except:
                    exit

# ------------------------------ GRÁFICOS DE RENDIMENTO ----------------------------

            if len(nome_do_ativo) == 8:

                import datetime
                fundamentalist = df.income_statement()
                fundamentalist['data'] = fundamentalist[
                    'asOfDate'].dt.strftime('%d/%m/%Y')
                fundamentalist = fundamentalist.drop_duplicates('asOfDate')
                fundamentalist = fundamentalist.loc[
                    fundamentalist['periodType'] == '12M']

                #volatilidade
                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.iloc[-360:]).reset_index()

                #sharpe ratio
                sharpe_ratio = returns.mean() / volatility
                sharpe = pd.DataFrame(sharpe_ratio.iloc[-360:]).reset_index()

                div = time.reset_index()
                div['year'] = pd.to_datetime(div['date']).dt.strftime('%Y')
                div_group = div.groupby('year').agg({
                    'close': 'mean',
                    'dividends': 'sum'
                })
                div_group['dividendo(%)'] = round(
                    (div_group['dividends'] * 100) / div_group['close'], 4)

                from plotly.subplots import make_subplots
                fig = make_subplots(
                    rows=3,
                    cols=2,
                    specs=[[{
                        "type": "bar"
                    }, {
                        "type": "bar"
                    }], [{
                        "type": "bar"
                    }, {
                        "type": "bar"
                    }], [{
                        "type": "scatter"
                    }, {
                        "type": "scatter"
                    }]],
                    subplot_titles=("Receita Total", "Lucro", 'Dividendos (%)',
                                    'Dividendos unitário R$', 'Volatilidade',
                                    'Sharpe ratio (Retorno/ Risco)'))

                fig.add_trace(go.Bar(
                    x=pfizer.financials.transpose().index,
                    y=pfizer.financials.transpose()['Total Revenue']),
                              row=1,
                              col=1)

                fig.add_trace(go.Bar(x=pfizer.financials.transpose().index,
                                     y=pfizer.financials.transpose()
                                     ['Net Income From Continuing Ops']),
                              row=1,
                              col=2)

                fig.add_trace(go.Bar(
                    x=div_group.reset_index().tail(5)['year'],
                    y=div_group.reset_index().tail(5)['dividendo(%)']),
                              row=2,
                              col=1)

                fig.add_trace(go.Bar(
                    x=div_group.reset_index().tail(5)['year'],
                    y=div_group.reset_index().tail(5)['dividends']),
                              row=2,
                              col=2)

                fig.add_trace(go.Scatter(x=vol['date'], y=vol['close']),
                              row=3,
                              col=1)

                fig.add_trace(go.Scatter(x=sharpe['date'], y=sharpe['close']),
                              row=3,
                              col=2)

                fig.update_layout(height=800, showlegend=False)

                st.plotly_chart(fig)

            else:
                #volatilidade
                TRADING_DAYS = 160
                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.iloc[-160:]).reset_index()

                #sharpe ratio
                sharpe_ratio = returns.mean() / volatility
                sharpe = pd.DataFrame(sharpe_ratio.iloc[-160:]).reset_index()

                from plotly.subplots import make_subplots
                fig = make_subplots(
                    rows=1,
                    cols=2,
                    specs=[[{
                        "type": "scatter"
                    }, {
                        "type": "scatter"
                    }]],
                    subplot_titles=('Volatilidade',
                                    'Sharpe ratio (Retorno/ Risco)'))

                fig.add_trace(go.Scatter(x=vol['date'], y=vol['close']),
                              row=1,
                              col=1)

                fig.add_trace(go.Scatter(x=sharpe['date'], y=sharpe['close']),
                              row=1,
                              col=2)

                fig.update_layout(height=800, showlegend=False)

                st.plotly_chart(fig)

# ------------------------------ GRÁFICOS DE Candlestick----------------------------

            fig = make_subplots(rows=2,
                                cols=1,
                                shared_xaxes=True,
                                vertical_spacing=0.03,
                                subplot_titles=('OHLC', 'Volume'),
                                row_width=[0.2, 0.7])

            # Plot OHLC on 1st row
            fig.add_trace(go.Candlestick(x=time.reset_index()['date'][-90:],
                                         open=time['open'][-90:],
                                         high=time['high'][-90:],
                                         low=time['low'][-90:],
                                         close=time['close'][-90:],
                                         name="OHLC"),
                          row=1,
                          col=1)

            # Bar trace for volumes on 2nd row without legend
            fig.add_trace(go.Bar(x=time.reset_index()['date'][-90:],
                                 y=time['volume'][-90:],
                                 showlegend=False),
                          row=2,
                          col=1)

            # Do not show OHLC's rangeslider plot
            fig.update(layout_xaxis_rangeslider_visible=False)
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )
            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE Retorno acumulado----------------------------

            layout = go.Layout(title="Retorno acumulado",
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="Retorno"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(
                    x=time.reset_index()['date'][-365:],
                    y=time.reset_index()['close'][-365:].pct_change().cumsum(),
                    mode='lines',
                    line_width=3,
                    line_color='rgb(0,0,0)'))
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE Médias móveis----------------------------

            rolling_50 = time['close'].rolling(window=50)
            rolling_mean_50 = rolling_50.mean()

            rolling_20 = time['close'].rolling(window=20)
            rolling_mean_20 = rolling_20.mean()

            rolling_10 = time['close'].rolling(window=10)
            rolling_mean_10 = rolling_10.mean()

            layout = go.Layout(title="Médias móveis",
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="Preço R$"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-120:],
                           y=time["close"][-120:],
                           mode='lines',
                           line_width=3,
                           name='Real',
                           line_color='rgb(0,0,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-120:],
                           y=rolling_mean_50[-120:],
                           mode='lines',
                           name='MM(50)',
                           opacity=0.6))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-120:],
                           y=rolling_mean_20[-120:],
                           mode='lines',
                           name='MM(20)',
                           opacity=0.6))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-120:],
                           y=rolling_mean_10[-120:],
                           mode='lines',
                           name='MM(10)',
                           opacity=0.6,
                           line_color='rgb(100,149,237)'))
            # fig.add_trace(go.Candlestick(x=time.reset_index()['date'][-120:], open=time['open'][-120:],high=time['high'][-120:],low=time['low'][-120:],close=time['close'][-120:]))
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE Retração de Fibonacci----------------------------

            time_fibo = time.copy()
            periodo_fibonacci = int(
                st.number_input(label='periodo fibonacci', value=90))

            Price_Min = time_fibo[-periodo_fibonacci:]['low'].min()
            Price_Max = time_fibo[-periodo_fibonacci:]['high'].max()

            Diff = Price_Max - Price_Min
            level1 = Price_Max - 0.236 * Diff
            level2 = Price_Max - 0.382 * Diff
            level3 = Price_Max - 0.618 * Diff

            st.write('0% >>' f'{round(Price_Max,2)}')
            st.write('23,6% >>' f'{round(level1,2)}')
            st.write('38,2% >>' f'{round(level2,2)}')
            st.write('61,8% >>' f'{round(level3,2)}')
            st.write('100% >>' f'{round(Price_Min,2)}')

            time_fibo['Price_Min'] = Price_Min
            time_fibo['level1'] = level1
            time_fibo['level2'] = level2
            time_fibo['level3'] = level3
            time_fibo['Price_Max'] = Price_Max

            layout = go.Layout(title=f'Retração de Fibonacci',
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="Preço"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].close,
                    mode='lines',
                    line_width=3,
                    name='Preço real',
                    line_color='rgb(0,0,0)'))
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].Price_Min,
                    mode='lines',
                    line_width=0.5,
                    name='100%',
                    line_color='rgb(255,0,0)',
                ))
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].level3,
                    mode='lines',
                    line_width=0.5,
                    name='61,8%',
                    line_color='rgb(255,255,0)',
                    fill='tonexty',
                    fillcolor="rgba(255, 0, 0, 0.2)"))
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].level2,
                    mode='lines',
                    line_width=0.5,
                    name='38,2%',
                    line_color='rgb(0,128,0)',
                    fill='tonexty',
                    fillcolor="rgba(255, 255, 0, 0.2)"))
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].level1,
                    mode='lines',
                    line_width=0.5,
                    name='23,6%',
                    line_color='rgb(128,128,128)',
                    fill='tonexty',
                    fillcolor="rgba(0, 128, 0, 0.2)"))
            fig.add_trace(
                go.Scatter(
                    x=time_fibo[-periodo_fibonacci:].reset_index()['date'],
                    y=time_fibo[-periodo_fibonacci:].Price_Max,
                    mode='lines',
                    line_width=0.5,
                    name='0%',
                    line_color='rgb(0,0,255)',
                    fill='tonexty',
                    fillcolor="rgba(128, 128, 128, 0.2)"))
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE RSI----------------------------

            periodo_RSI = int(st.number_input(label='periodo RSI', value=90))

            delta = time['close'][-periodo_RSI:].diff()
            up, down = delta.copy(), delta.copy()

            up[up < 0] = 0
            down[down > 0] = 0

            period = 14

            rUp = up.ewm(com=period - 1, adjust=False).mean()
            rDown = down.ewm(com=period - 1, adjust=False).mean().abs()

            time['RSI_' + str(period)] = 100 - 100 / (1 + rUp / rDown)
            time['RSI_' + str(period)].fillna(0, inplace=True)

            layout = go.Layout(title=f'RSI {periodo_RSI}',
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="%RSI"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_RSI:],
                           y=round(time['RSI_14'][-periodo_RSI:], 2),
                           mode='lines',
                           line_width=3,
                           name=f'RSI {periodo_RSI}',
                           line_color='rgb(0,0,0)'))

            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE pivôs----------------------------

            periodo_pivo = int(st.number_input(label='periodo RSI', value=20))

            time['PP'] = pd.Series(
                (time['high'] + time['low'] + time['close']) / 3)
            time['R1'] = pd.Series(2 * time['PP'] - time['low'])
            time['S1'] = pd.Series(2 * time['PP'] - time['high'])
            time['R2'] = pd.Series(time['PP'] + time['high'] - time['low'])
            time['S2'] = pd.Series(time['PP'] - time['high'] + time['low'])

            layout = go.Layout(title=f'Pivô',
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="Preço"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['close'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=3,
                           name=f'preço real',
                           line_color='rgb(0,0,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['PP'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Ponto do pivô',
                           line_color='rgb(0,128,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['R1'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Resistência 1',
                           line_color='rgb(100,149,237)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['S1'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Suporte 1',
                           line_color='rgb(100,149,237)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['R2'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Resistência 2',
                           line_color='rgb(255,0,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_pivo:],
                           y=round(time['S2'][-periodo_pivo:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Suporte 2',
                           line_color='rgb(255,0,0)'))
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE Bolinger----------------------------

            periodo_bolinger = int(
                st.number_input(label='periodo Bolinger', value=180))

            time['MA20'] = time['close'].rolling(20).mean()
            time['20 Day STD'] = time['close'].rolling(window=20).std()
            time['Upper Band'] = time['MA20'] + (time['20 Day STD'] * 2)
            time['Lower Band'] = time['MA20'] - (time['20 Day STD'] * 2)

            layout = go.Layout(title=f'Banda de Bolinger',
                               xaxis=dict(title="Data"),
                               yaxis=dict(title="Preço"))
            fig = go.Figure(layout=layout)
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_bolinger:],
                           y=round(time['Upper Band'][-periodo_bolinger:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Banda superior',
                           line_color='rgb(255,0,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_bolinger:],
                           y=round(time['Lower Band'][-periodo_bolinger:], 2),
                           mode='lines',
                           line_width=1,
                           name=f'Banda inferior',
                           line_color='rgb(255,0,0)',
                           fill='tonexty',
                           fillcolor="rgba(255, 0, 0, 0.1)",
                           opacity=0.2))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_bolinger:],
                           y=round(time['close'][-periodo_bolinger:], 2),
                           mode='lines',
                           line_width=3,
                           name=f'preço real',
                           line_color='rgb(0,0,0)'))
            fig.add_trace(
                go.Scatter(x=time.reset_index()['date'][-periodo_bolinger:],
                           y=round(time['MA20'][-periodo_bolinger:], 2),
                           mode='lines',
                           line_width=2,
                           name=f'MM 20',
                           line_color='rgb(0,128,0)'))
            fig.update_layout(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ Previsões----------------------------

            st.subheader('Previsões')

            st.write(
                'As previsões são feitas levando em conta apenas o movimento gráfico, porém o movimento do preço de um ativo é influenciado por diversos outros fatores, com isso, deve se considerar as previsões como uma hipótese de o preço do ativo variar somente pela sua variação gráfica'
            )

            st.write(
                'Previsão considerando os últimos 365 dias, pode ser entendida como uma tendência dos dados segundo o último ano'
            )

            time = time.reset_index()
            time = time[['date', 'close']]
            time.columns = ['ds', 'y']

            #Modelling
            m = Prophet()
            m.fit(time[-360:])
            future = m.make_future_dataframe(periods=30)
            forecast = m.predict(future[-30:])

            from fbprophet.plot import plot_plotly, plot_components_plotly

            fig1 = plot_plotly(m, forecast)
            st.plotly_chart(fig1)
            #st.plotly_chart(m, forecast)
            fig2 = m.plot_components(forecast)
            st.plotly_chart(fig2)

            #st.write('Previsão considerando as últimas semanas, pode ser entendida como uma tendência dos dados segundo os últimos dias. Leva em consideração diversos fatores como: Índice de força relativa RSI, oscilador estocástico %K, Indicador Willian %R além do movimento gráfico dos últimos dias')

            #predict = stocker.predict.tomorrow(nome_do_ativo)

            #st.write('Previsão para o dia:',f'{predict[2]}','é que a ação feche no valor de: R$',f'{predict[0]}')

            #preço_ontem= round(time['y'][-1:].values[0],2)
            #if predict[0] < preço_ontem:
            #st.write('Previsão para o dia:',f'{predict[2]}','é que a ação caia de ',f'{preço_ontem}', 'para valor de: R$ ',f'{predict[0]}')
            #else:
            #st.write('Previsão para o dia:',f'{predict[2]}','é que a ação suba de ',f'{preço_ontem}', 'para valor de: R$ ',f'{predict[0]}')

# ------------------------------ INÍCIO Comparação de ativos ------------------------------------------------------------------------------------

    if n_sprites == "Comparação de ativos":

        st.image('https://media.giphy.com/media/JtBZm3Getg3dqxK0zP/giphy.gif',
                 width=300)
        #image = Image.open('imagens/logo.jpg')
        #st.image(image, use_column_width=True)
        st.title('Comparação de ativos')
        st.subheader('Escolha até 4 ativos para comparar')
        nome_do_ativo1 = st.text_input('Nome do 1º ativo')
        nome_do_ativo2 = st.text_input('Nome do 2º ativo')
        nome_do_ativo3 = st.text_input('Nome do 3º ativo')
        nome_do_ativo4 = st.text_input('Nome do 4º ativo')

        if nome_do_ativo4 != "":
            st.subheader('Analisando os dados')
            nome_do_ativo1 = str(nome_do_ativo1 + '.SA')
            nome_do_ativo2 = str(nome_do_ativo2 + '.SA')
            nome_do_ativo3 = str(nome_do_ativo3 + '.SA')
            nome_do_ativo4 = str(nome_do_ativo4 + '.SA')

            df = Ticker([
                nome_do_ativo1, nome_do_ativo2, nome_do_ativo3, nome_do_ativo4
            ],
                        country='Brazil')
            time = df.history(
                start='2018-01-01',
                end=(dt.datetime.today() +
                     dt.timedelta(days=1)).strftime(format='20%y-%m-%d'))
            lista = 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')

            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)

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

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

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

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

            # ------------------------------ INÍCIO Comparação P/V---------------

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

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

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

            # ------------------------------ 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(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ 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(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ 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(
                autosize=False,
                width=800,
                height=800,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE volatilidade---------------------------

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE sharpe_ratio---------------------------

            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(
                autosize=False,
                width=800,
                height=400,
            )

            st.plotly_chart(fig)

            # ------------------------------ GRÁFICOS DE correlação--------------------------
            st.subheader('Correlação')
            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.pyplot()

            # ------------------------------ 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,
            )

            st.plotly_chart(fig)

# ------------------------------ INÍCIO Comparação de ativos ------------------------------------------------------------------------------------

    if n_sprites == "Descobrir novos ativos":

        st.image('https://media.giphy.com/media/3ohs4gux2zjc7f361O/giphy.gif',
                 width=400)
        #image = Image.open('imagens/logo.jpg')
        #st.image(image, use_column_width=True)
        st.title('Descobrir novos ativos')

        PL_mínimo = int(st.number_input(label='PL_mínimo', value=10))
        PL_máximo = int(st.number_input(label='PL_máximo', value=15))
        PVP_mínimo = int(st.number_input(label='PVP_mínimo', value=0.7))
        PVP_máximo = int(st.number_input(label='PVP_máximo', value=1.5))
        DY_mínimo = int(st.number_input(label='DY_mínimo', value=4))
        DY_máximo = int(st.number_input(label='DY_máximo', value=30))

        lista = 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')

        if st.checkbox("Filtrar"):

            st.dataframe(todos.loc[(todos['P/L'] >= PL_mínimo)
                                   & (todos['P/L'] <= PL_máximo) &
                                   (todos['P/VP'] >= PVP_mínimo) &
                                   (todos['P/VP'] <= PVP_máximo) &
                                   (todos['DY'] >= DY_mínimo) &
                                   (todos['DY'] <= DY_máximo)])
Пример #8
0
        support_lines = None

    for ta in tas:
        data = Data()
        data.support_lines = ta.resistance_lines("s")
        data.resistance_lines = ta.resistance_lines("r")
        ta.run(run, data)
        plot_ta(ta)
        current_price = ta.data['close'].iloc[-1]
        support = last_resistance_line(ta.resistance_lines('s'))
        support_distance = -(current_price - support) / current_price * 100
        resistance = last_resistance_line(ta.resistance_lines('r'))
        resistance_distance = (resistance -
                               current_price) / current_price * 100
        print("{:4.2f}  {:4.2f}  {:4.2f}  {:4.2f}  {:4.2f}".format(
            current_price, support, support_distance, resistance,
            resistance_distance))
        # plot_ta(ta)
    print(symbol_list)
    print(tickers[0].asset_profile)

    exit(0)
    # Informações financeiras
    petr = Ticker("PETR4.SA")
    petr = petr.income_statement()
    petr = petr.transpose()
    petr.columns = petr.iloc[0, :]
    petr = petr.iloc[2:, :-1]
    petr = petr.iloc[:, ::-1]
    print(petr)
Пример #9
0
    def get(self):
        s = Ticker(self.stock.symbol, timeout=15)

        # all numbers convert to million
        df = s.income_statement(frequency="q")
        if "unavailable" in df or "error" in df:
            logger.error("{}: {}".format(self.stock.symbol, df))
            return

        # DB doesn't like NaN
        df = df.where(pd.notnull(df), 0)

        mapping = {
            "basic_eps":
            "BasicEPS",
            "ebit":
            "EBIT",
            "net_income":
            "NetIncome",
            "normalized_ebitda":
            "NormalizedEBITDA",
            "operating_expense":
            "OperatingExpense",
            "operating_income":
            "OperatingIncome",
            "operating_revenue":
            "OperatingRevenue",
            "pretax_income":
            "PretaxIncome",
            "selling_general_and_administration":
            "SellingGeneralAndAdministration",
            "total_expenses":
            "TotalExpenses",
            "total_revenue":
            "TotalRevenue",
            "tax_rate":
            "TaxRateForCalcs",
            "gross_profit":
            "GrossProfit",
            "general_and_administrative_expense":
            "GeneralAndAdministrativeExpense",
            "research_and_development":
            "ResearchAndDevelopment",
            "selling_and_marketing_expense":
            "SellingAndMarketingExpense",
            "total_operating_income_as_reported":
            "TotalOperatingIncomeAsReported",
            "reconciled_cost_of_revenue":
            "ReconciledCostOfRevenue",
            "cost_of_revenue":
            "CostOfRevenue",
            "interest_expense_non_operating":
            "InterestExpenseNonOperating",
            "interest_income_non_operating":
            "InterestIncomeNonOperating",
            "other_income_expense":
            "OtherIncomeExpense",
            "other_non_operating_income_expenses":
            "OtherNonOperatingIncomeExpenses",
            "tax_provision":
            "TaxProvision",
            "net_income_common_stockholders":
            "NetIncomeCommonStockholders",
            "net_income_from_continuing_and_discontinued_operation":
            "NetIncomeFromContinuingAndDiscontinuedOperation",
            "interest_income":
            "InterestIncome",
            "interest_expense":
            "InterestExpense",
            "net_interest_income":
            "NetInterestIncome",
            "ebitda":
            "EBITDA",
            "reconciled_depreciation":
            "ReconciledDepreciation",
            "net_income_from_continuing_operation_net_minority_interest":
            "NetIncomeFromContinuingOperationNetMinorityInterest",
        }
        # enumerate data frame
        for row in df.itertuples(index=False):
            i, created = IncomeStatement.objects.get_or_create(
                stock=self.stock, on=row.asOfDate.date())

            for key, val in mapping.items():
                try:
                    tmp = float(getattr(row, val))
                except AttributeError:
                    tmp = 0

                # if tmp is a large number, it's unlikely a rate,
                # eg. tax rate, thus convert it to B.
                if abs(tmp) > M:
                    tmp = tmp / B

                # set value
                setattr(i, key, tmp)

            i.save()