Trading 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 dont 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.
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
Systematic Trading
Trading Approach Validation
Scientific Approach
Consistent Methodology
Time Management
Developing Trading Models
Model Purpose
Rules and Variations
Handling Data
Asset Class
Investment Universe
Allocation and Risk Level
Entry and Exit Rules
Rebalancing
Financial Risk
Quantifying Risk
Mark to Market
Common Risk Fallacies
Risk as Currency to Buy Performance
Introduction to Python
Some Assembly Required
Python Emerges as the Logical Choice
Programming Teaching Approach
Installing Python on your Computer
Lets Run Some Code
Working with Jupyter Notebook
Dictionary Lookup
Conditional Logic
Common Mistakes
Installing Libraries
Bring out the Pandas
Documentation and Help
Simple Python Simulation
Making a Correlation Graph
Prettier Graphs
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
Analyzing Backtest Results
Installing PyFolio
Portfolio Algorithm to Analyze
Analyzing Performance with PyFolio
Custom Analysis
Day Snapshot
Custom Time Series Analytics
Exchange Traded Funds
The Good
The Bad
The Worst
Shorting Exchange Traded Funds
Constructing ETF Models
Asset Allocation Model
Equities
The Most Difficult Asset Class
A word on Methodology
Equity Investment Universe
Dividends
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
Futures Models
Futures Basics
Futures Mechanics and Terminology
Futures and Currency Exposure
Futures and Leverage
Futures Modeling and Backtesting
Continuations
Zipline Continuation Behavior
Contracts, Continuations and Rolling
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
Time Return Trend Model
Investment Universe
Trading Frequency
Position Allocation
Trading Rules
Dynamic Performance Chart
Time Return Source Code
Time Return Model Performance
Rebalancing
Counter Trend Trading
Counter Model Logic
Quantifying Pullbacks
Rules Summary
Counter Trend Source Code
Counter Trend Results
Trading the Curve
Term Structure Basics
Quantifying Term Structure Effect
Curve Model Logic
Curve Trading Source Code
Curve Trading Results
Model Considerations
Comparing and Combining Models
Combining the Models
Implementing a Portfolio of Models
Performance Visualization and Combinations
Storing Model Results
How the Model Performance Analysis was done
How the Combined Portfolio Analysis was done
You cant beat all of the Monkeys all of the Time
Mr. Bubbles goes to Wall Street
The Problem is with the Index
Finding Mr. Bubbles
Guest Chapter: Measuring Relative Performance
Importing your Data
Making a Bundle
Zipline and Futures Data
Futures Data Bundle
Patching the Framework
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
Final Words Path Forward
Build Your Own Models
Other Backtesting Engines
How to Make Money in the Markets
References
Index
While I would like to claim that I managed to write this book with absolutely no help from anyone, nothing could be farther from the truth. The help that Ive received along the way has been invaluable, and I couldnt have completed this book without it. Without these people, this book would either never have gotten off the ground or it would have ended up a total disaster. In no particular order, Id like to express my gratitude towards these people.
John Grover, Matthew Martelli, Robert Carver, Riccardo Ronco, Thomas Starke, Tomasz Mierzejewski, Erk Subasi and Jonathan Larkin.
About this Book
This book will guide you step by step on how to get familiar with Python, how to set up a local quant modeling environment and how to construct and analyze trading strategies. Its by no means an exhaustive book, either on Python, backtesting or trading. It wont make you an expert in either topic, but will aim for giving you a solid foundation in all of them.
When approaching something as complex as backtesting trading strategies, every step on the way can be done in a multitude of ways. This book does not attempt to cover all the different ways that you could approach Python trading strategies. A book like that would require many times the text mass of this book. But more importantly, a book of that type would likely scare away the bulk of the people that I want to address.
Next page