Pular para conteúdo

Futures Data

futures(contract_code, date)

Fetches data for a specified futures contract based on type and reference date.

Parameters:

Name Type Description Default
contract_code str

The B3 futures contract code identifying the derivative. Supported contract codes are: - "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3. - "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3. - "FRC": Forward Rate Agreement (FRA) from B3. - "DAP": DI x IPCA Spread Futures. - "DOL": U.S. Dollar Futures from B3. - "WDO": Mini U.S. Dollar Futures from B3. - "IND": Ibovespa Futures from B3. - "WIN": Mini Ibovespa Futures from B3.

required
date DateScalar

The date for which to fetch the data. If the reference date is a string, it should be in 'DD-MM-YYYY' format.

required

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame containing the fetched data for the specified futures contract.

Raises:

Type Description
ValueError

If the futures contract code is not recognized or supported.

Examples:

>>> futures("DI1", "31-05-2024")
    TradeDate TickerSymbol  ... SettlementRate  ForwardRate
0  2024-05-31       DI1M24  ...        0.10399      0.10399
1  2024-05-31       DI1N24  ...         0.1039     0.103896
2  2024-05-31       DI1Q24  ...         0.1037     0.103517
3  2024-05-31       DI1U24  ...         0.1036       0.1034
...
>>> futures("DAP", "31-05-2024")
    TradeDate TickerSymbol  ... SettlementRate  ForwardRate
0  2024-05-31       DAPM24  ...         0.0555       0.0555
1  2024-05-31       DAPN24  ...        0.07524     0.086254
2  2024-05-31       DAPQ24  ...         0.0885     0.106631
3  2024-05-31       DAPU24  ...         0.0855     0.078171
...
Source code in pyield/b3/futures/__init__.py
def futures(
    contract_code: ContractOptions | str,
    date: DateScalar,
) -> pd.DataFrame:
    """
    Fetches data for a specified futures contract based on type and reference date.

    Args:
        contract_code (str): The B3 futures contract code identifying the derivative.
            Supported contract codes are:
            - "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
            - "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
            - "FRC": Forward Rate Agreement (FRA) from B3.
            - "DAP": DI x IPCA Spread Futures.
            - "DOL": U.S. Dollar Futures from B3.
            - "WDO": Mini U.S. Dollar Futures from B3.
            - "IND": Ibovespa Futures from B3.
            - "WIN": Mini Ibovespa Futures from B3.
        date (DateScalar): The date for which to fetch the data.
            If the reference date is a string, it should be in 'DD-MM-YYYY' format.

    Returns:
        pd.DataFrame: DataFrame containing the fetched data for the specified futures
            contract.

    Raises:
        ValueError: If the futures contract code is not recognized or supported.

    Examples:
        >>> futures("DI1", "31-05-2024")
            TradeDate TickerSymbol  ... SettlementRate  ForwardRate
        0  2024-05-31       DI1M24  ...        0.10399      0.10399
        1  2024-05-31       DI1N24  ...         0.1039     0.103896
        2  2024-05-31       DI1Q24  ...         0.1037     0.103517
        3  2024-05-31       DI1U24  ...         0.1036       0.1034
        ...

        >>> futures("DAP", "31-05-2024")
            TradeDate TickerSymbol  ... SettlementRate  ForwardRate
        0  2024-05-31       DAPM24  ...         0.0555       0.0555
        1  2024-05-31       DAPN24  ...        0.07524     0.086254
        2  2024-05-31       DAPQ24  ...         0.0885     0.106631
        3  2024-05-31       DAPU24  ...         0.0855     0.078171
        ...
    """
    converted_date = dc.convert_input_dates(date)
    selected_contract = str(contract_code).upper()

    if _is_trading_day(converted_date):
        # É um dia de negociação intraday
        time = dt.datetime.now(BZ_TIMEZONE).time()
        if time < INTRADAY_START_TIME:  # Mercado não está aberto ainda
            logger.warning("Market is not open yet. Returning an empty DataFrame. ")
            return pd.DataFrame()

        # Existe a chance de que os dados consolidados estejam disponíveis após as 20h
        if time >= HISTORICAL_START_TIME:
            df_hist = _get_historical_data(selected_contract, converted_date)
            if not df_hist.empty:
                logger.info("Consolidated data is already available and will be used.")
                return df_hist

        # Mercado está aberto e não há dados consolidados disponíveis ainda
        return fetch_intraday_df(selected_contract)

    else:  # É um dia histórico
        return _get_historical_data(selected_contract, converted_date)