Exemplo n.º 1
0
    def get_subj_mess(self, subject, sym=None):

        mess = ""

        if self.stocks is None or sym != self.stocks.iloc[0].sym:
            self.stocks = self.ss.get_trend_slice(table_name=None,
                                                  time_from="-120d",
                                                  time_to="0d",
                                                  symbol=sym)

        curr_price = self.stocks.iloc[-1].close

        # print(self.stocks)

        days_to_earnings = FinI.days_to_earnings(self.earnings)
        sector_mess, spy_mess, vol_mess = self.get_common_mess(self.stocks)

        mess = subject + " " + str(sym) + \
            self.subject_fund_info(self.financials, self.sentiment,
                                   self.earnings, days_to_earnings = days_to_earnings)

        mess += str(sector_mess)
        mess += str(spy_mess)
        mess += str(vol_mess)

        self.stocks = FinI.add_levels(self.stocks)
        # hl = FinI.get_fib_hl(self.stocks,  self.stocks.iloc[-1].close)
        pl = self.stocks.price_level.dropna()
        low, high = FinI.get_nearest_values(pl, curr_price)
        mess += " Price: " + str(curr_price)
        mess += " | Loss: " + str(Utils.calc_perc(
            curr_price,
            low[0])) + "%, " if low is not None and len(low) > 0 else ""
        mess += " " + "Prof.: " + str(Utils.calc_perc(
            curr_price,
            high[0])) + "% " if high is not None and len(high) > 0 else " | "

        print("get_subj_mess() - done")
        return mess, curr_price, days_to_earnings
Exemplo n.º 2
0
Arquivo: sl.py Projeto: 4crash/4crash
    def price_graph(self, option, df, m_df_spy=None, ax="y2"):
        st.write("Days chart: " + str(self.time_from))

        sets = [{
            'x': df.index,
            'open': df.open,
            'close': df.close,
            'high': df.high,
            'low': df.low,
            'yaxis': ax,
            "hovertext": "",
            'type': 'candlestick'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'green'
            },
            'name': 'Boll'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll + df.boll_2,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll + df.boll_3,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        # sets += [{'x': df.index, 'y': df.boll + df.boll_5, 'yaxis':  ax, 'type': 'scatter',
        #           'mode': 'lines', 'line': {'width': 0.3, 'color': 'green'}, 'name': '-'}]

        sets += [{
            'x': df.index,
            'y': df.boll + df.boll_6,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll - df.boll_2,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll - df.boll_3,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        # sets += [{'x': df.index, 'y': df.boll - df.boll_5, 'yaxis':  ax, 'type': 'scatter',
        #           'mode': 'lines', 'line': {'width': 0.3, 'color': 'green'}, 'name': '-'}]

        sets += [{
            'x': df.index,
            'y': df.boll - df.boll_6,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.3,
                'color': 'green',
                "dash": "dot"
            },
            'name': '-'
        }]

        sets += [{
            'x': df.index,
            'y': df.boll_ub,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'green'
            },
            'name': 'Boll UP'
        }]
        sets += [{
            'x': df.index,
            'y': df.boll,
            'yaxis': ax,
            'type': 'scatter',
            "fill": 'tonexty',
            'line': {
                'width': 0,
            },
            "fillcolor": 'rgba(128, 255, 128,0.2)'
        }]
        sets += [{
            'x': df.index,
            'y': df.boll_lb,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'green'
            },
            'name': 'Boll Down'
        }]
        sets += [{
            'x': df.index,
            'y': df.boll,
            'yaxis': ax,
            'type': 'scatter',
            "fill": 'tonexty',
            'line': {
                'width': 0,
            },
            "fillcolor": 'rgba(255, 128, 128,0.2)'
        }]

        sets += [{
            'x': df.index,
            'y': df.sma9,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'blue'
            },
            'name': 'sma9'
        }]

        sets += [{
            'x': df.index,
            'y': df.sma50,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'darkblue'
            },
            'name': 'sma50'
        }]

        sets += [{
            'x': df.index,
            'y': df.sma100,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'orange'
            },
            'name': 'sma100'
        }]

        df = FinI.add_levels(df)
        for i, r in df.iterrows():
            if r.price_level is not None:
                sets += [{
                    'x': [i, df.iloc[-1].name],
                    'y': [r.price_level, r.price_level],
                    'yaxis': ax,
                    'type': 'scatter',
                    'mode': 'lines',
                    'line': {
                        'width': 1,
                        'color': 'brown',
                        "dash": "dot"
                    },
                    'name': ''
                }]
        # print(levels)
        # sets += [{'x': df.index, 'y': df.price_level, 'yaxis':  ax, 'type': 'scatter',
        #           'mode': 'lines', 'line': {'width': 1, 'color': 'orange'}, 'name': 'sma100'}]

        # data += PlotP.plot_rsi(df, ax="y2")
        # data += PlotP.plot_macd_boll(df=df, ax="y3")
        # st.area(df, x=df.index, y=df.boll, color="continent",
        #         line_group="country")
        # fig = go.Figure(data=sets)
        fig = make_subplots(rows=3,
                            cols=1,
                            shared_xaxes=True,
                            vertical_spacing=0.009,
                            horizontal_spacing=0.009,
                            row_width=[0.2, 0.1, 0.5],
                            specs=[[{
                                "secondary_y": True
                            }], [{
                                "secondary_y": True
                            }], [{
                                "secondary_y": True
                            }]])
        fig.add_traces(data=sets, cols=1, rows=1)
        if m_df_spy is not None:
            fig.add_trace(
                {
                    'x': m_df_spy.index,
                    'y': m_df_spy.oc_mean,
                    'type': 'scatter',
                    'yaxis': "y",
                    'mode': 'lines',
                    'line': {
                        'width': 1,
                        'color': 'red'
                    },
                    'name': 'SPY'
                },
                1,
                1,
                secondary_y=True,
            )

        # data = PlotP.plot_rsi(df, ax="y")
        # fig.add_traces(data, 3, 1)
        mb = PlotP.plot_macd_boll(df=df, ax="y")
        fig.add_traces(
            data=mb,
            rows=3,
            cols=1,
        )

        rsi = PlotP.plot_rsi(df=df, ax="y3")
        fig.add_traces(data=rsi,
                       rows=3,
                       cols=1,
                       secondary_ys=[True, True, True, True, True])

        df.loc[df.open > df.close, "vol_color"] = "red"
        df.loc[df.open <= df.close, "vol_color"] = "green"
        # print(df.vol_color)

        fig.add_trace(
            {
                'x': df.index,
                'y': df.volume,
                'type': 'bar',
                'name': 'Volume',
                'marker_color': df.vol_color
            },
            2,
            1,
            secondary_y=False,
        )

        # fig['layout']['margin'] = {'l': 30, 'r': 10, 'b': 50, 't': 25}
        fig.update_yaxes(showgrid=True,
                         zeroline=False,
                         showticklabels=True,
                         showspikes=True,
                         spikemode='across',
                         spikesnap='cursor',
                         showline=True,
                         spikedash='dash',
                         spikethickness=0.5)

        fig.update_xaxes(showgrid=True,
                         zeroline=False,
                         rangeslider_visible=False,
                         showticklabels=True,
                         showspikes=True,
                         showline=True,
                         spikemode='across',
                         spikesnap='cursor',
                         spikedash='dash',
                         spikethickness=0.5)

        fig.update_layout(
            autosize=True,
            height=600,
            hoverdistance=1,
            hovermode='y',
            spikedistance=10000,
        )
        st.plotly_chart(
            fig,
            use_container_width=True,
            use_container_height=True,
            template="plotly_dark",
        )