Use your programming skills to create and optimize high-frequency trading systems in no time with Java, C++, and Python
Key Features
• Learn how to build high-frequency trading systems with ultra-low latency
• Understand the critical components of a trading system
• Optimize your systems with high-level programming techniques
Book Description
The world of trading markets is complex, but it can be made easier with technology. Sure, you know how to code, but where do you start? What programming language do you use? How do you solve the problem of latency? The Developing High-Frequency Trading Systems book answers all these questions.
This practical guide will help you navigate the fast-paced world of algorithmic trading and show you how to build a high-frequency trading system from complex technological components, supported by accurate data.
Starting off with an introduction to high-frequency trading, exchanges, and the critical components of a trading system, the book quickly moves on to the nitty-gritty of optimizing hardware and your operating system for low-latency trading, such as bypassing the kernel, memory allocation, and the danger of context switching. Monitoring your system’s performance is vital, so you’ll also get up to speed with logging and statistics.
As you move beyond the traditional high-frequency trading programming languages, such as C++ and Java, you’ll learn how to use Python to achieve high levels of performance. And what book on trading would be complete without diving into cryptocurrency?
By the end of this book, you’ll be ready to take on the markets with high-frequency trading systems.
What you will learn
• Understand the architecture of high-frequency trading systems
• Boost system performance to achieve the lowest possible latency
• Leverage the power of Python, C++, and Java to build your trading systems
• Bypass your kernel and optimize your operating system
• Use static analysis to improve code development
• Use C++ templates and Java multithreading for super-low latency
• Apply your knowledge to cryptocurrency trading
Who This Book Is For
This book is for software engineers, quantitative developers or researchers, and DevOps engineers who want to understand the technical side of high-frequency trading systems and the optimizations that are needed to get to ultra-low latency systems. Prior experience working with C++ and Java will help you grasp the topics covered in this book.
Author(s): Sebastien Donadio, Sourav Ghosh, John Rizzo, Romain Rossier
Edition: 1
Publisher: Packt Publishing
Year: 2022
Language: English
Commentary: Vector PDF
Pages: 320
City: Birmingham, UK
Tags: C++; Python; Java; Hardware; Finance; Networking; Trading; Software Architecture; High-Frequency Trading
Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Part 1: Trading Strategies, Trading Systems, and Exchanges
Chapter 1: Fundamentals of a High-Frequency Trading System
History of HFT
The post-1930s era
The modern era
Why have HFT?
What makes HFT so different from regular trading?
Effect of dark pools
Who trades HFT?
What do I need to start an HFT?
What are HFT strategies?
Asset classes
Liquidity
Tick-by-tick data and data distribution
Liquidity rebates
Matching engine
Market making
Scalping
Statistical arbitrage
Latency arbitrage
Impact of news
Momentum ignition
Rebate strategies
Pinging
Illegal activities
Summary
Chapter 2: The Critical Components of a Trading System
Understanding the trading system
Trading system architecture
Gateways connecting to trading exchanges
Making a trading system trade with exchanges
Examining the API for communication
Order book management
Order book considerations
Strategy making decisions on when to trade
The OMS
Critical components
Non-critical components
Command and control
Services
Summary
Chapter 3: Understanding the Trading Exchange Dynamics
Architecting a trading exchange for handling orders at a large scale
History of trading exchanges
Understanding features of an exchange
Exchange architecture
General order book and matching engine
Best price scenario
Partial fill scenario
No match scenario
Multiple orders with the same price
Summary
Part 2: How to Architect a High-Frequency Trading System
Chapter 4: HFT System Foundations – From Hardware to OS
Understanding HFT computers
CPUs, from multi-processor to multi-core
Main memory or RAM
Shared memory
I/O devices
Using the OS for HFT systems
User space and kernel space
Process scheduling and CPU resource management
Memory management
Paged memory and page tables
System calls
Threading
Interruption management
The role of compilers
Executable file formats
Static versus dynamic linking
Summary
Chapter 5: Networking in Motion
Understanding networking in HFT systems
Learning about network conceptual models
Network communications between systems in HFT
Comprehending how switches work
Important protocol concepts
Using Ethernet for HFT communication
Using IPv4 as a network layer
UDP and TCP for the transport layer
Designing financial protocols for HFT exchanges
FIX protocol
Interior networks versus exterior networks
Understanding the packet life cycle
Comprehending the packet life in the send/receive (TX/RX) path
Software layer receiving the packet
Monitoring the network
Packet capture and analysis
Valuing time distribution
Time-synchronization services
Summary
Chapter 6: HFT Optimization – Architecture and Operating System
Performance mental model
Understanding context switches 
Types of context switches
Why are context switches good
Steps and operations involved in a context switch operation
Why are context switches bad for HFT?
Techniques to avoid or minimize context switches
Building lock-free data structures 
When/why are locks needed (non-HFT applications)
Types of synchronization mechanisms
Problems and inefficiencies with using locks
Pre-fetching and pre-allocating memory
Memory hierarchy
Pre-fetching based alternatives to boost performance
Dynamic memory allocation
Pre-allocation-based alternatives to dynamic memory allocation
Summary
Chapter 7: HFT Optimization – Logging, Performance, and Networking
Comparing kernel space and user space 
What is kernel and user space?
Investigating performance – kernel versus user space
Using kernel bypass
Understanding why kernel bypass is the alternative
Presenting kernel bypass latencies
Learning about memory-mapped files
Using cable fiber, hollow fiber, and microwave technologies 
Evolution from cable fiber to hollow fiber to microwave
How hollow fiber works
How microwave works
Diving into logging and statistics
The need for logging in HFT
The need for online/live statistics computation in HFT
Measuring performance
Motivation for measuring performance
Linux tools for measuring performance
Custom techniques for measuring performance
Summary
Part 3: Implementation of a High-Frequency Trading System
Chapter 8: C++ – The Quest for Microsecond Latency
C++ 14/17 memory model 
What is a memory model?
The need for a memory model
The C++ 11 memory model and its rules
C++ memory model principles
Removing runtime decisions 
Motivation for removing runtime decisions
Virtual functions
Performance penalties
Dynamic memory allocation 
Runtime performance penalty
Using constexpr efficiently {ICON2}
Exceptions impeding performance {ICON2}
Templates reducing the runtime 
What are templates?
Template specialization {ICON2}
Why use templates?
Disadvantages of templates
Performance of templates
Standard Template Library (STL)
Static analysis
What is C++ static analysis?
The need for static analysis
Types of static analysis
Steps in static analysis
Benefits and drawbacks of static analysis
Use case-Building an FX high-frequency trading system
Summary
Chapter 9: Java and JVM for Low-Latency Systems
Introducing the basics of Java
Reducing the impact of the GC 
What to do to keep GC events low and fast
Warming up the JVM 
Tiered compilation in JVM
Optimizing the JVM for better startup performance
Measuring the performance of a Java software
Why are Java microbenchmarks difficult to create?
Real-time performance measures
Java threading 
Using a thread pool/queue with threads
High-performance task queue 
Queues
Circular buffer
LMAX disruptor
Logging and DB access
External or internal thread?
Summary
Chapter 10: Python – Interpreted but Open to High Performance
Introducing Python
Making use of Python for analytics
Why is Python slow?
How do we use libraries in Python?
Python and C++ for HFT
Using C++ in Python
Using Python with C++
Boost.Python library
Using ctypes/CFFI to accelerate Python code
SWIG
Improving the speed of Python code in HFT
Summary
Chapter 11: High-Frequency FPGA and Crypto
Reducing latencies with FPGA 
Evolution of the fierce competition of speed in HFT
Introduction to FPGA
Diving into FPGA trading systems
Advantages of FPGA trading systems
Disadvantages of FPGA trading systems
Final words on FPGAs
Exploring HFT with cryptocurrencies
What is crypto?
How do crypto transactions work?
What is a blockchain?
What is cryptocurrency mining?
Similarities between traditional asset trading and cryptocurrency trading
Main differences between traditional asset trading and cryptocurrency trading
Trading with cryptocurrency exchange
HFT strategies in crypto
Building a high-frequency system for crypto trading
How to build a trading system in the cloud
Summary
Index
Other Books You May Enjoy