Mastering Python for Finance
eBook - ePub

Mastering Python for Finance

Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition

James Ma Weiming

Share book
  1. 426 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Python for Finance

Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition

James Ma Weiming

Book details
Book preview
Table of contents
Citations

About This Book

Take your financial skills to the next level by mastering cutting-edge mathematical and statistical financial applications

Key Features

  • Explore advanced financial models used by the industry and ways of solving them using Python
  • Build state-of-the-art infrastructure for modeling, visualization, trading, and more
  • Empower your financial applications by applying machine learning and deep learning

Book Description

The second edition of Mastering Python for Finance will guide you through carrying out complex financial calculations practiced in the industry of finance by using next-generation methodologies. You will master the Python ecosystem by leveraging publicly available tools to successfully perform research studies and modeling, and learn to manage risks with the help of advanced examples.

You will start by setting up your Jupyter notebook to implement the tasks throughout the book. You will learn to make efficient and powerful data-driven financial decisions using popular libraries such as TensorFlow, Keras, Numpy, SciPy, and sklearn. You will also learn how to build financial applications by mastering concepts such as stocks, options, interest rates and their derivatives, and risk analytics using computational methods. With these foundations, you will learn to apply statistical analysis to time series data, and understand how time series data is useful for implementing an event-driven backtesting system and for working with high-frequency data in building an algorithmic trading platform. Finally, you will explore machine learning and deep learning techniques that are applied in finance.

By the end of this book, you will be able to apply Python to different paradigms in the financial industry and perform efficient data analysis.

What you will learn

  • Solve linear and nonlinear models representing various financial problems
  • Perform principal component analysis on the DOW index and its components
  • Analyze, predict, and forecast stationary and non-stationary time series processes
  • Create an event-driven backtesting tool and measure your strategies
  • Build a high-frequency algorithmic trading platform with Python
  • Replicate the CBOT VIX index with SPX options for studying VIX-based strategies
  • Perform regression-based and classification-based machine learning tasks for prediction
  • Use TensorFlow and Keras in deep learning neural network architecture

Who this book is for

If you are a financial or data analyst or a software developer in the financial industry who is interested in using advanced Python techniques for quantitative methods in finance, this is the book you need! You will also find this book useful if you want to extend the functionalities of your existing financial applications by using smart machine learning techniques. Prior experience in Python is required.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Python for Finance an online PDF/ePUB?
Yes, you can access Mastering Python for Finance by James Ma Weiming in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789345278
Edition
2

Section 1: Getting Started with Python

This section will help us to set up Python on our machine in preparation for running code examples in this book.
This section will contain only one chapter:
  • Chapter 1, Overview of Financial Analysis with Python

Overview of Financial Analysis with Python

Since the publication of my previous book Mastering Python for Finance, there have been significant upgrades to Python itself and many third-party libraries. Many tools and features have been deprecated in favor of new ones. This chapter walks you through how to get the latest tools available and how to prepare the environment that will be used throughout the rest of the book.
We will be using Quandl for the majority of datasets covered in this book. Quandl is a platform that serves financial, economic, and alternative data. These sources of data are contributed by various data publishers, including the United Nations, World Bank, central banks, trading exchanges, investment research firms, and even members of the Quandl community. With the Python Quandl module, you can easily download datasets and perform financial analytics to derive useful insights.
We will explore time series data manipulation using the pandas module. The two primary data structures in pandas are the Series object and the DataFrame object. Together, they can be used to plot charts and visualize complex information. Common methods of financial time series computation and analysis will be covered in this chapter.
The intention of this chapter is to serve as a foundation for setting up your working environment with libraries that will be used throughout this book. Over the years, like any software packages, the pandas module has evolved drastically with many breaking changes. Codes written years ago interfacing with older version of pandas will no longer work as many methods have been deprecated. The version of pandas used in this book is 0.23. Code written in this book conforms to this version of pandas.
In this chapter, we will cover the following:
  • Setting up Python, Jupyter, Quandl, and other libraries for your environment
  • Downloading datasets from Quandl and plotting your first chart
  • Plotting last prices, volumes, and candlestick charts
  • Calculating and plotting daily percentage and cumulative returns
  • Plotting volatility, histograms, and Q-Q plots
  • Visualizing correlations and generating the correlation matrix
  • Visualizing simple moving averages and exponential moving averages

Getting Python

At the time of writing, the latest Python version is 3.7.0. You may download the latest version for Windows, macOS X, Linux/UNIX, and other operating systems from the official Python website at https://www.python.org/downloads/. Follow the installation instructions to install the base Python interpreter on your operating system.
The installation process should add Python to your environment path. To check the version of your installed Python, type the following command into the terminal if you are using macOS X/Linux, or the command prompt on Windows:
$ python --version
Python 3.7.0
For easy installation of Python libraries, consider using an all-in-one Python distribution such as Anaconda (https://www.anaconda.com/download/), Miniconda (https://conda.io/miniconda.html), or Enthought Canopy (https://www.enthought.com/product/enthought-python-distribution/). Advanced users, however, may prefer to control which libraries get installed with their base Python interpreter.

Preparing a virtual environment

At this point, it is advisable to set up a Python virtual environment. Virtual environments allow you to manage separate package installations that you need for a particular project, isolating the packages installed in other environments.
To install the virtual environment package in your terminal window, type the following:
$ pip install virtualenv
On some systems, Python 3 may use a different pip executable and may need to be installed via an alternate pip command; for example: $ pip3 install virtualenv.
To create a virtual environment, go to your project's directory and run virtualenv. For example, if the name of your project folder is my_project_folder, type the following:
$ cd my_project_folder
$ virtualenv my_venv
virtualenv my_venv will create a folder in the current working directory that includes Python executable files of your base Python interpreter installed earlier, and a copy of the pip library, which you can use to install other packages.
Before using the new virtual environment, it needs to be activated. In a macOS X or Linux terminal, type the following command:
$ source my_venv/bin/activate
On Windows, the activation command is as follows:
$ my_project_folder\my_venv\Scripts\activate
The name of the current virtual environment will now appear on the left of the prompt (for example, (my_venv) current_folder$) to let you know that the selected Python environment is activated. Package installations from the same terminal window will be placed in the my_venv folder, isolated from the global Python interpreter.
Virtual environments can help prevent conflicts should you have multiple applications using the same module but from different versions. This step (creating a virtual environment) is entirely optional as you can still use your default base interpreter to install packages.

Running Jupyter Notebook

Jupyter Notebook is a browser-based interactive computational environment for creating, executing, and visualizing interactive data across various programming languages. It was formerly known as IPython Notebook. IPython continues to exist as a Python shell and a kernel for Jupyter. Jupyter is an open-source software, free for all to use and learn about a variety of topics, from basic programming to advanced statistics or quantum mechanics.
To install Jupyter, type the following command in your terminal window:
$ pip install jupyter
Once installed, start Jupyter with the following command:
$ jupyter notebook 
...
Copy/paste this URL into your browser when you connect for the first time, to login with a token:
http://127.0.0.1:8888/?token=27a16ee4d6042a53f6e31161449efcf7e71418f23e17549d
Watch your terminal window. When Jupyter has started, the console will provide information about this running status. You should also see a URL. Copy that URL into a web browser to bring you to the Jupyter computing interface.
Since Jupyter starts in the directory where you have issued the preceding command, Jupyter will list all saved notebooks in the work...

Table of contents