Example #1
0
def test_get_rating_over_time_invalid_status(mocker):
    mock_response = requests.Response()
    mock_response.status_code = 400
    mocker.patch(
        target="requests.get",
        new=mocker.Mock(return_value=mock_response),
    )
    result_df = finnhub_model.get_rating_over_time(ticker="FAILING_REQUEST")

    assert result_df.empty
def rating_over_time(ticker: str, num: int, raw: bool, export: str):
    """Rating over time (monthly). [Source: Finnhub]

    Parameters
    ----------
    ticker : str
        Ticker to get ratings from
    num : int
        Number of last months ratings to show
    raw : bool
        Display raw data only
    export : str
        Export dataframe data to csv,json,xlsx file
    """
    df_rot = finnhub_model.get_rating_over_time(ticker)

    if df_rot.empty:
        print("No ratings over time found", "\n")
        return

    if raw:
        d_cols = {
            "strongSell": "Strong Sell",
            "sell": "Sell",
            "hold": "Hold",
            "buy": "Buy",
            "strongBuy": "Strong Buy",
        }
        df_rot_raw = (
            df_rot[["period", "strongSell", "sell", "hold", "buy", "strongBuy"]]
            .rename(columns=d_cols)
            .head(num)
        )
        print(
            tabulate(
                df_rot_raw,
                headers=df_rot_raw.columns,
                floatfmt=".2f",
                showindex=False,
                tablefmt="fancy_grid",
            )
        )
    else:
        plot_rating_over_time(df_rot.head(num), ticker)

    print("")

    export_data(
        export,
        os.path.dirname(os.path.abspath(__file__)),
        "rot",
        df_rot,
    )
Example #3
0
def rating_over_time(
    ticker: str,
    num: int,
    raw: bool,
    export: str,
    external_axes: Optional[List[plt.Axes]] = None,
):
    """Rating over time (monthly). [Source: Finnhub]

    Parameters
    ----------
    ticker : str
        Ticker to get ratings from
    num : int
        Number of last months ratings to show
    raw : bool
        Display raw data only
    export : str
        Export dataframe data to csv,json,xlsx file
    """
    df_rot = finnhub_model.get_rating_over_time(ticker)

    if df_rot.empty:
        return

    if raw:
        d_cols = {
            "strongSell": "Strong Sell",
            "sell": "Sell",
            "hold": "Hold",
            "buy": "Buy",
            "strongBuy": "Strong Buy",
        }
        df_rot_raw = (
            df_rot[["period", "strongSell", "sell", "hold", "buy", "strongBuy"]]
            .rename(columns=d_cols)
            .head(num)
        )
        print_rich_table(
            df_rot_raw,
            headers=list(df_rot_raw.columns),
            show_index=False,
            title="Monthly Rating",
        )
    else:
        plot_rating_over_time(df_rot.head(num), ticker, external_axes)

    export_data(
        export,
        os.path.dirname(os.path.abspath(__file__)),
        "rot",
        df_rot,
    )
def test_get_rating_over_time_invalid_status(mocker):

    attrs = {
        "status_code": 400,
        "json.return_value": {"error": "mock error message"},
    }

    mock_response = mocker.Mock(**attrs)

    mocker.patch(
        target="requests.get",
        new=mocker.Mock(return_value=mock_response),
    )
    result_df = finnhub_model.get_rating_over_time(ticker="FAILING_REQUEST")

    assert result_df.empty
def test_get_rating_over_time_invalid_ticker():
    result_df = finnhub_model.get_rating_over_time(ticker="INVALID_TICKER")

    assert result_df.empty
def test_get_rating_over_time(recorder):
    result_df = finnhub_model.get_rating_over_time(ticker="TSLA")

    recorder.capture(result_df)