Pular para conteúdo

Anbima Data

intraday_ettj()

Retrieves and processes the intraday Brazilian yield curve data from ANBIMA.

This function fetches the most recent intraday yield curve data published by ANBIMA, containing real rates (IPCA-indexed), nominal rates, and implied inflation at various vertices (time points). The curve is published at around 12:30 PM BRT.

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame containing the intraday ETTJ data.

DataFrame columns
  • date: Reference date of the yield curve
  • vertex: Time point in business days
  • nominal_rate: Zero-coupon nominal interest rate
  • real_rate: Zero-coupon real interest rate (IPCA-indexed)
  • implied_inflation: Implied inflation rate (break-even inflation)
Note

All rates are expressed in decimal format (e.g., 0.12 for 12%).

Source code in pyield/anbima/ettj.py
def intraday_ettj() -> pd.DataFrame:
    """
    Retrieves and processes the intraday Brazilian yield curve data from ANBIMA.

    This function fetches the most recent intraday yield curve data published by ANBIMA,
    containing real rates (IPCA-indexed), nominal rates, and implied inflation
    at various vertices (time points). The curve is published at around 12:30 PM BRT.

    Returns:
        pd.DataFrame: A DataFrame containing the intraday ETTJ data.

    DataFrame columns:
        - date: Reference date of the yield curve
        - vertex: Time point in business days
        - nominal_rate: Zero-coupon nominal interest rate
        - real_rate: Zero-coupon real interest rate (IPCA-indexed)
        - implied_inflation: Implied inflation rate (break-even inflation)

    Note:
        All rates are expressed in decimal format (e.g., 0.12 for 12%).
    """

    request_payload = {"Dt_Ref": "", "saida": "csv"}
    response = requests.post(INTRADAY_ETTJ_URL, data=request_payload)
    text_parts = response.text.split("ETTJ IPCA (%a.a./252)")

    nominal_text = text_parts[0]
    lines = nominal_text.splitlines()
    date_str = lines[1]
    date = pd.to_datetime(date_str, format="%d/%m/%Y")
    df_text = "\n".join(lines[2:])
    df_nominal = pd.read_csv(StringIO(df_text), sep=";", decimal=",", thousands=".")
    df_nominal = df_nominal.drop(columns=["Fechamento D -1"])
    df_nominal = df_nominal.rename(columns={"D0": "nominal_rate"})

    real_text = text_parts[1]
    lines = real_text.splitlines()
    df_text = "\n".join(lines[2:])
    df_real = pd.read_csv(StringIO(df_text), sep=";", decimal=",", thousands=".")
    df_real = df_real.drop(columns=["Fechamento D -1"])
    df_real = df_real.rename(columns={"D0": "real_rate"})

    df = pd.merge(df_nominal, df_real, on="Vertices", how="right")
    df = df.rename(columns={"Vertices": "vertex"})

    # Divide float columns by 100 and round to 6 decimal places
    df["real_rate"] = (df["real_rate"] / 100).round(ROUND_DIGITS)
    df["nominal_rate"] = (df["nominal_rate"] / 100).round(ROUND_DIGITS)
    df["implied_inflation"] = (df["nominal_rate"] + 1) / (df["real_rate"] + 1) - 1
    df["implied_inflation"] = df["implied_inflation"].round(ROUND_DIGITS)

    df["date"] = date
    column_order = ["date", "vertex", "nominal_rate", "real_rate", "implied_inflation"]
    return df[column_order].copy()

ipca_projection()

Retrieves the current IPCA projection from the ANBIMA website.

This function makes an HTTP request to the ANBIMA website, extracts HTML tables containing economic indicators, and specifically processes the IPCA projection data.

Process
  1. Accesses the ANBIMA indicators webpage
  2. Extracts the third table that contains the IPCA projection
  3. Locates the row labeled as "IPCA1"
  4. Extracts the projection value and converts it to decimal format
  5. Extracts and formats the reference month of the projection
  6. Extracts the date and time of the last update

Returns:

Name Type Description
IndicatorProjection IndicatorProjection

An object containing: - reference_period (pd.Period): Reference period of the projection - projected_value (float): Projected IPCA value as a decimal number - last_updated (pd.Timestamp): Date and time of the last data update

Raises:

Type Description
RequestException

If there are connection issues with the ANBIMA site

ValueError

If the expected data is not found in the page structure

Error

If the 'pt_BR.UTF-8' locale is not available on the system

Notes
  • The function requires internet connection to access the ANBIMA website
  • The structure of the ANBIMA page may change, which could affect the function
  • Temporarily changes the locale to pt_BR.UTF-8 to process dates in Portuguese
Source code in pyield/anbima/ipca.py
def ipca_projection() -> IndicatorProjection:
    """
    Retrieves the current IPCA projection from the ANBIMA website.

    This function makes an HTTP request to the ANBIMA website, extracts HTML tables
    containing economic indicators, and specifically processes the IPCA projection data.

    Process:
        1. Accesses the ANBIMA indicators webpage
        2. Extracts the third table that contains the IPCA projection
        3. Locates the row labeled as "IPCA1"
        4. Extracts the projection value and converts it to decimal format
        5. Extracts and formats the reference month of the projection
        6. Extracts the date and time of the last update

    Returns:
        IndicatorProjection: An object containing:
            - reference_period (pd.Period): Reference period of the projection
            - projected_value (float): Projected IPCA value as a decimal number
            - last_updated (pd.Timestamp): Date and time of the last data update

    Raises:
        requests.RequestException: If there are connection issues with the ANBIMA site
        ValueError: If the expected data is not found in the page structure
        locale.Error: If the 'pt_BR.UTF-8' locale is not available on the system

    Notes:
        - The function requires internet connection to access the ANBIMA website
        - The structure of the ANBIMA page may change, which could affect the function
        - Temporarily changes the locale to pt_BR.UTF-8 to process dates in Portuguese
    """
    url = "https://www.anbima.com.br/informacoes/indicadores/"
    r = requests.get(url)
    r.encoding = "latin1"
    dfs = pd.read_html(
        io.StringIO(r.text),
        flavor="html5lib",
        decimal=",",
        thousands=".",
        dtype_backend="numpy_nullable",
    )
    # The IPCA projection is in the third table
    df = dfs[2]

    last_update_str = df.iat[0, 0].split("Atualização:")[-1].strip()
    last_update = pd.to_datetime(last_update_str, format="%d/%m/%Y - %H:%M h")

    ipca_row = df.loc[df[0] == "IPCA1"]
    ipca_value = ipca_row.iloc[0, 2]
    ipca_value = pd.to_numeric(ipca_value, errors="coerce")
    ipca_value = round(float(ipca_value) / 100, 4)

    # Extract and format the reference month
    ipca_date = ipca_row.iloc[0, 1]
    ipca_date = str(ipca_date)
    ipca_date = ipca_date.split("(")[-1].split(")")[0]
    locale.setlocale(locale.LC_TIME, "pt_BR.UTF-8")
    ipca_date = pd.to_datetime(ipca_date, format="%b/%y")
    reference_period = ipca_date.to_period("M")
    locale.setlocale(locale.LC_TIME, "")  # Reset locale to default

    return IndicatorProjection(
        last_updated=last_update,
        reference_period=reference_period,
        projected_value=ipca_value,
    )

last_ettj()

Retrieves and processes the latest Brazilian yield curve data from ANBIMA.

This function fetches the most recent yield curve data published by ANBIMA, containing real rates (IPCA-indexed), nominal rates, and implied inflation at various vertices (time points).

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame containing the latest ETTJ data.

DataFrame columns
  • date: Reference date of the yield curve
  • vertex: Time point in business days
  • nominal_rate: Zero-coupon nominal interest rate
  • real_rate: Zero-coupon real interest rate (IPCA-indexed)
  • implied_inflation: Implied inflation rate (break-even inflation)
Note

All rates are expressed in decimal format (e.g., 0.12 for 12%).

Source code in pyield/anbima/ettj.py
def last_ettj() -> pd.DataFrame:
    """
    Retrieves and processes the latest Brazilian yield curve data from ANBIMA.

    This function fetches the most recent yield curve data published by ANBIMA,
    containing real rates (IPCA-indexed), nominal rates, and implied inflation
    at various vertices (time points).

    Returns:
        pd.DataFrame: A DataFrame containing the latest ETTJ data.

    DataFrame columns:
        - date: Reference date of the yield curve
        - vertex: Time point in business days
        - nominal_rate: Zero-coupon nominal interest rate
        - real_rate: Zero-coupon real interest rate (IPCA-indexed)
        - implied_inflation: Implied inflation rate (break-even inflation)

    Note:
        All rates are expressed in decimal format (e.g., 0.12 for 12%).
    """
    text = _get_last_content()
    df = _convert_text_to_df(text)
    return _process_df(df)

last_ima(ima_type=None)

Fetch and process the last IMA market data available from ANBIMA.

This function processes the data into a structured DataFrame. It handles conversion of date formats, renames columns to English, and converts certain numeric columns to integer types. In the event of an error during data fetching or processing, an empty DataFrame is returned.

Parameters:

Name Type Description Default
ima_type str

Type of IMA index to filter the data. If None, all IMA indexes are returned. Defaults to None.

None

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame containing the IMA data.

DataFrame columns
  • Date: reference date of the data.
  • IMAType: type of IMA index.
  • BondType: type of bond.
  • Maturity: bond maturity date.
  • SelicCode: bond code in the SELIC system.
  • ISIN: international Securities Identification Number.
  • BDToMat: business days to maturity.
  • Duration: duration of the bond in business years (252 days/year).
  • IndicativeRate: indicative rate.
  • Price: bond price.
  • InterestPrice: interest price.
  • DV01: DV01 in R$.
  • PMR: average repurchase term.
  • Weight: weight of the bond in the index.
  • Convexity: convexity of the bond.
  • TheoreticalQuantity: theoretical quantity.
  • NumberOfOperations: number of operations.
  • NegotiatedQuantity: negotiated quantity.
  • NegotiatedValue: negotiated value.
  • MarketQuantity: market quantity.
  • MarketDV01: market DV01 in R$.
  • MarketValue: market value in R$.

Raises:

Type Description
Exception

Logs error and returns an empty DataFrame if any error occurs during fetching or processing.

Source code in pyield/anbima/ima.py
def last_ima(ima_type: ima_types | None = None) -> pd.DataFrame:
    """
    Fetch and process the last IMA market data available from ANBIMA.

    This function processes the data into a structured DataFrame.
    It handles conversion of date formats, renames columns to English, and converts
    certain numeric columns to integer types. In the event of an error during data
    fetching or processing, an empty DataFrame is returned.

    Args:
        ima_type (str, optional): Type of IMA index to filter the data. If None, all
            IMA indexes are returned. Defaults to None.

    Returns:
        pd.DataFrame: A DataFrame containing the IMA data.

    DataFrame columns:
        - Date: reference date of the data.
        - IMAType: type of IMA index.
        - BondType: type of bond.
        - Maturity: bond maturity date.
        - SelicCode: bond code in the SELIC system.
        - ISIN: international Securities Identification Number.
        - BDToMat: business days to maturity.
        - Duration: duration of the bond in business years (252 days/year).
        - IndicativeRate: indicative rate.
        - Price: bond price.
        - InterestPrice: interest price.
        - DV01: DV01 in R$.
        - PMR: average repurchase term.
        - Weight: weight of the bond in the index.
        - Convexity: convexity of the bond.
        - TheoreticalQuantity: theoretical quantity.
        - NumberOfOperations: number of operations.
        - NegotiatedQuantity: negotiated quantity.
        - NegotiatedValue: negotiated value.
        - MarketQuantity: market quantity.
        - MarketDV01: market DV01 in R$.
        - MarketValue: market value in R$.

    Raises:
        Exception: Logs error and returns an empty DataFrame if any error occurs during
            fetching or processing.
    """
    try:
        df = _fetch_last_ima()
        df = _process_last_ima(df)
        df = _reorder_last_ima(df)
        if ima_type is not None:
            df = df.query("IMAType == @ima_type").reset_index(drop=True)
        df = df.sort_values(["IMAType", "BondType", "Maturity"]).reset_index(drop=True)
        return df
    except Exception as e:
        logger.exception(f"Error fetching or processing the last IMA data: {e}")
        return pd.DataFrame()

tpf_data(date, bond_type=None, adj_maturities=False)

Retrieve indicative rates for bonds from ANBIMA data.

This function fetches indicative rates for bonds from ANBIMA, initially attempting to retrieve data from a cached dataset. If the data is not available in the cache, it fetches it directly from the ANBIMA website.

Parameters:

Name Type Description Default
date DateScalar

The reference date for the rates.

required
bond_type str

Filter rates by bond type. Defaults to None, which returns rates for all bond types.

None
adj_maturities bool

Adjust maturity dates to the next business day. Defaults to False.

False

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame with the following columns: - BondType: The type of bond. - MaturityDate: The maturity date of the bond. - IndicativeRate: The indicative rate of the bond. - Price: The price (PU) of the bond.

Source code in pyield/anbima/tpf.py
def tpf_data(
    date: DateScalar,
    bond_type: str | None = None,
    adj_maturities: bool = False,
) -> pd.DataFrame:
    """Retrieve indicative rates for bonds from ANBIMA data.

    This function fetches indicative rates for bonds from ANBIMA,
    initially attempting to retrieve data from a cached dataset. If the data
    is not available in the cache, it fetches it directly from the ANBIMA website.

    Args:
        date (DateScalar): The reference date for the rates.
        bond_type (str, optional): Filter rates by bond type.
            Defaults to None, which returns rates for all bond types.
        adj_maturities (bool, optional): Adjust maturity dates to the next
            business day. Defaults to False.

    Returns:
        pd.DataFrame: A DataFrame with the following columns:
            - BondType: The type of bond.
            - MaturityDate: The maturity date of the bond.
            - IndicativeRate: The indicative rate of the bond.
            - Price: The price (PU) of the bond.
    """

    df = get_cached_dataset("tpf")
    date = convert_input_dates(date)
    df = df.query("ReferenceDate == @date").reset_index(drop=True)

    if df.empty:
        # Try to fetch the data from the Anbima website
        df = tpf_web_data(date)

    if df.empty:
        # If the data is still empty, return an empty DataFrame
        return pd.DataFrame()

    if bond_type:
        df = df.query("BondType == @bond_type").reset_index(drop=True)

    if adj_maturities:
        df["MaturityDate"] = bday.offset(df["MaturityDate"], 0)

    return (
        df[["ReferenceDate", "BondType", "MaturityDate", "IndicativeRate", "Price"]]
        .sort_values(["BondType", "MaturityDate"])
        .reset_index(drop=True)
    )

tpf_fixed_rate_maturities(date)

Retrieve pre-defined maturity dates for LTN and NTN-F bonds.

This function fetches pre-defined maturity dates for 'LTN' (prefixadas) and 'NTN-F' (indexadas ao CDI) bond types from the cached ANBIMA dataset for a given reference date.

Parameters:

Name Type Description Default
date DateScalar

The reference date for maturity dates.

required

Returns:

Type Description
Series

pd.Series: A Series containing unique maturity dates for 'LTN' and 'NTN-F' bonds, sorted in ascending order.

Source code in pyield/anbima/tpf.py
def tpf_fixed_rate_maturities(date: DateScalar) -> pd.Series:
    """Retrieve pre-defined maturity dates for LTN and NTN-F bonds.

    This function fetches pre-defined maturity dates for 'LTN' (prefixadas) and
    'NTN-F' (indexadas ao CDI) bond types from the cached ANBIMA dataset
    for a given reference date.

    Args:
        date (DateScalar): The reference date for maturity dates.

    Returns:
        pd.Series: A Series containing unique maturity dates for 'LTN' and
            'NTN-F' bonds, sorted in ascending order.
    """
    maturity_dates = (
        tpf_data(date)
        .query("BondType in ['LTN', 'NTN-F']")["MaturityDate"]
        .drop_duplicates()
        .sort_values(ignore_index=True)
        .reset_index(drop=True)
    )
    return maturity_dates

tpf_web_data(date, bond_type=None)

Fetch and process TPF secondary market data directly from the ANBIMA website. Only the last 5 days of data are available in the ANBIMA website.

This function retrieves bond market data from the ANBIMA website for a specified reference date. It handles different file formats based on the date and attempts to download the data from both member and non-member URLs.

Parameters:

Name Type Description Default
date DateScalar

The reference date for the data.

required
bond_type str

Filter data by bond type. Defaults to None, which returns data for all bond types.

None

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame containing bond market data. Returns an empty DataFrame if data is not available for the specified date (weekends, holidays, or unavailable data).

Source code in pyield/anbima/tpf.py
def tpf_web_data(
    date: DateScalar, bond_type: str | BOND_TYPES | None = None
) -> pd.DataFrame:
    """Fetch and process TPF secondary market data directly from the ANBIMA website.
    Only the last 5 days of data are available in the ANBIMA website.

    This function retrieves bond market data from the ANBIMA website for a
    specified reference date. It handles different file formats based on the date
    and attempts to download the data from both member and non-member URLs.

    Args:
        date (DateScalar): The reference date for the data.
        bond_type (str, optional):  Filter data by bond type.
            Defaults to None, which returns data for all bond types.

    Returns:
        pd.DataFrame: A DataFrame containing bond market data.
            Returns an empty DataFrame if data is not available for the
            specified date (weekends, holidays, or unavailable data).
    """
    date = convert_input_dates(date)
    date_log = date.strftime("%d/%m/%Y")

    try:
        file_url = _build_file_url(date)
        df = _read_raw_df(file_url)
        if df.empty:
            logger.info(
                f"No Anbima TPF secondary market data ANBIMA for {date_log}."
                "Returning empty DataFrame."
            )
            return df
        df = _process_raw_df(df)
        if bond_type:
            norm_bond_type = _bond_type_mapping(bond_type)  # noqa
            df = df.query("BondType == @norm_bond_type").reset_index(drop=True)
        return df.sort_values(["BondType", "MaturityDate"]).reset_index(drop=True)
    except HTTPError as e:
        if e.code == 404:  # noqa
            logger.info(
                f"No Anbima TPF secondary market data for {date_log}. "
                "Returning empty DataFrame."
            )
            return pd.DataFrame()
        raise  # Propagate other HTTP errors
    except Exception:
        logger.exception(f"Error fetching TPF data for {date_log}")
        raise