Explore 1.5M+ audiobooks & ebooks free for days

From $11.99/month after trial. Cancel anytime.

Mastering Python for Finance - Second Edition: Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition
Mastering Python for Finance - Second Edition: Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition
Mastering Python for Finance - Second Edition: Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition
Ebook792 pages5 hours

Mastering Python for Finance - Second Edition: Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

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.

LanguageEnglish
PublisherPackt Publishing
Release dateApr 30, 2019
ISBN9781789345278
Mastering Python for Finance - Second Edition: Implement advanced state-of-the-art financial statistical applications using Python, 2nd Edition

Related to Mastering Python for Finance - Second Edition

Related ebooks

Computers For You

View More

Reviews for Mastering Python for Finance - Second Edition

Rating: 4 out of 5 stars
4/5

3 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Mastering Python for Finance - Second Edition - James Ma Weiming

    Mastering Python for Finance.

    Mastering Python for Finance

    Second Edition

    Implement advanced state-of-the-art financial statistical applications using Python

    James Ma Weiming

    BIRMINGHAM - MUMBAI

    Mastering Python for Finance Second Edition

    Copyright © 2019 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    Commissioning Editor: Sunith Shetty

    Acquisition Editor: Devika Battike

    Content Development Editor: Snehal Kolte

    Technical Editor: Manikandan Kurup

    Copy Editor: Safis Editing

    Project Coordinator: Manthan Patel

    Proofreader: Safis Editing

    Indexer: Tejal Daruwale Soni

    Graphics: Jisha Chirayil

    Production Coordinator: Arvindkumar Gupta

    First published: April 2015

    Second edition: April 2019

    Production reference: 1300419

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham

    B3 2PB, UK.

    ISBN 978-1-78934-646-6

    www.packtpub.com

    To the team at Skyline Trading on the CBOT trading floor - Mr. Milt Robinson, Brian Hickman, and Frank.

    To my family, friends, and colleagues.

    And, of course, to Paik Yen.

    Contributors

    About the author

    James Ma Weiming is a software engineer based in Singapore. His studies and research are focused on financial technology, machine learning, data sciences, and computational finance. James started his career in financial services working with treasury fixed income and foreign exchange products, and fund distribution. His interests in derivatives led him to Chicago, where he worked with veteran traders of the Chicago Board of Trade to devise high-frequency, low-latency strategies to game the market. He holds an MS degree in finance from Illinois Tech's Stuart School of Business in the United States and a bachelor's degree in computer engineering from Nanyang Technological University.

    About the reviewers

    Anil Omanwar has over 11 years' experience in researching cognitive computing, while natural language processing (NLP), machine learning, information visualization, and text analytics are a few key areas of his research interests. He is proficient in sentiment analysis, questionnaire-based feedback, text clustering, and phrase extraction in diverse domains such as banking, oil and gas, life sciences, manufacturing, retail, social media, and others. He is currently associated with IBM Australia for NLP and IBM Watson in the oil and gas domain. The objective of his research is to automate critical manual decisions and assist domain experts to optimize human-machine capabilities. He holds multiple patents on emerging technologies, including NLP automation and device intelligence.

    Rahul Shendge has a bachelor's degree in computer engineering from University of Pune and is certified in multiple technologies. He is an open source enthusiast and works as a senior software engineer. He has worked in multiple domains, including finance, healthcare, and education. He has hands-on experience of cloud, designing, and trading algorithms with machine learning. He is constantly exploring technical novelties and he is open-minded and eager to learn about new technologies. He is passionate about helping clients to make valuable business decisions using analytics in respective areas. His interests include working with and exploring data analytics solutions.

    Packt is searching for authors like you

    If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

    mapt.io

    Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

    Why subscribe?

    Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

    Improve your learning with Skill Plans built especially for you

    Get a free eBook or video every month

    Mapt is fully searchable

    Copy and paste, print, and bookmark content

    Packt.com

    Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

    At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

    Table of Contents

    Title Page

    Copyright and Credits

    Mastering Python for Finance Second Edition

    Dedication

    Contributors

    About the author

    About the reviewers

    Packt is searching for authors like you

    About Packt

    Why subscribe?

    Packt.com

    Preface

    Who this book is for

    What this book covers

    To get the most out of this book

    Download the example code files

    Download the color images

    Conventions used

    Get in touch

    Reviews

    Section 1: Getting Started with Python

    Overview of Financial Analysis with Python

    Getting Python

    Preparing a virtual environment

    Running Jupyter Notebook

    The Python Enhancement Proposal

    What is a PEP?

    The Zen of Python

    Introduction to Quandl

    Setting up Quandl for your environment

    Plotting a time series chart

    Retrieving datasets from Quandl

    Plotting a price and volume chart

    Plotting a candlestick chart

    Performing financial analytics on time series data

    Plotting returns

    Plotting cumulative returns

    Plotting a histogram

    Plotting volatility

    A quantile-quantile plot

    Downloading multiple time series data

    Displaying the correlation matrix

    Plotting correlations

    Simple moving averages

    Exponential moving averages

    Summary

    Section 2: Financial Concepts

    The Importance of Linearity in Finance

    The Capital Asset Pricing Model and the security market line

    The Arbitrage Pricing Theory model

    Multivariate linear regression of factor models

    Linear optimization

    Getting Pulp

    A maximization example with linear programming

    Outcomes of linear programs

    Integer programming

    A minimization example with integer programming

    Integer programming with binary conditions

    Solving linear equations using matrices

    The LU decomposition

    The Cholesky decomposition

    The QR decomposition

    Solving with other matrix algebra methods

    The Jacobi method

    The Gauss-Seidel method

    Summary

    Nonlinearity in Finance

    Nonlinearity modeling

    Examples of nonlinear models

    The implied volatility model

    The Markov regime-switching model

    The threshold autoregressive model

    Smooth transition models

    Root-finding algorithms

    Incremental search

    The bisection method

    Newton's method

    The secant method

    Combing root-finding methods

    SciPy implementations in root-finding

    Root-finding scalar functions

    General nonlinear solvers

    Summary

    Numerical Methods for Pricing Options

    Introduction to options

    Binomial trees in option pricing

    Pricing European options

    Writing the StockOption base class

    A class for European options using a binomial tree

    A class for American options using a binomial tree

    The Cox–Ross–Rubinstein model

    A class for the CRR binomial tree option pricing model

    Using a Leisen-Reimer tree

    A class for the LR binomial tree option pricing model

    The Greeks for free

    A class for Greeks with the LR binomial tree

    Trinomial trees in option pricing

    A class for the trinomial tree option pricing model

    Lattices in option pricing

    Using a binomial lattice

    A class for the CRR binomial lattice option pricing model

    Using the trinomial lattice

    A class for the trinomial lattice option pricing model

    Finite differences in option pricing

    The explicit method

    Writing the finite difference base class

    A class for pricing European options using the explicit method of finite differences

    The implicit method

    A class for pricing European options using the implicit method of finite differences

    The Crank-Nicolson method

    A class for pricing European options using the Crank-Nicolson method of finite differences

    Pricing exotic barrier options

    A down-and-out option

    A class for pricing down-and-out-options using the Crank-Nicolson method of finite differences

    Pricing American options with finite differences

    A class for pricing American options using the Crank-Nicolson method of finite differences

    Putting it all together – implied volatility modeling

    Implied volatilities of the AAPL American put option

    Summary

    Modeling Interest Rates and Derivatives

    Fixed-income securities

    Yield curves

    Valuing a zero-coupon bond

    Spot and zero rates

    Bootstrapping a yield curve

    An example of bootstrapping the yield curve

    Writing the yield curve bootstrapping class

    Forward rates

    Calculating the yield to maturity

    Calculating the price of a bond

    Bond duration

    Bond convexity

    Short–rate modeling

    The Vasicek model

    The Cox-Ingersoll-Ross model

    The Rendleman and Bartter model

    The Brennan and Schwartz model

    Bond options

    Callable bonds

    Puttable bonds

    Convertible bonds

    Preferred stocks

    Pricing a callable bond option

    Pricing a zero-coupon bond by the Vasicek model

    The value of early exercise

    Policy iteration by finite differences

    Other considerations in callable bond pricing

    Summary

    Statistical Analysis of Time Series Data

    The Dow Jones industrial average and its 30 components

    Downloading Dow component datasets from Quandl

    About Alpha Vantage

    Obtaining an Alpha Vantage API key

    Installing the Alpha Vantage Python wrapper

    Downloading the DJIA dataset from Alpha Vantage

    Applying a kernel PCA

    Finding eigenvectors and eigenvalues

    Reconstructing the Dow index with PCA

    Stationary and non-stationary time series

    Stationarity and non-stationarity

    Checking for stationarity

    Types of non-stationary processes

    Types of stationary processes

    The Augmented Dickey-Fuller Test

    Analyzing a time series with trends

    Making a time series stationary

    Detrending

    Removing trend by differencing

    Seasonal decomposing

    Drawbacks of ADF testing

    Forecasting and predicting a time series

    About the Autoregressive Integrated Moving Average 

    Finding model parameters by grid search

    Fitting the SARIMAX model

    Predicting and forecasting the SARIMAX model

    Summary

    Section 3: A Hands-On Approach

    Interactive Financial Analytics with the VIX

    Volatility derivatives

    STOXX and the Eurex

    The EURO STOXX 50 Index

    The VSTOXX

    The S&P 500 Index

    SPX options

    The VIX

    Financial analytics of the S&P 500 and the VIX

    Gathering the data

    Performing analytics

    The correlation between the SPX and the VIX

    Calculating the VIX Index

    Importing SPX options data

    Parsing SPX options data

    Finding near-term and next-term options

    Calculating the required minutes

    Calculating the forward SPX Index level

    Finding the required forward strike prices

    Determining strike price boundaries

    Tabulating contributions by strike prices

    Calculating the volatilities

    Calculating the next-term options

    Calculating the VIX Index

    Calculating multiple VIX indexes

    Comparing the results

    Summary

    Building an Algorithmic Trading Platform

    Introducing algorithmic trading

    Trading platforms with a public API

    Choosing a programming language

    System functionalities

    Building an algorithmic trading platform

    Designing a broker interface

    Python library requirements

    Installing v20

    Writing an event-driven broker class

    Storing the price event handler

    Storing the order event handler

    Storing the position event handler

    Declaring an abstract method for getting prices

    Declaring an abstract method for streaming prices

    Declaring an abstract method for sending orders

    Implementing the broker class

    Initializing the broker class

    Implementing the method for getting prices

    Implementing the method for streaming prices

    Implementing the method for sending market orders

    Implementing the method for fetching positions

    Getting the prices

    Sending a simple market order

    Getting position updates

    Building a mean-reverting algorithmic trading system

    Designing the mean-reversion algorithm

    Implementing the mean-reversion trader class

    Adding event listeners

    Writing the mean-reversion signal generators

    Running our trading system

    Building a trend-following trading platform

    Designing the trend-following algorithm

    Writing the trend-following trader class

    Writing the trend-following signal generators

    Running the trend-following trading system

    VaR for risk management

    Summary

    Implementing a Backtesting System

    Introducing backtesting

    Concerns in backtesting

    Concept of an event-driven backtesting system

    Designing and implementing a backtesting system

    Writing a class to store tick data

    Writing a class to store market data

    Writing a class to generate sources of market data

    Writing the order class

    Writing a class to keep track of positions

    Writing an abstract strategy class

    Writing a mean-reverting strategy class

    Binding our modules with a backtesting engine

    Running our backtesting engine

    Multiple runs of the backtest engine

    Improving your backtesting system

    Ten considerations for a backtesting model

    Resources restricting your model

    Criteria of evaluation of the model

    Estimating the quality of backtest parameters

    Be prepared to face model risk

    Performance of a backtest with in–sample data

    Addressing common pitfalls in backtesting

    Have a common-sense idea of your model

    Understanding the context for the model

    Make sure you have the right data

    Data mine your results

    Discussion of algorithms in backtesting

    K-means clustering

    K-nearest neighbors machine learning algorithm

    Classification and regression tree analysis

    The 2k factorial design

    The genetic algorithm

    Summary

    Machine Learning for Finance

    Introduction to machine learning

    Uses of machine learning in finance

    Algorithmic trading

    Portfolio management

    Supervisory and regulatory functions

    Insurance and loan underwriting

    News sentiment analysis

    Machine learning beyond finance

    Supervised and unsupervised learning

    Supervised learning

    Unsupervised learning

    Classification and regression in supervised machine learning

    Overfitting and underfitting models

    Feature engineering

    Scikit-learn for machine learning

    Predicting prices with a single-asset regression model

    Linear regression by OLS

    Preparing the independent and target variables

    Writing the linear regression model

    Risk metrics for measuring prediction performance

    Mean absolute error as a risk metric

    Mean squared error as a risk metric

    Explained variance score as a risk metric

    R2 as a risk metric

    Ridge regression

    Other regression models

    Lasso regression

    Elastic net

    Conclusion

    Predicting returns with a cross-asset momentum model

    Preparing the independent variables

    Preparing the target variables

    A multi-asset linear regression model

    An ensemble of decision trees

    Bagging regressor

    Gradient tree boosting regression model

    Random forests regression

    More ensemble models

    Predicting trends with classification-based machine learning

    Preparing the target variables

    Preparing the dataset of multiple assets as input variables

    Logistic regression

    Risk metrics for measuring classification-based predictions

    Confusion matrix

    Accuracy score

    Precision score

    Recall score

    F1 score

    Support vector classifier

    Other types of classifiers

    Stochastic gradient descent

    Linear discriminant analysis

    Quadratic discriminant analysis

    KNN classifier

    Conclusion on the use of machine learning algorithms

    Summary

    Deep Learning for Finance

    A brief introduction to deep learning

    What is deep learning ?

    The artificial neuron

    Activation function

    Loss functions

    Optimizers

    Network architecture

    TensorFlow and other deep learning frameworks

    What is a tensor ?

    A deep learning price prediction model with TensorFlow

    Feature engineering our model

    Requirements

    Intrinio as our data provider

    Compatible Python environment for TensorFlow

    The requests library

    The TensorFlow library

    Downloading the dataset

    Scaling and splitting the data

    Building an artificial neural network with TensorFlow

    Phase 1 – assembling the graph

    Phase 2 – training our model

    Plotting predicted and actual values

    Credit card payment default prediction with Keras

    Introduction to Keras

    Installing Keras

    Obtaining the dataset

    Splitting and scaling the data

    Designing a deep neural network with five hidden layers using Keras

    Measuring the performance of our model

    Running risk metrics

    Displaying recorded events in Keras history

    Summary

    Other Books You May Enjoy

    Leave a review - let other readers know what you think

    Preface

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

    You will start by setting up a Jupyter notebook to implement the tasks throughout the book. You will learn how to make efficient and powerful data-driven financial decisions using popular libraries such as TensorFlow, Keras, NumPy, SciPy, scikit-learn, and so on. 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 how to apply statistical analysis on time series data and understand how to harness high-frequency data to devise trading strategies in building an algorithmic trading platform. You will learn to validate your trading strategies by implementing an event-driven backtesting system and measure its performance. Finally, you will explore machine learning and deep learning techniques that are applied in finance.

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

    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, 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 using smart machine learning techniques. 

    What this book covers

    Chapter 1, Overview of Financial Analysis with Python, goes briefly through setting up a Python environment, including a Jupyter Notebook, so that you can proceed with the rest of the chapters in this book. Within Jupyter, we will perform some time series analysis with pandas, using plots for analysis.

    Chapter 2, The Importance of Linearity in Finance, uses Python to solve systems of linear equations, perform integer programming, and apply matrix algebra to the linear optimization of portfolio allocation.

    Chapter 3,  Nonlinearity in Finance, explores some methods that will help us extract information from nonlinear models. You will learn root-finding methods in nonlinear volatility modeling. The optimize module of SciPy contains the root and fsolve functions, which can also help us to perform root finding on non-linear models.

    Chapter 4, Numerical Methods for Pricing Options, explores trees, lattices, and finite differencing schemes for the valuation of options.

    Chapter 5, Modeling Interest Rates and Derivatives, discusses the bootstrapping process of the yield curve and covers some short-rate models for pricing interest rate derivatives with Python.

    Chapter 6, Statistical Analysis of Time Series Data, introduces principal component analysis for identifying principal components. The Dicker-Fuller test is used for testing whether a time series is stationary.

    Chapter 7, Interactive Financial Analytics with VIX, discusses volatility indexes. We will perform analytics on a US stock index and VIX data, and replicate the main index using the options prices of the sub-indexes.

    Chapter 8, Building an Algorithmic Trading Platform, takes a step-by-step approach to developing a mean-reverting and trend-following live trading infrastructure using a broker API.

    Chapter 9, Implementing a Backtesting System, discusses how to design and implement an event-driven backtesting system and helps you to visualize the performance of our simulated trading strategy.

    Chapter 10, Machine Learning for Finance, introduces us to machine learning, allowing us to study its concepts and applications in finance. We will also look at some practical examples for applying machine learning to assist in trading decisions.

    Chapter 11, Deep Learning for Finance, encourages us to take a hands-on approach to learning TensorFlow and Keras by building deep learning prediction models using neural networks.

    To get the most out of this book

    Prior experience in Python is required.

    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.packt.com/support and register to have the files emailed directly to you.

    You can download the code files by following these steps:

    Log in or register at www.packt.com.

    Select the SUPPORT tab.

    Click on Code Downloads & Errata.

    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/Mastering-Python-for-Finance-Second-Edition. 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

    We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781789346466_ColorImages.pdf.

    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: By default, pandas' .plot() command uses the matplotlib library to display graphs.

    A block of code is set as follows:

    In [ ]:

        %matplotlib inline

        import quandl

        quandl.ApiConfig.api_key = QUANDL_API_KEY

        df = quandl.get('EURONEXT/ABN.4')

        daily_changes = df.pct_change(periods=1)

        daily_changes.plot();

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    2015-02-26 TICK WIKI/AAPL open: 128.785 close: 130.415

    2015-02-26 FILLED BUY 1 WIKI/AAPL at 128.785

    2015-02-26 POSITION value:-128.785 upnl:1.630 rpnl:0.000

    2015-02-27 TICK WIKI/AAPL open: 130.0 close: 128.46

    Any command-line input or output is written as follows:

    $ cd my_project_folder

    $ virtualenv my_env

    Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "To start your first notebook, select New, then Python 3."

    Warnings or important notes appear like this.

    Tips and tricks appear like this.

    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 [email protected].

    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.packt.com/submit-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 [email protected] 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.

    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 working directory. If this is the first time you are working in the directory, the list will be empty.

    To start your first notebook, select New, then Python 3. A new Jupyter Notebook will open in a new window. Henceforth, most computations in this book will be performed in Jupyter.

    The Python Enhancement Proposal

    Any design considerations in the Python programming language are documented as a Python Enhancement Proposal (PEP). Hundreds of PEPs have been written down, but probably the one that you should be familiar with is PEP 8, a style guide for Python developers to write better, readable code. The official repository for PEPs is https://github.com/python/peps.

    What is a PEP?

    PEPs are a numbered collection of design documents describing a feature, process, or environment related to Python. Each PEP is carefully maintained in a text file, containing technical specifications of a particular feature and its rationale for its existence. For example, PEP 0 serves as the index of all PEPs, while PEP 1 provides the purpose and guidelines of PEPs. As software developers, we often read code more than we write code. To create clear, concise, and readable code, we should always use a style guide as a coding convention. PEP 8 is a set of style guidelines for writing presentable Python code. You can read more about PEP 8 at https://www.python.org/dev/peps/pep-0008/.

    The Zen of Python

    PEP 20 embodies the Zen

    Enjoying the preview?
    Page 1 of 1