IB@stoqey/ib
@stoqey/ibkr (High-Level Wrapper)Getting Started

Installation & Quickstart

How to install @stoqey/ibkr and initialize the client connection.

Package Installation

Install @stoqey/ibkr using your preferred Node.js package manager:

# npm
npm install @stoqey/ibkr

# yarn
yarn add @stoqey/ibkr

# pnpm
pnpm add @stoqey/ibkr

Initializing @stoqey/ibkr

@stoqey/ibkr provides a single asynchronous entry point ibkr() that establishes socket connections to Interactive Brokers and initializes all cached state managers (AccountSummary, Portfolios, MarketDataManager, Orders).

Method 1: Environment Variables (Default)

Create a .env file in the root of your project:

IBKR_HOST=127.0.0.1
IBKR_PORT=7497
IBKR_CLIENT_ID=0

Initialize @stoqey/ibkr in code:

import ibkr from "@stoqey/ibkr";

async function main() {
  // Connects to IBKR using .env options
  await ibkr();

  console.log("IBKR Client Connected & Managers Initialized!");
}

main();

Method 2: Programmatic Options

You can pass creation options directly to ibkr():

import ibkr from "@stoqey/ibkr";

await ibkr({
  host: "127.0.0.1",
  port: 7497,
  clientId: 123,
  reconnectInterval: 5000,
  connectionWatchdogInterval: 1,
});

What Happens During Initialization?

When await ibkr() resolves:

  1. TCP socket connection is established to TWS or IB Gateway.
  2. AccountSummary.Instance starts listening and caching account balances.
  3. Portfolios.Instance requests current open positions and caches portfolio state.
  4. Orders.Instance subscribes to live open orders and filled trade events.
  5. MarketDataManager.Instance becomes ready for contract lookup and quote streaming.
  6. IBKREvents.Instance fires the IBKR_CONNECTED event.

On this page