Skip to main content

ib_async.ibcontroller

Generated from the installed ib_async 2.1.0 package. Signatures and defaults are version-specific.

Programmatic control over the TWS/gateway client software.

Public constants

NameValue
IB<class 'ib_async.ib.IB'>

IBC

IBC(twsVersion: int = 0, gateway: bool = False, tradingMode: str = '', twsPath: str = '', twsSettingsPath: str = '', ibcPath: str = '', ibcIni: str = '', javaPath: str = '', userid: str = '', password: str = '', fixuserid: str = '', fixpassword: str = '', on2fatimeout: str = '') -> None

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:

import asyncio asyncio.set_event_loop(asyncio.ProactorEventLoop())

Example usage:

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

Fields

NameTypeDefault
twsVersionint0
gatewayboolFalse
tradingModestr``
twsPathstr``
twsSettingsPathstr``
ibcPathstr``
ibcInistr``
javaPathstr``
useridstr``
passwordstr``
fixuseridstr``
fixpasswordstr``
on2fatimeoutstr``

__enter__

__enter__(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

__exit__

__exit__(self, *_exc)

No library docstring is provided; consult the signature, type fields, and operational guides.

dict

dict(obj) -> dict

Return dataclass values as dict. This is a non-recursive variant of dataclasses.asdict.

monitorAsync

monitorAsync(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

nonDefaults

nonDefaults(obj) -> dict[str, typing.Any]

For a dataclass instance get the fields that are different from the default values and return as dict.

start

start(self)

Launch TWS/IBG.

startAsync

startAsync(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

terminate

terminate(self)

Terminate TWS/IBG.

terminateAsync

terminateAsync(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

tuple

tuple(obj) -> tuple[typing.Any, ...]

Return dataclass values as tuple. This is a non-recursive variant of dataclasses.astuple.

update

update(obj, *srcObjs, **kwargs) -> object

Update fields of the given dataclass object from zero or more dataclass source objects and/or from keyword arguments.

Watchdog

Watchdog(controller: ib_async.ibcontroller.IBC, ib: ib_async.ib.IB, host: str = '127.0.0.1', port: int = 7497, clientId: int = 1, connectTimeout: float = 2, appStartupTime: float = 30, appTimeout: float = 20, retryDelay: float = 2, readonly: bool = False, account: str = '', raiseSyncErrors: bool = False, probeContract: ib_async.contract.Contract = Forex('EURUSD', exchange='IDEALPRO'), probeTimeout: float = 4) -> None

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:

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: .Watchdog)
  • startedEvent (watchdog: .Watchdog)
  • stoppingEvent (watchdog: .Watchdog)
  • stoppedEvent (watchdog: .Watchdog)
  • softTimeoutEvent (watchdog: .Watchdog)
  • hardTimeoutEvent (watchdog: .Watchdog)

Fields

NameTypeDefault
controllerIBCrequired
ibIBrequired
hoststr127.0.0.1
portint7497
clientIdint1
connectTimeoutfloat2
appStartupTimefloat30
appTimeoutfloat20
retryDelayfloat2
readonlyboolFalse
accountstr``
raiseSyncErrorsboolFalse
probeContractContractForex('EURUSD', exchange='IDEALPRO')
probeTimeoutfloat4

dict

dict(obj) -> dict

Return dataclass values as dict. This is a non-recursive variant of dataclasses.asdict.

nonDefaults

nonDefaults(obj) -> dict[str, typing.Any]

For a dataclass instance get the fields that are different from the default values and return as dict.

runAsync

runAsync(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

start

start(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

stop

stop(self)

No library docstring is provided; consult the signature, type fields, and operational guides.

tuple

tuple(obj) -> tuple[typing.Any, ...]

Return dataclass values as tuple. This is a non-recursive variant of dataclasses.astuple.

update

update(obj, *srcObjs, **kwargs) -> object

Update fields of the given dataclass object from zero or more dataclass source objects and/or from keyword arguments.