Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Low Latency Applications with C++

You're reading from  Building Low Latency Applications with C++

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781837639359
Pages 506 pages
Edition 1st Edition
Languages
Author (1):
Sourav Ghosh Sourav Ghosh
Profile icon Sourav Ghosh

Table of Contents (19) Chapters

Preface 1. Part 1:Introducing C++ Concepts and Exploring Important Low-Latency Applications
2. Chapter 1: Introducing Low Latency Application Development in C++ 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

Building the C++ Matching Engine

We spent the last chapter discussing the design of the electronic trading ecosystem we will build in this book. The first component we will start with is the matching engine at the exchange. In this chapter, we will focus on the task of building the order book of the exchange matching engine, based on orders that clients enter. We will implement the various data structures and algorithms needed to track these orders, perform matching when orders cross each other, and update the order book. Crossing means when a buy order has a price equal to or greater than a sell order and then can execute against each other, but we will discuss this in greater detail in this chapter. We will focus on achieving the lowest latencies possible in these operations, since the exchanges with the best infrastructure are likely to do the most business and be preferred by participants. For now, we will not worry about the details of the market data publisher and order gateway...

Technical requirements

All the code for this book can be found in the GitHub repository for this book at https://github.com/PacktPublishing/Building-Low-Latency-Applications-with-CPP. The source code for this chapter is in the Chapter6 directory in the repository.

It is important that you have read and understand the design of the electronic trading ecosystem presented in the Designing Our Trading Ecosystem chapter. Note that in this chapter, we will also use the code we built in Chapter 4, Building the C++ Building Blocks for Low Latency Applications, which can be found in the Chapter6/common/ directory in the GitHub repository for this book.

The specifications of the environment in which the source code for this book was developed are shown as follows. We present the details of this environment because all the C++ code presented in this book is not necessarily portable and might require some minor changes to work in your environment:

  • OS: Linux 5.19.0-41-generic #42...

Defining the operations and interactions in our matching engine

Here, we will declare and define the types, constants, and structures we will need as we build the matching engine in this chapter.

Defining some types and constants

Let us define a few common typedefs to document the types we will use in the rest of this book. We will also define some constants to represent some assumptions that exist, purely for the purpose of simplifying the design of our matching engine. Note that you don’t need these limits/constants, and we leave this enhancement up to the interested among you. All the code for this subsection can be found in the Chapter6/common/types.h file in the GitHub repository for this book.

Defining a few basic types

We will define some types to hold different attributes in our electronic trading system, such as the following:

  • OrderId to identify orders
  • TickerId to identify trading instruments
  • ClientId for the exchange to identify different...

Building the matching engine and exchanging external data

In this section, we will build various parts of the matching engine class. A lot of the heavy lifting of handling client requests, building and updating the limit order book, and generating order responses and market updates will be offloaded to the order book class, which we will discuss in the next section. Please reread the Designing the C++ matching engine in a trading exchange section in the previous chapter, Designing Our Trading Ecosystem, for a refresher on the components we will build in this section and the design principles behind them. We present the diagram from that chapter here for easy reference, showing the design of the matching engine.

Figure 6.2 – The design of our matching engine component

Figure 6.2 – The design of our matching engine component

The matching engine is an independent thread of execution that consumes order requests from ClientRequestLFQueue, publishes order responses to ClientResponseLFQueue, and publishes market...

Building the order book and matching orders

This final section implements the order book functionality. Remember that the order book handles client order requests forwarded from the matching engine. It checks the order request type, updates the order book, generates order responses for the client, and generates market data updates for the public market data feed. All the code for the limit order book in the matching engine is in the me_order_book.h and me_order_book.cpp source files, saved in the Chapter6/exchange/matcher/ directory in the GitHub repository for this book.

Building the internal data structures

First, we will declare the data members for the limit order book. We presented a diagram depicting the data structures that make up the limit order book previously, in Figure 6.1. The limit order book contains the following important data members:

  • A matching_engine_ pointer variable to the MatchingEngine parent for the order book to publish order responses and market...

Summary

We started the C++ implementation of our electronic trading ecosystem in this chapter. The first component we built was the exchange matching engine in charge of accepting and answering order requests from the order server component in the exchange infrastructure. This component is also responsible for generating and publishing market data updates to the market data publisher component in the exchange’s infrastructure.

First, we declared some assumptions in our matching engine and limit order books. We also defined a couple of basic Plain Old Data (POD)-style structures to encapsulate information for a single order in the limit order book, a single order request sent from the order server, an order response sent back to the order server, and a single market data update. We showed how to use the lock-free queue to facilitate communication between the matching engine and order server and market data publisher for order requests, order responses, and market data updates...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building Low Latency Applications with C++
Published in: Jul 2023 Publisher: Packt ISBN-13: 9781837639359
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 €14.99/month. Cancel anytime}