This is a course in programming with the Trader Workstation Application Programming Interface (TWS API) for Python developers. In this course, we describe how to get started in developing Python applications that use the API.
What is the TWS API?
In this lesson we’ll introduce Trader Workstation (TWS), IBKR's flagship desktop trading application, and the TWS Application Programming Interface (API). We’ll cover what the TWS API is, what it can provide, and where it can be found. We’ll discuss the hardware and software requirements. This course is for experienced Python programmers.
Installing & Configuring TWS for the API
In this lesson, we’ll show you where to find the software applications Trader Workstation (TWS) and IB Gateway on the IBKR website for download and installation. TWS and IB Gateway are the standalone software applications which integrates with our API that can be used with custom or third party trading applications. We’ll then discuss the settings in TWS/IB Gateway which are necessary to connect an API client and those which can affect order placement.
Accessing the TWS Python API Source Code
We’ll begin by describing how to obtain access to the TWS Python API open source code repository on GitHub, and explain the differences between the public download site and the private GitHub repository which has bleeding-edge source. We then discuss what the source code consists of and what it does, with a high-level view of the API architecture, and mention some of the many different environments in which Python API programs are being run.
Essential components of TWS API programs
This lesson will explore the essential components of a TWS API Python program. This includes the API classes EClient and EWrapper, a function call to create a connection to TWS, and a run loop for processing returned messages in the queue. We’ll walk through a simple “Hello World” example which implements each of these components in order to send a query for details about a financial instrument and then print received details to the console. We'll then discuss the more comprehensive sample program "Program.py" which is included with the API download and shows the syntax of all API functions.
Requesting Market Data – Code Walkthrough
This lesson will explore how to request market and historical data using the TWS Python API. Code examples will be presented which show the minimum Python code necessary to request streaming and historical data and display market data in the console. Finally, we will discuss limitations on requesting data, and the types of data which are included in IBKR’s real time feed as compared to the historical database.
Placing Orders using TWS Python API
In this lesson we discuss how orders can be placed, monitored, modified, and cancelled from the TWS API. To demonstrate the essential components necessary to place an order, a simple Python program is introduced which places an AAPL order to a paper account, and then prints order status messages to the console. Important topics associated with placing and monitoring orders from the API, including the master client ID, and the API maximum message rate are introduced.
TWS Python API Placing Complex Orders
In this lesson, we discuss how to use the TWS API to portfolio data, and account information. An overview is given of API functionality for receiving portfolio and account data with descriptions of each of the different functions available and the account structures with which they are commonly used. The function reqAccountUpdates is demonstrated in a Python program to show how it can be used to subscribe to both portfolio and account data for an individual account under a subscribe-and-publish model to continuously receive updates in real time.