Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Building Low Latency Applications with C++

You're reading from   Building Low Latency Applications with C++ Develop a complete low latency trading ecosystem from scratch using modern C++

Arrow left icon
Product type Paperback
Published in Jul 2023
Publisher Packt
ISBN-13 9781837639359
Length 506 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sourav Ghosh Sourav Ghosh
Author Profile Icon Sourav Ghosh
Sourav Ghosh
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Part 1:Introducing C++ Concepts and Exploring Important Low-Latency Applications
2. Chapter 1: Introducing Low Latency Application Development in C++ FREE CHAPTER 3. Chapter 2: Designing Some Common Low Latency Applications in C++ 4. Chapter 3: Exploring C++ Concepts from A Low-Latency Application’s Perspective 5. Chapter 4: Building the C++ Building Blocks for Low Latency Applications 6. Part 2:Building a Live Trading Exchange in C++
7. Chapter 5: Designing Our Trading Ecosystem 8. Chapter 6: Building the C++ Matching Engine 9. Chapter 7: Communicating with Market Participants 10. Part 3:Building Real-Time C++ Algorithmic Trading Systems
11. Chapter 8: Processing Market Data and Sending Orders to the Exchange in C++ 12. Chapter 9: Building the C++ Trading Algorithm’s Building Blocks 13. Chapter 10: Building the C++ Market Making and Liquidity Taking Algorithms 14. Part 4:Analyzing and Improving Performance
15. Chapter 11: Adding Instrumentation and Measuring Performance 16. Chapter 12: Analyzing and Optimizing the Performance of Our C++ System 17. Index 18. Other Books You May Enjoy

Understanding the layout of the electronic trading ecosystem

First, we start by providing the higher-level layout of the electronic trading ecosystem we will be building in the rest of this book. Before we get into the details, we start with the disclaimer that this is a simplified design of what happens in practice in electronic trading markets. Simplification was necessary to limit the scope to what can be covered in this book; however, it is still an accurate but simplified representation of what you will find in practice. The other thing to note is that the goal here is to understand the design and implementation of low-latency applications, so we ask you to focus more on the application of C++ and computer science principles that we apply and less on the details of the trading ecosystem itself.

Now, let us kick off this introduction by defining and explaining the overall topology of the electronic trading ecosystem and the components involved.

Defining the topology of the electronic trading ecosystem

Let us first provide a bird’s eye view of the system with the diagram presented here:

Figure 5.1 – Topology of a simple electronic trading ecosystem

Figure 5.1 – Topology of a simple electronic trading ecosystem

The major components, as laid out in the preceding diagram, are the following, split at a high level depending on whether it belongs on the exchange side or the trading client/market-participant side.

These are the exchange components:

  • Matching engine at the electronic trading exchange
  • Order gateway server and protocol encoder and decoder at the trading exchange
  • Market data encoder and publisher at the exchange

These are the trading client components:

  • Market data consumer and decoder for a market participant interested in this market data
  • Order gateway encoder and decoder client in the market participant’s system
  • Trading engine inside the participant’s system

We will quickly introduce each one of these components in the next section and then discuss them in detail in the rest of this chapter.

Introducing the components of the electronic trading ecosystem

Here, we will quickly introduce the different components that make up the electronic trading ecosystem. One thing to keep in mind is that each one of these components in a competitive ecosystem needs to be designed such that they can process events and data with the lowest latencies possible. Also note that during periods of heightened volatility, these systems must be able to keep up with and react to large bursts in market activity.

Introducing the market data publisher

The market data publisher at the trading exchange is responsible for communicating every change to the limit order book maintained by the matching engine to the market participants. Compared to the order gateway, the difference here is that the market data publisher publishes public data meant for all participants, and it typically hides details of which order belongs to which participant to maintain fairness. Another difference is that the order gateway infrastructure only communicates order updates to the market participants whose orders were impacted by the change and not to all market participants. The market data publisher can use TCP or UDP to publish market data, but given the large volume of market data updates, UDP multicast is the preferred network-level protocol. The market data publisher is also responsible for converting the internal matching engine format into the market data format before publishing the updates.

Introducing the matching engine

The matching engine at the electronic trading exchange is the most critical piece of the trading exchange. It is responsible for handling requests from market participants for their orders and updating the limit order book that it maintains. These requests are generated when the clients want to add a new order, replace an existing order, cancel an existing order, and so on. The limit order book is a collection of all orders sent by all participants aggregated into a central single book consisting of bids (buy orders) and asks (sell orders). The matching engine is also responsible for performing matches between orders that cross in price (i.e., matching buy orders with sell orders when the buy price is higher than or equal to the sell price). During special market states such as PreOpen (right before the market opens), Auction/Opening (right at the moment at which the market opens), PreOpenNoCancel (orders can be entered but not canceled), and so on, the rules are slightly different, but we will not worry about those rules or implement them to keep the focus on low-latency application development.

Introducing the order gateway server at the exchange

The order gateway server at the exchange is responsible for accepting connections from market participants so that they can send requests for orders and receive notifications when there are updates to their respective orders. The order gateway server is also responsible for translating messages between the matching engine format and the order gateway messaging protocol. The network protocol used for the order gateway server is always TCP to enforce in-order delivery of messages and reliability.

Introducing the market data consumer at the market participant level

The market data consumer is the complement of the exchange market data publisher component on the market participants’ side. This component is responsible for subscribing to the UDP stream or the TCP server set up by the market data publisher, consuming the market data updates, and decoding the market data protocol into an internal format used by the rest of the trading engine.

Introducing the order gateway encoder and decoder client

The order gateway client component is the complement of the exchange order gateway server on the market participants’ side. The responsibility of this component is to establish and maintain TCP connections with the exchange’s order gateway infrastructure. It is also responsible for encoding strategy order requests in the correct exchange order messaging protocol and decoding exchange responses into an internal format that the trading engine uses.

Introducing the trading engine in the market participants’ systems

The trading engine is the brain of a market participant’s trading system. This is where intelligence resides, and where the trading decisions are made. This component is responsible for consuming the normalized market data updates from the market data consumer component. It will usually also build the complete limit order book to reflect the state of the market or, at the very least, a simplified variant of the order book, depending on the requirements of the trading strategies. It usually also builds analytics on top of the liquidity and prices from the order book and makes automated trading decisions. This component uses the order gateway client component to communicate with the trading exchange.

Now that we have introduced the major components involved in our electronic trading ecosystem, we will look at these components in greater detail. First, we will start with the matching engine, which resides in the electronic trading exchange system.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime