Machine Trading
Machine Trading
Founded in 1807, John Wiley & Sons is the oldest independent pub-
lishing company in the United States. With offices in North America,
Europe, Australia, and Asia, Wiley is globally committed to developing
and marketing print and electronic products and services for our
customers’ professional and personal knowledge and understanding.
The Wiley Trading series features books by traders who have sur-
vived the market’s ever-changing temperament and have prospered—
some by reinventing systems, others by getting back to basics.
Whether a novice trader, professional, or somewhere in between,
these books will provide the advice and strategies needed to prosper
today and well into the future.
For more on this series, visit our website at www.WileyTrading.com.
MACHINE
TRADING
Deploying Computer Algorithms to Conquer
the Markets
Ernest P. Chan
Copyright © 2017 by Ernest P. Chan. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any
form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise,
except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without
either the prior written permission of the Publisher, or authorization through payment of the
appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600, or on the Web at www.copyright.com. Requests
to the Publisher for permission should be addressed to the Permissions Department, John Wiley &
Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online
at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best
e orts in preparing this book, they make no representations or warranties with respect to the
accuracy or completeness of the contents of this book and speci cally disclaim any implied
warranties of merchantability or tness for a particular purpose. No warranty may be created or
extended by sales representatives or written sales materials. The advice and strategies contained
herein may not be suitable for your situation. You should consult with a professional where
appropriate. Neither the publisher nor author shall be liable for any loss of pro t or any other
commercial damages, including but not limited to special, incidental, consequential, or other
damages.
For general information on our other products and services or for technical support, please contact
our Customer Care Department within the United States at (800) 762-2974, outside the United
States at (317) 572-3993, or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material
included with standard print versions of this book may not be included in e-books or in
print-on-demand. If this book refers to media such as a CD or DVD that is not included in the
version you purchased, you may download this material at http://booksupport.wiley.com. For
more information about Wiley products, visit www.wiley.com.
10 9 8 7 6 5 4 3 2 1
To my mom, Ching, my spouse, Ben, and to the memory
of my beloved father, Hung Yip.
CONTENTS
Preface ix
Bibliography 227
About the Author 235
Index 237
P R E FA C E
PREFACE
to purchase or rent them from their original sources, all of which are
described in Chapter 1.
I have bene ted from tips, ideas, and help from many people in putting
the content together. An incomplete list would include:
■ Stephen Aikin, a renowned author (Aikin, 2012) and lecturer, who helped
me understand implied quotes due to calendar spreads in the futures
markets (Chapter 6).
■ David Don and Joseph Signorelli of Lime Brokerage, who corrected some
of my misunderstanding of the market microstructure (Chapter 6).
■ Jonathan Shore, in nitely knowledgeable about bitcoins, who helped
compile some order book data in that market and shared that with me
(Chapter 7).
■ Dr. Roger Hunter, CTO at our rm, QTS Capital Management, who
reviewed my manuscript and who never failed to nd software bugs in
my codes.
■ The team at Interactive Brokers (especially Joanne, Ragini, Mike, Greg,
Ian, and Ralph) whose in nite patience with my questions about all issues
related to trading are much appreciated.
I would like to thank Professor Thomas Miller of Northwestern Uni-
versity for hiring me to teach the Risk Analytics course at the Master of
Science in Predictive Analytics program. In the same vein, I would also
like to thank Matthew Clements and Jim Biss at Global Markets Training
for organizing the London workshops for me over the years. Quite a few
nuggets of knowledge in this book come out of materials or discussions
from these courses and workshops.
Trading and research have been made a lot more interesting and enjoyable
because I was able to work closely with our team at QTS, who contributed
to research, ideas, and general knowledge, some of which nd their way into
this book. Among them, Roger, of course, without whom there wouldn’t be
QTS, but also Yang, Marcin, Sam, and last but not least, Ray.
Of course, none of my books would come into existence without the
support of Wiley, especially my long-time editor Bill Falloon, development
editor Julie Kerr, production editor Caroline Maria, and copy editor Cheryl
Ferguson (from whom no missing ‘‘end’’ to a ‘‘for’’-loop can escape). It was
xii truly a great pleasure to work with them, and their enthusiasm and profes-
sionalism are greatly appreciated.
PREFACE
MACHINE TRADING
CHAPTER 1
The Basics
of Algorithmic
Trading
A n algorithmic trading strategy feeds market data (historical or live) into
a computer (backtest or automated execution) program. The program
then submits orders to the broker through an API, and receives order status
1
noti cations back from the broker. The owchart in Figure 1.1 illustrates
this process.
Notice that I deliberately use the same box to indicate the computer pro-
gram that generates backtest results and live orders: This is the best way to
ensure we are trading the exact same model that we have backtested.
In this chapter, I will discuss the latest services, products, and their ven-
dors applicable to each of the blocks in Figure 1.1. In addition, I will describe
my favorite performance metrics, the way to determine the optimal lever-
age, and the simplest asset allocation method. Though I have touched on
many (but not all) of these issues in my previous books, I have updated them
here based on the state of the art. The FinTech industry has not been stand-
ing still, nor has my understanding of issues ranging from brokers’ safety to
subtleties of portfolio optimization.