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.
In this initial lesson we introduce Trader Workstation (TWS), IBKR's flagship desktop trading application, and the TWS Application Programming Interface (API). An overview is provided of what the TWS API is, what it can provide, and where it can be found. We discuss the hardware and software requirements for this course on programming with the TWS API, and describe experienced programmers as the intended audience. We end by discussing the most frequently-asked questions about the API, with their respective answers.
n 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.
In this third lesson we 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 actually 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.
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.
This lesson will explore receiving market data and historical candlestick data from TWS using the Python API. We will discuss the different functions available for receiving streaming updates, real time bars, tick-by-tick data, and candlesticks, and discuss additional data modes available for receiving frozen and delayed data. In this context we will again consider the API Contract class, introduced in the previous lesson, and discuss how a Contract object is used in each real time and historical data request. 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.
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, the Order Efficiency Ratio (OER), and the API maximum message rate are introduced. Finally, we address a few special cases in which commissions and fees are different for API orders than for orders created directly in an IBKR trading application.
In this lesson, we discuss how to use the TWS API to receive option chains, portfolio data, and account information. For option chain data from the API, there are two functions available- reqContractDetails and reqSecDefOptParams -which are each appropriate in different situations, and sometimes used best in tandem. A short Python program is run to demonstrate how to request option chain data with each function and show the content of the returned data. Next, 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.
n this lesson we move beyond core API functionality and build on previous lessons to examine a case study in which advanced order types may be advantageous in implementing a specific trading strategy. The TWS API supports more than 60 of the order types and attributes available in TWS. Here we consider a scenario in which the Pegged-To-Primary/Relative order type is used with orders in a hedging pair trade to place related orders for two different instruments, such that placement of the second order is handled automatically by the IBKR server. We walk-through the code of a Python program to place the initial and the hedging order and demonstrate how they will appear in TWS.