0% found this document useful (4 votes)
1K views

Machine Trading

Machine Trading Deploying Computer Algorithms to Conquer the Markets

Uploaded by

Ricardo Gerhardt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (4 votes)
1K views

Machine Trading

Machine Trading Deploying Computer Algorithms to Conquer the Markets

Uploaded by

Ricardo Gerhardt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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.

Published by John Wiley & Sons, Inc., Hoboken, New Jersey.


Published simultaneously in Canada.

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.

Library of Congress Cataloging-in-Publication Data is available:

ISBN 978-1-119-21960-6 (Hardcover)


ISBN 978-1-119-21967-5 (ePDF)
ISBN 978-1-119-21965-1 (ePub)

Cover Design: Wiley


Cover Images: Wave © kdrkara90/Shutterstock; Abstract background © Marina
Koven/Shutterstock; Fractal Realms © agsandrew/Shutterstock

Printed in the United States of America

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

CHAPTER 1 The Basics of Algorithmic Trading 1

CHAPTER 2 Factor Models 27 vii

CHAPTER 3 Time-Series Analysis 59

CHAPTER 4 Arti cial Intelligence Techniques 83

CHAPTER 5 Options Strategies 119

CHAPTER 6 Intraday Trading and Market


Microstructure 159

CHAPTER 7 Bitcoins 201

CHAPTER 8 Algorithmic Trading Is Good for Body


and Soul 215

Bibliography 227
About the Author 235
Index 237
P R E FA C E

T he best way to learn something really well is to teach it to someone else


(Bargh and Schul, 1980). So I confess that one major motivation for my
writing this book, the third and the most advanced to date in a series, is to
force myself to study in more depth the following topics:
ix
■ The latest backtesting and trading platforms and the best and most
cost-e ective vendors for all manners of data (Chapter 1);
■ How to pick the best broker for algorithmic executions and what precau-
tions we should take (Chapter 1);
■ The simplest way to optimize allocations to di erent assets and strategies
(Chapter 1);
■ Factor models in all their glory, including those derived from the options
market, and why they can be useful to short-term traders (Chapter 2);
■ Time series techniques: ARIMA, VAR, and state space models (with hid-
den variables) as applied to practical trading (Chapter 3);
■ Arti cial intelligence/machine learning techniques: particularly methods
that will reduce over tting (Chapter 4);
■ Options and volatility trading strategies, including those that involve
portfolios of options (Chapter 5);
■ Intraday and higher frequency trading: market microstructure, order
types and routing optimization, dark pools, adverse selection, order
ow, and how to backtest intraday strategies with tick data (Chapter 6);
■ Bitcoins: bringing some of the techniques we covered to this new asset
class (Chapter 7);
■ How to keep up with the latest knowledge (Chapter 8);
■ Transitioning from a proprietary trader to an investment advisor
(Chapter 8).
I don’t know if these topics will excite you or bring you pro ts, but my
study of them has certainly improved my own money management skills.
Besides, sharing knowledge and ideas is fun and ultimately conducive to cre-
ativity and pro ts.
You will nd most of the materials quite accessible to anyone who has
some experience in a quantitative eld, be it computer science, engineer-
ing, or physics. Not much prior knowledge of trading and nance is assumed
(except for the chapter on options, where we do assume basic familiar-
ity). However, if you are completely new to trading, you may nd my more
basic treatments in Quantitative Trading (Chan, 2009) and Algorithmic Trading
x
(Chan, 2013) easier to understand. This book can be treated as a continua-
tion of my rst two books, with coverage on topics that I have not discussed
PREFACE

before, but it can also be read independently.


Although many prototype trading strategies have been included as
examples, one should de nitely not treat them as shrink-wrapped products
ready to deploy in live trading. As I have emphasized in my previous
books, nobody should trade someone else’s strategies without a thorough,
independent backtest, removing all likely sources of biases and data errors,
and adding various variations for improvement. Most, if not all, the
strategies I describe contain hidden biases in one way or another, waiting
for you to unearth and eliminate.
I use MATLAB for all of my research in trading. I nd it extremely
user-friendly, with constantly improving and new features, and with an
increasing number of specialized toolboxes that I can draw on. For example,
without the Statistics and Machine Learning Toolbox, it would take much
longer to explore using AI/ML techniques for trading. (See why Google
scientist and machine learning expert Kevin Murphy prefers MATLAB to R
for AI/ML research in Murphy, 2015.) In the past, readers have complained
about the high price of a MATLAB license. But now, it costs only $150 for a
‘‘Home’’ license, with each additional toolbox costing only $45. No serious
traders should compromise their productivity because of this small cost.
I am also familiar with R, which is a close relative to MATLAB. But frankly,
it is no match for MATLAB in terms of performance and user-friendliness.
A detailed comparison of these languages can be found in Chapters 1 and 6.
If you don’t already know MATLAB, it is very easy to request a one-month
trial license from mathworks.com and use its many free online tutorials
to learn the language. One great advantage of MATLAB over R or other
open-source languages is that there is excellent customer support: If you
have a question, just email or call the sta at Mathworks. (Often, someone
with a PhD will answer your questions.)
I have taught many of these topics to both retail and institutional traders
at my biannual workshops in London, as well as online (www.epchan.com).
In order to facilitate lecturers who would like to use this as a textbook for a
special topics course on Algorithmic Trading, I have included many exercises
at the end of most chapters. Some of these exercises should be treated as
suggestions for open-ended projects; there are no ready-made answers.
Readers will also nd all of the software and some data used in the
examples on epchan.com/book3. The userid and password are embedded
in Box 1.1. But unlike my previous books, some of the data involved in xi
the example strategies are under strict licensing restrictions and therefore
are unavailable for free download from my website. Readers are invited

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.

You might also like