This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Duration: 4:47

Level: Intermediate

In this lesson, we describe where to find the software applications Trader Workstation (TWS) and IB Gateway on the IBKR website for downloading and installation. TWS and IB Gateway are the standalone software applications which offer a Python API that can be used with custom or third party trading applications. We then discuss the settings in TWS/IB Gateway which are necessary to connect an API client and those which can affect order placement.

Read More

Study Notes:

Interactive Brokers’ flagship desktop trading application- TWS -is free to download from the website and
run. The download can be found at under the Technology menu at TWS

On the download site you’ll notice that there are ‘Online’ versions which update automatically with new
releases, as well as ‘Offline’ versions at the bottom which never update. When you go to the site, the
server will automatically detect the operating system of the browser and provide the corresponding
download automatically, however all versions of TWS are available for use with Windows, Linux, or Mac.

The alternative to using TWS for API users is to use a program called IB Gateway. The IB Gateway
download can be found on the website under the “Login” menu (though it is not necessary to login to
download and install).

From an API perspective, IB Gateway is nearly identical to TWS. An application that connects to and uses
the API with TWS can also use IB Gateway. The major difference of IB Gateway is that it is entirely
geared towards API functionality and does not provide access to account information via a GUI in the
same way as TWS does. Because of this, it is a smaller application that requires fewer resources.
Equivalent versions of TWS and IB Gateway are always released together. For instance, if the current
TWS Latest version is 974, then the current IB Gateway Latest build is also 974.

After running the installer TWS, will be installed locally and a new icon will be created on the desktop labelled “Trader Workstation”. Double-clicking on the icon will bring up the TWS login screen which has inputs to manually enter a username and password.

There is also an option where you can select to login to either your live or paper account. Paper accounts are compatible to use with the TWS API, and in fact it is recommended to test all programs in a paper environment before moving to production trading. If you don’t have an IB account, it’s possible to login to a demo account by clicking the link “Try the Demo” and using an email address instead. Demo accounts are compatible with the TWS API, though they will not have live data available for most instrument types. To receive live or historical candlestick data from the API, it is necessary to first havelive data permissions enabled in TWS. For most instrument types, this requires first having a live funded account with active market data subscriptions.

After you fully logged into TWS and it has loaded completely for the first time, there is a setting which
must be enabled to allow the API connection, and several others which some users decide to configure
differently for their applications. The API settings section in TWS Global Configuration can be found at
the API section in Global Configuration.

The setting necessary to enable the socket client such as Python is the first one, called “Enable ActiveX and Socket Clients”. The other settings which affect the initial connection are first:

  • Socket Port”: By default, TWS uses socket port 7496 for live sessions and 7497 for paper
    sessions. IB Gateway by contrast uses 4001 for live sessions and 4002 for paper sessions. However these are just defaults, and can be modified as desired- the socket port in TWS must be configured to match the socket port used in the API clients’ connect() function.
  • Next, there is a setting “Allow Connections from Localhost Only”: Checking this box configures TWS to (1) only accept incoming connections from API applications on the same computer, and (2) avoids the need to manually accept incoming API connections.

To enable logging of API messages for troubleshooting specific API issues there are two settings which
are important:

  • To create an API log file which has a record of all API messages, the setting “Create API Message Log” must be checked.
  • There’s also a setting called “Logging Level” which effects the amount of detail that is logged. If this is set to the highest level called “Detail”, the API messages will be recorded with TWS as well.

For more information visit the reference guide on the web site.

Some settings in Global Configuration which will affect if orders from the API are accepted:

  • The setting ‘Read-Only’ is enabled by default and will block all API orders. It must be unchecked
    to allow any orders from the API.
  • The ‘Precautionary Settings’ under API in Global Configuration menu will cause certain orders to be paused by default if they exceed certain precautionary settings until they are manually confirmed in TWS.
  • Some order attributes which can’t be set from the API are set from TWS. For instance, there is a setting in TWS Global Configuration called “Active order in pre-open session” which is not available to be set by the API but is in TWS and will also affect API orders.

Disclosure: Interactive Brokers

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

Supporting documentation for any claims and statistical information will be provided upon request.

Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

trading top
Translate »