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)

To get the most out of this book

For this book, we assume that you have the following:

  • A good understanding of programming in Python and machine/deep learning models
  • Knowledge of how to use popular libraries, such as NumPy, pandas, and matplotlib
  • Knowledge of basic statistics and quantitative finance

In this book, we attempt to give you a high-level overview of various techniques; however, we will focus on the practical applications of these methods. For a deeper dive into the theoretical foundations, we provide references for further reading.

The best way to learn anything is by doing. That is why we highly encourage you to experiment with the code samples provided (the code can be found in the accompanying GitHub repository), apply the techniques to different datasets, and explore possible extensions.

The code for this book was successfully run on a MacBook; however, it should work on any operating system. Additionally, you can always use online services such as Google Colab.

At the very beginning of each notebook (available on the book's GitHub repository), we run
a few cells that import and set up plotting with matplotlib. We will not mention this later on, as this would be repetitive, so at any time, assume that matplotlib is imported.

In the first cell, we first set up the backend of matplotlib to inline:

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

By doing so, each plotted figure will appear below the cell that generated it and the plot will also be visible in the Notebook should it be exported to another format (such as PDF or
HTML). The second line is used for MacBooks and displays the plot in higher resolution for
Retina displays.
The second cell appears as follows:

import matplotlib.pyplot as plt
import warnings'seaborn')
plt.rcParams['figure.figsize'] = [16, 9]
plt.rcParams['figure.dpi'] = 300
warnings.simplefilter(action='ignore', category=FutureWarning)

In this cell, we import matplotlib and warnings, set up the style of the plots to
'seaborn' (this is a personal preference), as well as default plot settings, such as figure
size and resolution. We also disable (ignore) some warnings. In some chapters, we might
modify these settings for better readability of the figures (especially in black and white).

Download the example code files

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Finally, we took the natural logarithm of the divided values by using np.log."

A block of code is set as follows:

df_yahoo ='AAPL', 

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "A single candlestick (typically corresponding to one day, but a higher frequency is possible) combines the open, high, low, and close prices (OHLC)."

Warnings or important notes appear like this.
Tips and tricks appear like this.