Quantstrip docs
Open Dashboard

Connecting

IB wraps ibapi's asynchronous request/callback API in blocking methods and is intended for use as a context manager. Client IDs auto-assign from a thread-safe pool of 1–999 if none is specified:

with IB() as ib:                  # auto-assigned client_id
    df = ib.get_historical_data(contract, ...)

with IB() as ib1:                 # auto-assigned client_id=1
    with IB() as ib2:              # auto-assigned client_id=2
        ...                        # both connected simultaneously

To connect explicitly: IB(host="127.0.0.1", port=7497, client_id=None), then ib.connect_client(host, port, client_id) / ib.disconnect_client(). Default port 7497 is TWS's paper-trading socket port; verify the port in TWS/Gateway API settings for other configurations.

IB — blocking methods

MethodReturns
get_next_order_id(timeout=5.0)Next valid order ID from IB.
get_historical_data(contract, ...)Historical bars as a DataFrame.
get_executions(executionFilter=None, timeout=10.0)Trade executions, list[dict].
get_commissions(executionFilter=None, timeout=10.0)Commission reports, list[dict].
get_last_price(contract, timeout=5.0)One-shot last-trade price, float or None.
get_market_snapshot(contract, timeout=5.0)Dict with last/bid/ask/size.
get_positions(timeout=10.0)Account positions, list[dict].
get_open_orders(timeout=10.0)Open orders, list[dict].
get_account_summary(group="All", tags=None, timeout=10.0)Account summary as a DataFrame.
get_portfolio(account="", timeout=10.0)Portfolio snapshot, list[dict].
get_order_status(order_id=None, timeout=5.0)Status for one order, or a full map if order_id is omitted.
get_contract_details(contract, timeout=10.0)Contract detail records, list[dict].
is_connected()bool.

Every method blocks on a threading.Event with its own timeout and returns an empty collection or None on timeout. Errors surface as log messages, not exceptions, matching IB's own asynchronous error-callback behavior.

ib_objects.py — contract and order helpers

def ib_contract(symbol):
    """ Creates an IB contract object for US ETF contract """
    contract = Contract()
    contract.currency = "USD"
    contract.exchange = "SMART"
    contract.secType = "STK"
    contract.symbol = symbol
    return contract

def ib_order(quantity, order_ref='', orderType="MOC"):
    """ Creates an IB order object """
    direction = "BUY" if quantity > 0 else "SELL"
    order = Order()
    order.action = direction
    order.orderType = orderType
    order.totalQuantity = abs(quantity)
    order.exchange = "SMART"
    order.orderRef = order_ref
    return order
ib_contract hardcodes secType="STK", currency="USD", and exchange="SMART", limiting it to US-listed stocks and ETFs. Options, futures, or non-USD instruments require custom Contract construction; no parameterized version of this helper exists. ib_order's orderType defaults to "MOC" (market-on-close), not a market order; pass orderType="MKT" explicitly for a market order (see IBKR template).