Hands-On Financial Trading with Python

Book description

Build and backtest your algorithmic trading strategies to gain a true advantage in the market

Key Features

  • Get quality insights from market data, stock analysis, and create your own data visualisations
  • Learn how to navigate the different features in Python's data analysis libraries
  • Start systematically approaching quantitative research and strategy generation/backtesting in algorithmic trading

Book Description

Creating an effective system to automate your trading can help you achieve two of every trader's key goals; saving time and making money. But to devise a system that will work for you, you need guidance to show you the ropes around building a system and monitoring its performance. This is where Hands-on Financial Trading with Python can give you the advantage.

This practical Python book will introduce you to Python and tell you exactly why it's the best platform for developing trading strategies. You'll then cover quantitative analysis using Python, and learn how to build algorithmic trading strategies with Zipline using various market data sources.

Using Zipline as the backtesting library allows access to complimentary US historical daily market data until 2018. As you advance, you will gain an in-depth understanding of Python libraries such as NumPy and pandas for analyzing financial datasets, and explore Matplotlib, statsmodels, and scikit-learn libraries for advanced analytics.

As you progress, you'll pick up lots of skills like time series forecasting, covering pmdarima and Facebook Prophet.

By the end of this trading book, you will be able to build predictive trading signals, adopt basic and advanced algorithmic trading strategies, and perform portfolio optimization to help you get —and stay—ahead of the markets.

What you will learn

  • Discover how quantitative analysis works by covering financial statistics and ARIMA
  • Use core Python libraries to perform quantitative research and strategy development using real datasets
  • Understand how to access financial and economic data in Python
  • Implement effective data visualization with Matplotlib
  • Apply scientific computing and data visualization with popular Python libraries
  • Build and deploy backtesting algorithmic trading strategies

Who this book is for

If you're a financial trader or a data analyst who wants a hands-on introduction to designing algorithmic trading strategies, then this book is for you. You don't have to be a fully-fledged programmer to dive into this book, but knowing how to use Python's core libraries and a solid grasp on statistics will help you get the most out of this book.

Table of contents

  1. Hands-On Financial Trading with Python
  2. Contributors
  3. About the authors
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  6. Section 1: Introduction to Algorithmic Trading
  7. Chapter 1: Introduction to Algorithmic Trading
    1. Walking through the evolution of algorithmic trading
    2. Understanding financial asset classes
    3. Going through the modern electronic trading exchange
      1. Order types
      2. Limit order books
      3. The exchange matching engine
    4. Understanding the components of an algorithmic trading system
      1. The core infrastructure of an algorithmic trading system
      2. The quantitative infrastructure of an algorithmic trading system
    5. Summary
  8. Section 2: In-Depth Look at Python Libraries for the Analysis of Financial Datasets
  9. Chapter 2: Exploratory Data Analysis in Python
    1. Technical requirements
    2. Introduction to EDA
      1. Steps in EDA
      2. Revelation of the identity of A, B, and C and EDA's conclusions
    3. Special Python libraries for EDA
    4. Summary
  10. Chapter 3: High-Speed Scientific Computing Using NumPy
    1. Technical requirements
    2. Introduction to NumPy
    3. Creating NumPy ndarrays
      1. Creating 1D ndarrays
      2. Creating 2D ndarrays
      3. Creating any-dimension ndarrays
      4. Creating an ndarray with np.zeros(...)
      5. Creating an ndarray with np.ones(...)
      6. Creating an ndarray with np.identity(...)
      7. Creating an ndarray with np.arange(...)
      8. Creating an ndarray with np.random.randn(…)
    4. Data types used with NumPy ndarrays
      1. Creating a numpy.float64 array
      2. Creating a numpy.bool array
      3. ndarrays' dtype attribute
      4. Converting underlying data types of ndarray with numpy.ndarrays.astype(...)
    5. Indexing of ndarrays
      1. Direct access to an ndarray's element
      2. ndarray slicing
      3. Boolean indexing
      4. Indexing with arrays
    6. Basic ndarray operations
      1. Scalar multiplication with an ndarray
      2. Linear combinations of ndarrays
      3. Exponentiation of ndarrays
      4. Addition of an ndarray with a scalar
      5. Transposing a matrix
      6. Changing the layout of an ndarray
      7. Finding the minimum value in an ndarray
      8. Calculating the absolute value
      9. Calculating the mean of an ndarray
      10. Finding the index of the maximum value in an ndarray
      11. Calculating the cumulative sum of elements of an ndarray
      12. Finding NaNs in an ndarray
      13. Finding the truth values of x1>x2 of two ndarrays
      14. any and all Boolean operations on ndarrays
      15. Sorting ndarrays
      16. Searching within ndarrays
    7. File operations on ndarrays
      1. File operations with text files
      2. File operations with binary files
    8. Summary
  11. Chapter 4: Data Manipulation and Analysis with pandas
    1. Technical requirements
    2. Introducing pandas Series, pandas DataFrames, and pandas Indexes
      1. pandas.Series
      2. pandas.DataFrame
      3. pandas.Index
    3. Learning essential pandas.DataFrame operations
      1. Indexing, selection, and filtering of DataFrames
      2. Dropping rows and columns from a DataFrame
      3. Sorting values and ranking the values' order within a DataFrame
      4. Arithmetic operations on DataFrames
      5. Merging and combining multiple DataFrames into a single DataFrame
      6. Hierarchical indexing
      7. Grouping operations in DataFrames
      8. Transforming values in DataFrames' axis indices
      9. Handling missing data in DataFrames
      10. The transformation of DataFrames with functions and mappings
      11. Discretization/bucketing of DataFrame values
      12. Permuting and sampling DataFrame values to generate new DataFrames
    4. Exploring file operations with pandas.DataFrames
      1. CSV files
      2. JSON files
    5. Summary
  12. Chapter 5: Data Visualization Using Matplotlib
    1. Technical requirements
    2. Creating figures and subplots
      1. Defining figures' subplots
      2. Plotting in subplots
    3. Enriching plots with colors, markers, and line styles
    4. Enriching axes with ticks, labels, and legends
    5. Enriching data points with annotations
    6. Saving plots to files
    7. Charting a pandas DataFrame with Matplotlib
      1. Creating line plots of a DataFrame column
      2. Creating bar plots of a DataFrame column
      3. Creating histogram and density plots of a DataFrame column
      4. Creating scatter plots of two DataFrame columns
      5. Plotting time series data
    8. Summary
  13. Chapter 6: Statistical Estimation, Inference, and Prediction
    1. Technical requirements
    2. Introduction to statsmodels
      1. Normal distribution test with Q-Q plots
      2. Time series modeling with statsmodels
      3. ETS analysis of a time series
      4. Augmented Dickey-Fuller test for stationarity of a time series
      5. Autocorrelation and partial autocorrelation of a time series
      6. ARIMA time series model
    3. Using a SARIMAX time series model with pmdarima
    4. Time series forecasting with Facebook's Prophet library
    5. Introduction to scikit-learn regression and classification
      1. Generating the dataset
      2. Running RidgeCV regression on the dataset
      3. Running a classification method on the dataset
    6. Summary
  14. Section 3: Algorithmic Trading in Python
  15. Chapter 7: Financial Market Data Access in Python
    1. Technical requirements
    2. Exploring the yahoofinancials Python library
      1. Single-ticker retrieval
      2. Multiple-tickers retrieval
    3. Exploring the pandas_datareader Python library
      1. Access to Yahoo Finance
      2. Access to EconDB
      3. Access to the Federal Reserve Bank of St Louis' FRED
      4. Caching queries
    4. Exploring the Quandl data source
    5. Exploring the IEX Cloud data source
    6. Exploring the MarketStack data source
    7. Summary
  16. Chapter 8: Introduction to Zipline and PyFolio
    1. Technical requirements
    2. Introduction to Zipline and PyFolio
    3. Installing Zipline and PyFolio
      1. Installing Zipline
      2. Installing PyFolio
    4. Importing market data into a Zipline/PyFolio backtesting system
      1. Importing data from the historical Quandl bundle
      2. Importing data from the CSV files bundle
      3. Importing data from custom bundles
    5. Structuring Zipline/PyFolio backtesting modules
      1. Trading happens every day
      2. Trading happens on a custom schedule
    6. Reviewing the key Zipline API reference
      1. Types of orders
      2. Commission models
      3. Slippage models
    7. Running Zipline backtesting from the command line
    8. Introduction to risk management with PyFolio
      1. Market volatility, PnL variance, and PnL standard deviation
      2. Trade-level Sharpe ratio
      3. Maximum drawdown
    9. Summary
  17. Chapter 9: Fundamental Algorithmic Trading Strategies
    1. Technical requirements
    2. What is an algorithmic trading strategy?
    3. Learning momentum-based/trend-following strategies
      1. Rolling window mean strategy
      2. Simple moving averages strategy
      3. Exponentially weighted moving averages strategy
      4. RSI strategy
      5. MACD crossover strategy
      6. RSI and MACD strategies
      7. Triple exponential average strategy
      8. Williams R% strategy
    4. Learning mean-reversion strategies
      1. Bollinger band strategy
      2. Pairs trading strategy
    5. Learning mathematical model-based strategies
      1. Minimization of the portfolio volatility strategy with monthly trading
      2. Maximum Sharpe ratio strategy with monthly trading
    6. Learning time series prediction-based strategies
      1. SARIMAX strategy
      2. Prophet strategy
    7. Summary
  18. Appendix A: How to Setup a Python Environment
    1. Technical requirements
    2. Initial setup
    3. Downloading the complimentary Quandl data bundle
    4. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Financial Trading with Python
  • Author(s): Jiri Pik, Sourav Ghosh
  • Release date: April 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781838982881