Algorithmic Trading with Interactive Brokers: Python and C++
by Matthew Scarpino
2019, Matthew Scarpino. All rights reserved.
No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law.
The content of this book does not constitute financial advice. Like all financial trading, algorithmic trading carries a signficant amount of risk and may not be suitable for all investors. Past performance is not indicative of future results.
Printed in the United States of America
Table of Contents
Chapter 1:
1.1:
1.2:
1.3:
1.4:
1.5:
Chapter 2:
2.1:
2.2:
2.3:
2.4:
2.5:
Chapter 3:
3.1:
3.2:
3.3:
3.4:
3.5:
3.6:
3.7:
3.8:
Chapter 4:
4.1:
4.2:
4.3:
4.4:
4.5:
4.6:
Chapter 5:
5.1:
5.2:
5.3:
5.4:
5.5:
Chapter 6:
6.1:
6.2:
6.3:
6.4:
Chapter 7:
7.1:
7.2:
7.3:
7.4:
7.5:
7.6:
Chapter 8:
8.1:
8.2:
8.3:
8.4:
8.5:
Chapter 9:
9.1:
9.2:
9.3:
9.4:
Chapter 10:
10.1:
10.2:
10.3:
10.4:
10.5:
10.6:
Chapter 11:
11.1:
11.2:
11.3:
11.4:
11.5:
Chapter 12:
12.1:
12.2:
12.3:
12.4:
Chapter 13:
13.1:
13.2:
13.3:
13.4:
Chapter 14:
14.1:
14.2:
14.3:
14.4:
14.5:
14.6:
14.7:
Appendix A:
A.1:
A.2:
A.3:
A.4:
Appendix B:
B.1:
B.2:
B.3:
B.4:
Chapter 1
Introducing Algorithmic Trading and Interactive Brokers
Michael Lewis has written several books on finance and politics, but one of his most fascinating books involves baseball. His 2003 book Moneyball covers the Oakland Athletics and their revolutionary method of recruiting players. Instead of relying on agents and scouts, the Athletics developed an algorithm that focused primarily on a player's ability to get on base.
As a result of this approach, the Athletics recruited players overlooked by teams with deeper pockets. These players succeeded beyond anyone's expectations, and the Athletics reached the Major League playoffs in 2002 and 2003a feat that stunned the world of baseball. Inspired by the Athletics, the Boston Red Sox adopted a similar recruiting method and won the World Series in 2013.
As I read Moneyball , I thought of how I could improve my investing by following a similar method. What if I picked stocks using statistics instead of rumors and recommendations? What if I focused on undervalued companies that get on base instead of overvalued companies that hit home runs? And what if I programmed a computer to select securities and submit orders?
The idea of using a computer to place trades is called algorithmic trading , and I'm far from the first to consider it. One of the first instances of algorithmic trading took place in 1987, when Thomas Peterffy programmed a computer to read NASDAQ prices and place trades. Horrified, the exchange promptly banned the program, but Peterffy won in the endhe started Interactive Brokers, the first brokerage firm devoted solely to electronic trading.
Interactive Brokers is also one of the first brokerages to provide free access to algorithmic trading. This makes it possible to toss aside human biases and psychological issues and take advantage of the speed and memory of modern computers.
In writing this book, my goal is to teach you how to implement algorithms using the tools provided by Interactive Brokers. To be precise, I'll show you how to write applications in Python and C++ capable of scanning securities, reading financial data, and submitting orders automatically. I can't promise that you'll be as successful as Thomas Peterffy, but your success will be limited only by the shrewdness of your algorithm and the quality of your programming.
The goal of this chapter is far less ambitious. I'll start by describing Interactive Brokers and the process of opening an account. Then I'll present a non-technical overview of their programming tools and the content of this book.
1.1 Interactive Brokers
When I first became interested in algorithmic trading, I looked at a number of programming interfaces, including those provided by Fidelity, TD Ameritrade, E*Trade, and LightSpeed. Most were focused on simplicity and ease of development, but in the end, I chose Interactive Brokers (or IB) because of three factors:
- IB's API is mature, flexible, and intended for high-speed data acquisition and order execution.
- IB provides access to a wide variety of financial instruments, including IPOs, corporate bonds, and international securities.
- IB's commissions are significantly lower than those of other trading firms.
This section discusses the process of starting an account with IB and the fees it charges. But first, I'd like to provide a brief history of IB as a company.
1.1.1 Brief History
While most brokerage firms are founded by bankers, traders, and financiers, Interactive Brokers was started by a programmer. In 1987, Thomas Peterffy coded one of the very first algorithmic trading programs, which read NASDAQ prices and placed orders automatically. NASDAQ banned Peterffy's program but only strengthened his belief in the importance of computers in trading.
In 1993, Peterffy incorporated Interactive Brokers to provide high-speed electronic trading. In 1995, IB released the first version of its Trader Workstation (TWS) platform for trading. The company grew rapidly, and in 2001, IB handled more than 200,000 trades daily.
Today, IB is the largest electronic brokerage firm in the U.S., with more than 600,000 client accounts. Barron's has rated it the #1 online broker in 2018 and 2019, and it has won several awards from Investopedia and StockBroker.com.
1.1.2 Starting an Account
If you want to use IB's tools for automatic trading, you'll need to become a client of the brokerage. Thankfully, IB makes it easy to open an account and the brokerage doesn't require any initial minimum balance.
IB supports many different types of accounts, including accounts for institutions, small businesses, and individuals. Applying for an individual account requires ten steps:
- Visit the web page https://www.interactivebrokers.com/en/index.php?f=4695.
- Click the red button labeled INDIVIDUAL INVESTOR OR TRADER .
- Fill out the Application page, which requires an email address, username, and password. Then click the SAVE & CONTINUE button in the lower right.
- IB will send a message to your listed email account. To confirm your email address, click the message's Verify Account button. Then enter your username and password.
- The following page asks for your place of residence and account type (Individual). Fill in the information and click SAVE & CONTINUE in the lower right.
- Now the real application starts. The first page asks for personal information, such as addresses and contact information. When finished, click SAVE & CONTINUE .