@stoqey/ib (Low-Level SDK)IBApi Reference
Historical Data & Scanners
Requesting historical OHLCV candlestick bars, tick data, and market scanner subscriptions.
Requesting Historical Bars (reqHistoricalData)
Retrieve historical Open, High, Low, Close, Volume (OHLCV) candlestick bars:
import { IBApi, EventName, Contract, SecType, BarSizeSetting, WhatToShow } from "@stoqey/ib";
const ib = new IBApi({ port: 7497 });
const contract: Contract = {
symbol: "MSFT",
secType: SecType.STK,
currency: "USD",
exchange: "SMART",
};
ib.on(EventName.historicalData, (reqId: number, bar: any) => {
console.log(`Bar Date: ${bar.date} | Open: ${bar.open} | High: ${bar.high} | Low: ${bar.low} | Close: ${bar.close} | Vol: ${bar.volume}`);
});
ib.once(EventName.historicalDataEnd, (reqId: number, start: string, end: string) => {
console.log(`Finished receiving historical bars from ${start} to ${end}`);
ib.disconnect();
});
ib.connect();
ib.once(EventName.nextValidId, () => {
ib.reqHistoricalData(
1, // reqId
contract, // contract
"", // endDateTime (empty string = current time)
"1 M", // durationStr ("1 D", "1 W", "1 M", "1 Y")
BarSizeSetting.MIN_1, // barSizeSetting ("1 mins", "5 mins", "1 day")
WhatToShow.TRADES, // whatToShow ("TRADES", "MIDPOINT", "BID", "ASK")
1, // useRTH (1 = Regular Trading Hours only, 0 = All hours)
1, // formatDate (1 = YYYYMMDD HH:mm:ss, 2 = epoch format)
false // keepUpToDate (streaming updates)
);
});Valid Bar Sizes & Duration Strings
Common BarSizeSetting Options
BarSizeSetting.SEC_1("1 secs")BarSizeSetting.MIN_1("1 min")BarSizeSetting.MIN_5("5 mins")BarSizeSetting.MIN_15("15 mins")BarSizeSetting.HOUR_1("1 hour")BarSizeSetting.DAY_1("1 day")
Duration String Formats
"60 S"- 60 Seconds"1 D"- 1 Day"1 W"- 1 Week"1 M"- 1 Month"1 Y"- 1 Year
Market Scanner (reqScannerSubscription)
Scan market instruments matching criteria (e.g., Top % Gainers, Most Active, High Option Volume):
import { ScannerSubscription } from "@stoqey/ib";
const scanSub: ScannerSubscription = {
numberOfRows: 10,
instrument: "STK",
locationCode: "STK.US.MAJOR",
scanCode: "TOP_PERC_GAIN",
};
ib.on(EventName.scannerData, (reqId, rank, details, distance, benchmark, projection, legsStr) => {
console.log(`#${rank + 1}: ${details.contract.symbol} (${details.contract.secType})`);
});
ib.reqScannerSubscription(1, scanSub, [], []);