### Install @stoqey/ib with npm Source: https://stoqey.github.io/ib-doc/index Command to install the @stoqey/ib library using npm. ```Shell $ npm install @stoqey/ib ``` -------------------------------- ### Local Testing Setup with Docker for IBApiNext Source: https://stoqey.github.io/ib-doc/index This snippet provides a step-by-step guide to set up a local testing environment for IBApiNext using Docker and IB Gateway. It covers dependency installation, environment configuration, TypeScript compilation, and Docker container management. ```Shell yarn ``` ```Shell cp sample.env .env ``` ```Shell yarn build ``` ```Shell docker-compose up ``` ```Shell docker-compose down ``` -------------------------------- ### Install @stoqey/ib with yarn Source: https://stoqey.github.io/ib-doc/index Command to install the @stoqey/ib library using yarn. ```Shell $ yarn add @stoqey/ib ``` -------------------------------- ### Run IBApiNext Account Summary Tool Source: https://stoqey.github.io/ib-doc/index This command-line example demonstrates how to run the `account-summary.js` tool from the `src/tools` folder using Node.js. It fetches account summary details, filters by specific tags, and watches for updates on a specified port. ```Shell node ./dist/tools/account-summary.js -group=All -tags="NetLiquidation,MaintMarginReq" -watch -inc -port=4002 ``` -------------------------------- ### Print all portfolio positions using @stoqey/ib Source: https://stoqey.github.io/ib-doc/index This example demonstrates how to connect to Interactive Brokers TWS/IB Gateway, request all portfolio positions, and print them to the console. It handles errors and disconnects after all positions are received. ```Typescript /* Example: Print all portfolio positions to console. */ import { IBApi, EventName, ErrorCode, Contract } from "@stoqey/ib"; // create IBApi object const ib = new IBApi({ // clientId: 0, // host: '127.0.0.1', port: 7497, }); // register event handler let positionsCount = 0; ib.on(EventName.error, (err: Error, code: ErrorCode, reqId: number) => { console.error(`${err.message} - code: ${code} - reqId: ${reqId}`); }) .on( EventName.position, (account: string, contract: Contract, pos: number, avgCost?: number) => { console.log(`${account}: ${pos} x ${contract.symbol} @ ${avgCost}`); positionsCount++; }, ) .once(EventName.positionEnd, () => { console.log(`Total: ${positionsCount} positions.`); ib.disconnect(); }); // call API functions ib.connect(); ib.reqPositions(); ``` -------------------------------- ### Place a limit order using @stoqey/ib Source: https://stoqey.github.io/ib-doc/index This example shows how to place a limit order for a stock (AMZN) using the @stoqey/ib library. It first requests a valid order ID, then constructs a contract and an order object, and finally places the order. ```Typescript ib.once(EventName.nextValidId, (orderId: number) => { const contract: Contract = { symbol: "AMZN", exchange: "SMART", currency: "USD", secType: SecType.STK, }; const order: Order = { orderType: OrderType.LMT, action: OrderAction.BUY, lmtPrice: 1, orderId, totalQuantity: 1, account: "YOUR_ACCOUNT_ID", }; ib.placeOrder(orderId, contract, order); }); ib.connect(); ib.reqIds(); ``` -------------------------------- ### Overview of IBApi and IBApiNext in @stoqey/ib Source: https://stoqey.github.io/ib-doc/index Documentation detailing the two main APIs provided by @stoqey/ib: IBApi, which closely replicates the official TWS API, and IBApiNext, a preview API focusing on usability with RxJS instead of request/event design. ```APIDOC IBApi: - Replicates official TWS API closely. - Easy to migrate/port existing code. - Implements all functions and provides same event callbacks as official TWS API. IBApiNext (Preview): - Goal: Same functionality as IBApi, but focused on usability. - Uses RxJS instead of request/event design. - Still in preview stage; not all functions available; interfaces may change (though public signatures of existing functions are expected to be stable). ``` -------------------------------- ### Run Jest Tests for IBApiNext Source: https://stoqey.github.io/ib-doc/index This snippet shows how to execute unit tests for IBApiNext using Jest. It includes commands for running a single test file, multiple tests by path, and all tests in the project. ```Shell jest src/test/unit/api/api.test.ts ``` ```Shell yarn test ``` -------------------------------- ### IBApiNext Modules and Enums Reference Source: https://stoqey.github.io/ib-doc/index This section provides a reference to key modules and enumeration types available in the `@stoqey/ib` library, used for interacting with the Interactive Brokers API. It lists various settings and data types for API operations. ```APIDOC @stoqey/ib modules: - default - BarSizeSetting (enum) - ConjunctionConnection (enum) - ConnectionState (enum) - DurationUnit (enum) - ErrorCode (enum) - EventName (enum) - FADataType (enum) - IBApiNextTickType (enum) - IBApiTickType (enum) - Instrument (enum) - LocationCode (enum) - LogLevel (enum) - MIN_SERVER_VER (enum) - MarketDataType (enum) - OptionExerciseAction (enum) - OptionType (enum) - OrderAction (enum) - OrderConditionType (enum) - OrderStatus (enum) - OrderType (enum) ``` -------------------------------- ### Interactive Brokers TWS/IB Gateway Socket Ports Source: https://stoqey.github.io/ib-doc/index Reference table for default socket ports used to connect to Interactive Brokers TWS (Trader Workstation) and IB Gateway for both live and paper trading accounts. ```APIDOC Platform | Port --- | --- IB Gateway live account | 4001 IB Gateway paper account | 4002 TWS Live Account | 7496 TWS papertrading account | 7497 ``` -------------------------------- ### Handle pnlSingle event signature change in @stoqey/ib 1.2.x Source: https://stoqey.github.io/ib-doc/index Demonstrates the breaking change in @stoqey/ib versions 1.2.1 and higher, where `Number.MAX_VALUE` is replaced by `undefined` for unavailable values in the `pnlSingle` event callback. The first code block shows the old signature, and the second shows the new one. ```Typescript ib.on(EventName.pnlSingle, ( reqId: number, pos: number, dailyPnL: number, unrealizedPnL: number, realizedPnL: number, value: number ) => { ... } ); // now is (look at `unrealizedPnL` and `realizedPnL`) ib.on(EventName.pnlSingle, ( reqId: number, pos: number, dailyPnL: number, unrealizedPnL: number | undefined, realizedPnL: number | undefined, value: number ) => { ... } ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.