Typescript API
@stoqey/ib
is an unofficial Interactive Brokers TWS (or IB Gateway) Typescript API client library for Node.js. It is a direct port of Interactive Brokers' Java Client Version 9.76 from May 08 2019.
Refer to the Trader Workstation API for official documentation and the C#/Java/VB/C++/Python client.
The module makes a socket connection to TWS (or IB Gateway) using the net module and all messages are entirely processed in Typescript. It uses EventEmitter to pass the result back to user.
Installation
$ npm install @stoqey/ib
or
$ yarn add @stoqey/ib
API Documenation
See API documentation here.
IB socket ports
Platform | Port |
---|
IB Gateway live account | 4001 |
IB Gateway paper account | 4002 |
TWS Live Account | 7496 |
TWS papertrading account | 7497 |
Important
API is returning Number.MAX_SAFE_INTEGER
when there is no value from IB, commonly seen when there is no bid / offer or other missing market data.
Example
import { IBApi, EventName, ErrorCode, Contract } from "@stoqey/ib";
const ib = new IBApi({
port: 7497,
});
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} posistions.`);
ib.disconnect();
});
ib.connect();
ib.reqPositions();
How to contribute
IB does regularly release new API versions, so this library will need permanent maintenance in oder to stay up-to-date with latest TWS features.
Also, there is not much testing code yet. Ideally there should be at least one test-case for each public function.
In addition to that.. a little demo / example app would be nice, to demonstrate API usage (something like a little live-portoflio-viewer app for node.js console?).
Any kind of bugfixes are welcome as well.
If you want to contribute, read the Developer Guide and start coding.
License
The MIT License (MIT)
Copyright (c) 2020 Stoqey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.