-
-
Notifications
You must be signed in to change notification settings - Fork 48
IBApi next generation (IBApiNext)
Matthias Frener edited this page Feb 21, 2021
·
17 revisions
IBApiNext will be a second API interface, in parallel to the IBApi.
While the goal of IBApi is it to replicate the official TWS API as close as possible, IBApiNext will add additional convenience that makes developer-life easier.
- No more request-function, no more cancel-function, not more event-callback, no more request ids, not more ticker ids.
It is rxjs now. Example: keep track on the PnL for a position with IBApiNext
apiNext.
.getPnLSingle(
account,
model,
conId
)
.subscribe(
(pnlSingle) => {
this.updatePositionPnl(pnlSingle);
},
(err: IBApiError) => {
console.error("getPnLSingle failed with" + err.error.message});
}
);
-
No more Number.MAX_VALUE. If a value is not available, it is no more Number.MAX_VALUE, but is is undefined on IBApiNext.
-
Support for full and differntial updates
On functions that return a list, you can select if you want to get full or differential updates only.
Example:
/**
* Create a subscription to receive the positions on all accessible accounts.
*
* All positions are sent on the first event.
* Use incrementalUpdates argument to switch between incremental or full update mode.
* With incremental updates, only changed positions will be sent after the initial complete list.
* If a positions is closed, the positions size will be 0.
* Without incremental updates, the complete list of positions will be sent again if any of it has changed.
*
* @param incrementalUpdates Set to true to enable incremental updates, or false to disable it.
*/
getPositions(incrementalUpdates: boolean): Observable<Position[]>
- The "IB Shell"
Every function on the IBApiNext will have a command line tool to run to from console.
These tools serve as demo code, utilities for i.e., quickly searching contract ID, can be used for testing or you can even trade on console (or via .sh scripts) rather than from TWS – if you want to :)
See https://github.com/stoqey/ib/tree/api-next/src/tools
Example:
node .\dist\tools\account-summary.js
[
[
"DUxxxxxx",
{
"account": "DUxxxxxx",
"values": [
[
"GrossPositionValue",
{
"value": "221224.97",
"currency": "EUR"
}
],
[
"NetLiquidation",
{
"value": "823696.56",
"currency": "EUR"
}
],
[
"TotalCashValue",
{
"value": "602471.59",
"currency": "EUR"
}
]
]
}
]
]
- Auto re-connect
IBApiNext does support automatic reconnection with a configurable re-connection interval.
After a re-connect, observed subscriptions will be renewed so that rxjs Observables continue to work. You do not need to care about re-requesting PnLs, positions, market-data, ... after a connection-loss. IBApiNext will do it for you.
Also, IBApiNext does run a watchdog that constantly polls TWS to ensure that dead connections are detected early w/o relying on socket termination events.
Example (account-summary app with -watch and -log=debug, so you can see what it does):
node .\dist\tools\account-summary.js -watch -log=debug
[16:17:41] [DEBUG] [IBApiNext]: connect(undefined)
[16:17:41] [INFO] [IBApiAutoConnection]: Connecting to TWS with client id 0
[16:17:41] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:17:41] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:17:41] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
[16:17:41] ERROR: connect ECONNREFUSED 127.0.0.1:4002
[16:17:51] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS with client id 1
[16:17:51] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:17:51] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:17:51] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
[16:17:51] ERROR: connect ECONNREFUSED 127.0.0.1:4002
[16:18:01] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS with client id 2
[16:18:01] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:18:01] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:18:01] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
Preview is available on https://github.com/stoqey/ib/tree/api-next
IBApiNext: https://github.com/stoqey/ib/tree/api-next/src/api-next
"IB Shell" tools: https://github.com/stoqey/ib/tree/api-next/src/tools
Feedback is very welcome.