IB@stoqey/ib
@stoqey/ib (Low-Level SDK)Core Architecture

Orders & Execution

Creating order objects, order types, placing orders, and tracking order execution status.

Creating an Order

An Order object specifies the action, quantity, order type, price limits, and account details.

import { Order, OrderAction, OrderType } from "@stoqey/ib";

const order: Order = {
  orderId: 1001,           // Unique numeric Order ID
  action: OrderAction.BUY, // 'BUY' or 'SELL'
  orderType: OrderType.LMT, // 'LMT', 'MKT', 'STP', etc.
  totalQuantity: 10,
  lmtPrice: 150.00,
  account: "DU1234567",
  tif: "DAY",              // Time in Force: 'DAY', 'GTC', etc.
};

Placing an Order (placeOrder)

To place an order, request the next valid order ID from TWS first via reqIds():

import { IBApi, EventName, Contract, SecType, Order, OrderAction, OrderType } from "@stoqey/ib";

const ib = new IBApi({ port: 7497 });

ib.once(EventName.nextValidId, (orderId: number) => {
  const contract: Contract = {
    symbol: "NVDA",
    secType: SecType.STK,
    currency: "USD",
    exchange: "SMART",
  };

  const order: Order = {
    orderId,
    action: OrderAction.BUY,
    orderType: OrderType.MKT,
    totalQuantity: 5,
  };

  // Submit order to TWS
  ib.placeOrder(orderId, contract, order);
});

ib.connect();
ib.reqIds();

Order Status Events

Listen for live order updates using orderStatus and openOrder:

ib.on(
  EventName.orderStatus,
  (
    orderId: number,
    status: string,
    filled: number,
    remaining: number,
    avgFillPrice: number,
    permId: number,
    parentId: number,
    lastFillPrice: number,
    clientId: number,
    whyHeld?: string,
    mktCapPrice?: number
  ) => {
    console.log(`Order ${orderId} Status: ${status} | Filled: ${filled}/${filled + remaining} @ $${avgFillPrice}`);
  }
);

Cancelling Orders

To cancel an active order:

ib.cancelOrder(orderId, "");

On this page