def test_get_next_earnings(self, mock_get_earnings_html, pages,
                               mock_next_earnings_rets, expected_result):
        mock_get_earnings_html.side_effect = mock_next_earnings_rets
        ret = get_next_earnings(pages)

        assertions.assertEqual(ret.to_csv().replace("\r\n", "\n"),
                               expected_result.replace("\r\n", "\n"))
示例#2
0
def earnings_command():
    """Display Upcoming Earnings. [Source: Seeking Alpha]"""

    # Debug
    if imps.DEBUG:
        logger.debug("earnings")

    df_earnings = seeking_alpha_model.get_next_earnings(2)
    for n_days, earning_date in enumerate(df_earnings.index.unique()):

        df_earn = df_earnings[earning_date == df_earnings.index][[
            "Ticker", "Name"
        ]].dropna()

        df_earn.index = df_earn["Ticker"].values
        df_earn.drop(columns=["Ticker"], inplace=True)

    title = f"Earnings on {earning_date.date()}"

    dindex = len(df_earn.index)
    if dindex > 15:
        embeds: list = []
        # Output
        i, i2, end = 0, 0, 15
        df_pg, embeds_img, images_list = [], [], []
        while i < dindex:
            df_pg = df_earn.iloc[i:end]
            df_pg.append(df_pg)
            fig = imps.plot_df(
                df_pg,
                fig_size=(800, (40 + (40 * dindex))),
                col_width=[1, 5],
                tbl_header=imps.PLT_TBL_HEADER,
                tbl_cells=imps.PLT_TBL_CELLS,
                font=imps.PLT_TBL_FONT,
                row_fill_color=imps.PLT_TBL_ROW_COLORS,
                paper_bgcolor="rgba(0, 0, 0, 0)",
            )
            fig.update_traces(cells=(dict(align=["center", "left"])))
            imagefile = "disc-upcoming.png"
            imagefile = imps.save_image(imagefile, fig)

            if imps.IMAGES_URL or not imps.IMG_HOST_ACTIVE:
                image_link = imps.multi_image(imagefile)
                images_list.append(imagefile)
            else:
                image_link = imps.multi_image(imagefile)

            embeds_img.append(f"{image_link}", )
            embeds.append(disnake.Embed(
                title=title,
                colour=imps.COLOR,
            ), )
            i2 += 1
            i += 15
            end += 15

        # Author/Footer
        for i in range(0, i2):
            embeds[i].set_author(
                name=imps.AUTHOR_NAME,
                url=imps.AUTHOR_URL,
                icon_url=imps.AUTHOR_ICON_URL,
            )
            embeds[i].set_footer(
                text=imps.AUTHOR_NAME,
                icon_url=imps.AUTHOR_ICON_URL,
            )

        i = 0
        for i in range(0, i2):
            embeds[i].set_image(url=embeds_img[i])

            i += 1
        embeds[0].set_footer(text=f"Page 1 of {len(embeds)}")
        choices = [
            disnake.SelectOption(label="Home", value="0", emoji="🟢"),
        ]

        output = {
            "view": imps.Menu,
            "title": title,
            "embed": embeds,
            "choices": choices,
            "embeds_img": embeds_img,
            "images_list": images_list,
        }
    else:
        fig = imps.plot_df(
            df_earn,
            fig_size=(800, (40 + (40 * dindex))),
            col_width=[1, 5],
            tbl_header=imps.PLT_TBL_HEADER,
            tbl_cells=imps.PLT_TBL_CELLS,
            font=imps.PLT_TBL_FONT,
            row_fill_color=imps.PLT_TBL_ROW_COLORS,
            paper_bgcolor="rgba(0, 0, 0, 0)",
        )
        fig.update_traces(cells=(dict(align=["center", "left"])))
        imagefile = imps.save_image("disc-upcoming.png", fig)

        output = {
            "title": title,
            "imagefile": imagefile,
        }

    return output
示例#3
0
def test_get_next_earnings(recorder):
    df_earnings = seeking_alpha_model.get_next_earnings(1)
    recorder.capture(df_earnings)
def upcoming_earning_release_dates(num_pages: int, num_earnings: int,
                                   export: str):
    """Displays upcoming earnings release dates

    Parameters
    ----------
    num_pages: int
        Number of pages to scrap
    num_earnings: int
        Number of upcoming earnings release dates
    export : str
        Export dataframe data to csv,json,xlsx file
    """
    # TODO: Check why there are repeated companies
    # TODO: Create a similar command that returns not only upcoming, but antecipated earnings
    # i.e. companies where expectation on their returns are high

    df_earnings = seeking_alpha_model.get_next_earnings(num_pages)

    pd.set_option("display.max_colwidth", None)
    if export:
        l_earnings = []
        l_earnings_dates = []

    for n_days, earning_date in enumerate(df_earnings.index.unique()):
        if n_days > (num_earnings - 1):
            break

        # TODO: Potentially extract Market Cap for each Ticker, and sort
        # by Market Cap. Then cut the number of tickers shown to 10 with
        # bigger market cap. Didier attempted this with yfinance, but
        # the computational time involved wasn't worth pursuing that solution.

        df_earn = df_earnings[earning_date == df_earnings.index][[
            "Ticker", "Name"
        ]].dropna()

        if export:
            l_earnings_dates.append(earning_date.date())
            l_earnings.append(df_earn)

        df_earn.index = df_earn["Ticker"].values
        df_earn.drop(columns=["Ticker"], inplace=True)

        print(
            tabulate(
                df_earn,
                showindex=True,
                headers=[f"Earnings on {earning_date.date()}"],
                tablefmt="fancy_grid",
            ),
            "\n",
        )

    if export:
        for i, _ in enumerate(l_earnings):
            l_earnings[i].reset_index(drop=True, inplace=True)
        df_data = pd.concat(l_earnings, axis=1, ignore_index=True)
        df_data.columns = l_earnings_dates

        export_data(
            export,
            os.path.dirname(os.path.abspath(__file__)),
            "upcoming",
            df_data,
        )