Пример #1
0
def display_historical(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
    normalize: bool = True,
    export: str = "",
):
    """Display historical stock prices. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    normalize : bool, optional
        Boolean to normalize all stock prices using MinMax defaults True
    export : str, optional
        Format to export historical prices, by default ""
    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start,
                                                    candle_type)
    df_similar = df_similar[similar_tickers]

    if np.any(df_similar.isna()):
        nan_tickers = df_similar.columns[
            df_similar.isna().sum() >= 1].to_list()
        console.print(
            f"NaN values found in: {', '.join(nan_tickers)}.  Replacing with zeros."
        )
        df_similar = df_similar.fillna(0)
    fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    # This puts everything on 0-1 scale for visualizing
    if normalize:
        mm_scale = MinMaxScaler()
        df_similar = pd.DataFrame(
            mm_scale.fit_transform(df_similar),
            columns=df_similar.columns,
            index=df_similar.index,
        )
    df_similar.plot(ax=ax)
    ax.set_title("Historical price of similar companies")
    ax.set_xlabel("Time")
    ax.set_ylabel(
        f"{['','Normalized'][normalize]} Share Price {['($)',''][normalize]}")
    ax.grid(b=True, which="major", color="#666666", linestyle="-")
    # ensures that the historical data starts from same datapoint
    ax.set_xlim([df_similar.index[0], df_similar.index[-1]])
    plt.gcf().autofmt_xdate()
    fig.tight_layout()
    if gtff.USE_ION:
        plt.ion()
    plt.show()
    export_data(export, os.path.dirname(os.path.abspath(__file__)),
                "historical", df_similar)
    console.print("")
def display_historical(
    ticker: str,
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
    normalize: bool = True,
    export: str = "",
):
    """Display historical stock prices

    Parameters
    ----------
    ticker : str
        Base ticker
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    normalize : bool, optional
        Boolean to normalize all stock prices using MinMax defaults True
    export : str, optional
        Format to export historical prices, by default ""
    """
    ordered_tickers = [ticker, *similar_tickers]
    df_similar = yahoo_finance_model.get_historical(ticker, similar_tickers,
                                                    start, candle_type)
    # To plot with ticker first
    df_similar = df_similar[ordered_tickers]

    fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    # This puts everything on 0-1 scale for visualizing
    if normalize:
        mm_scale = MinMaxScaler()
        df_similar = pd.DataFrame(
            mm_scale.fit_transform(df_similar),
            columns=df_similar.columns,
            index=df_similar.index,
        )
    df_similar.plot(ax=ax)
    ax.set_title(f"Similar companies to {ticker}")
    ax.plot(df_similar.index, df_similar[ticker].values)
    ax.set_xlabel("Time")
    ax.set_ylabel(
        f"{['','Normalized'][normalize]} Share Price {['($)',''][normalize]}")
    ax.grid(b=True, which="major", color="#666666", linestyle="-")
    # ensures that the historical data starts from same datapoint
    ax.set_xlim([df_similar.index[0], df_similar.index[-1]])
    plt.gcf().autofmt_xdate()
    fig.tight_layout()
    plt.show()
    export_data(export, os.path.dirname(os.path.abspath(__file__)),
                "historical", df_similar)
    print("")
Пример #3
0
def display_correlation(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
):
    """
    Correlation heatmap based on historical price comparison
    between similar companies. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start,
                                                    candle_type)
    df_similar = df_similar[similar_tickers]

    if np.any(df_similar.isna()):
        nan_tickers = df_similar.columns[
            df_similar.isna().sum() >= 1].to_list()
        console.print(
            f"NaN values found in: {', '.join(nan_tickers)}.  Backfilling data"
        )
        df_similar = df_similar.fillna(method="bfill")

    df_similar = df_similar.dropna(axis=1, how="all")

    mask = np.zeros((df_similar.shape[1], df_similar.shape[1]), dtype=bool)
    mask[np.triu_indices(len(mask))] = True

    plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)

    sns.heatmap(
        df_similar.corr(),
        cbar_kws={"ticks": [-1.0, -0.5, 0.0, 0.5, 1.0]},
        cmap="RdYlGn",
        linewidths=1,
        annot=True,
        vmin=-1,
        vmax=1,
        mask=mask,
    )
    plt.title(f"Correlation Heatmap of similar companies from {start}")
    if gtff.USE_ION:
        plt.ion()
    plt.show()
    console.print("")
def display_volume(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    export: str = "",
    external_axes: Optional[List[plt.Axes]] = None,
):
    """Display volume stock prices. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    export : str, optional
        Format to export historical prices, by default ""
    external_axes : Optional[List[plt.Axes]], optional
        External axes (1 axis is expected in the list), by default None
    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start, "v")
    df_similar = df_similar[similar_tickers]

    # This plot has 1 axis
    if not external_axes:
        _, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    else:
        if len(external_axes) != 1:
            logger.error("Expected list of one axis item.")
            console.print("[red]Expected list of one axis item./n[/red]")
            return
        (ax,) = external_axes

    df_similar = df_similar.div(1_000_000)
    companies_names = df_similar.columns.to_list()

    ax.plot(df_similar, label=companies_names)
    ax.set_title("Historical volume of similar companies")
    ax.set_ylabel("Volume [M]")
    # ensures that the historical data starts from same datapoint
    ax.set_xlim([df_similar.index[0], df_similar.index[-1]])

    ax.legend()
    theme.style_primary_axis(ax)

    if not external_axes:
        theme.visualize_output()

    export_data(
        export, os.path.dirname(os.path.abspath(__file__)), "volume", df_similar
    )
    console.print("")
Пример #5
0
def test_get_historical(mocker, recorder):
    # FORCE SINGLE THREADING
    yf_download = yahoo_finance_model.yf.download

    def mock_yf_download(*args, **kwargs):
        kwargs["threads"] = False
        return yf_download(*args, **kwargs)

    mocker.patch("yfinance.download", side_effect=mock_yf_download)

    result_df = yahoo_finance_model.get_historical(
        similar_tickers=["TSLA", "GM"],
        start=datetime.strptime("2020-12-21", "%Y-%m-%d"),
        candle_type="o",
    )

    recorder.capture(result_df)
def display_correlation(
    ticker: str,
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
):
    """
    Correlation heatmap based on historical price comparison
    between similar companies. [Source: Yahoo Finance]

    Parameters
    ----------
    ticker : str
        Base ticker
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    """
    ordered_tickers = [ticker, *similar_tickers]
    df_similar = yahoo_finance_model.get_historical(ticker, similar_tickers,
                                                    start, candle_type)
    # To plot with ticker first
    df_similar = df_similar[ordered_tickers]

    mask = np.zeros((df_similar.shape[1], df_similar.shape[1]), dtype=bool)
    mask[np.triu_indices(len(mask))] = True

    sns.heatmap(
        df_similar.corr(),
        cbar_kws={"ticks": [-1.0, -0.5, 0.0, 0.5, 1.0]},
        cmap="RdYlGn",
        linewidths=1,
        annot=True,
        vmin=-1,
        vmax=1,
        mask=mask,
    )
    plt.title("Correlation Heatmap")
    plt.show()
    print("")
Пример #7
0
def display_volume(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    export: str = "",
):
    """Display volume stock prices. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    export : str, optional
        Format to export historical prices, by default ""
    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start,
                                                    "v")
    df_similar = df_similar[similar_tickers]

    fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    df_similar = df_similar.div(1_000_000)

    df_similar.plot(ax=ax)
    ax.set_title("Historical volume of similar companies")
    # ax.plot(df_similar.index, df_similar[ticker].values/1_000_000)
    ax.set_xlabel("Date")
    ax.set_ylabel("Volume [M]")
    ax.grid(b=True, which="major", color="#666666", linestyle="-")
    # ensures that the historical data starts from same datapoint
    ax.set_xlim([df_similar.index[0], df_similar.index[-1]])
    plt.gcf().autofmt_xdate()
    fig.tight_layout()
    if gtff.USE_ION:
        plt.ion()
    plt.show()
    export_data(export, os.path.dirname(os.path.abspath(__file__)), "volume",
                df_similar)
    console.print("")
def display_historical(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
    normalize: bool = True,
    export: str = "",
    external_axes: Optional[List[plt.Axes]] = None,
):
    """Display historical stock prices. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    normalize : bool, optional
        Boolean to normalize all stock prices using MinMax defaults True
    export : str, optional
        Format to export historical prices, by default ""
    external_axes : Optional[List[plt.Axes]], optional
        External axes (1 axis is expected in the list), by default None

    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start, candle_type)
    df_similar = df_similar[similar_tickers]

    if np.any(df_similar.isna()):
        nan_tickers = df_similar.columns[df_similar.isna().sum() >= 1].to_list()
        console.print(
            f"NaN values found in: {', '.join(nan_tickers)}.  Replacing with zeros."
        )
        df_similar = df_similar.fillna(0)

    # This puts everything on 0-1 scale for visualizing
    if normalize:
        mm_scale = MinMaxScaler()
        df_similar = pd.DataFrame(
            mm_scale.fit_transform(df_similar),
            columns=df_similar.columns,
            index=df_similar.index,
        )

    # This plot has 1 axis
    if not external_axes:
        _, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    else:
        if len(external_axes) != 1:
            console.print("[red]Expected list of one axis item./n[/red]")
            return
        (ax,) = external_axes

    # breakpoint()
    companies_names = df_similar.columns.to_list()
    ax.plot(df_similar, label=companies_names)
    ax.set_title("Historical price of similar companies")
    ax.set_ylabel(f"{['','Normalized'][normalize]} Share Price {['($)',''][normalize]}")
    # ensures that the historical data starts from same datapoint
    ax.set_xlim([df_similar.index[0], df_similar.index[-1]])
    ax.legend(loc="best")
    theme.style_primary_axis(ax)

    if not external_axes:
        theme.visualize_output()

    export_data(
        export, os.path.dirname(os.path.abspath(__file__)), "historical", df_similar
    )
    console.print("")
def display_correlation(
    similar_tickers: List[str],
    start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
    candle_type: str = "a",
    external_axes: Optional[List[plt.Axes]] = None,
):
    """
    Correlation heatmap based on historical price comparison
    between similar companies. [Source: Yahoo Finance]

    Parameters
    ----------
    similar_tickers : List[str]
        List of similar tickers
    start : str, optional
        Start date of comparison, by default 1 year ago
    candle_type : str, optional
        OHLCA column to use, by default "a" for Adjusted Close
    external_axes : Optional[List[plt.Axes]], optional
        External axes (1 axis is expected in the list), by default None

    """
    df_similar = yahoo_finance_model.get_historical(similar_tickers, start, candle_type)
    df_similar = df_similar[similar_tickers]

    if np.any(df_similar.isna()):
        nan_tickers = df_similar.columns[df_similar.isna().sum() >= 1].to_list()
        console.print(
            f"NaN values found in: {', '.join(nan_tickers)}.  Backfilling data"
        )
        df_similar = df_similar.fillna(method="bfill")

    df_similar = df_similar.dropna(axis=1, how="all")

    mask = np.zeros((df_similar.shape[1], df_similar.shape[1]), dtype=bool)
    mask[np.triu_indices(len(mask))] = True

    # This plot has 1 axis
    if not external_axes:
        _, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
    else:
        if len(external_axes) != 1:
            console.print("[red]Expected list of one axis item./n[/red]")
            return
        (ax,) = external_axes

    sns.heatmap(
        df_similar.corr(),
        cbar_kws={"ticks": [-1.0, -0.5, 0.0, 0.5, 1.0]},
        cmap="RdYlGn",
        linewidths=1,
        annot=True,
        vmin=-1,
        vmax=1,
        mask=mask,
        ax=ax,
    )
    ax.set_title(f"Correlation Heatmap of similar companies from {start}")

    if not external_axes:
        theme.visualize_output()

    console.print("")