Python for Algorithmic Trading
by Yves Hilpisch
Copyright 2021 Yves Hilpisch. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Michelle Smith
- Development Editor: Michele Cronin
- Production Editor: Daniel Elfanbaum
- Copyeditor: Piper Editorial LLC
- Proofreader: nSight, Inc.
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Jose Marzan
- Illustrator: Kate Dullea
- November 2020: First Edition
Revision History for the First Edition
- 2020-11-11: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492053354 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Python for Algorithmic Trading, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. This book is not intended as financial advice. Please consult a qualified professional if you require financial advice.
978-1-492-05335-4
[LSI]
Preface
Yuval Noah Harari
Finding the right algorithm to automatically and successfully trade in financial markets is the holy grail in finance. Not too long ago, algorithmic trading was only available and possible for institutional players with deep pockets and lots of assets under management. Recent developments in the areas of open source, open data, cloud compute, and cloud storage, as well as online trading platforms, have leveled the playing field for smaller institutions and individual traders, making it possible to get started in this fascinating discipline while equipped only with a typical notebook or desktop computer and a reliable internet connection.
Nowadays, Python and its ecosystem of powerful packages is the technology platform of choice for algorithmic trading. Among other things, Python allows you to do efficientdata analytics (with pandas
, for example), to apply machine learning to stock market prediction (with scikit-learn
, for example), or even to make use of Googles deep learning technology with TensorFlow
.
This is a book about Python for algorithmic trading, primarily in the context of alpha generating strategies (see). Such a book at the intersection of two vast and exciting fields can hardly cover all topics of relevance. However, it can cover a range of important meta topics in depth.
These topics include:
Financial data
Financial data is at the core of every algorithmic trading project. Python and packages like NumPy
and pandas
do a great job of handling and working with structured financial data of any kind (end-of-day, intraday, high frequency).
Backtesting
There should be no automated algorithmic trading without a rigorous testing of the trading strategy to be deployed. The book covers, among other things, trading strategies based on simple moving averages, momentum, mean-reversion, and machine/deep-learning based prediction.
Real-time data
Algorithmic trading requires dealing with real-time data, online algorithms based on it, and visualization in real time. The book provides an introduction to socket programming with ZeroMQ
and streaming visualization.
Online platforms
No trading can take place without a trading platform. The book covers two popular electronic trading platforms: Oanda and FXCM.
Automation
The beauty, as well as some major challenges, in algorithmic trading results from the automation of the trading operation. The book shows how to deploy Python in the cloud and how to set up an environment appropriate for automated algorithmic trading.
The book offers a unique learning experience with the following features and benefits:
Coverage of relevant topics
This is the only book covering such a breadth and depth with regard to relevant topics in Python for algorithmic trading (see the following).
Self-contained code base
The book is accompanied by a Git repository with all codes in a self-contained, executable form. The repository is available on the Quant Platform.
Real trading as the goal
The coverage of two different online trading platforms puts the reader in the position to start both paper and live trading efficiently. To this end, the book equips the reader with relevant, practical, and valuable background knowledge.
Do-it-yourself and self-paced approach
Since the material and the code are self-contained and only rely on standard Python packages, the reader has full knowledge of and full control over what is going on, how to use the code examples, how to change them, and so on. There is no need to rely on third-party platforms, for instance, to do the backtesting or to connect to the trading platforms. With this book, the reader can do all this on their own at a convenient pace and has every single line of code to do so.
User forum
Although the reader should be able to follow along seamlessly, the author and The Python Quants are there to help. The reader can post questions and comments in the user forum on the Quant Platform at any time (accounts are free).
Online/video training (paid subscription)
The Python Quants offer comprehensive online training programs that make use of the contents presented in the book and that add additional content, covering important topics such as financial data science, artificial intelligence in finance, Python for Excel and databases, and additional Python tools and skills.
Contents and Structure
Heres a quick overview of the topics and contents presented in each chapter.
The first chapter is an introduction to the topic of algorithmic tradingthat is, the automated trading of financial instruments based on computer algorithms. It discusses fundamental notions in this context and also addresses, among other things, what the expected prerequisites for reading the book are.
This chapter lays the technical foundations for all subsequent chapters in that it shows how to set up a proper Python environment. This chapter mainly uses conda
as a package and environment manager. It illustrates Python deployment via Docker containers and in the cloud.
Financial time series data is central to every algorithmic trading project. This chapter shows you how to retrieve financial data from different public data and proprietary data sources. It also demonstrates how to store financial time series data efficiently with Python.
Vectorization is a powerful approach in numerical computation in general and for financial analytics in particular. This chapter introduces vectorization with