TWS Connection

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

Overview

To begin programming with the Interactive Brokers API, it is first essential to understand the API information exchange. Our program is structured to make the information preparation and exchange as comprehensible as possible. Therefore, the main content of ibProgram1.py is divided into nine parts:

1) Import statements: Defines the classes we import from relative locations in the file tree.

2) Global Variables: Store and allow access to values throughout the program. These can also later be limited in scope to local variables as desired. 

3) Custom classes and methods: Classes and methods that are created throughout the guide on an as-needed basis. 

4) TestWrapper Class: Class to implement the EWrapper interface and handle the returns from our server requests. In other words, this is the “GET” portion of the information exchange. The wrapper is where we receive and handle information from the TWS by overriding API functions.

 5) TestClient Class: Class which implements the ClientSocket and sends the request to the server. The client is the “POST” portion of the information exchange. In this section, we invoke functions to send messages to the TWS.

6) TestApp: Establish connection variables, algorithm preferences, and initialize the program.

7) Execution: Function where we start the program, call associated methods, and print messages to the console. 

8) Input area: Where we connect to an input source and listen for incoming information. In this guide, scrape information from news sources and monitor for trigger words.

9) Processing area: Interprets the input source and delivers a decision to the correct Interactive Brokers object. That is, if there is a word grouping trigger on Apple, we trade that stock in Execution.

To begin truly understanding this architecture, create a new file named ibProgram1.py. This script is where we will add the major program components. Let’s start by outlining the structure we just defined with comments (Code 3.1). With many advanced programs, organization and functions can quickly get out of hand. So, it is good practice to leave thoughtful comments along the way.

# Below are the import statements

# Below are the global variables

# Below are the custom classes and methods

# Below is the TestWrapper/EWrapper class

# Below is the TestClient/EClient class

# Below is the TestApp

# Below is the program execution

# Below is the import area

# Below is the logic processing area

(Code 3.1)

Overview

To inherit the necessary classes and functions from the API interfaces, we need to import everything at the top of the program. To avoid redundancy and errors down the line, we use star imports to get known functions. Generally, star imports are not recommended due to their performance issues; however, for our purposes, star imports are quick enough. It’s easy to revise imports later on after you discover the exact needs of your program and begin optimizing for speed. 

At this point, if you are getting path errors, make sure you have the API files and main ibProgram1.py file set up as below (Figure 3.1). We are using dependent selectors to target the files in the IB API folder, so your file tree must be set up accordingly. 

(Figure 3.1)

 

Now, inside ibProgram1.py, we import the necessary files from the “ibapi” folder. We are implementing the wrapper, client, contract, and order object from the API files. Then queue, date time, and time from the native Python package (Code 3.2).

# Below are the import statements

from ibapi.wrapper import *
from ibapi.client import *
from ibapi.contract import *
from ibapi.order import *
from threading import Thread
import queue
import datetime
import time
import math from bs4
import BeautifulSoup
import requests
from random import randint

(Code 3.2)

 

As we work through the program, you will understand exactly how each of these is implemented. But for now, it’s helpful to gather the anticipated imports so we don’t need to continually backtrack during development.