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
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.