Table of Contents....2
Preface....3
Acquiring Financial Data....14
Getting data from Yahoo Finance....16
Getting data from Nasdaq Data Link....21
Getting data from Intrinio....26
Getting data from Alpha Vantage....35
Getting data from CoinGecko....42
Summary....46
Data Preprocessing....48
Converting prices to returns....48
Adjusting the returns for inflation....52
Changing the frequency of time series data....58
Different ways of imputing missing data....62
Converting currencies....69
Different ways of aggregating trade data....73
Summary....82
Visualizing Financial Time Series....83
Basic visualization of time series data....84
Visualizing seasonal patterns....92
Creating interactive visualizations....99
Creating a candlestick chart....106
Summary....113
Exploring Financial Time Series Data....114
Outlier detection using rolling statistics....115
Outlier detection with the Hampel filter....120
Detecting changepoints in time series....126
Detecting trends in time series....133
Detecting patterns in a time series using the Hurst exponent....136
Investigating stylized facts of asset returns....142
Summary....157
Technical Analysis and Building Interactive Dashboards....159
Calculating the most popular technical indicators....160
Downloading the technical indicators....167
Recognizing candlestick patterns....172
Building an interactive web app for technical analysis using Streamlit....179
Deploying the technical analysis app....193
Summary....197
Time Series Analysis and Forecasting....198
Time series decomposition....199
Testing for stationarity in time series....211
Correcting for stationarity in time series....219
Modeling time series with exponential smoothing methods....228
Modeling time series with ARIMA class models....241
Finding the best-fitting ARIMA model with auto-ARIMA....258
Summary....274
Machine Learning-Based Approaches to Time Series Forecasting....275
Validation methods for time series....276
Feature engineering for time series....293
Time series forecasting as reduced regression....312
Forecasting with Meta’s Prophet....329
AutoML for time series forecasting with PyCaret....347
Summary....361
Multi-Factor Models....364
Estimating the CAPM....365
Estimating the Fama-French three-factor model....374
Estimating the rolling three-factor model on a portfolio of assets....382
Estimating the four- and five-factor models....386
Estimating cross-sectional factor models using the Fama-MacBeth regression....393
Summary....401
Modeling Volatility with GARCH Class Models....402
Modeling stock returns’ volatility with ARCH models....403
Modeling stock returns’ volatility with GARCH models....412
Forecasting volatility using GARCH models....420
Multivariate volatility forecasting with the CCC-GARCH model....430
Forecasting the conditional covariance matrix using DCC-GARCH....436
Summary....447
Monte Carlo Simulations in Finance....448
Simulating stock price dynamics using a geometric Brownian motion....449
Pricing European options using simulations....459
Pricing American options with Least Squares Monte Carlo....467
Pricing American options using QuantLib....473
Pricing barrier options....478
Estimating Value-at-Risk using Monte Carlo....482
Summary....490
Asset Allocation....491
Evaluating an equally-weighted portfolio’s performance....493
Finding the efficient frontier using Monte Carlo simulations....505
Finding the efficient frontier using optimization with SciPy....515
Finding the efficient frontier using convex optimization with CVXPY....525
Finding the optimal portfolio with Hierarchical Risk Parity....536
Summary....546
Backtesting Trading Strategies....547
Vectorized backtesting with pandas....550
Event-driven backtesting with backtrader....557
Backtesting a long/short strategy based on the RSI....569
Backtesting a buy/sell strategy based on Bollinger bands....579
Backtesting a moving average crossover strategy using crypto data....588
Backtesting a mean-variance portfolio optimization....596
Summary....603
Applied Machine Learning: Identifying Credit Default....605
Loading data and managing data types....606
Exploratory data analysis....617
Splitting data into training and test sets....637
Identifying and dealing with missing values....643
Encoding categorical variables....653
Fitting a decision tree classifier....663
Organizing the project with pipelines....682
Tuning hyperparameters using grid searches and cross-validation....694
Summary....711
Advanced Concepts for Machine Learning Projects....713
Exploring ensemble classifiers....715
Exploring alternative approaches to encoding categorical features....728
Investigating different approaches to handling imbalanced data....740
Leveraging the wisdom of the crowds with stacked ensembles....755
Bayesian hyperparameter optimization....764
Investigating feature importance....784
Exploring feature selection techniques....800
Exploring explainable AI techniques....819
Summary....846
Deep Learning in Finance....849
Exploring fastai’s Tabular Learner....850
Exploring Google’s TabNet....866
Time series forecasting with Amazon’s DeepAR....880
Time series forecasting with NeuralProphet....896
Summary....915
Other Books You May Enjoy....920
Index....923
Python is one of the most popular programming languages in the financial industry, with a huge collection of accompanying libraries. In this new edition of the Python for Finance Cookbook, you will explore classical quantitative finance approaches to data modeling, such as GARCH, CAPM, factor models, as well as modern machine learning and deep learning solutions.
You will use popular Python libraries that, in a few lines of code, provide the means to quickly process, analyze, and draw conclusions from financial data. In this new edition, more emphasis was put on exploratory data analysis to help you visualize and better understand financial data. While doing so, you will also learn how to use Streamlit to create elegant, interactive web applications to present the results of technical analyses.
Using the recipes in this book, you will become proficient in financial data analysis, be it for personal or professional projects. You will also understand which potential issues to expect with such analyses and, more importantly, how to overcome them.
This book is intended for financial analysts, data analysts and scientists, and Python developers with a familiarity with financial concepts. You'll learn how to correctly use advanced approaches for analysis, avoid potential pitfalls and common mistakes, and reach correct conclusions for a broad range of finance problems.
Working knowledge of the Python programming language (particularly libraries such as pandas and NumPy) is necessary.