Table Of ContentTrading Evolved
Anyone can Build
Killer Trading Strategies
in Python
Book version 1.1
Copyright © 2019 Andreas F. Clenow
Registered Office: Equilateral Capital Management GmbH,
Lowenstrasse 1, 8001 Zurich, Switzerland
For details of editorial policies and information for how to apply
for permission to reuse the copyright material in this book please
see our website at www.FollowingTheTrend.com.
The right of the author to be identified as the author of this work
has been asserted in accordance with the Copyright, Designs and
Patents act 1998. All rights reserved. No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in
any form by any means, electronic, mechanical, photocopying,
teleportation, recording or otherwise except as permitted by the UK
Copyright, Designs and Patents Act 1998 or other irrelevant but
good sounding legal paragraphs, without the prior permission of
the publisher. Doing so would make you a very naughty boy and
you don’t want that, do you.
Designations used by companies to distinguish their products are
often claimed as trademarks. All brand names and product names
used in this book are trade names, service marks, trademarks or
registered trademarks of their respective owners. The publisher is
not associated with any product or vendor mentioned in this book
except if expressly stated.
Limit of Liability/Disclaimer of Warranty: While the publisher and
the author have used their best efforts in preparing this book, they
make no representations or warranties with the respect to the
accuracy or completeness of the contents of this book and
specifically disclaim any implied warranties of merchantability or
fitness for a particular purpose. It is sold on the understanding that
the publisher is not engaged in rendering professional services and
neither the publisher nor the author shall be liable for damages
arising herefrom. If professional advice or other expert assistance is
required, the services of a competent professional should be sought.
ISBN: 9781091983786
To my wife Eng Cheng and my son Brandon, not only because
of their love and support but also because they have been asking for
months about whether they will get a dedication in this book.
1 ABOUT THIS BOOK
The Trading Strategies in this Book
How to Read this Book
How this book is written
How the code is written
Errata
Support
2 SYSTEMATIC TRADING
Trading Approach Validation
Scientific Approach
Consistent Methodology
Time Management
3 DEVELOPING TRADING MODELS
Model Purpose
Rules and Variations
Handling Data
Asset Class
Investment Universe
Allocation and Risk Level
Entry and Exit Rules
Rebalancing
4 FINANCIAL RISK
Quantifying Risk
Mark to Market
Common Risk Fallacies
Risk as Currency to Buy Performance
5 INTRODUCTION TO PYTHON
Some Assembly Required
Python Emerges as the Logical Choice
Programming Teaching Approach
Installing Python on your Computer
Let’s Run Some Code
Working with Jupyter Notebook
Dictionary Lookup
Conditional Logic
Common Mistakes
Installing Libraries
6 BRING OUT THE PANDAS
Documentation and Help
Simple Python Simulation
Making a Correlation Graph
Prettier Graphs
7 BACKTESTING TRADING STRATEGIES
Python Backtesting Engines
Zipline and Quantopian
Pros and Cons
Installing Zipline
Problems with Installing Zipline
Patching the Framework
Zipline and Data
Ingesting the Quandl Bundle
Installing Useful Libraries
Where to Write Backtest Algos
Your First Zipline Backtest
Portfolio Backtest
Data Used for this Book
8 ANALYZING BACKTEST RESULTS
Installing PyFolio
Portfolio Algorithm to Analyze
Analyzing Performance with PyFolio
Custom Analysis
Day Snapshot
Custom Time Series Analytics
9 EXCHANGE TRADED FUNDS
The Good
The Bad
The Worst
Shorting Exchange Traded Funds
10 CONSTRUCTING ETF MODELS
Asset Allocation Model
11 EQUITIES
The Most Difficult Asset Class
A word on Methodology
Equity Investment Universe
Dividends
12 SYSTEMATIC MOMENTUM
Replicating this Model
Momentum Model Rules Summary
Investment Universe
Momentum Ranking
Position Allocation
Momentum Model Logic
Downside Protection
Momentum Model Source Code
Performance
Equity Momentum Model Results
13 FUTURES MODELS
Futures Basics
Futures Mechanics and Terminology
Futures and Currency Exposure
Futures and Leverage
14 FUTURES MODELING AND BACKTESTING
Continuations
Zipline Continuation Behavior
Contracts, Continuations and Rolling
15 FUTURES TREND FOLLOWING
Principles of Trend Following
Revisiting the Core Trend Model
Model Purpose
Investment Universe
Trading Frequency
Position Allocation
Entry Rules
Exit Rules
Costs and Slippage
Interest on Liquidity
Trend Model Source Code
Core Trend Model Results
16 TIME RETURN TREND MODEL
Investment Universe
Trading Frequency
Position Allocation
Trading Rules
Dynamic Performance Chart
Time Return Source Code
Time Return Model Performance
Rebalancing
17 COUNTER TREND TRADING
Counter Model Logic
Quantifying Pullbacks
Rules Summary
Counter Trend Source Code
Counter Trend Results
18 TRADING THE CURVE
Term Structure Basics
Quantifying Term Structure Effect
Curve Model Logic
Curve Trading Source Code
Curve Trading Results
Model Considerations
19 COMPARING AND COMBINING MODELS
Combining the Models
Implementing a Portfolio of Models
20 PERFORMANCE VISUALIZATION AND
COMBINATIONS
Storing Model Results
How the Model Performance Analysis was done
How the Combined Portfolio Analysis was done
21 YOU CAN’T BEAT ALL OF THE MONKEYS ALL OF
THE TIME
Mr. Bubbles goes to Wall Street
The Problem is with the Index
Finding Mr. Bubbles
22 GUEST CHAPTER: MEASURING RELATIVE
PERFORMANCE
23 IMPORTING YOUR DATA
Making a Bundle
Zipline and Futures Data
Futures Data Bundle
Patching the Framework
24 DATA AND DATABASES
Your Very Own Securities Database
Installing MySQL Server
Making an Equities Time-Series Table
Populating the Database
Querying the Database
Making a Database Bundle
25 FINAL WORDS – PATH FORWARD