Python for Algorithmic Trading

Book description

Algorithmic trading, once the exclusive domain of institutional players, is now open to small organizations and individual traders using online platforms. The tool of choice for many traders today is Python and its ecosystem of powerful packages. In this practical book, author Yves Hilpisch shows students, academics, and practitioners how to use Python in the fascinating field of algorithmic trading.

You'll learn several ways to apply Python to different aspects of algorithmic trading, such as backtesting trading strategies and interacting with online trading platforms. Some of the biggest buy- and sell-side institutions make heavy use of Python. By exploring options for systematically building and deploying automated algorithmic trading strategies, this book will help you level the playing field.

  • Set up a proper Python environment for algorithmic trading
  • Learn how to retrieve financial data from public and proprietary data sources
  • Explore vectorization for financial analytics with NumPy and pandas
  • Master vectorized backtesting of different algorithmic trading strategies
  • Generate market predictions by using machine learning and deep learning
  • Tackle real-time processing of streaming data with socket programming tools
  • Implement automated algorithmic trading strategies with the OANDA and FXCM trading platforms

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Contents and Structure
    2. Who This Book Is For
    3. Conventions Used in This Book
    4. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Python and Algorithmic Trading
    1. Python for Finance
      1. Python Versus Pseudo-Code
      2. NumPy and Vectorization
      3. pandas and the DataFrame Class
    2. Algorithmic Trading
    3. Python for Algorithmic Trading
    4. Focus and Prerequisites
    5. Trading Strategies
      1. Simple Moving Averages
      2. Momentum
      3. Mean Reversion
      4. Machine and Deep Learning
    6. Conclusions
    7. References and Further Resources
  3. 2. Python Infrastructure
    1. Conda as a Package Manager
      1. Installing Miniconda
      2. Basic Operations with Conda
    2. Conda as a Virtual Environment Manager
    3. Using Docker Containers
      1. Docker Images and Containers
      2. Building a Ubuntu and Python Docker Image
    4. Using Cloud Instances
      1. RSA Public and Private Keys
      2. Jupyter Notebook Configuration File
      3. Installation Script for Python and Jupyter Lab
      4. Script to Orchestrate the Droplet Set Up
    5. Conclusions
    6. References and Further Resources
  4. 3. Working with Financial Data
    1. Reading Financial Data From Different Sources
      1. The Data Set
      2. Reading from a CSV File with Python
      3. Reading from a CSV File with pandas
      4. Exporting to Excel and JSON
      5. Reading from Excel and JSON
    2. Working with Open Data Sources
    3. Eikon Data API
      1. Retrieving Historical Structured Data
      2. Retrieving Historical Unstructured Data
    4. Storing Financial Data Efficiently
      1. Storing DataFrame Objects
      2. Using TsTables
      3. Storing Data with SQLite3
    5. Conclusions
    6. References and Further Resources
    7. Python Scripts
  5. 4. Mastering Vectorized Backtesting
    1. Making Use of Vectorization
      1. Vectorization with NumPy
      2. Vectorization with pandas
    2. Strategies Based on Simple Moving Averages
      1. Getting into the Basics
      2. Generalizing the Approach
    3. Strategies Based on Momentum
      1. Getting into the Basics
      2. Generalizing the Approach
    4. Strategies Based on Mean Reversion
      1. Getting into the Basics
      2. Generalizing the Approach
    5. Data Snooping and Overfitting
    6. Conclusions
    7. References and Further Resources
    8. Python Scripts
      1. SMA Backtesting Class
      2. Momentum Backtesting Class
      3. Mean Reversion Backtesting Class
  6. 5. Predicting Market Movements with Machine Learning
    1. Using Linear Regression for Market Movement Prediction
      1. A Quick Review of Linear Regression
      2. The Basic Idea for Price Prediction
      3. Predicting Index Levels
      4. Predicting Future Returns
      5. Predicting Future Market Direction
      6. Vectorized Backtesting of Regression-Based Strategy
      7. Generalizing the Approach
    2. Using Machine Learning for Market Movement Prediction
      1. Linear Regression with scikit-learn
      2. A Simple Classification Problem
      3. Using Logistic Regression to Predict Market Direction
      4. Generalizing the Approach
    3. Using Deep Learning for Market Movement Prediction
      1. The Simple Classification Problem Revisited
      2. Using Deep Neural Networks to Predict Market Direction
      3. Adding Different Types of Features
    4. Conclusions
    5. References and Further Resources
    6. Python Scripts
      1. Linear Regression Backtesting Class
      2. Classification Algorithm Backtesting Class
  7. 6. Building Classes for Event-Based Backtesting
    1. Backtesting Base Class
    2. Long-Only Backtesting Class
    3. Long-Short Backtesting Class
    4. Conclusions
    5. References and Further Resources
    6. Python Scripts
      1. Backtesting Base Class
      2. Long-Only Backtesting Class
      3. Long-Short Backtesting Class
  8. 7. Working with Real-Time Data and Sockets
    1. Running a Simple Tick Data Server
    2. Connecting a Simple Tick Data Client
    3. Signal Generation in Real Time
    4. Visualizing Streaming Data with Plotly
      1. The Basics
      2. Three Real-Time Streams
      3. Three Sub-Plots for Three Streams
      4. Streaming Data as Bars
    5. Conclusions
    6. References and Further Resources
    7. Python Scripts
      1. Sample Tick Data Server
      2. Tick Data Client
      3. Momentum Online Algorithm
      4. Sample Data Server for Bar Plot
  9. 8. CFD Trading with Oanda
    1. Setting Up an Account
    2. The Oanda API
    3. Retrieving Historical Data
      1. Looking Up Instruments Available for Trading
      2. Backtesting a Momentum Strategy on Minute Bars
      3. Factoring In Leverage and Margin
    4. Working with Streaming Data
    5. Placing Market Orders
    6. Implementing Trading Strategies in Real Time
    7. Retrieving Account Information
    8. Conclusions
    9. References and Further Resources
    10. Python Script
  10. 9. FX Trading with FXCM
    1. Getting Started
    2. Retrieving Data
      1. Retrieving Tick Data
      2. Retrieving Candles Data
    3. Working with the API
      1. Retrieving Historical Data
      2. Retrieving Streaming Data
      3. Placing Orders
      4. Account Information
    4. Conclusions
    5. References and Further Resources
  11. 10. Automating Trading Operations
    1. Capital Management
      1. Kelly Criterion in Binomial Setting
      2. Kelly Criterion for Stocks and Indices
    2. ML-Based Trading Strategy
      1. Vectorized Backtesting
      2. Optimal Leverage
      3. Risk Analysis
      4. Persisting the Model Object
    3. Online Algorithm
    4. Infrastructure and Deployment
    5. Logging and Monitoring
    6. Visual Step-by-Step Overview
      1. Configuring Oanda Account
      2. Setting Up the Hardware
      3. Setting Up the Python Environment
      4. Uploading the Code
      5. Running the Code
      6. Real-Time Monitoring
    7. Conclusions
    8. References and Further Resources
    9. Python Script
      1. Automated Trading Strategy
      2. Strategy Monitoring
  12. Appendix. Python, NumPy, matplotlib, pandas
    1. Python Basics
      1. Data Types
      2. Data Structures
      3. Control Structures
      4. Python Idioms
    2. NumPy
      1. Regular ndarray Object
      2. Vectorized Operations
      3. Boolean Operations
      4. ndarray Methods and NumPy Functions
      5. ndarray Creation
      6. Random Numbers
    3. matplotlib
    4. pandas
      1. DataFrame Class
      2. Numerical Operations
      3. Data Selection
      4. Boolean Operations
      5. Plotting with pandas
      6. Input-Output Operations
    5. Case Study
    6. Conclusions
    7. Further Resources
  13. Index

Product information

  • Title: Python for Algorithmic Trading
  • Author(s): Yves Hilpisch
  • Release date: November 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492053354