@stoqey/ib (Low-Level SDK)IBApi Reference
Account & Portfolio API
Requesting account summaries, open positions, PnL updates, and margin information.
Account Summary (reqAccountSummary)
Retrieve aggregated account details (e.g. Net Liquidation Value, Total Cash Value, Buying Power, Maintenance Margin):
import { IBApi, EventName } from "@stoqey/ib";
const ib = new IBApi({ port: 7497 });
ib.on(EventName.accountSummary, (reqId: number, account: string, tag: string, value: string, currency: string) => {
console.log(`[${account}] ${tag} = ${value} ${currency}`);
});
ib.once(EventName.accountSummaryEnd, (reqId: number) => {
console.log("Account summary request completed.");
ib.cancelAccountSummary(reqId);
});
ib.connect();
ib.reqAccountSummary(1, "All", "NetLiquidation,TotalCashValue,BuyingPower,GrossPositionValue");Positions (reqPositions)
Request all current portfolio positions across managed accounts:
import { IBApi, EventName, Contract } from "@stoqey/ib";
ib.on(EventName.position, (account: string, contract: Contract, pos: number, avgCost?: number) => {
console.log(`${account} holds ${pos} shares of ${contract.symbol} @ $${avgCost ?? 0}`);
});
ib.once(EventName.positionEnd, () => {
console.log("Finished receiving positions.");
ib.cancelPositions();
});
ib.reqPositions();Profit & Loss (reqPnL & reqPnLSingle)
Subscribe to real-time PnL updates for an account or individual position:
Account PnL
ib.on(EventName.pnl, (reqId: number, dailyPnL: number, unrealizedPnL?: number, realizedPnL?: number) => {
console.log(`Daily PnL: $${dailyPnL} | Unrealized: $${unrealizedPnL ?? 0} | Realized: $${realizedPnL ?? 0}`);
});
ib.reqPnL(1, "DU1234567", "");Position PnL
ib.on(EventName.pnlSingle, (reqId: number, pos: number, dailyPnL: number, unrealizedPnL?: number, realizedPnL?: number, value?: number) => {
console.log(`Position PnL: Daily $${dailyPnL} | Unrealized: $${unrealizedPnL ?? 0}`);
});
ib.reqPnLSingle(2, "DU1234567", "", 265598); // 265598 = conId for AAPL