-
-
Notifications
You must be signed in to change notification settings - Fork 47
IBApi next generation (IBApiNext)
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 (such as getPositions or getAccountSummaries), the Obersvable always the delivers both, the full cached list and the diff to the previous event.
Example:
/**
* Create subscription to receive the positions on all accessible accounts.
*/
getPositions(incrementalUpdates: boolean): Observable<PositionsUpdate>
...
**
* An update on the positions.
*/
export class PositionsUpdate {
/** List of positions, that have been added since last [[PositionsUpdate]]. */
added: Position[] = [];
/** List of positions, that have been changed since last [[PositionsUpdate]]. */
changed: Position[] = [];
/** List of positions, that have been removed since last [[PositionsUpdate]]. */
removed: Position[] = [];
/** List of all currently open positions. */
all: 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, started before IB Gateway, with -watch and -log=debug, so you can see what it does):
node .\dist\tools\account-summary.js -watch -log=debug
[16:27:24] [DEBUG] [IBApiNext]: connect(undefined)
[16:27:24] [INFO] [IBApiAutoConnection]: Connecting to TWS with client id 0
[16:27:24] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:27:24] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:27:24] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
[16:27:24] ERROR: connect ECONNREFUSED 127.0.0.1:4002
[16:27:34] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS with client id 1
[16:27:34] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:27:34] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:27:34] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
[16:27:34] ERROR: connect ECONNREFUSED 127.0.0.1:4002
[16:27:44] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS with client id 2
[16:27:44] [ERROR] [TWS]: connect ECONNREFUSED 127.0.0.1:4002 - Code: 502 - ReqId: -1
[16:27:44] [DEBUG] [IBApiAutoConnection]: onDisconnected()
[16:27:44] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS in 10s...
[16:27:44] ERROR: connect ECONNREFUSED 127.0.0.1:4002
[16:27:54] [INFO] [IBApiAutoConnection]: Re-Connecting to TWS with client id 3
[16:27:54] [INFO] [IBApiAutoConnection]: Successfully connected to TWS with client id 3.
[16:27:54] [DEBUG] [IBApiAutoConnection]: Starting connection watchdog with 4000ms interval.
[16:27:54] [INFO] [TWS]: Server Version: 151. Connection time 20210221 16:27:54 CET
[16:27:54] [INFO] [TWS]: Sec-def data farm connection is OK:secdefil
[
[
"DUxxxxxx",
{
"account": "DUxxxxxx",
"values": [
[
"GrossPositionValue",
{
"value": "221224.97",
"currency": "EUR"
}
],
[
"NetLiquidation",
{
"value": "823696.56",
"currency": "EUR"
}
],
[
"TotalCashValue",
{
"value": "602471.59",
"currency": "EUR"
}
]
]
}
]
]
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.