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