Reader small image

You're reading from  Python for Finance Cookbook

Product typeBook
Published inJan 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781789618518
Edition1st Edition
Languages
Right arrow
Author (1)
Eryk Lewinson
Eryk Lewinson
author image
Eryk Lewinson

Eryk Lewinson received his master's degree in Quantitative Finance from Erasmus University Rotterdam. In his professional career, he has gained experience in the practical application of data science methods while working in risk management and data science departments of two "big 4" companies, a Dutch neo-broker and most recently the Netherlands' largest online retailer. Outside of work, he has written over a hundred articles about topics related to data science, which have been viewed more than 3 million times. In his free time, he enjoys playing video games, reading books, and traveling with his girlfriend.
Read more about Eryk Lewinson

Right arrow

Preface

This book begins by exploring various ways of downloading financial data and preparing it for modeling. We check the basic statistical properties of asset prices and returns, and investigate the existence of so-called stylized facts. We then calculate popular indicators used in technical analysis (such as Bollinger Bands, Moving Average Convergence Divergence (MACD), and Relative Strength Index (RSI)) and backtest automatic trading strategies built on their basis.

The next section introduces time series analysis and explores popular models such as exponential smoothing, AutoRegressive Integrated Moving Average (ARIMA), and Generalized Autoregressive Conditional Heteroskedasticity (GARCH) (including multivariate specifications). We also introduce you to factor models, including the famous Capital Asset Pricing Model (CAPM) and the Fama-French three-factor model. We end this section by demonstrating different ways to optimize asset allocation, and we use Monte Carlo simulations for tasks such as calculating the price of American options or estimating the Value at Risk (VaR).

In the last part of the book, we carry out an entire data science project in the financial domain. We approach credit card fraud/default problems using advanced classifiers such as random forest, XGBoost, LightGBM, stacked models, and many more. We also tune the hyperparameters of the models (including Bayesian optimization) and handle class imbalance. We conclude the book by demonstrating how deep learning (using PyTorch) can solve numerous financial problems.

Who this book is for

This book is for financial analysts, data analysts/scientists, and Python developers who want to learn how to implement a broad range of tasks in the financial domain. This book should also be helpful to data scientists who want to devise intelligent financial strategies in order to perform efficient financial analytics. Working knowledge of the Python programming language is mandatory.

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).

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

plt.style.use('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 www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support 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 www.packt.com.
  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 https://github.com/PacktPublishing/Book-Name. 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 https://github.com/PacktPublishing/. Check them out!

Download the color images

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 = yf.download('AAPL', 
start='2000-01-01',
end='2010-12-31',
progress=False)

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.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python for Finance Cookbook
Published in: Jan 2020Publisher: PacktISBN-13: 9781789618518
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Eryk Lewinson

Eryk Lewinson received his master's degree in Quantitative Finance from Erasmus University Rotterdam. In his professional career, he has gained experience in the practical application of data science methods while working in risk management and data science departments of two "big 4" companies, a Dutch neo-broker and most recently the Netherlands' largest online retailer. Outside of work, he has written over a hundred articles about topics related to data science, which have been viewed more than 3 million times. In his free time, he enjoys playing video games, reading books, and traveling with his girlfriend.
Read more about Eryk Lewinson