node-ibapi-addon
Interactive Brokers API addon for Node.js compatible with IB API 9.72
This addon uses the latest stable Interactive Brokers POSIX C++ API.
Author: Jae Yang - dchem
Important
Note:
The inbound messages' are in the order it was received. Also, the outbound messages are rate limited to 50 msg/sec as IB requires it. Default behavior is that if your outbound rate is greater than that the 51st message will be handled in the next second.
If you would like to see the jsdoc of this project, just run it on ibapi.js file.
WARNING:
- Server messageId has changed from
svrError
toerror
Change Notes:
- YYYY-MM-DD - SEMVER - Notes
- 2019-09-19 - 2.6.2 - Fix build error in nodejs 10.16.0 LTS for Windows
- 2019-07-01 - 2.6.0 - Fix build error in nodejs 10.16.0 LTS
- 2018-08-01 - 2.5.6 - Fix CVE-2016-10593
- 2017-10-21 - 2.5.5 - Fix OS X installation
- 2017-06-14 - 2.5.4 - Supports API 9.72
- 2015-11-21 - 2.4.0 - Fixes server error message handling
- 2015-06-27 - 2.3.3 - Fixes build (removes sourceforge)
- 2015-06-09 - 2.3.1 - Swapped over to SEMVER, older download should use OldVer.
- 2015-03-01 - 2.3.0 - Node 0.12 & io.js 1.4.2 build compatible. Code cleanup and updated examples.
- 2015-01-07 - 2.2.0 - More CPU efficient
- 2014-12-29 - 2.1.0 - Uses event handlers instead of event emitters
- 2014-11-12 - 1.23.0 - Includes lib into the package itself
- 2014-09-10 - 1.21.0 - Supports API 9.71
- 2014-09-09 - 1.19.0 - Adds order.js and placeOrder can use order obj
- 2014-04-22 - 1.17.0 - Compatibility fix for API 9.70
- 2014-03-17 - 1.13.0 - Smoother installation to multiple OSes
- 2014-01-17 - 1.0.0 - all EWrapper events bound in ibapi.js
- 2013-09-02 - 0.0.0 - Initial commit
Linux Installation:
- Install Python 2.7
- Install unzip
sudo apt-get install unzip
npm install ibapi
Windows Installation
First step in Windows:
- Install Microsoft Windows Build Tools, open windows commandline:
npm install --global --production windows-build-tools
Install in Windows with POSIX library:
- Install MinGW and MSYS
- Install GCC library using MSYS
- Install msys-unzip instead of unzip from mingw-msys
mingw-get install msys-unzip
npm install ibapi
Install in Windows with Windows library:
- Download and install Git For Windows (https://gitforwindows.org)
- Open Git Bash
npm install ibapi
Additional Windows trickery:
IMPORTANT: You do not need to do the following if installing windows-build-tools succeeded
For Windows with MSVS 2017:
npm install ibapi --msvs_version=2017
Alternatively, include GYP_MSVS_VERSION=2017
in environment variables for Windows.
OS X
- Install xcode command line tools
- Install homebrew
npm install ibapi
Installation from git repo:
- Install Python 2.7
- Make sure to have node-gyp installed
node install -g node-gyp
- Run the preinstall.sh which does the following:
- Download the latest stable IB API into /import
- Copy the contents of IB API package's IBJts/source/CppClient/Shared into /import directory
- Copy the contents of IB API package's IBJts/source/CppClient/src into /import directory
- Add
#define IB_USE_STD_STRING
into the following files in /import directory: EClientSocketBase.cpp EPosixClientSocket.cpp - Downloads libjson 7.6.1
node-gyp rebuild
- If build fails because you have VS2012, use
node-gyp --msvs_version=2012 rebuild
Usage
- Set either IB Gateway or IB TWS to accept socket clients
- Require ibapi
- Create event handlers
- Register event handlers for messageIds
- Invoke connect()
- ...
- Profit!
// This file shows you one example of a barebones program that handles// server error messages. // Required package name is 'ibapi' If you use your own project path,// just use require('ibapi') from your project root as you would // normally do.var addon = messageIds = addonmessageIds contract = addoncontract order = addonorder; // The api object handles the client methods. For details, refer to // IB API documentation.var api = ; // Interactive Broker requires that you use orderId for every new order// inputted. The orderId is incremented everytime you submit an order.// Make sure you keep track of this.var orderId = -1; // Here we specify the event handlers.// Please follow this guideline for event handlers:// 1. Add handlers to listen to messages// 2. Each handler must have be a function (message) signaturevar { orderId = messageorderId; console;}; var { console;}; var { console; console;}; var { console; process;}; // After that, you must register the event handler with a messageId// For list of valid messageIds, see messageIds.js file.apihandlersmessageIdsnextValidId = handleValidOrderId;apihandlersmessageIdserror = handleServerError;apihandlersmessageIdsclientError = handleClientError;apihandlersmessageIdsdisconnected = handleDisconnected; // Connect to the TWS client or IB Gatewayvar connected = api; // Once connected, start processing incoming and outgoing messagesif connected api;
Included libraries
These are used as state stores and abstraction for IB API. You also have to pass instance of the object, such as 'order' to some of the client request functions.
Each of theese come with a constructor:
- order
- contract
- execution
- scannerSubscription
- contractDetails
Addon methods
beginProcessing()
: Entry point - starts the message processing.doAction(action)
: An action, custom function or API requests, can be bound to each eventsplaceSimpleOrder()
: Simpler order input method.
IB API Client Methods
processMsg()
: Msg processor - a POSIX implementation of network message
handler. This does not need to be called explicitly by the user
IB API Client Requests
Following commands are used for requesting specific action through IB API:
// returns right away // returns right away// placeOrder can take either // or // only accepts 1 for the time being (9.71) // not yet implemented // not yet implemented // not implemented
EWrapper Events
See messageIds.js
tickPrice
tickSize
tickOptionComputation
tickGeneric
tickString
tickEFP
orderStatus
openOrder
openOrderEnd
clientError
connectionClosed
updateAccountValue
updatePortfolio
updateAccountTime
accountDownloadEnd
nextValidId
contractDetails
bondContractDetails
contractDetailsEnd
execDetails
execDetailsEnd
error
updateMktDepth
updateMktDepthL2
updateNewsBulletin
managedAccounts
receiveFA
historicalData
scannerParameters
scannerData
scannerDataEnd
realtimeBar
fundamentalData
deltaNeutralValidation
tickSnapshotEnd
marketDataType
commissionReport
position
positionEnd
accountSummary
accountSummaryEnd
verifyMessageAPI
verifyCompleted
displayGroupList
displayGroupUpdated
nextValidId
disconnected
positionMulti
positionMultiEnd
accountUpdateMulti
accountUpdateMultiEnd
securityDefinitionOptionalParameter
securityDefinitionOptionalParameterEnd
softDollarTiers
Tests:
Uses mocha, so install it.
Issues:
License
Copyright (c) 2019 Jae Yang. See LICENSE file for license rights and limitations (MIT).
Why no github?
Because they are terrible.