IbkrApi.Websocket (ibkr_api v1.0.3)

View Source

WebSocket client for Interactive Brokers Client Portal API streaming data.

This module provides a WebSocket client that connects to the IBKR Client Portal Gateway to receive real-time market data, order updates, and portfolio P&L streams.

Usage

To use this module, create your own module and use IbkrApi.Websocket:

defmodule MyIbkrClient do
  use IbkrApi.Websocket

  def handle_event({:market_data, data}, state) do
    IO.inspect(data, label: "Market Data")
    {:ok, state}
  end

  def handle_event({:order_update, order}, state) do
    IO.inspect(order, label: "Order Update")
    {:ok, state}
  end

  def handle_event({:pnl_update, pnl}, state) do
    IO.inspect(pnl, label: "P&L Update")
    {:ok, state}
  end

  def handle_event(_event, state) do
    {:ok, state}
  end
end

# Start the WebSocket client
{:ok, pid} = MyIbkrClient.start_link(%{})

# Subscribe to market data for contract ID 8314 (IBM)
MyIbkrClient.subscribe_to_market_data(pid, [8314], ["31", "83"])

# Subscribe to order updates
MyIbkrClient.subscribe_to_order_updates(pid)

# Subscribe to P&L updates
MyIbkrClient.subscribe_to_pnl(pid)

Connection Requirements

Before connecting, ensure:

  1. IBKR Client Portal Gateway is running (usually on localhost:5000)
  2. You are logged in via the Gateway web interface
  3. Your account has appropriate market data subscriptions

Message Formats

Market Data Subscription

  • Subscribe: smd+<CONID>+{"fields":["31","83"],"tempo":1000,"snapshot":true}
  • Unsubscribe: umd+<CONID>+{}

Order Updates

  • Subscribe: sor+{}
  • Unsubscribe: uor+{}

P&L Updates

  • Subscribe: spl+{}
  • Unsubscribe: upl+{}

Heartbeat

  • Send: ech+hb (recommended every 10 seconds)

Field IDs for Market Data

Common field IDs for market data subscriptions:

  • "31": Last price
  • "83": Percent change
  • "84": High
  • "85": Low
  • "86": Volume
  • "87": Close
  • "88": Bid
  • "89": Ask
  • "7295": Market cap
  • "7296": Company name

Rate Limits

IBKR limits concurrent market data streams to approximately 5 instruments per session. Plan your subscriptions accordingly.

SSL Configuration

The local Gateway uses a self-signed SSL certificate. This module automatically configures SSL options to accept self-signed certificates for localhost connections.

Summary

Functions

Sends a heartbeat message to keep the connection alive.

Starts a WebSocket connection to the IBKR Client Portal Gateway.

Subscribes to market data for the given contract IDs.

Subscribes to order updates for all accounts in the current session.

Subscribes to portfolio P&L updates.

Unsubscribes from market data for the given contract IDs.

Unsubscribes from order updates.

Unsubscribes from portfolio P&L updates.

Functions

send_heartbeat(pid)

Sends a heartbeat message to keep the connection alive.

start_link(module, initial_state, opts \\ [])

Starts a WebSocket connection to the IBKR Client Portal Gateway.

Options

  • :url - WebSocket URL (default: "wss://localhost:5000/v1/api/ws")
  • :ssl_opts - SSL options for the connection
  • :heartbeat - Whether to send automatic heartbeats (default: true)

subscribe_to_market_data(pid, contract_ids, fields, opts \\ %{})

Subscribes to market data for the given contract IDs.

Parameters

  • pid - WebSocket process PID
  • contract_ids - List of contract IDs to subscribe to
  • fields - List of field IDs to request (e.g., ["31", "83"])
  • opts - Additional options:
    • :tempo - Update frequency in milliseconds (default: 1000)
    • :snapshot - Request initial snapshot (default: true)

subscribe_to_order_updates(pid)

Subscribes to order updates for all accounts in the current session.

subscribe_to_pnl(pid)

Subscribes to portfolio P&L updates.

unsubscribe_from_market_data(pid, contract_ids)

Unsubscribes from market data for the given contract IDs.

unsubscribe_from_order_updates(pid)

Unsubscribes from order updates.

unsubscribe_from_pnl(pid)

Unsubscribes from portfolio P&L updates.