Book Image

Python for Finance Cookbook

By : Eryk Lewinson
Book Image

Python for Finance Cookbook

By: Eryk Lewinson

Overview of this book

Python is one of the most popular programming languages used in the financial industry, with a huge set of accompanying libraries. In this book, you'll cover different ways of downloading financial data and preparing it for modeling. You'll calculate popular indicators used in technical analysis, such as Bollinger Bands, MACD, RSI, and backtest automatic trading strategies. Next, you'll cover time series analysis and models, such as exponential smoothing, ARIMA, and GARCH (including multivariate specifications), before exploring the popular CAPM and the Fama-French three-factor model. You'll then discover how to optimize asset allocation and use Monte Carlo simulations for tasks such as calculating the price of American options and estimating the Value at Risk (VaR). In later chapters, you'll work through an entire data science project in the financial domain. You'll also learn how to solve the credit card fraud and default problems using advanced classifiers such as random forest, XGBoost, LightGBM, and stacked models. You'll then be able to tune the hyperparameters of the models and handle class imbalance. Finally, you'll focus on learning how to use deep learning (PyTorch) for approaching financial tasks. By the end of this book, you’ll have learned how to effectively analyze financial data using a recipe-based approach.
Table of Contents (12 chapters)

What this book covers

Chapter 1, Financial Data and Preprocessing, explores how financial data is different from other types of data commonly used in machine learning tasks. You will be able to use the functions provided to download financial data from a number of sources (such as Yahoo Finance and Quandl) and preprocess it for further analysis. Finally, you will learn how to investigate whether the data follows the stylized facts of asset returns.

Chapter 2, Technical Analysis in Python, demonstrates some fundamental basics of technical analysis as well as how to quickly create elegant dashboards in Python. You will be able to draw some insights into patterns emerging from a selection of the most commonly used metrics (such as MACD and RSI).

Chapter 3, Time Series Modeling, introduces the basics of time series modeling (including time series decomposition and statistical stationarity). Then, we look at two of the most widely used approaches of time series modeling—exponential smoothing methods and ARIMA class models. Lastly, we present a novel approach to modeling a time series using the additive model from Facebook's Prophet library.

Chapter 4, Multi-Factor Models, shows you how to estimate various factor models in Python. We start with the simplest one-factor model and then explain how to estimate more advanced three-, four-, and five-factor models.

Chapter 5, Modeling Volatility with GARCH Class Models, introduces you to the concept of volatility forecasting using (G)ARCH class models, how to choose the best-fitting model, and how to interpret your results.

Chapter 6, Monte Carlo Simulations in Finance, introduces you to the concept of Monte Carlo simulations and how to use them for simulating stock prices, the valuation of European/American options, and for calculating the VaR.

Chapter 7, Asset Allocation in Python, introduces the concept of Modern Portfolio Theory and shows you how to obtain the Efficient Frontier in Python. Then, we look at how to identify specific portfolios, such as minimum variance or the maximum Sharpe ratio. We also show you how to evaluate the performance of such portfolios.

Chapter 8, Identifying Credit Default with Machine Learning, presents a case of using machine learning for predicting credit default. You will get to know the state-of-the-art classification algorithms, learn how to tune the hyperparameters of the models, and handle problems with imbalanced data.

Chapter 9, Advanced Machine Learning Models in Finance, introduces you to a selection of advanced classifiers (including stacking multiple models). Additionally, we look at how to deal with class imbalance, use Bayesian optimization for hyperparameter tuning, and retrieve feature importance from a model.

Chapter 10, Deep Learning in Finance, demonstrates how to use deep learning techniques for working with time series and tabular data. The networks will be trained using PyTorch (with possible GPU acceleration).