Skip to content

Contracts

ibc.rest.contract.Contracts

Client for managing contract-related operations via the Interactive Brokers API.

__init__(ib_client, ib_session)

Initializes the Contracts client.

Parameters

ib_client : InteractiveBrokersClient The InteractiveBrokersClient Python Client.

InteractiveBrokersSession

The IB session handler.

contract_info(contract_id)

Get contract details, you can use this to prefill your order before you submit an order.

Parameters

contract_id : str The contract ID you want details for.

Returns

Contract: A Contract resource.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.contract_info(
    contract_id='265598'
)

search_futures(symbols)

Returns a list of non-expired future contracts for given symbol(s).

Parameters

symbols : str List of case-sensitive symbols separated by comma

Returns

list: A collection of Futures resource.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.search_futures(
    symbols=['CL', 'ES']
)

search_symbol(symbol, name=False, security_type=None)

Search by symbol or name.

Parameters

symbol : str The symbol to be searched.

bool (optional, Default=False)

Set to True if searching by name, False if searching by symbol.

str (optional, default=True)

The security type of the symbol.

Returns

list: A collection of Contract resources.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.search_symbol(
    symbol='AAPL',
    name='Apple'
)

search_multiple_contracts(contract_ids)

Returns a list of security definitions for the given conids.

Parameters

contract_ids : List[str] A list of Contract IDs.

Returns

list: A collection of Contract resources.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.search_multiple_contracts(
    contract_ids=['265598']
)

search_stocks(symbols)

Returns a list of stock contracts for the given symbol(s).

Parameters

symbols : List[str] List of case-sensitive symbols separated by comma.

Returns

dict: A collection of stock contract resources.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.search_stocks(
    symbols=['AAPL', 'MSFT']
)

trading_schedule(asset_class, symbol, exchange=None)

Returns the trading schedule for the given symbol.

Parameters

asset_class : str The asset class of the instrument (e.g. STK, OPT, FUT).

str

The symbol to query.

str (optional, Default=None)

The exchange to query. If not provided, returns all exchanges.

Returns

dict: A trading schedule resource.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.trading_schedule(
    asset_class='STK',
    symbol='AAPL'
)

secdef_strikes(contract_id, sectype, month, exchange=None)

Returns available strikes for an option or warrant contract.

Parameters

contract_id : str The underlying contract ID.

str

The security type (e.g. OPT, WAR).

str

The expiry month in YYYYMM format.

str (optional, Default=None)

The exchange to filter by.

Returns

dict: A collection of available strikes.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.secdef_strikes(
    contract_id='265598',
    sectype='OPT',
    month='202501'
)

secdef_info(contract_id, sectype, month=None, exchange=None, strike=None, right=None)

Returns detailed security definition information.

Parameters

contract_id : str The underlying contract ID.

str

The security type (e.g. OPT, FUT, WAR).

str (optional, Default=None)

The expiry month in YYYYMM format.

str (optional, Default=None)

The exchange to filter by.

str (optional, Default=None)

The strike price.

str (optional, Default=None)

The right side (C for call, P for put).

Returns

list[SecdefInfo]: A collection of security definition resources.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.secdef_info(
    contract_id='265598',
    sectype='OPT',
    month='202501',
    strike='150',
    right='C'
)

contract_algos(contract_id, algos=None, add_description=None, add_params=None)

Returns the IB algorithm parameters for a given contract.

Parameters

contract_id : str The contract ID.

str (optional, Default=None)

List of algo ids delimited by semicolon to filter.

str (optional, Default=None)

Whether to add algo description (1 for yes).

str (optional, Default=None)

Whether to add algo parameters (1 for yes).

Returns

list: A collection of algorithm parameter resources.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.contract_algos(
    contract_id='265598'
)

contract_rules(contract_id, is_buy=True)

Returns trading rules for a specific contract.

Parameters

contract_id : str The contract ID.

bool (optional, Default=True)

Whether the rules are for a buy (True) or sell (False).

Returns

dict: A contract rules resource with order types, TIF types, etc.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.contract_rules(
    contract_id='265598',
    is_buy=True
)

contract_info_and_rules(contract_id, is_buy=True)

Returns both contract info and trading rules in a single call.

Parameters

contract_id : str The contract ID.

bool (optional, Default=True)

Whether the rules are for a buy (True) or sell (False).

Returns

dict: A combined contract info and rules resource.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.contract_info_and_rules(
    contract_id='265598',
    is_buy=True
)

currency_pairs(currency)

Returns available currency pairs for trading.

Parameters

currency : str The base currency (e.g. USD).

Returns

dict: A collection of available currency pairs.

Usage

>>> contracts_service = ibc_client.contracts
>>> contracts_service.currency_pairs(currency='USD')