Machine Learning for Algorithmic Trading - Second Edition

Book description

Leverage machine learning to design and back-test automated trading strategies for real-world markets using pandas, TA-Lib, scikit-learn, LightGBM, SpaCy, Gensim, TensorFlow 2, Zipline, backtrader, Alphalens, and pyfolio.

Purchase of the print or Kindle book includes a free eBook in the PDF format.

Key Features

  • Design, train, and evaluate machine learning algorithms that underpin automated trading strategies
  • Create a research and strategy development process to apply predictive modeling to trading decisions
  • Leverage NLP and deep learning to extract tradeable signals from market and alternative data

Book Description

The explosive growth of digital data has boosted the demand for expertise in trading strategies that use machine learning (ML). This revised and expanded second edition enables you to build and evaluate sophisticated supervised, unsupervised, and reinforcement learning models.

This book introduces end-to-end machine learning for the trading workflow, from the idea and feature engineering to model optimization, strategy design, and backtesting. It illustrates this by using examples ranging from linear models and tree-based ensembles to deep-learning techniques from cutting edge research.

This edition shows how to work with market, fundamental, and alternative data, such as tick data, minute and daily bars, SEC filings, earnings call transcripts, financial news, or satellite images to generate tradeable signals. It illustrates how to engineer financial features or alpha factors that enable an ML model to predict returns from price data for US and international stocks and ETFs. It also shows how to assess the signal content of new features using Alphalens and SHAP values and includes a new appendix with over one hundred alpha factor examples.

By the end, you will be proficient in translating ML model predictions into a trading strategy that operates at daily or intraday horizons, and in evaluating its performance.

What you will learn

  • Leverage market, fundamental, and alternative text and image data
  • Research and evaluate alpha factors using statistics, Alphalens, and SHAP values
  • Implement machine learning techniques to solve investment and trading problems
  • Backtest and evaluate trading strategies based on machine learning using Zipline and Backtrader
  • Optimize portfolio risk and performance analysis using pandas, NumPy, and pyfolio
  • Create a pairs trading strategy based on cointegration for US equities and ETFs
  • Train a gradient boosting model to predict intraday returns using AlgoSeek's high-quality trades and quotes data

Who this book is for

If you are a data analyst, data scientist, Python developer, investment analyst, or portfolio manager interested in getting hands-on machine learning knowledge for trading, this book is for you. This book is for you if you want to learn how to extract value from a diverse set of data sources using machine learning to design your own systematic trading strategies.

Some understanding of Python and machine learning techniques is required.

Table of contents

  1. Preface
    1. What to expect
    2. What's new in the second edition
    3. Who should read this book
    4. What this book covers
    5. To get the most out of this book
    6. Get in touch
  2. Machine Learning for Trading – From Idea to Execution
    1. The rise of ML in the investment industry
      1. From electronic to high-frequency trading
      2. Factor investing and smart beta funds
      3. Algorithmic pioneers outperform humans
        1. ML-driven funds attract $1 trillion in AUM
        2. The emergence of quantamental funds
        3. Investments in strategic capabilities
      4. ML and alternative data
      5. Crowdsourcing trading algorithms
    2. Designing and executing an ML-driven strategy
      1. Sourcing and managing data
      2. From alpha factor research to portfolio management
        1. The research phase
        2. The execution phase
      3. Strategy backtesting
    3. ML for trading – strategies and use cases
      1. The evolution of algorithmic strategies
      2. Use cases of ML for trading
        1. Data mining for feature extraction and insights
        2. Supervised learning for alpha factor creation
        3. Asset allocation
        4. Testing trade ideas
        5. Reinforcement learning
    4. Summary
  3. Market and Fundamental Data – Sources and Techniques
    1. Market data reflects its environment
      1. Market microstructure – the nuts and bolts
      2. How to trade – different types of orders
      3. Where to trade – from exchanges to dark pools
    2. Working with high-frequency data
      1. How to work with Nasdaq order book data
      2. Communicating trades with the FIX protocol
      3. The Nasdaq TotalView-ITCH data feed
        1. How to parse binary order messages
        2. Summarizing the trading activity for all 8,500 stocks
        3. How to reconstruct all trades and the order book
      4. From ticks to bars – how to regularize market data
        1. The raw material – tick bars
        2. Plain-vanilla denoising – time bars
        3. Accounting for order fragmentation – volume bars
        4. Accounting for price changes – dollar bars
      5. AlgoSeek minute bars – equity quote and trade data
        1. From the consolidated feed to minute bars
        2. Quote and trade data fields
        3. How to process AlgoSeek intraday data
    3. API access to market data
      1. Remote data access using pandas
        1. Reading HTML tables
        2. pandas-datareader for market data
      2. yfinance – scraping data from Yahoo! Finance
        1. How to download end-of-day and intraday prices
        2. How to download the option chain and prices
      3. Quantopian
      4. Zipline
      5. Quandl
      6. Other market data providers
    4. How to work with fundamental data
      1. Financial statement data
        1. Automated processing – XBRL
        2. Building a fundamental data time series
      2. Other fundamental data sources
        1. pandas-datareader – macro and industry data
    5. Efficient data storage with pandas
    6. Summary
  4. Alternative Data for Finance â€“ Categories and Use Cases
    1. The alternative data revolution
    2. Sources of alternative data
      1. Individuals
      2. Business processes
      3. Sensors
        1. Satellites
        2. Geolocation data
    3. Criteria for evaluating alternative data
      1. Quality of the signal content
        1. Asset classes
        2. Investment style
        3. Risk premiums
        4. Alpha content and quality
      2. Quality of the data
        1. Legal and reputational risks
        2. Exclusivity
        3. Time horizon
        4. Frequency
        5. Reliability
      3. Technical aspects
        1. Latency
        2. Format
    4. The market for alternative data
      1. Data providers and use cases
        1. Social sentiment data
        2. Satellite data
        3. Geolocation data
        4. Email receipt data
    5. Working with alternative data
      1. Scraping OpenTable data
        1. Parsing data from HTML with Requests and BeautifulSoup
        2. Introducing Selenium – using browser automation
        3. Building a dataset of restaurant bookings and ratings
        4. Taking automation one step further with Scrapy and Splash
      2. Scraping and parsing earnings call transcripts
    6. Summary
  5. Financial Feature Engineering – How to Research Alpha Factors
    1. Alpha factors in practice – from data to signals
    2. Building on decades of factor research
      1. Momentum and sentiment – the trend is your friend
        1. Why might momentum and sentiment drive excess returns?
        2. How to measure momentum and sentiment
      2. Value factors – hunting fundamental bargains
        1. Relative value strategies
        2. Why do value factors help predict returns?
        3. How to capture value effects
      3. Volatility and size anomalies
        1. Why do volatility and size predict returns?
        2. How to measure volatility and size
      4. Quality factors for quantitative investing
        1. Why quality matters
        2. How to measure asset quality
    3. Engineering alpha factors that predict returns
      1. How to engineer factors using pandas and NumPy
        1. Loading, slicing, and reshaping the data
        2. Resampling – from daily to monthly frequency
        3. How to compute returns for multiple historical periods
        4. Using lagged returns and different holding periods
        5. Computing factor betas
        6. How to add momentum factors
        7. Adding time indicators to capture seasonal effects
        8. How to create lagged return features
        9. How to create forward returns
      2. How to use TA-Lib to create technical alpha factors
      3. Denoising alpha factors with the Kalman filter
        1. How does the Kalman filter work?
        2. How to apply a Kalman filter using pykalman
      4. How to preprocess your noisy signals using wavelets
    4. From signals to trades – Zipline for backtests
      1. How to backtest a single-factor strategy
        1. A single alpha factor from market data
        2. Built-in Quantopian factors
      2. Combining factors from diverse data sources
    5. Separating signal from noise with Alphalens
      1. Creating forward returns and factor quantiles
      2. Predictive performance by factor quantiles
      3. The information coefficient
      4. Factor turnover
    6. Alpha factor resources
      1. Alternative algorithmic trading libraries
    7. Summary
  6. Portfolio Optimization and Performance Evaluation
    1. How to measure portfolio performance
      1. Capturing risk-return trade-offs in a single number
        1. The Sharpe ratio
        2. The information ratio
      2. The fundamental law of active management
    2. How to manage portfolio risk and return
      1. The evolution of modern portfolio management
      2. Mean-variance optimization
        1. How it works
        2. Finding the efficient frontier in Python
        3. Challenges and shortcomings
      3. Alternatives to mean-variance optimization
        1. The 1/N portfolio
        2. The minimum-variance portfolio
        3. Global Portfolio Optimization – the Black-Litterman approach
        4. How to size your bets – the Kelly criterion
        5. Optimal investment – multiple assets
      4. Risk parity
      5. Risk factor investment
      6. Hierarchical risk parity
    3. Trading and managing portfolios with Zipline
      1. Scheduling signal generation and trade execution
      2. Implementing mean-variance portfolio optimization
    4. Measuring backtest performance with pyfolio
      1. Creating the returns and benchmark inputs
        1. Getting pyfolio input from Alphalens
        2. Getting pyfolio input from a Zipline backtest
      2. Walk-forward testing – out-of-sample returns
        1. Summary performance statistics
        2. Drawdown periods and factor exposure
        3. Modeling event risk
    5. Summary
  7. The Machine Learning Process
    1. How machine learning from data works
      1. The challenge – matching the algorithm to the task
      2. Supervised learning – teaching by example
      3. Unsupervised learning – uncovering useful patterns
        1. Use cases – from risk management to text processing
        2. Cluster algorithms – seeking similar observations
        3. Dimensionality reduction – compressing information
      4. Reinforcement learning – learning by trial and error
    2. The machine learning workflow
      1. Basic walkthrough – k-nearest neighbors
      2. Framing the problem – from goals to metrics
        1. Prediction versus inference
        2. Regression – popular loss functions and error metrics
        3. Classification – making sense of the confusion matrix
      3. Collecting and preparing the data
      4. Exploring, extracting, and engineering features
        1. Using information theory to evaluate features
      5. Selecting an ML algorithm
      6. Design and tune the model
        1. The bias-variance trade-off
        2. Underfitting versus overfitting – a visual example
        3. How to manage the bias-variance trade-off
        4. Learning curves
      7. How to select a model using cross-validation
      8. How to implement cross-validation in Python
        1. KFold iterator
        2. Leave-one-out CV
        3. Leave-P-Out CV
        4. ShuffleSplit
      9. Challenges with cross-validation in finance
        1. Time series cross-validation with scikit-learn
        2. Purging, embargoing, and combinatorial CV
      10. Parameter tuning with scikit-learn and Yellowbrick
        1. Validation curves – plotting the impact of hyperparameters
        2. Learning curves – diagnosing the bias-variance trade-off
        3. Parameter tuning using GridSearchCV and pipeline
    3. Summary
  8. Linear Models – From Risk Factors to Return Forecasts
    1. From inference to prediction
    2. The baseline model – multiple linear regression
      1. How to formulate the model
      2. How to train the model
        1. Ordinary least squares – how to fit a hyperplane to the data
        2. Maximum likelihood estimation
        3. Gradient descent
      3. The Gauss–Markov theorem
      4. How to conduct statistical inference
      5. How to diagnose and remedy problems
        1. Goodness of fit
        2. Heteroskedasticity
        3. Serial correlation
        4. Multicollinearity
    3. How to run linear regression in practice
      1. OLS with statsmodels
      2. Stochastic gradient descent with sklearn
    4. How to build a linear factor model
      1. From the CAPM to the Fama–French factor models
      2. Obtaining the risk factors
      3. Fama–Macbeth regression
    5. Regularizing linear regression using shrinkage
      1. How to hedge against overfitting
      2. How ridge regression works
      3. How lasso regression works
    6. How to predict returns with linear regression
      1. Preparing model features and forward returns
        1. Creating the investment universe
        2. Selecting and computing alpha factors using TA-Lib
        3. Adding lagged returns
        4. Generating target forward returns
        5. Dummy encoding of categorical variables
      2. Linear OLS regression using statsmodels
        1. Selecting the relevant universe
        2. Estimating the vanilla OLS regression
        3. Diagnostic statistics
      3. Linear regression using scikit-learn
        1. Selecting features and targets
        2. Cross-validating the model
        3. Evaluating the results – information coefficient and RMSE
      4. Ridge regression using scikit-learn
        1. Tuning the regularization parameters using cross-validation
        2. Cross-validation results and ridge coefficient paths
        3. Top 10 coefficients
      5. Lasso regression using sklearn
        1. Cross-validating the lasso model
        2. Evaluating the results – IC and lasso path
      6. Comparing the quality of the predictive signals
    7. Linear classification
      1. The logistic regression model
        1. The objective function
        2. The logistic function
        3. Maximum likelihood estimation
      2. How to conduct inference with statsmodels
      3. Predicting price movements with logistic regression
        1. How to convert a regression into a classification problem
        2. Cross-validating the logistic regression hyperparameters
        3. Evaluating the results using AUC and IC
    8. Summary
  9. The ML4T Workflow – From Model to Strategy Backtesting
    1. How to backtest an ML-driven strategy
    2. Backtesting pitfalls and how to avoid them
      1. Getting the data right
        1. Look-ahead bias – use only point-in-time data
        2. Survivorship bias – track your historical universe
        3. Outlier control – do not exclude realistic extremes
        4. Sample period – try to represent relevant future scenarios
      2. Getting the simulation right
        1. Mark-to-market performance – track risks over time
        2. Transaction costs – assume a realistic trading environment
        3. Timing of decisions – properly sequence signals and trades
      3. Getting the statistics right
        1. The minimum backtest length and the deflated SR
        2. Optimal stopping for backtests
    3. How a backtesting engine works
      1. Vectorized versus event-driven backtesting
      2. Key implementation aspects
        1. Data ingestion – format, frequency, and timing
        2. Factor engineering – built-in factors versus libraries
        3. ML models, predictions, and signals
        4. Trading rules and execution
        5. Performance evaluation
    4. backtrader – a flexible tool for local backtests
      1. Key concepts of backtrader's Cerebro architecture
        1. Data feeds, lines, and indicators
        2. From data and signals to trades – strategy
        3. Commissions instead of commission schemes
        4. Making it all happen – Cerebro
      2. How to use backtrader in practice
        1. How to load price and other data
        2. How to formulate the trading logic
        3. How to configure the Cerebro instance
      3. backtrader summary and next steps
    5. Zipline – scalable backtesting by Quantopian
      1. Calendars and the Pipeline for robust simulations
        1. Bundles – point-in-time data with on-the-fly adjustments
        2. The Algorithm API – backtests on a schedule
        3. Known issues
      2. Ingesting your own bundles with minute data
        1. Getting your data ready to be bundled
        2. Writing your custom bundle ingest function
        3. Registering your bundle
        4. Creating and registering a custom TradingCalendar
      3. The Pipeline API – backtesting an ML signal
        1. Enabling the DataFrameLoader for our Pipeline
        2. Creating a pipeline with a custom ML factor
      4. How to train a model during the backtest
        1. Preparing the features – how to define pipeline factors
        2. How to design a custom ML factor
        3. Tracking model performance during a backtest
      5. Instead of how to use
    6. Summary
  10. Time-Series Models for Volatility Forecasts and Statistical Arbitrage
    1. Tools for diagnostics and feature extraction
      1. How to decompose time-series patterns
      2. Rolling window statistics and moving averages
      3. How to measure autocorrelation
    2. How to diagnose and achieve stationarity
      1. Transforming a time series to achieve stationarity
      2. Handling instead of how to handle
        1. On unit roots and random walks
        2. How to diagnose a unit root
        3. How to remove unit roots and work with the resulting series
      3. Time-series transformations in practice
    3. Univariate time-series models
      1. How to build autoregressive models
        1. How to identify the number of lags
        2. How to diagnose model fit
      2. How to build moving-average models
        1. How to identify the number of lags
        2. The relationship between the AR and MA models
      3. How to build ARIMA models and extensions
        1. How to model differenced series
        2. How to identify the number of AR and MA terms
        3. Adding features – ARMAX
        4. Adding seasonal differencing – SARIMAX
      4. How to forecast macro fundamentals
      5. How to use time-series models to forecast volatility
        1. The ARCH model
        2. Generalizing ARCH – the GARCH model
        3. How to build a model that forecasts volatility
    4. Multivariate time-series models
      1. Systems of equations
      2. The vector autoregressive (VAR) model
      3. Using the VAR model for macro forecasts
    5. Cointegration – time series with a shared trend
      1. The Engle-Granger two-step method
      2. The Johansen likelihood-ratio test
    6. Statistical arbitrage with cointegration
      1. How to select and trade comoving asset pairs
      2. Pairs trading in practice
        1. Distance-based heuristics to find cointegrated pairs
        2. How well do the heuristics predict significant cointegration?
      3. Preparing the strategy backtest
        1. Precomputing the cointegration tests
        2. Getting entry and exit trades
      4. Backtesting the strategy using backtrader
        1. Tracking pairs with a custom DataClass
        2. Running and evaluating the strategy
      5. Extensions – how to do better
    7. Summary
  11. Bayesian ML – Dynamic Sharpe Ratios and Pairs Trading
    1. How Bayesian machine learning works
      1. How to update assumptions from empirical evidence
      2. Exact inference – maximum a posteriori estimation
        1. How to select priors
        2. How to keep inference simple – conjugate priors
        3. Dynamic probability estimates of asset price moves
      3. Deterministic and stochastic approximate inference
        1. Markov chain MonteCarlo sampling
        2. Variational inference and automatic differentiation
    2. Probabilistic programming with PyMC3
      1. Bayesian machine learning with Theano
      2. The PyMC3 workflow – predicting a recession
        1. The data – leading recession indicators
        2. Model definition – Bayesian logistic regression
        3. Exact MAP inference
        4. Approximate inference – MCMC
        5. Approximate inference – variational Bayes
        6. Model diagnostics
        7. How to generate predictions
        8. Summary and key takeaways
    3. Bayesian ML for trading
      1. Bayesian Sharpe ratio for performance comparison
        1. Defining a custom probability model
        2. Comparing the performance of two return series
      2. Bayesian rolling regression for pairs trading
      3. Stochastic volatility models
    4. Summary
  12. Random Forests – A Long-Short Strategy for Japanese Stocks
    1. Decision trees – learning rules from data
      1. How trees learn and apply decision rules
      2. Decision trees in practice
        1. The data – monthly stock returns and features
        2. Building a regression tree with time-series data
        3. Building a classification tree
        4. Visualizing a decision tree
        5. Evaluating decision tree predictions
      3. Overfitting and regularization
        1. How to regularize a decision tree
        2. Decision tree pruning
      4. Hyperparameter tuning
        1. Using GridsearchCV with a custom metric
        2. How to inspect the tree structure
        3. Comparing regression and classification performance
        4. Diagnosing training set size with learning curves
        5. Gaining insight from feature importance
        6. Strengths and weaknesses of decision trees
    2. Random forests – making trees more reliable
      1. Why ensemble models perform better
      2. Bootstrap aggregation
        1. How bagging lowers model variance
        2. Bagged decision trees
      3. How to build a random forest
      4. How to train and tune a random forest
      5. Feature importance for random forests
      6. Out-of-bag testing
      7. Pros and cons of random forests
    3. Long-short signals for Japanese stocks
      1. The data – Japanese equities
        1. The features – lagged returns and technical indicators
        2. The outcomes – forward returns for different horizons
      2. The ML4T workflow with LightGBM
        1. From universe selection to hyperparameter tuning
        2. Sampling tickers to speed up cross-validation
        3. Defining lookback, lookahead, and roll-forward periods
        4. Hyperparameter tuning with LightGBM
        5. Cross-validating signals over various horizons
        6. Analyzing cross-validation performance
        7. Ensembling forecasts – signal analysis using Alphalens
      3. The strategy – backtest with Zipline
        1. Ingesting Japanese Equities into Zipline
        2. Running an in- and out-of-sample strategy backtest
        3. The results – evaluation with pyfolio
    4. Summary
  13. Boosting Your Trading Strategy
    1. Getting started – adaptive boosting
      1. The AdaBoost algorithm
      2. Using AdaBoost to predict monthly price moves
    2. Gradient boosting – ensembles for most tasks
      1. How to train and tune GBM models
        1. Ensemble size and early stopping
        2. Shrinkage and learning rate
        3. Subsampling and stochastic gradient boosting
      2. How to use gradient boosting with sklearn
        1. How to tune parameters with GridSearchCV
        2. Parameter impact on test scores
        3. How to test on the holdout set
    3. Using XGBoost, LightGBM, and CatBoost
      1. How algorithmic innovations boost performance
        1. Second-order loss function approximation
        2. Simplified split-finding algorithms
        3. Depth-wise versus leaf-wise growth
        4. GPU-based training
        5. DART – dropout for additive regression trees
        6. Treatment of categorical features
        7. Additional features and optimizations
    4. A long-short trading strategy with boosting
      1. Generating signals with LightGBM and CatBoost
        1. From Python to C++ – creating binary data formats
        2. How to tune hyperparameters
        3. How to evaluate the results
      2. Inside the black box – interpreting GBM results
        1. Feature importance
        2. Partial dependence plots
        3. SHapley Additive exPlanations
      3. Backtesting a strategy based on a boosting ensemble
      4. Lessons learned and next steps
    5. Boosting for an intraday strategy
      1. Engineering features for high-frequency data
      2. Minute-frequency signals with LightGBM
      3. Evaluating the trading signal quality
    6. Summary
  14. Data-Driven Risk Factors and Asset Allocation with Unsupervised Learning
    1. Dimensionality reduction
      1. The curse of dimensionality
      2. Linear dimensionality reduction
        1. Principal component analysis
        2. Independent component analysis
      3. Manifold learning – nonlinear dimensionality reduction
        1. t-distributed Stochastic Neighbor Embedding
        2. Uniform Manifold Approximation and Projection
    2. PCA for trading
      1. Data-driven risk factors
        1. Preparing the data – top 350 US stocks
        2. Running PCA to identify the key return drivers
      2. Eigenportfolios
    3. Clustering
      1. k-means clustering
        1. Assigning observations to clusters
        2. Evaluating cluster quality
      2. Hierarchical clustering
        1. Different strategies and dissimilarity measures
        2. Visualization – dendrograms
      3. Density-based clustering
        1. DBSCAN
        2. Hierarchical DBSCAN
      4. Gaussian mixture models
    4. Hierarchical clustering for optimal portfolios
      1. How hierarchical risk parity works
      2. Backtesting HRP using an ML trading strategy
        1. Ensembling the gradient boosting model predictions
        2. Using PyPortfolioOpt to compute HRP weights
        3. Performance comparison with pyfolio
    5. Summary
  15. Text Data for Trading – Sentiment Analysis
    1. ML with text data – from language to features
      1. Key challenges of working with text data
      2. The NLP workflow
        1. Parsing and tokenizing text data – selecting the vocabulary
        2. Linguistic annotation – relationships among tokens
        3. Semantic annotation – from entities to knowledge graphs
        4. Labeling – assigning outcomes for predictive modeling
      3. Applications
    2. From text to tokens – the NLP pipeline
      1. NLP pipeline with spaCy and textacy
        1. Parsing, tokenizing, and annotating a sentence
        2. Batch-processing documents
        3. Sentence boundary detection
        4. Named entity recognition
        5. N-grams
        6. spaCy's streaming API
        7. Multi-language NLP
      2. NLP with TextBlob
        1. Stemming
        2. Sentiment polarity and subjectivity
    3. Counting tokens – the document-term matrix
      1. The bag-of-words model
        1. Creating the document-term matrix
        2. Measuring the similarity of documents
      2. Document-term matrix with scikit-learn
        1. Using CountVectorizer
        2. TfidfTransformer and TfidfVectorizer
      3. Key lessons instead of lessons learned
    4. NLP for trading
      1. The naive Bayes classifier
        1. Bayes' theorem refresher
        2. The conditional independence assumption
      2. Classifying news articles
      3. Sentiment analysis with Twitter and Yelp data
        1. Binary sentiment classification with Twitter data
        2. Multiclass sentiment analysis with Yelp business reviews
    5. Summary
  16. Topic Modeling – Summarizing Financial News
    1. Learning latent topics – Goals and approaches
      1. How to implement LSI using sklearn
      2. Strengths and limitations
    2. Probabilistic latent semantic analysis
      1. How to implement pLSA using sklearn
      2. Strengths and limitations
    3. Latent Dirichlet allocation
      1. How LDA works
        1. The Dirichlet distribution
        2. The generative model
        3. Reverse engineering the process
      2. How to evaluate LDA topics
        1. Perplexity
        2. Topic coherence
      3. How to implement LDA using sklearn
      4. How to visualize LDA results using pyLDAvis
      5. How to implement LDA using Gensim
    4. Modeling topics discussed in earnings calls
      1. Data preprocessing
      2. Model training and evaluation
      3. Running experiments
    5. Topic modeling for with financial news
    6. Summary
  17. Word Embeddings for Earnings Calls and SEC Filings
    1. How word embeddings encode semantics
      1. How neural language models learn usage in context
      2. word2vec – scalable word and phrase embeddings
        1. Model objective – simplifying the softmax
        2. Automating phrase detection
      3. Evaluating embeddings using semantic arithmetic
    2. How to use pretrained word vectors
      1. GloVe – Global vectors for word representation
    3. Custom embeddings for financial news
      1. Preprocessing – sentence detection and n-grams
      2. The skip-gram architecture in TensorFlow 2
        1. Noise-contrastive estimation – creating validation samples
        2. Generating target-context word pairs
        3. Creating the word2vec model layers
      3. Visualizing embeddings using TensorBoard
      4. How to train embeddings faster with Gensim
    4. word2vec for trading with SEC filings
      1. Preprocessing – sentence detection and n-grams
        1. Automatic phrase detection
        2. Labeling filings with returns to predict earnings surprises
      2. Model training
        1. Model evaluation
        2. Performance impact of parameter settings
    5. Sentiment analysis using doc2vec embeddings
      1. Creating doc2vec input from Yelp sentiment data
      2. Training a doc2vec model
      3. Training a classifier with document vectors
      4. Lessons learned and next steps
    6. New frontiers – pretrained transformer models
      1. Attention is all you need
      2. BERT – towards a more universal language model
        1. Key innovations – deeper attention and pretraining
        2. Using pretrained state-of-the-art models
      3. Trading on text data – lessons learned and next steps
    7. Summary
  18. Deep Learning for Trading
    1. Deep learning – what's new and why it matters
      1. Hierarchical features tame high-dimensional data
      2. DL as representation learning
        1. How DL extracts hierarchical features from data
        2. Good and bad news – the universal approximation theorem
      3. How DL relates to ML and AI
    2. Designing an NN
      1. A simple feedforward neural network architecture
      2. Key design choices
        1. Hidden units and activation functions
        2. Output units and cost functions
      3. How to regularize deep NNs
        1. Parameter norm penalties
        2. Early stopping
        3. Dropout
      4. Training faster – optimizations for deep learning
        1. Stochastic gradient descent
        2. Momentum
        3. Adaptive learning rates
      5. Summary – how to tune key hyperparameters
    3. A neural network from scratch in Python
      1. The input layer
      2. The hidden layer
      3. The output layer
      4. Forward propagation
      5. The cross-entropy cost function
      6. How to implement backprop using Python
        1. How to compute the gradient
        2. The loss function gradient
        3. The output layer gradients
        4. The hidden layer gradients
        5. Putting it all together
        6. Training the network
    4. Popular deep learning libraries
      1. Leveraging GPU acceleration
      2. How to use TensorFlow 2
      3. How to use TensorBoard
      4. How to use PyTorch 1.4
        1. How to create a PyTorch DataLoader
        2. How to define the neural network architecture
        3. How to train the model
        4. How to evaluate the model predictions
      5. Alternative options
        1. Apache MXNet
        2. Microsoft Cognitive Toolkit (CNTK)
        3. Fastai
    5. Optimizing an NN for a long-short strategy
      1. Engineering features to predict daily stock returns
      2. Defining an NN architecture framework
      3. Cross-validating design options to tune the NN
      4. Evaluating the predictive performance
      5. Backtesting a strategy based on ensembled signals
        1. Ensembling predictions to produce tradeable signals
        2. Evaluating signal quality using Alphalens
        3. Backtesting the strategy using Zipline
      6. How to further improve the results
    6. Summary
  19. CNNs for Financial Time Series and Satellite Images
    1. How CNNs learn to model grid-like data
      1. From hand-coding to learning filters from data
      2. How the elements of a convolutional layer operate
        1. The convolution stage – extracting local features
        2. The detector stage – adding nonlinearity
        3. The pooling stage – downsampling the feature maps
      3. The evolution of CNN architectures – key innovations
        1. Performance breakthroughs and network size
        2. Lessons learned
    2. CNNs for satellite images and object detection
      1. LeNet5 – The first CNN with industrial applications
        1. "Hello World" for CNNs – handwritten digit classification
        2. Defining the LeNet5 architecture
        3. Training and evaluating the model
      2. AlexNet – reigniting deep learning research
        1. Preprocessing CIFAR-10 data using image augmentation
        2. Defining the model architecture
        3. Comparing AlexNet performance
      3. Transfer learning – faster training with less data
        1. Alternative approaches to transfer learning
        2. Building on state-of-the-art architectures
        3. Transfer learning with VGG16 in practice
        4. Classifying satellite images with transfer learning
      4. Object detection and segmentation
      5. Object detection in practice
        1. Preprocessing the source images
        2. Transfer learning with a custom final layer
        3. Creating a custom loss function and evaluation metrics
        4. Fine-tuning the VGG16 weights and final layer
        5. Lessons learned
    3. CNNs for time-series data – predicting returns
      1. An autoregressive CNN with 1D convolutions
        1. Preprocessing the data
        2. Defining the model architecture
        3. Model training and performance evaluation
      2. CNN-TA – clustering time series in 2D format
        1. Creating technical indicators at different intervals
        2. Computing rolling factor betas for different horizons
        3. Features selecting based on mutual information
        4. Hierarchical feature clustering
        5. Creating and training a convolutional neural network
        6. Assembling the best models to generate tradeable signals
        7. Backtesting a long-short trading strategy
        8. Summary and lessons learned
    4. Summary
  20. RNNs for Multivariate Time Series and Sentiment Analysis
    1. How recurrent neural nets work
      1. Unfolding a computational graph with cycles
      2. Backpropagation through time
      3. Alternative RNN architectures
        1. Output recurrence and teacher forcing
        2. Bidirectional RNNs
        3. Encoder-decoder architectures, attention, and transformers
      4. How to design deep RNNs
      5. The challenge of learning long-range dependencies
        1. Long short-term memory – learning how much to forget
      6. Gated recurrent units
    2. RNNs for time series with TensorFlow 2
      1. Univariate regression – predicting the S&P 500
      2. How to get time series data into shape for an RNN
        1. How to define a two-layer RNN with a single LSTM layer
        2. Training and evaluating the model
        3. Re-scaling the predictions
      3. Stacked LSTM – predicting price moves and returns
        1. Preparing the data – how to create weekly stock returns
        2. How to create multiple inputs in RNN format
        3. How to define the architecture using Keras' Functional API
        4. Predicting returns instead of directional price moves
      4. Multivariate time-series regression for macro data
        1. Loading sentiment and industrial production data
        2. Making the data stationary and adjusting the scale
        3. Creating multivariate RNN inputs
        4. Defining and training the model
    3. RNNs for text data
      1. LSTM with embeddings for sentiment classification
        1. Loading the IMDB movie review data
        2. Defining embedding and the RNN architecture
      2. Sentiment analysis with pretrained word vectors
        1. Preprocessing the text data
        2. Loading the pretrained GloVe embeddings
        3. Defining the architecture with frozen weights
      3. Predicting returns from SEC filing embeddings
        1. Source stock price data using yfinance
        2. Preprocessing SEC filing data
        3. Preparing data for the RNN model
        4. Building, training, and evaluating the RNN model
        5. Lessons learned and next steps
    4. Summary
  21. Autoencoders for Conditional Risk Factors and Asset Pricing
    1. Autoencoders for nonlinear feature extraction
      1. Generalizing linear dimensionality reduction
      2. Convolutional autoencoders for image compression
      3. Managing overfitting with regularized autoencoders
      4. Fixing corrupted data with denoising autoencoders
      5. Seq2seq autoencoders for time series features
      6. Generative modeling with variational autoencoders
    2. Implementing autoencoders with TensorFlow 2
      1. How to prepare the data
      2. One-layer feedforward autoencoder
        1. Defining the encoder
        2. Defining the decoder
        3. Training the model
        4. Evaluating the results
      3. Feedforward autoencoder with sparsity constraints
      4. Deep feedforward autoencoder
        1. Visualizing the encoding
      5. Convolutional autoencoders
      6. Denoising autoencoders
    3. A conditional autoencoder for trading
      1. Sourcing stock prices and metadata information
      2. Computing predictive asset characteristics
      3. Creating the conditional autoencoder architecture
      4. Lessons learned and next steps
    4. Summary
  22. Generative Adversarial Networks for Synthetic Time-Series Data
    1. Creating synthetic data with GANs
      1. Comparing generative and discriminative models
      2. Adversarial training – a zero-sum game of trickery
      3. The rapid evolution of the GAN architecture zoo
        1. Deep convolutional GANs for representation learning
        2. Conditional GANs for image-to-image translation
      4. GAN applications to images and time-series data
        1. CycleGAN – unpaired image-to-image translation
        2. StackGAN – text-to-photo image synthesis
        3. SRGAN – photorealistic single image super-resolution
        4. Synthetic time series with recurrent conditional GANs
    2. How to build a GAN using TensorFlow 2
      1. Building the generator network
      2. Creating the discriminator network
      3. Setting up the adversarial training process
        1. Defining the generator and discriminator loss functions
        2. The core – designing the training step
        3. Putting it together – the training loop
      4. Evaluating the results
    3. TimeGAN for synthetic financial data
      1. Learning to generate data across features and time
        1. Combining adversarial and supervised training
        2. The four components of the TimeGAN architecture
        3. Joint training of an autoencoder and adversarial network
      2. Implementing TimeGAN using TensorFlow 2
        1. Preparing the real and random input series
        2. Creating the TimeGAN model components
        3. Training phase 1 – autoencoder with real data
        4. Training phase 2 – supervised learning with real data
        5. Training phase 3 – joint training with real and random data
        6. Generating synthetic time series
      3. Evaluating the quality of synthetic time-series data
        1. Assessing diversity – visualization using PCA and t-SNE
        2. Assessing fidelity – time-series classification performance
        3. Assessing usefulness – train on synthetic, test on real
      4. Lessons learned and next steps
    4. Summary
  23. Deep Reinforcement Learning – Building a Trading Agent
    1. Elements of a reinforcement learning system
      1. The policy – translating states into actions
      2. Rewards – learning from actions
      3. The value function – optimal choice for the long run
      4. With or without a model – look before you leap?
    2. How to solve reinforcement learning problems
      1. Key challenges in solving RL problems
        1. Credit assignment
        2. Exploration versus exploitation
      2. Fundamental approaches to solving RL problems
    3. Solving dynamic programming problems
      1. Finite Markov decision problems
        1. Sequences of states, actions, and rewards
        2. Value functions – how to estimate the long-run reward
        3. The Bellman equations
        4. From a value function to an optimal policy
      2. Policy iteration
      3. Value iteration
      4. Generalized policy iteration
      5. Dynamic programming in Python
        1. Setting up the gridworld
        2. Computing the transition matrix
        3. Implementing the value iteration algorithm
        4. Defining and running policy iteration
        5. Solving MDPs using pymdptoolbox
        6. Lessons learned
    4. Q-learning – finding an optimal policy on the go
      1. Exploration versus exploitation – ε-greedy policy
      2. The Q-learning algorithm
      3. How to train a Q-learning agent using Python
    5. Deep RL for trading with the OpenAI Gym
      1. Value function approximation with neural networks
      2. The Deep Q-learning algorithm and extensions
        1. (Prioritized) Experience replay – focusing on past mistakes
        2. The target network – decorrelating the learning process
        3. Double deep Q-learning – decoupling action and prediction
      3. Introducing the OpenAI Gym
      4. How to implement DDQN using TensorFlow 2
        1. Creating the DDQN agent
        2. Adapting the DDQN architecture to the Lunar Lander
        3. Memorizing transitions and replaying the experience
        4. Setting up the OpenAI environment
        5. Key hyperparameter choices
        6. Lunar Lander learning performance
      5. Creating a simple trading agent
      6. How to design a custom OpenAI trading environment
        1. Designing a DataSource class
        2. The TradingSimulator class
        3. The TradingEnvironment class
        4. Registering and parameterizing the custom environment
      7. Deep Q-learning on the stock market
        1. Adapting and training the DDQN agent
        2. Benchmarking DDQN agent performance
      8. Lessons learned
    6. Summary
  24. Conclusions and Next Steps
    1. Key takeaways and lessons learned
      1. Data is the single most important ingredient
        1. The new oil? Quality control for raw and intermediate data
        2. Data integration – the whole exceeds the sum of its parts
      2. Domain expertise – telling the signal from the noise
      3. ML is a toolkit for solving problems with data
        1. Model diagnostics help speed up optimization
        2. Making do without a free lunch
        3. Managing the bias-variance trade-off
        4. Defining targeted model objectives
        5. The optimization verification test
      4. Beware of backtest overfitting
      5. How to gain insights from black-box models
    2. ML for trading in practice
      1. Data management technologies
        1. Database systems
        2. Big data technologies – from Hadoop to Spark
      2. ML tools
      3. Online trading platforms
        1. Quantopian
        2. QuantConnect
        3. QuantRocket
    3. Conclusion
  25. Appendix: Alpha Factor Library
    1. Common alpha factors implemented in TA-Lib
      1. A key building block – moving averages
        1. Simple moving average
        2. Exponential moving average
        3. Weighted moving average
        4. Double exponential moving average
        5. Triple exponential moving average
        6. Triangular moving average
        7. Kaufman adaptive moving average
        8. MESA adaptive moving average
        9. Visual comparison of moving averages
      2. Overlap studies – price and volatility trends
        1. Bollinger Bands
        2. Parabolic SAR
      3. Momentum indicators
        1. Average directional movement indicators
        2. Aroon Oscillator
        3. Balance of power
        4. Commodity channel index
        5. Moving average convergence divergence
        6. Stochastic relative strength index
        7. Stochastic oscillator
        8. Ultimate oscillator
      4. Volume and liquidity indicators
        1. Chaikin accumulation/distribution line and oscillator
        2. On-balance volume
      5. Volatility indicators
        1. Average true range
        2. Normalized average true range
      6. Fundamental risk factors
    2. WorldQuant's quest for formulaic alphas
      1. Cross-sectional and time-series functions
      2. Formulaic alpha expressions
        1. Alpha 001
        2. Alpha 054
    3. Bivariate and multivariate factor evaluation
      1. Information coefficient and mutual information
      2. Feature importance and SHAP values
      3. Comparison – the top 25 features for each metric
      4. Financial performance – Alphalens
  26. References
  27. Index

Product information

  • Title: Machine Learning for Algorithmic Trading - Second Edition
  • Author(s): Stefan Jansen
  • Release date: July 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839217715