@stoqey/ib (Low-Level SDK)IBApi Reference
Real-Time Market Data
Subscribing to streaming market quotes, tick prices, tick sizes, and Level II market depth.
Requesting Live Quotes (reqMktData)
Subscribe to real-time streaming quotes (Bid, Ask, Last, Volume, High, Low, Close) for a contract:
import { IBApi, EventName, Contract, SecType, TickType } from "@stoqey/ib";
const ib = new IBApi({ port: 7497 });
const contract: Contract = {
symbol: "AAPL",
secType: SecType.STK,
currency: "USD",
exchange: "SMART",
};
// Listen for price updates
ib.on(EventName.tickPrice, (reqId: number, tickType: TickType, price: number, attrib: any) => {
console.log(`ReqId ${reqId} TickType ${tickType} Price: $${price}`);
});
// Listen for volume/size updates
ib.on(EventName.tickSize, (reqId: number, tickType: TickType, size: number) => {
console.log(`ReqId ${reqId} TickType ${tickType} Size: ${size}`);
});
ib.connect();
ib.once(EventName.nextValidId, () => {
const reqId = 1;
const genericTickList = "233"; // 233 = RTVolume (Time & Sales)
const snapshot = false; // streaming
ib.reqMktData(reqId, contract, genericTickList, snapshot, false);
});Cancelling Market Data (cancelMktData)
To stop receiving streaming updates for a specific reqId:
ib.cancelMktData(reqId);Market Depth / Level II (reqMktDepth)
Request order book depth (bids and asks by price level and market maker):
ib.on(
EventName.updateMktDepth,
(reqId: number, position: number, operation: number, side: number, price: number, size: number) => {
const sideName = side === 1 ? "BID" : "ASK";
console.log(`[Level II] Row ${position} ${sideName} | Price: $${price} | Size: ${size}`);
}
);
ib.reqMktDepth(2, contract, 10, false, []);