Book Image

Hands-On Machine Learning for Algorithmic Trading

By : Stefan Jansen
Book Image

Hands-On Machine Learning for Algorithmic Trading

By: Stefan Jansen

Overview of this book

The explosive growth of digital data has boosted the demand for expertise in trading strategies that use machine learning (ML). This book enables you to use a broad range of supervised and unsupervised algorithms to extract signals from a wide variety of data sources and create powerful investment strategies. This book shows how to access market, fundamental, and alternative data via API or web scraping and offers a framework to evaluate alternative data. You’ll practice the ML work?ow from model design, loss metric definition, and parameter tuning to performance evaluation in a time series context. You will understand ML algorithms such as Bayesian and ensemble methods and manifold learning, and will know how to train and tune these models using pandas, statsmodels, sklearn, PyMC3, xgboost, lightgbm, and catboost. This book also teaches you how to extract features from text data using spaCy, classify news and assign sentiment scores, and to use gensim to model topics and learn word embeddings from financial reports. You will also build and evaluate neural networks, including RNNs and CNNs, using Keras and PyTorch to exploit unstructured data for sophisticated strategies. Finally, you will apply transfer learning to satellite images to predict economic activity and use reinforcement learning to build agents that learn to trade in the OpenAI Gym.
Table of Contents (23 chapters)

How to measure performance with pyfolio

ML is about optimizing objective functions. In algorithmic trading, the objectives are the return and the risk of the overall investment portfolio, typically relative to a benchmark (which may be cash or the risk-free interest rate).

There are several metrics to evaluate these objectives. We will briefly review the most commonly-used metrics and how to compute them using the pyfolio library, which is also used by zipline and Quantopian. We will also review how to apply these metrics on Quantopian when testing an algorithmic trading strategy.

We'll use some simple notations: let R be the time series of one-period simple portfolio returns, R=(r1, ..., rT), from dates 1 to T, and Rf =(rf1, ..., rfT) be the matching time series of risk-free rates, so that Re=R-Rf =(r1-rf1,..., rT-rfT) is the excess return.

...