Build Your Own
AI Investor
With Machine Learning and Python, Step by Step
Even if youve never coded before.
Damon Lee
Disclaimer
The author does not make any guarantee or other promise as to any results that are contained within or may be obtained from the content of this book. You should never make any investment decision without first consulting with your financial advisor and conducting your own research and due diligence. To the maximum extent permitted by law, the author disclaims any implied warranties of merchantability and liability in the event any information contained in this book proves to be inaccurate, incomplete or unreliable or results in any investment or other losses.
Copyright 2020 Damon Lee
All rights reserved.
ISBN : 978-1-8381322-0-0
Table of Contents
Preface
About This Book
This book aims to teach almost anyone to be able to use Machine Learning with Python to build their own AI stock investor with freely available online tools, line by line. The concept being that readers would finish with some knowledge about investing as well as a program of immediate use, made with their own hands, even if they never wrote code before.
Investing is a difficult activity fraught with uncertainty, compounded by questionable incentives, and occasionally involving unscrupulous characters. If a layperson can program their own Robo-investor, they can eliminate the last two issues and help minimise the first. At the very least they will have a better understanding of the strengths and limitations of their creations.
The performance benchmark of the AI investor will be the S&P500 index. The performance of the investing AI made from the code shown in this book will be displayed publicly on the website www.valueinvestingai.com .
Although beating the S&P500 benchmark with instructions from a book sounds too good to be true, it has been done before. In fact, it has been done many times. It has been shown to be possible when approached from a value investing perspective, with long investing time horizons and acceptance of high volatility.
This guide is not intended to be an exhaustive course on Python, nor does it teach the intricacies of all Machine Learning algorithms. This guide is intended to be practical and hands-on, where the Machine Learning concepts are explored in intuitively useful code tutorials in a narrative manner toward the goal, building competency along the way. Furthermore it aims to and to show where to look to delve deeper if desired.
Everything is written in plain English where possible, with all the code to create the AI present in print. Readers who want to skip to the end and get the AI investor working immediately can do so. This book assumes some basic knowledge of mathematics, though nothing is present that cant be looked up on the internet and understood quickly.
This book is intended to be self-contained apart from using freely available code libraries and market data on the internet from simfin.com , though to product up to date stock selection a SimFin+ subscription is required (20 as of time of writing).
Because of the source, survivorship bias is present in the models, though is well mitigated by high backtest performance giving a margin of safety, as well as the use of Altman Z scores to account for default probability in stock selection. This issue may be eliminated by purchasing more detailed market data online should the reader wish to do so with little code change (though this is more expensive than simfin.com ). In this book only common stocks listed in the USA are considered.
Who Should Read This Book
Besides people who just want to make the investing AI, there are others whom this book will be useful:
People learning to code with Python will find a book that holds your hand from Python installation and setup all the way to creating a working AI stock picker.
Those who have some programming experience but want to understand Machine Learning algorithms will find tutorials to teach the concepts required, with a final hands-on application at the end that is useful.
Programmers with no investing knowledge will get an understanding of the stock market approached from a data perspective with good theoretical backing, in doing so understanding why there is more to a stock than a ticker symbol that goes up and down. It will provide perspective on how the investing problem should be approached, and how Machine Learning can be applied in a theoretically consistent manner.
Stock investors who want to know what Machine Learning and Python tools can do for them, will find that the coding knowledge will enable them to make far more interesting screens than what online services offer as well as gaining Machine Learning knowledge that can be used in investment in as many ways as they have the creativity to imagine.
Book Roadmap
Chapter 1 is an introduction that contains the theoretical underpinning of our investing AI, explaining why it works, why it makes sense to correlate specific things, and revealing why the correlations found are unlikely to be co-incidental fleeting things that may disappear tomorrow.
Chapters 2-3 gets into Python coding and basic Machine Learning concepts, step by step in tutorials designed to develop an intuitive understanding in the reader. The tutorials develop an understanding of the limitations of what can be used and why the Machine Learning algorithms work.
Chapter 4 draws on the previous coding and Machine Learning lessons to build the investing AI. All code is shown as a set of walkthrough instructions with explanations of what each line does and reasoning behind any design decisions.
Chapters 5-7 subjects our investing AIs to backtesting with historic stock price data to whittle down the number of potential prediction algorithms to get the best stock picker. The backtesting also allows us to see how the AI is likely to perform in reality as well as the impact that tweaks in the algorithm have on performance and volatility. Chapter 7 contains the final investing AI stock picks for 2020.
Miscellaneous
Python code executed in Jupyter Notebook cells is written with the below font and grey background colour. Constant-width font is executable code, Constant-width font that is Italic after a # is a code comment. Output is displayed immediately below without the grey background to match the formatting in notebooks.
# This is a code comment
a = [ , , , , ]
print (a) # Here some output is printed out
[1, 2, 3, 4, 5]
Sometimes obvious or trivial code is left out. If displaying code that is part of a larger body [] will be used.
Exercise 0
Throughout this book, there are small programming exercises to make things easier to understand through hands-on activity. These are formatted like the text here. Typically this takes the form of a Jupyter Notebook download from the Github repository with easy to follow the instruction comments inside.
Italics are used for emphasis , book titles , websites and code in body text. When code functions are presented in the text they will have brackets after them so you know to pass arguments between the brackets: my_function() .
All code is available from the following Github repository online: https://github.com/Damonlee92/Build_Your_Own_AI_Investor . If youre used to using Github, feel free to fork the code and show the community your personal AI Investor (provided you give original attribution).
Chapter 1 - Introduction
Investing in Stocks
What are stocks and why do they have value? Why do they keep changing in value? Its worth briefly going over the basics by answering these questions because our AI will be purchasing stocks for investment, so we need to know what it would be buying. A good book that fully covers these fundamentals is Why Stock Go Up And Down by William Pike and Patrick Gregory.