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

Processing Market Data and Sending Orders to the Exchange in C++

In this chapter, we will build the client’s C++ system that receives and processes market data updates from the trading exchange. We will also have to deal with creating and reading from UDP sockets, dealing with packet losses, etc. We will discuss the design of an order book on the client side to track the order book maintained at the trading exchange. We will also implement the C++ components needed to establish and maintain TCP connections to the trading exchange. We will also implement functionality to send orders to the exchange from the strategies and receive and process order responses.

In this chapter, we will cover the following topics:

  • Subscribing to market data and decoding the market data protocol
  • Building order books from market data
  • Connecting to the exchange, sending order requests, and receiving responses

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 for this chapter is in the Chapter 8 directory in the repository.

You must read and understand the design of the electronic trading ecosystem presented in the chapter Designing Our Trading Ecosystem. The components we build in this chapter will interact with the electronic trading exchange application we built in the chapter Communicating With Market Participants, so we assume you are familiar with that. The limit order book we will build in the client application’s trade engine component is almost identical to the order book we built inside the matching engine in the chapter Building the C++ Matching Engine within the Building the order book and matching orders section. So, we assume the reader is very familiar with that chapter and the code we discussed there as we will make references...

Subscribing to market data and decoding the market data protocol

The first component we need to build inside the market participants’ trading system is the market data consumer. This component is responsible for subscribing to the multicast stream of public market data updates published by the trading exchange. It needs to decode the market data stream generated by the exchange from the public MDPMarketUpdate format we discussed earlier. Because of the choice of the Simple Binary Encoding (SBE) protocol, the decoding step is straightforward in our application and does not involve any complicated stream decoding logic. Another important responsibility of this component is detecting packet drops on the incremental market data stream and providing mechanisms to recover and synchronize with the market data stream again. This mechanism is also required for trading systems that subscribe to the market data stream after there is a non-empty order book, i.e. after the trading exchange...

Building order books from market data

In the previous section, we built the market data consumer component, which subscribes to the market data stream, synchronizes between the snapshot and incremental streams, and decodes the market data updates and publishes them to the trading engine component. The trading engine component then needs to process these market data updates and build a limited order book like the one that the matching engine builds, except this is a much more limited version of the matching engine’s order book. As a reminder, we discussed this in the chapter Designing Our Trading Ecosystem in the Designing a framework for low latency C++ trading algorithms section. One last thing to note is that we will re-use the design and code of the order book in the matching engine to create the order book in the client’s system. We will re-use the source code we built in the chapter Building the C++ Matching Engine in the Building the order book and matching orders...

Connecting to the exchange and sending and receiving order flow

The order gateway client component in the market participant’s trading infrastructure receives order requests from the trading engine through a lock-free queue and sends order responses back to the trading engine through another lock-free queue. It also establishes a TCP connection to the order gateway server in the exchange side infrastructure. It encodes order requests in the exchange’s order format and sends them over the TCP connection. It also consumes order responses sent by the exchange over that TCP connection and decodes them from the order data format. We present the order gateway client diagram again to refresh your memory on that component’s design.

Figure 8.4 – Diagram presenting the order gateway client component inside the client’s trading infrastructure

Figure 8.4 – Diagram presenting the order gateway client component inside the client’s trading infrastructure

We will start the implementation of this order gateway client component by defining the...

Summary

This chapter was dedicated to building the important core infrastructure components inside the market participant’s trading system. First, we build the market data consumer component, which is responsible for subscribing to the multicast market data stream generated by the exchange. It needs to detect gaps in market data updates on the incremental market data stream and initiate snapshot recovery and synchronization mechanisms to re-synchronize with the incremental market data stream. It decodes the market data updates from the format that the exchange publishes to a simpler internal market data format.

The order book sub-component inside the trading engine component processes the market data updates it receives from the market data consumer. It builds and updates an order book data structure from these updates for the trading engine to get an accurate view of the market.

The order gateway component inside the trading system establishes and maintains a bi-directional...

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}