Environment Setup

Table Of Contents

  1. Introduction
    1. Why Automate Finance?
    2. Why Interactive Brokers?
    3. Why Python?
    4. Why Use this Guide?
  2. Environment Setup
    1. Account Setup
    2. The Trader Workstation
    3. API Files
  3. Connection
    1. Program Overview
    2. Import Statements
    3. Test Wrapper (In book, preview coming soon)
    4. Test Client (In book, preview coming soon)
    5. Test App (In book, preview coming soon)
    6. Program Execution (In book, preview coming soon)
  4. Chapters 4-10 In book


    Likely your algorithm is developed in a local text editor (Sublime Text, VS Code, Textedit, etc.) and run in the local command terminal. The script then communicates directly with the Interactive Brokers servers through the Trader Workstation. The Trader Workstation is a GUI that is downloaded locally and listens for an incoming connection.

    Account Registration

    To gain access to the Interactive Brokers system, register an account online.

    Begin by searching "open an interactive brokers account" or following this link. You may also use a demo account at login to become comfortable with the Trader Workstation by selecting “No username? Try The Demo”. However, the demo account will not work with the API and you will run into errors later on if you do not go through the account approval process.

    On the page you just navigated to, titled "Open an Account", there are several options you can select depending on your situation. Most users will choose "Individual Investor or Trader".

    Selecting "Individual Investor or Trader" leads to a page with a large red button called "Start Application", where you can begin the sign-up process. After choosing an email, username, and password, you will receive a verification email where you can complete the full sign-up after logging in.

    The application asks questions required for personal identification. These include questions about residency, investment experience, and income. Further, some user agreements require consent and approval before continuing to the end of the application.

    Note: In this process, you should select a Pro account, instead of the Lite account. Pro accounts are not generally designed for casual investors; however, Lite does not offer access to the API, which this program operates on.

    The application process is fairly lengthy and requires approval at the end that can sometimes take days. Unfortunately, this can not be circumvented and can slow down the process if you are eager to begin. However, many traders on Interactive Brokers carry large balances and are subject to great risk. So, Interactive Brokers takes great measures to ensure users are ready.

    This account is used to log in to the Trader Workstation. For the majority of the development of the application, we are using a paper trading account; however, in a later section, the account will need to be funded in order to gain permission to subscribe to live market data.

    Trader Workstation Download

    To begin the download of the Trader Workstation, search “Trader workstation download” or click here, and find the page with the latest version (Figure 2.2).

    Note: you may need to click the “Download for Other Operating Systems” link if your version does not match the operating system displayed.

    (Figure 2.2)

    By clicking download, a .dmg (or .exe) file will be downloaded, and you can begin the installation wizard as instructed. The wizard installs the Trader Workstation on your desktop or in the applications folder. Double click on the icon to start the program. You will know you are successful when the login screen appears (Figure 2.3).   

    Trader Workstation Login Preview
    (Figure 2.3)

    At this point, you will be able to log in to your account and start experimenting with TWS's features. It's valuable to familiarize yourself with the options available on the Trader Workstation as there are many similarities to the options offered through the API.

    To familiarize yourself with the TWS, you can begin by placing orders and observing the changes in the portfolio on the right-hand side (recommend logging into a paper trading account). We will be referring to the portfolio section frequently in the future, as this is where outputs from the program are most noticeable (Figure 2.4).   

    TWS Home Screen Preview
    (Figure 2.4)

    Beyond this section, there is no need for detailed coverage of the TWS interface. The Trader Workstation is a Graphical User Interface (GUI) that has roughly the same features we will use in our Python script; however, this GUI lacks the automation that makes our program special.

    Note: There are other tutorials online that offer walkthroughs of the GUI if you'd like additional information.


    To begin diving into the API, download the necessary files by navigating to http://interactivebrokers.github.io. Read over the terms and conditions, then download the latest stable (downloading latest may cause errors) software that matches your operating system. After unzipping the download, move the API files to the directory (folder) you have created for this project. Our program files will be adjacent to the file tree to these API files.

    Note: You're welcome to relocate the main Python script; however, you would then need to modify the imports.

    Once the files are in a directory that is easily accessible to you, we now need to complete the following installations and download the necessary Python packages. First, it is also important to build a source distribution so the files can access the correct targets. To start these downloads, we will be accessing setup.py, a file included in the API that directs us to the correct downloads. Setup.py is under the folder called "pythonclient". To access this file from the root folder, cd through the file tree with the following command (which may differ depending on device):

    cd twsapi_macunix/IBJts/source/pythonclient

    In the pythonclient folder, type “ls” to ensure that setup.py is there (Figure 2.5).  

    (Figure 2.5)

    If it is, then you’re in the right level of the folder. From within the pythonclient folder, run the following commands in your CLI:

    To install IB API Python packages:

    python3 setup.py install

    To build the source distribution:

    python3 setup.py sdist

    python3 setup.py bdist_wheel

    python3 -m pip install --user --upgrade dist/ibapi-9.73.7-py3-none-any.whl

    Note: The ibapi-version number (the number after dist/ibapi-) may differ on this last command depending on your installation. Refer to the README inside pythonclient for the most up to date commands.

    After these installs are complete, your API files are ready to send information to the TWS and Interactive Brokers servers.


    The final part of the environment setup is to configure the TWS configuration settings. With the TWS logged in, navigate to global configurations (Figure 2.6).


    (Figure 2.6)

    Under setting configuration note several important setting options (Figure 2.7):

    1) Enable ActiveX Socket Clients: This must be enabled to allow the TWS to accept API connections.

    2) Read-Only API: Disable (or uncheck) this as sending orders to the TWS require write access.

    3) Socket Port: Set this to 7496 for regular brokerage accounts and 7497 for paper trading accounts.

    4) Logging Level: Set this to "Detail" as this records all program logs that can be used for future reference or debugging.

    5) Allow Connections from localhost only: For this guide, we are using the localhost to send requests. You can change this later if you’d like to access from an external or virtual machine.

    (Figure 2.7)

    With the TWS downloaded and configured and the API files downloaded, it is now time to begin writing the first line of the program!