Skip to main content

IB Controller

ib_interface.api.ibcontroller

Programmatic control over the TWS/gateway client software.

IBC

Programmatic control over starting and stopping TWS/Gateway using IBC (https://github.com/IbcAlpha/IBC).

Args: twsVersion (int): (required) The major version number for TWS or gateway. gateway (bool):

  • True = gateway

  • False = TWS tradingMode (str): 'live' or 'paper'. userid (str): IB account username. It is recommended to set the real username/password in a secured IBC config file. password (str): IB account password. twsPath (str): Path to the TWS installation folder. Defaults:

  • Linux: ~/Jts

  • OS X: ~/Applications

  • Windows: C:\Jts twsSettingsPath (str): Path to the TWS settings folder. Defaults:

  • Linux: ~/Jts

  • OS X: ~/Jts

  • Windows: Not available ibcPath (str): Path to the IBC installation folder. Defaults:

  • Linux: /opt/ibc

  • OS X: /opt/ibc

  • Windows: C:\IBC ibcIni (str): Path to the IBC configuration file. Defaults:

  • Linux: ~/ibc/config.ini

  • OS X: ~/ibc/config.ini

  • Windows: %%HOMEPATH%%\Documents\IBC\config.ini javaPath (str): Path to Java executable. Default is to use the Java VM included with TWS/gateway. fixuserid (str): FIX account user id (gateway only). fixpassword (str): FIX account password (gateway only). on2fatimeout (str): What to do if 2-factor authentication times out; Can be 'restart' or 'exit'.

This is not intended to be run in a notebook.

To use IBC on Windows, the proactor (or quamash) event loop must have been set:

.. code-block:: python

import asyncio

asyncio.set_event_loop(asyncio.ProactorEventLoop())

Example usage:

.. code-block:: python

ibc = IBC(976, gateway=True, tradingMode="live", userid="edemo", password="demouser") ibc.start() IB.run()

start

def start()

Launch TWS/IBG.

terminate

def terminate()

Terminate TWS/IBG.

startAsync

def startAsync()

terminateAsync

def terminateAsync()

monitorAsync

def monitorAsync()

Watchdog

Start, connect and watch over the TWS or gateway app and try to keep it up and running. It is intended to be used in an event-driven application that properly initializes itself upon (re-)connect.

It is not intended to be used in a notebook or in imperative-style code. Do not expect Watchdog to magically shield you from reality. Do not use Watchdog unless you understand what it does and doesn't do.

Args: controller (IBC): (required) IBC instance. ib (IB): (required) IB instance to be used. Do not connect this instance as Watchdog takes care of that. host (str): Used for connecting IB instance. port (int): Used for connecting IB instance. clientId (int): Used for connecting IB instance. connectTimeout (float): Used for connecting IB instance. readonly (bool): Used for connecting IB instance. appStartupTime (float): Time (in seconds) that the app is given to start up. Make sure that it is given ample time. appTimeout (float): Timeout (in seconds) for network traffic idle time. retryDelay (float): Time (in seconds) to restart app after a previous failure. probeContract (Contract): Contract to use for historical data probe requests (default is EURUSD). probeTimeout (float): Timeout (in seconds) for the probe request.

The idea is to wait until there is no traffic coming from the app for a certain amount of time (the appTimeout parameter). This triggers a historical request to be placed just to see if the app is still alive and well. If yes, then continue, if no then restart the whole app and reconnect. Restarting will also occur directly on errors 1100 and 100.

Example usage:

.. code-block:: python

def onConnected(): print(ib.accountValues())

ibc = IBC(974, gateway=True, tradingMode="paper") ib = IB() ib.connectedEvent += onConnected watchdog = Watchdog(ibc, ib, port=4002) watchdog.start() ib.run()

Events:

  • startingEvent (watchdog: :class:.Watchdog)
  • startedEvent (watchdog: :class:.Watchdog)
  • stoppingEvent (watchdog: :class:.Watchdog)
  • stoppedEvent (watchdog: :class:.Watchdog)
  • softTimeoutEvent (watchdog: :class:.Watchdog)
  • hardTimeoutEvent (watchdog: :class:.Watchdog)

start

def start()

stop

def stop()

runAsync

def runAsync()