Skip to content

Market Data

ibc.rest.market_data.MarketData

Client for interacting with the Market Data endpoints of the Interactive Brokers API.

__init__(ib_client, ib_session)

Initializes the MarketData client.

Parameters

ib_client : InteractiveBrokersClient The InteractiveBrokersClient Python Client.

InteractiveBrokersSession

The IB session handler.

snapshot(contract_ids, since=None, fields=None)

Get Market Data for the given conid(s).

Overview

The end-point will return by default bid, ask, last, change, change pct, close, listing exchange. The endpoint /iserver/accounts should be called prior to /iserver/marketdata/snapshot. To receive all available fields the /snapshot endpoint will need to be called several times.

Parameters

contract_ids : List[str] A list of contract Ids.

Union[str, Enum]

Frequency of cumulative performance data points: 'D'aily, 'M'onthly,'Q'uarterly. Can be one of 3 possible values: "D" "M" "Q".

Returns

list[MarketData]: A collection of `MarketData` resources.
Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.snapshot(contract_ids=['265598'])

market_history(contract_id, period, market_bar=None, exchange=None, outside_regular_trading_hours=True)

Get historical market Data for given conid, length of data is controlled by 'period' and 'bar'.

Parameters

contract_id : str A contract Id.

str

Available time period: {1-30}min, {1-8}h, {1-1000}d, {1-792}w, {1-182}m, {1-15}y

Union[str, Enum] (optional, Default=None):

The bar type you want the data in.

str (optional, Default=None):

Exchange of the conid.

bool (optional, Default=True)

For contracts that support it, will determine if historical data includes outside of regular trading hours.

Returns

HistoryData: A HistoryData resource containing bar data.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.market_history(contract_id='265598', period='1d')

unsubscribe(contract_id)

Cancel market data subscription for the given contract.

Parameters

contract_id : str The contract ID to unsubscribe from.

Returns

dict: An unsubscribe confirmation resource.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.unsubscribe(contract_id='265598')

unsubscribe_all()

Cancel all market data subscriptions.

Returns

dict: An unsubscribe confirmation resource.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.unsubscribe_all()

market_history_beta(contract_id, period, market_bar=None, outside_regular_trading_hours=True)

Get historical market data using the beta HMDS endpoint.

Parameters

contract_id : str A contract ID.

str

Available time period (e.g. 1d, 1w, 1m).

Union[str, Enum] (optional, Default=None)

The bar size.

bool (optional, Default=True)

Whether to include outside regular trading hours data.

Returns

dict: A collection of Bar resources.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.market_history_beta(
    contract_id='265598',
    period='1d',
    bar='1min'
)

snapshot_beta(contract_ids, fields=None)

Get market data snapshot using the beta endpoint.

Unlike :meth:snapshot, this uses the /api/md/snapshot beta endpoint and does not accept a since parameter.

Parameters

contract_ids : list[str] A list of contract IDs.

list[str | Enum] | None (optional, Default=None)

Fields to return.

Returns

list[MarketDataModel]: A collection of MarketData resources.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.snapshot_beta(contract_ids=['265598'])

scanner_beta(scanner)

Run a market scanner using the beta HMDS endpoint.

Parameters

scanner : dict A scanner definition payload.

Returns

dict: A collection of contract resources matching the scanner criteria.

Usage

>>> market_data_services = ibc_client.market_data
>>> market_data_services.scanner_beta(
    scanner={
        "instrument": "STK",
        "type": "TOP_PERC_GAIN",
        "location": "STK.US.MAJOR",
        "size": "25"
    }
)