Skip to content

IBApi next generation (IBApiNext)

Matthias Frener edited this page Feb 21, 2021 · 17 revisions

Abstract

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.

Features

  • 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, started before IB Gateway, with -watch and -log=debug, so you can see what it does):
[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.

Clone this wiki locally