Example #1
0
    def set_prices(self,
                   sym=None,
                   last_date=None,
                   time_from="-90d",
                   time_to="0d"):
        if last_date:
            self.db.last_date = last_date

        self.in_day_stocks = sdf.retype(self.db.get_last_n_days(sym))

        self.stocks = sdf.retype(
            self.db.load_data(TableName.DAY,
                              sym,
                              time_from=time_from,
                              time_to=time_to))

        self.stocks = FinI.add_indicators(sdf.retype(self.stocks))

        self.stocks = FinI.add_fib(self.stocks, last_rows=10)

        self.spy = sdf.retype(
            self.db.load_data(TableName.DAY, ["SPY"],
                              time_from=time_from,
                              time_to=time_to))
        print("set_prices() - done")
Example #2
0
    def plot_weeks(ax, df):

        df = FinI.add_weekday(df)

        # print(df)
        # ax.axvspan(date2num(datetime(2020,11,9)), date2num(datetime(2020,11,12)),
        #    label="March", color="crimson", alpha=0.3)
        color = "gray"
        for val in range(0, len(df) - 1):

            # in progress version with hollidays
            last_week_day = None
            first_week_day = None
            weekend_percents = 0
            week_percents = 0

            try:
                if df.iloc[val].weekday != (df.iloc[val + 1].weekday - 1):
                    last_week_day = df.iloc[val].weekday
                    weekend_percents = Utils.calc_perc(df.iloc[val].close,
                                                       df.iloc[val + 1].open)
                    ax.text(date2num(df.iloc[val].date),
                            ax.get_ylim()[1],
                            str(round(weekend_percents, 1)) + '%',
                            fontsize=8,
                            color="green" if weekend_percents >= 0 else "red")

                if (df.iloc[val - 1].weekday - df.iloc[val].weekday) > 1:
                    first_week_day = df.iloc[val].weekday

                    days_to_end = 1
                    while (df.iloc[val + days_to_end].weekday -
                           df.iloc[val].weekday) > 0:
                        days_to_end += 1

                    week_percents = Utils.calc_perc(
                        df.iloc[val].open,
                        df.iloc[val + days_to_end - 1].close)
                    color = "green" if week_percents >= 0 else "red"

                    # change in week
                    ax.text(date2num(df.iloc[val].date),
                            ax.get_ylim()[0],
                            str(round(week_percents, 1)) + '%',
                            fontsize=8,
                            color=color)

                    ax.axvspan(date2num(df.iloc[val].date),
                               date2num(df.iloc[val + days_to_end - 1].date),
                               alpha=0.05,
                               color=color)

                    # print(df.iloc[val].date.day_name() + " - " + df.iloc[val+days_to_end-1].date.day_name())

            except IndexError:
                print("plot_weeks Index error")

        return ax
Example #3
0
    def show_sym_bs_stats(self, sym: str):

        self.db.limit = None
        dfp, financials, sentiment, earnings, spy = self.db.get_all_data(
            self.db.time_from, sym)
        dfp = FinI.add_indicators(dfp)
        dfp = FinI.add_fib(dfp, last_rows=10)
        # print(dfp)
        # last 5 financials
        financials = financials.tail(5)
        days_to_earnings = FinI.days_to_earnings(earnings)
        # print(earnings)
        if days_to_earnings is None:
            earnings = None
        fig, axs = plt.subplots(2, 1, figsize=(16, 16))
        PlotI.set_margins(plt)

        PlotI.plot_spy(axs[0], spy.loc[spy.index.isin(dfp.index)])
        axsp = PlotI.plot_stock_prices(axs[0].twinx(), dfp, sym, 0.5)
        axsp = PlotI.plot_sma(axsp, dfp, ["sma9", "sma50"])
        axsp.xaxis.set_major_formatter(plt.NullFormatter())
        axsp = PlotI.plot_candles(dfp,
                                  axsp,
                                  body_w=0.5,
                                  shadow_w=0.1,
                                  alpha=0.5)
        axsp = PlotI.plot_fib(dfp, axsp, alpha=0.4)
        axsp = PlotI.plot_fib(dfp, axsp, alpha=0.4, fib_name="fb_mid")
        axsp = PlotI.plot_fib(dfp, axsp, alpha=0.4, fib_name="fb_bot")
        axsp = PlotI.plot_weeks(axsp, dfp)

        PlotI.plot_boll(axsp, dfp, sym)

        axsp2 = PlotI.plot_volume_bars(axsp.twinx(), dfp)

        axsp2 = PlotI.plot_rsi(axs[1], dfp)
        axsp2 = PlotI.plot_macd_boll(axs[1].twinx(), dfp)
        # ax_macd = PlotI.plot_macd(ax_macd,dfp)
        axsp2 = PlotI.plot_volume_bars(axs[1].twinx(), dfp)

        if self.buy_sell_closed is not None and len(self.buy_sell_closed) > 0:
            axsp = PlotI.plot_bs(axsp, self.buy_sell_closed)
            axsp2 = PlotI.plot_bs(axsp2.twinx(), self.buy_sell_closed)

        plt.show()
Example #4
0
File: sl.py Project: 4crash/4crash
    async def get_inday_price_graph(self, sym=None):

        if sym is None:
            sym = self.selected_stock

        inday_df = self.db.get_last_n_days(sym,
                                           n_days_back=self.inday_days,
                                           table=TableName.MIN15)

        inday_df = FinI.add_indicators(sdf.retype(inday_df))

        await self.inday_price_graph(sym, inday_df)
Example #5
0
    def up_down_stats(self):

        m_df = self.db.load_data(table_name=TableName.DAY,
                                 time_from=self.time_from,
                                 symbols=[self.selected_stock])

        m_df_spy = self.db.load_data(table_name=TableName.DAY,
                                     time_from=self.time_from,
                                     symbols=["SPY"])

        m_df, m_df_spy = FinI.get_sizes(m_df, m_df_spy)

        self.candle_chart(m_df, m_df_spy)
        # above_sma9, under_sma9, above_boll,under_boll = {"1":0,"2":0,"3":0,"4":0,"5":0,"6":0}
        ind = pd.DataFrame()
        ws = pd.DataFrame()
        u = 0
        pred = pd.DataFrame()

        for index, row in m_df.iterrows():

            if row.weekday == 0:
                i = 1

                while m_df.iloc[
                        m_df.index.get_loc(index) +
                        i].weekday == m_df.iloc[m_df.index.get_loc(index) + 1 +
                                                i].weekday - 1:
                    rw = m_df.iloc[m_df.index.get_loc(index) + i]

                    st.write(str(rw.date) + " - " + str(rw.size_body))
                    i += 1
            st.write(row.week_in_month)

            if True or row.size_sma9 > 0 and row.size_boll > 0:
                if row.size_body > 0:
                    # st.write(u)
                    if u < 0:
                        ind = ind.append([u])
                        u = 0

                    u += 1
                elif row.size_body < 0:
                    if u > 0:
                        ind = ind.append([u])
                        #   st.write(ind)
                        u = 0

                    u -= 1
        #
        st.dataframe(ind)
        st.dataframe(ind.groupby(ind.columns[0]).size())
Example #6
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
Example #7
0
    def plot_macd_boll(ax, df):
        """
        docstring
        """

        # df = self.add_bollinger_bands(df)
        if "boll" not in df or "macd" not in df:
            df = FinI.add_indicators(df)

        df.plot(kind="line",
                use_index=True,
                y=["macd", "boll_lb_macd", "boll_ub_macd"],
                legend=False,
                ax=ax,
                grid=True,
                linewidth=0.7,
                color=["black", "red", "green"],
                alpha=0.7)

        ax.fill_between(df.index,
                        df.boll_lb_macd,
                        df.macd,
                        where=df.boll_lb_macd > df.macd,
                        facecolor='red',
                        interpolate=True,
                        alpha=0.3)
        ax.fill_between(df.index,
                        df.boll_ub_macd,
                        df.macd,
                        where=df.boll_ub_macd < df.macd,
                        facecolor='green',
                        interpolate=True,
                        alpha=0.3)

        ax.fill_between(df.index,
                        df.boll_ub_macd,
                        df.boll_lb_macd,
                        facecolor='blue',
                        interpolate=True,
                        alpha=0.3)

        for tick in ax.get_xticklabels():
            tick.set_rotation(0)

        return ax
Example #8
0
    def prepare_data_for_ml(self):

        m_df = self.db.load_data(table_name=TableName.DAY,
                                 time_from=self.time_from,
                                 symbols=[self.selected_stock])

        m_df_spy = self.db.load_data(table_name=TableName.DAY,
                                     time_from=self.time_from,
                                     symbols=["SPY"])

        m_df, m_df_spy = FinI.get_sizes(m_df, m_df_spy)

        st.dataframe(m_df)
        self.process_data(m_df)
        self.candle_chart(m_df, m_df_spy)
        self.show_sizes(m_df)
        self.get_heatmap(m_df)
        self.nn_pred(m_df)
Example #9
0
File: sl.py Project: 4crash/4crash
    async def get_price_detail(self, sym=None):

        if sym is None:
            sym = self.selected_stock
        time_from = self.set_time_to()
        df = self.db.load_data(
            table_name=TableName.DAY,
            symbols=sym,
            time_from=time_from,
            time_to=self.time_to,
        )
        m_df_spy = self.db.load_data(table_name=TableName.DAY,
                                     time_from=time_from,
                                     time_to=self.time_to,
                                     symbols=["SPY"])
        m_df_spy["oc_mean"] = (m_df_spy.close + m_df_spy.open) / 2
        # self.stocks = FinI.add_change(self.stocks)
        df = FinI.add_indicators(sdf.retype(df))
        await self.price_graph(sym, df, m_df_spy=m_df_spy)

        # self.macd_rsi_graph(option, df)
        self._max_width_()
Example #10
0
    def plot_volume_bars(ax, df, width=0.85):
        """
        docstring
        """
        import numpy as np

        # pricesup = df[df.close >= df.open]
        # pricesdown = df[df.close < df.open]
        df = FinI.add_oc_change(df)
        # alpha = float((df.ch_oc / 4) * 0.1)
        # df['alpha'] = float((df.ch_oc / 4) * 0.1)
        # print(df["ch_oc"])
        # ax.bar(pricesup.index, pricesup.volume, width,
        #        bottom=0, color='g', alpha=rgba_colors)
        # ax.bar(pricesdown.index, pricesdown.volume,
        #        width, bottom=0, color='r', alpha=rgba_colors)
        for index, row in df.iterrows():
            # print(str())
            color = "g" if row.open < row.close else "r"
            alpha = Utils.zero_one_vals(df.ch_oc.min(), df.ch_oc.max(),
                                        row.ch_oc)
            alpha = alpha if row.open < row.close else abs(alpha - 1)
            ax.bar(index,
                   row.volume,
                   width,
                   bottom=0,
                   color=color,
                   alpha=float(alpha))
        # df.plot(kind='line', linestyle="o",
        #                 y=["volume"], ax=ax, grid=True, linewidth=0.5, color=["green"])
        # ax.fill_between(df.index, df.macds, df.macd, where=df.macds > df.macd,
        #                     facecolor='orange', interpolate=True, alpha=0.2)
        # ax.fill_between(df.index, df.macds, df.macd, where=df.macd >= df.macds,
        #                     facecolor='green', interpolate=True, alpha=0.2)
        for tick in ax.get_xticklabels():
            tick.set_rotation(0)

        return ax
Example #11
0
File: sl.py Project: 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",
        )
Example #12
0
    def get_fund_mess(self,
                      financials,
                      curr_price,
                      earnings,
                      sentiment,
                      days_to_earnings,
                      day_stocks=None):
        mess = ""
        mess += ''.join("Detail | ")
        financials.sort_values(by=["date"], inplace=True, ascending=True)
        if len(financials) > 0:
            cols = [
                "date", "shortRatio", "sharesShortPriorMonth",
                "fiftyDayAverage", "beta", "dividendRate", "exDividendDate",
                "priceToSalesTrailing12Months", "enterpriseToRevenue",
                "profitMargins", "enterpriseToEbitda", "trailingEps",
                "forwardEps", "priceToBook", "bookValue", "pegRatio",
                "earningsQuarterlyGrowth", "bid", "volume", "trailingPE",
                "forwardPE", "heldPercentInstitutions", "heldPercentInsiders"
            ]

            mess += ''.join([
                "T_EPS->F_EPS: ",
                str(
                    Utils.calc_perc(financials.iloc[0].trailingEps,
                                    financials.iloc[0].forwardEps)), "%\n\r",
                "T_PE->F_PE: ",
                str(
                    Utils.calc_perc(financials.iloc[0].trailingPE,
                                    financials.iloc[0].forwardPE)) + "%\n\r"
            ])

            for item in cols:

                # financials[item].dropna(inplace=True)

                first = financials.iloc[0][item]
                last = financials.iloc[-1][item]

                mess += ''.join([
                    item + ": ",
                    str(Utils.human_format(first)),
                    " -> ",
                    str(Utils.human_format(last)),
                    " | ",
                    str(Utils.calc_perc(first, last)),
                    "%\n\r",
                ])

            mess += ''.join([
                str(financials.iloc[0].sector), ' | ',
                str(financials.iloc[0].industry), "\n\r",
                str("Current Price: "), ' | ',
                str(curr_price), "\n\r",
                str("Days to earn.: "), ' | ',
                str(days_to_earnings), " D", "\n\r"
            ])

        if earnings is not None and len(earnings) > 0:
            mess += str("Earn. est. | act. | surp.:  ") + str(earnings.iloc[-1].epsestimate) + \
                ' | ' + str(earnings.iloc[-1].epsactual) + \
                ' | ' + str(earnings.iloc[-1].epssurprisepct) + "\n\r"
        if sentiment is not None and len(sentiment) > 0:
            mess += str("Sentiment article/title: ") + str(sentiment.sentiment_summary_avg.to_list()) + \
                '/' + str(sentiment.sentiment_title_avg.to_list()) if sentiment is not None and len(sentiment)>0 else str("Sentiment: NaN ") + "\n\r"
        if financials is not None and len(financials) > 0:
            mess += str(financials.iloc[0].longBusinessSummary) + "\n\r"

        sector_mess, spy_mess, vol_mess = self.get_common_mess(self.stocks)
        mess += "\n\r" + sector_mess
        mess += "\n\r" + spy_mess
        mess += "\n\r" + vol_mess

        hl = FinI.get_fib_hl(self.stocks, curr_price)
        mess += "\n\r" + "Loss: " + str(Utils.calc_perc(curr_price, hl["l"])) + "% " + "  " + str(hl['l']) +" | "+\
            " Price: " + str(curr_price) + \
            " | " + "Profit: " + str(hl['h']) + \
            "  " + str(Utils.calc_perc(curr_price, hl["h"])) + "% \n\r"

        mess += self.get_fib_mess(self.stocks, curr_price) + "\n\r"
        print("get_fund_mess() - done")
        return mess
Example #13
0
    def show_sym_stats(self, sym, save_img=False):

        # last  financials
        if self.financials is not None and len(self.financials) > 0:
            self.financials = self.financials.tail(5)
        days_to_earnings = FinI.days_to_earnings(self.earnings)
        # print(earnings)
        if days_to_earnings is None:
            self.earnings = None
        plots_num = 4 if save_img else 5

        fig, axs = plt.subplots(plots_num, 1, figsize=(16, 18))
        PlotI.set_margins(plt)

        if self.spy is not None:
            PlotI.plot_spy(
                axs[0], self.spy.loc[self.spy.index.isin(self.stocks.index)])

        axsp = PlotI.plot_stock_prices(axs[0].twinx(),
                                       self.stocks,
                                       sym,
                                       alpha=0.3)
        axsp = PlotI.plot_sma(axsp, self.stocks, ["sma9", "sma50"])
        axsp = PlotI.plot_candles(self.stocks,
                                  axsp,
                                  body_w=0.5,
                                  shadow_w=0.1,
                                  alpha=0.5)
        axsp = PlotI.plot_fib(self.stocks, axsp, alpha=0.4)
        axsp = PlotI.plot_fib(self.stocks, axsp, alpha=0.4, fib_name="fb_mid")
        axsp = PlotI.plot_fib(self.stocks, axsp, alpha=0.4, fib_name="fb_bot")

        PlotI.plot_boll(axsp, self.stocks, sym)
        PlotI.plot_weeks(axsp, self.stocks)

        PlotI.plot_rsi(axs[1], self.stocks)
        ax_macd = PlotI.plot_macd_boll(axs[1].twinx(), self.stocks)
        # ax_macd = PlotI.plot_macd(ax_macd,dfp)

        PlotI.plot_volume_bars(axs[1].twinx(), self.stocks)

        if self.in_day_stocks is None:
            self.in_day_stocks = sdf.retype(
                self.db.load_data(TableName.MIN15,
                                  sym,
                                  time_from="-1d",
                                  time_to="0d"))
            if len(self.in_day_stocks) < 1:
                self.in_day_stocks = sdf.retype(
                    self.db.load_data(TableName.MIN15, sym, limit=20))

        # self.plot_volume(axs[2], last_prices)

        ax = PlotI.plot_candlesticks2(axs[2], self.in_day_stocks)
        self.in_day_stocks = FinI.add_fib_from_day_df(self.in_day_stocks,
                                                      self.stocks)
        ax = PlotI.plot_fib(self.in_day_stocks,
                            axs[2],
                            alpha=0.4,
                            fib_name="fb_mid")
        # PlotI.plot_boll(ax, last_prices, sym)

        sectors = self.ss.sectors_day_stats()
        PlotI.plot_sector_stats(axs[3], sectors, self.stocks.iloc[0].sector)
        if (save_img):
            return plt

        # self.plot_spy(axs[2], self.spy)
        #self.plot_yahoo_candles(last_prices)
        # self.plot_volume(axs[2], last_prices)
        # set rotation of tick labels

        axs[3].text(0.02,
                    0.9,
                    str(self.financials.beta.name) + ' | ' +
                    str(self.financials.beta.to_list()),
                    fontsize=8)
        axs[3].text(
            0.02,
            0.8,
            str(self.financials.priceToSalesTrailing12Months.name) + ' | ' +
            str(self.financials.priceToSalesTrailing12Months.to_list()),
            fontsize=8)
        axs[3].text(0.02,
                    0.7,
                    str(self.financials.enterpriseToRevenue.name) + ' | ' +
                    str(self.financials.enterpriseToRevenue.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.6,
                    str(self.financials.profitMargins.name) + ' | ' +
                    str(self.financials.profitMargins.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.5,
                    str(self.financials.enterpriseToEbitda.name) + ' | ' +
                    str(self.financials.enterpriseToEbitda.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.4,
                    str(self.financials.trailingEps.name) + ' | ' +
                    str(self.financials.trailingEps.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.3,
                    str(self.financials.forwardEps.name) + ' | ' +
                    str(self.financials.forwardEps.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.2,
                    str(self.financials.priceToBook.name) + ' | ' +
                    str(self.financials.priceToBook.to_list()),
                    fontsize=8)
        axs[3].text(0.02,
                    0.1,
                    str(self.financials.bookValue.name) + ' | ' +
                    str(self.financials.bookValue.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.9,
                    str(self.financials.shortRatio.name) + ' | ' +
                    str(self.financials.shortRatio.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.8,
                    str(self.financials.sharesShortPriorMonth.name) + ' | ' +
                    str(self.financials.sharesShortPriorMonth.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.7,
                    str(self.financials.pegRatio.name) + ' | ' +
                    str(self.financials.pegRatio.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.6,
                    str(self.financials.earningsQuarterlyGrowth.name) + ' | ' +
                    str(self.financials.earningsQuarterlyGrowth.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.5,
                    str(self.financials.bid.name) + ' | ' +
                    str(self.financials.bid.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.4,
                    str(self.financials.trailingPE.name) + ' | ' +
                    str(self.financials.trailingPE.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.3,
                    str(self.financials.forwardPE.name) + ' | ' +
                    str(self.financials.forwardPE.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.2,
                    str(self.financials.industry.to_list()) + ' | ' +
                    str(self.financials.sector.to_list()),
                    fontsize=8)
        axs[3].text(0.4,
                    0.1,
                    str(self.financials.heldPercentInstitutions.name) + ' | ' +
                    str(self.financials.heldPercentInstitutions.to_list()) +
                    ' ||| ' + str(self.financials.heldPercentInsiders.name) +
                    ' | ' + str(self.financials.heldPercentInsiders.to_list()),
                    fontsize=8)
        axs[3].text(0.6,
                    0.9,
                    str(self.financials.fiftyDayAverage.name) + ' | ' +
                    str(self.financials.fiftyDayAverage.to_list()),
                    fontsize=8)
        axs[3].text(0.6,
                    0.7,
                    str("Last CLose Price: ") + ' | ' +
                    str(self.in_day_stocks.iloc[-1].close),
                    fontsize=8)
        axs[3].text(
            0.6,
            0.5,
            str("Days to earn.: ") + ' | ' + str(days_to_earnings.days) +
            " D" if self.earnings is not None else str("Days to earn.: NaN "),
            fontsize=8)
        axs[3].text(0.6,
                    0.4,
                    str("Earn. est. | act. | surp.:  ") +
                    str(self.earnings.iloc[-1].epsestimate) + ' | ' +
                    str(self.earnings.iloc[-1].epsactual) + ' | ' +
                    str(self.earnings.iloc[-1].epssurprisepct)
                    if self.earnings is not None else str("Earn est.: NaN "),
                    fontsize=8)
        axs[3].text(0.6,
                    0.3,
                    str("  Sentiment article/title: ") +
                    str(self.sentiment.sentiment_summary_avg.to_list()) + '/' +
                    str(self.sentiment.sentiment_title_avg.to_list())
                    if self.sentiment is not None and len(self.sentiment) > 0
                    else str("  Sentiment: NaN "),
                    fontsize=8)

        axs[3].text(0.02,
                    0.01,
                    str(self.financials.longBusinessSummary.to_list()),
                    fontsize=8)

        # self.plot_candlesticks(last_prices)
        # axs[3].plot([2], [1], 'o')
        # plt.text()

        # plt.show()
        return plt
Example #14
0
    def plot_macd_boll(df=None, ax="y"):
        """
        docstring
        """
        out = []
        # df = self.add_bollinger_bands(df)
        if "boll" not in df or "macd" not in df:
            df = FinI.add_indicators(df)

        out += [{
            'x': df.index,
            'y': df.macd,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 1,
                'color': 'rgba(128, 128, 256,1)'
            },
            'name': 'Macd'
        }]

        out += [{
            'x': df.index,
            'y': df.boll_lb_macd,
            'yaxis': ax,
            'type': 'scatter',
            'line': {
                'width': 0,
                'color': 'rgba(256, 128, 128,0.1)'
            },
            "fill": 'tonexty',
            "fillcolor": 'rgba(128, 128, 128,0.2)'
        }]

        out += [{
            'x': df.index,
            'y': df.boll_ub_macd,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.6,
                'color': 'rgba(128, 128, 256,0.6)'
            },
            'name': 'bollup'
        }]

        # out += [{'x': df.index, 'y': df.macd, 'yaxis':  ax,
        #          'type': 'scatter', "fill": 'tonexty', "fillcolor": 'rgba(51, 204, 51,0.3)'}]

        out += [{
            'x': df.index,
            'y': df.boll_lb_macd,
            'yaxis': ax,
            'type': 'scatter',
            'mode': 'lines',
            'line': {
                'width': 0.6,
                'color': 'rgba(128, 128, 256,0.6)'
            },
            'name': 'boll dwn'
        }]

        out += [{
            'x': df.index,
            'y': df.boll_ub_macd,
            'yaxis': ax,
            'type': 'scatter',
            'line': {
                'width': 0,
                'color': 'rgba(128, 128, 256,0.6)'
            },
            "fill": 'tonexty',
            "fillcolor": 'rgba(128, 128, 128,0.2)'
        }]

        # print(out)
        return out
Example #15
0
    def buy_sell(self, stocks_day, stocks_15, spy, spy_row15, spy_row_day):
        """ By sell backtrading simaltion with specific strategy

        Args:
            stocks_day ([type]): [description]
            stocks_15 ([type]): [description]
            spy ([type]): [description]
            spy_row15 ([type]): [description]
            spy_row_day ([type]): [description]
        """

        # self.stocks = self.stocks.append(stocks_day.iloc[-1])
        self.db.last_date = stocks_15.iloc[-1].name
        logging.info(self.db.last_date)
        sym = stocks_day.iloc[-1].sym
        # send only one buy suggestion per day
        hash_warn = hash(sym + str(stocks_15.index[-1].strftime("%d/%m/%Y")))

        hl = FinI.get_fib_hl(stocks_day, stocks_day.iloc[-1].close)

        # logging.info(str(stocks_day))
        if len(stocks_day) > 1:
            # stocks_day = sdf.retype(stocks_day)
            # stocks_day = FinI.add_indicators(stocks_day)
            stocks_day.sort_index(ascending=True, inplace=True)
            stocks_day["flpd"] = Utils.calc_flpd(stocks_day)
            hl = FinI.get_fib_hl(stocks_day, stocks_15.iloc[-1].close)
            #OLD CHECK SELL moved to this Fce
            # self.check_sell(stocks_15.iloc[-1])

            earnings, sentiment, financials = self.db.get_fundamentals(
                stocks_day.iloc[-1]["sym"])
            self.set_mess_data(fin=financials,
                               sen=sentiment,
                               earn=earnings,
                               spy=spy,
                               stc=stocks_day)

            # st.write("BUYSELL OPEN trades amount:" + str(len(self.bs.buy_sell_open)))
            #-----------------------------SELL------------------------------------------
            if len(self.bs.buy_sell_open) > 0:

                logging.info("not selled stocks:" + str(self.bs.buy_sell_open[
                    self.bs.buy_sell_open.state == "open"]))
                logging.info("Current SYM: " + str(stocks_15.iloc[-1].sym))

                bs = self.bs.buy_sell_open[self.bs.buy_sell_open.sym ==
                                           stocks_15.iloc[-1].sym]

                if len(bs) > 0:
                    st.write(
                        "--------------------SELLING----------------------------"
                    )
                    for index, row in bs.iterrows():
                        if Utils.calc_perc(stocks_15.iloc[-1].close, hl["h"], 2) <= 1 or \
                                chi.check_sma9(stocks=stocks_day, live_stocks=stocks_15, buy=False):

                            # self.bs.buy_sell_open[(self.bs.buy_sell_open.sym == stocks_15.iloc[-1].sym) & (self.bs.buy_sell_open.state == "open")] = self.bs.sell_stock_t(stocks_15.iloc[-1],  sell_price=stocks_15.iloc[-1].close,
                            sold_stock = self.bs.sell_stock_t(
                                sym=stocks_15.iloc[-1].sym,
                                price=stocks_15.iloc[-1].close,
                                sell_date=stocks_15.iloc[-1].name)
                            st.write(sold_stock)
                            # st.write(sym + " | Selling Profit: " + str(stocks_15.iloc[-1].sym) + " | " + str(stocks_15.index[-1]) + " | " +
                            #                                      " | B.S.:" + str(row["buy"]) + "/" + str(stocks_15.iloc[-1].close) +
                            #                                      " | " + str(Utils.calc_perc(row["buy"], stocks_15.iloc[-1].close)) +
                            #                                      "% | " + str(stocks_15.iloc[-1].name) +
                            #                                      " | ", stocks_15.iloc[-1].name)

                            # asyncio.run(self.sm.a_mail_sym_stats(sym, "Selling Profit: " + str(stocks_15.iloc[-1].sym) + " | " + str(stocks_15.index[-1]) + " | " +
                            #                                      " | B.S.:" + str(row["buy"]) + "/" + str(stocks_15.iloc[-1].close) +
                            #                                      " | " + str(Utils.calc_perc(row["buy"], stocks_15.iloc[-1].close)) +
                            #                                      "% | " + str(stocks_15.iloc[-1].name) +
                            #                                      " | ", stocks_15.iloc[-1].name), debug=True)

            #------------------------------------------------------------BUY---------------------------------------------
            if hash_warn not in self.warning_check_list \
                    and stocks_day.iloc[-1]["flpd"] > 0 \
                    and chi.check_financials(financials) \
                    and (len(self.bs.buy_sell_open) == 0 or stocks_15.iloc[-1].sym not in self.bs.buy_sell_open[self.bs.buy_sell_open.state == "open"].sym)  \
                    and chi.check_pre_sma9(stocks_day, live_stocks=stocks_15):

                # and chi.check_sentiment(sentiment) \
                # and chi.check_financials(financials) \

                self.warning_check_list.append(hash_warn)
                self.bs.buy_stock_t(stocks_15.iloc[-1],
                                    stocks_15.iloc[-1].close,
                                    table_name="buy_sell_lt",
                                    profit_loss={
                                        "profit": hl["h"],
                                        "loss": hl["l"]
                                    })

                st.write(
                    "---------------------------BUYiNG-------------------------------"
                )
                # logging.info(self.bs.buy_sell)

                st.write(
                    sym + " | Buy: " + str(stocks_15.iloc[-1].sym) + " | " +
                    str(stocks_15.index[-1]) + " | " +
                    str(stocks_15.iloc[-1].close) + " | " + str(hl),
                    stocks_15.iloc[-1].name)
Example #16
0
    def plot_boll(ax, df, sym, fib=True):
        """
        docstring
        """

        # df = self.add_bollinger_bands(df)
        df = FinI.add_indicators(df)

        df.plot(kind="line",
                use_index=True,
                y=["boll", "boll_lb", "boll_ub"],
                legend=False,
                ax=ax,
                grid=True,
                linewidth=0.7,
                color=["brown", "orange", "green"],
                alpha=0.7)

        ax.fill_between(df.index,
                        df.boll_lb,
                        df.boll_ub,
                        where=df.boll > df.close,
                        facecolor='red',
                        interpolate=True,
                        alpha=0.2)
        ax.fill_between(df.index,
                        df.boll_lb,
                        df.boll_ub,
                        where=df.boll <= df.close,
                        facecolor='green',
                        interpolate=True,
                        alpha=0.2)

        def fill_bands(boll, bmin, bmax, alpha=0.2, color="white"):
            nonlocal ax
            ax.fill_between(df.index,
                            boll + bmin,
                            boll + bmax,
                            facecolor=color,
                            interpolate=True,
                            alpha=alpha)
            ax.fill_between(df.index,
                            boll - bmin,
                            boll - bmax,
                            facecolor=color,
                            interpolate=True,
                            alpha=alpha)
            return ax

        if fib:

            # print("slnfvadklngůdnfsld s")
            ax = fill_bands(df.boll, 0, df.boll_2, alpha=0.1, color="white")
            ax = fill_bands(df.boll,
                            df.boll_2,
                            df.boll_3,
                            alpha=0.2,
                            color="white")
            # ax = fill_bands(df.boll, df.boll_3, df.boll_5,
            #                 alpha=0.1, color="blue")
            ax = fill_bands(df.boll,
                            df.boll_5,
                            df.boll_6,
                            alpha=0.2,
                            color="green")
            ax = fill_bands(df.boll,
                            df.boll_6,
                            df.boll_10,
                            alpha=0,
                            color="brown")

        else:
            ax.fill_between(df.index,
                            df.boll_mid_lb,
                            df.boll_lb,
                            facecolor='brown',
                            interpolate=True,
                            alpha=0.2)
            ax.fill_between(df.index,
                            df.boll_mid_ub,
                            df.boll_ub,
                            facecolor='brown',
                            interpolate=True,
                            alpha=0.2)

        for tick in ax.get_xticklabels():
            tick.set_rotation(0)

        return ax
Example #17
0
    def logistic_regresion(self, symbols=None):

        if symbols is None:
            symbols = [self.selected_stock]
        # LOGISTIC REGRESSION  sucesfully finish this logic
        st.write(f"Logistic regression: {self.selected_stock}")

        df = self.db.load_data(table_name=TableName.DAY,
                               time_from=self.time_from,
                               symbols=symbols)

        m_df_spy = self.db.load_data(table_name=TableName.DAY,
                                     time_from=self.time_from,
                                     symbols=["SPY"])

        df_best_buy = pd.DataFrame()
        df_lr_raw = self.logistic_regression_raw()
        st.write("logistic regression RAW SPY")
        st.dataframe(df_lr_raw)

        # remove volume, industry, symbol cols
        # df = df.iloc[:,:4]
        # df =  df.dropna()
        # df = df.iloc[:, :4]
        # df = df.retype(spy)
        df['open-close'] = df['close'] - df['open'].shift(1)
        df['close-close'] = df['close'].shift(-1) - df['close']
        # wrong close close only for research
        df['close-close-prev'] = df['close'] - df['close'].shift(1)
        # df['close-close-1'] = df['close'] - df['close'].shift(1)
        # df['close-close-2'] = df['close'].shift(1) - df['close'].shift(2)
        # df['close-close-3'] = df['close'].shift(2) - df['close'].shift(3)
        # df['close-close-4'] = df['close'].shift(3)- df['close'].shift(4)
        df, m_df_spy = FinI.get_sizes(df, m_df_spy)
        # df = FinI.add_weekday(df)
        # df = FinI.add_week_of_month(df)
        df = FinI.add_yearweek(df)

        # df = FinI.add_levels(df)
        # only first nine rows be aware of this

        # df = FinI.add_indicators(df)
        # df["MACD"] = ta.trend.macd(close=df['close'])
        # df['S_9'] = df['close'].rolling(window=9).mean()
        # df['S_20'] = df['close'].rolling(window=20).mean()
        # df['S_50'] = df['close'].rolling(window=50).mean()

        # df['Corr'] = df['close'].rolling(window=10).corr(df['S_9'])
        # df['RSI'] = ta.momentum.rsi(close= df['close'], n=9)
        # Initialize Bollinger Bands Indicator

        # Add Bollinger Bands features
        # df['bb_bbm'] = indicator_bb.bollinger_mavg()
        # df['bb_bbh'] = indicator_bb.bollinger_hband()
        # df['bb_bbl'] = indicator_bb.bollinger_lband()

        df['S_9'] = df['close'].rolling(window=9).mean()
        df['S_20'] = df['close'].rolling(window=20).mean()
        # df['S_50'] = df['close'].rolling(window=50).mean()
        # df['S_200'] = df['close'].rolling(window=200).mean()
        df['Corr_9'] = df['close'].rolling(window=9).corr(df['S_9'])
        df['Corr_20'] = df['close'].rolling(window=9).corr(df['S_20'])
        df['RSI'] = ta.momentum.rsi(close=df['close'])
        # df["MACD"] = ta.trend.macd(close=df['close'])
        df = df.fillna(0)
        st.write("Correlation Matrix - Heatmap")
        self.get_heatmap(df)
        # remove open, high, low
        # df = df.iloc[:,3:]
        y = np.where(df['close'].shift(-1) > df['close'], 1, -1)
        st.write("Rows:")
        st.write(len(df))
        # df = df.drop(columns= ["date",
        #                        "yearweek",
        #                        "size_boll_ub",
        #                        "size_boll_lb",
        #                        "size_boll",
        #                        "up_down_row",
        #                        "green_red_row",
        #                        "price_level",
        #                        "weekday",
        #                        "boll",
        #                        "sma9",
        #                        "close_20_sma",
        #                        "size_top",
        #                        "size_btm",
        #                        "close_20_mstd",
        #                        "size_top-2",
        #                        "size_top-1",
        #                        "size_btm-2",
        #                        "size_btm-1",
        #                        "boll_ub",
        #                        "boll_lb",
        #                        "size_btm-3",
        #                        "size_top-3",
        #                        "size_sma9",
        #                        "size_sma20",
        #                         "sma20",
        #                        "open",
        #                        "close",
        #                        "high",
        #                        "low",
        #                        "size_body-1",
        #                        "size_body-2",
        #                        "size_body-3",
        #                        "week_in_month",
        #                        "size_body",
        #                        "size_prev_chng",
        #                        "symbol",
        #                        "sector",
        #                        "sym",
        #                        "industry",
        #                        "amount",
        #                        "volume",
        #  "open-close",
        # "close-close"
        #                        ])
        df_strategy = df.copy()

        # relatively good negative prediction
        # df = df[["size_sma9"]]
        # values by corelation matrix
        # df = df[["size_sma20","size_sma9","up_down_row"]]
        # df = df[["green_red_row", "size_sma9", "up_down_row", "size_body"]]
        # df = df[["green_red_row", "size_sma9", "up_down_row", "size_body","volume"]]
        # origin
        # df = df[["Corr_9","Corr_20", "open-close", "close-close-prev", "RSI","sma9"]]
        df = df[["Corr_9", "open-close", "close-close-prev", "RSI", "sma9"]]
        # overnight, negative precision
        # df = df[["green_red_row", "size_body-1","size_body-2","size_body-3", "size_prev_chng","weekday"]]

        # df = df[["close", "sma9", "sma20"]]
        st.write("Dataframe")
        st.dataframe(df)
        X = df.iloc[:, :30]
        st.write(len(y))
        st.write(len(X))
        # split to test dataset

        split = int(0.7 * len(df))

        X_train, X_test, y_train, y_test = X[:split], X[split:], y[:split], y[
            split:]

        # We will instantiate the logistic regression in Python using ‘LogisticRegression’
        # function and fit the model on the training dataset using ‘fit’ function.
        model = LogisticRegression()
        model = model.fit(X_train, y_train)

        # Examine coeficients
        pd.DataFrame(zip(X.columns, np.transpose(model.coef_)))
        st.write("Examine The Coefficients")
        st.write(pd.DataFrame(zip(X.columns, np.transpose(model.coef_))))

        #We will calculate the probabilities of the class for the test dataset using ‘predict_proba’ function.
        st.write("probability")
        probability = model.predict_proba(X_test)
        st.write(probability)

        st.write("predicted")
        predicted = model.predict(X_test)
        st.write(predicted)

        st.write("Y test")
        st.write(y_test)
        st.write(len(X_test))
        st.write("Confusion matrix")
        conf_matrix = metrics.confusion_matrix(y_test, predicted)
        st.write(conf_matrix)

        st.write("classification report")
        st.write(metrics.classification_report(y_test, predicted))

        st.write("model accuracy")
        st.write(model.score(X_test, y_test))

        st.write("cross validation accuracy")
        cross_val = cross_val_score(LogisticRegression(),
                                    X,
                                    y,
                                    scoring='accuracy',
                                    cv=10)
        st.write(cross_val)
        st.write(cross_val.mean())

        st.write("Trading Strategy")

        df['Predicted_Signal'] = model.predict(X)
        st.write("Predicted signals")
        st.dataframe(df)

        #STRARTEGY count
        st.dataframe(df_strategy)

        # df_strategy = df_strategy.tail(len(df_strategy) - split)
        df_strategy["Predicted_Signal"] = model.predict(X)
        price_change = df_strategy.iloc[len(df_strategy) -
                                        1].close - df_strategy.iloc[0].close

        st.write("Perfect gain")
        df_strategy["perfect_gain"] = df_strategy["close-close"].where(
            df_strategy["close-close"] > 0).sum()
        st.write(df_strategy["perfect_gain"].iloc[0])

        st.write("Invest Gain")
        st.write(price_change)

        st.write("L.G. gain")
        df_strategy["lg_gain"] = df_strategy["close-close"].where(
            df_strategy["Predicted_Signal"] > 0).sum()
        st.write(df_strategy["lg_gain"].iloc[0])

        gap = -2
        st.write("sma9 gain: " + str(gap))
        df_strategy["sma9_gain"] = df_strategy["close-close"].where(
            df_strategy["size_sma9"] > gap).sum()
        st.write(df_strategy["sma9_gain"].iloc[0])

        st.write("sma9 gain 0")
        df_strategy["sma9_gain"] = df_strategy["close-close"].where(
            df_strategy["size_sma9"] > 0).sum()
        st.write(df_strategy["sma9_gain"].iloc[0])

        st.write("sma20 gain")
        df_strategy["sma20_gain"] = df_strategy["close-close"].where(
            df_strategy["size_sma20"] > -2).sum()
        st.write(df_strategy["sma20_gain"].iloc[0])