Reader small image

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

Product typeBook
Published inJul 2023
PublisherPackt
ISBN-139781837639359
Edition1st Edition
Right arrow
Author (1)
Sourav Ghosh
Sourav Ghosh
author image
Sourav Ghosh

Sourav Ghosh has worked in several proprietary, high-frequency algorithmic trading firms over the last decade. He has built and deployed extremely low latency, high-throughput automated trading systems for trading exchanges around the world, across multiple asset classes. He specializes in statistical arbitrage market-making and pairs trading strategies with the most liquid global futures contracts. He is currently the vice president at an investment bank based in São Paulo, Brazil. He holds a master's in computer science from the University of Southern California. His areas of interest include computer architecture, FinTech, probability theory and stochastic processes, statistical learning and inference methods, and natural language processing.
Read more about Sourav Ghosh

Right arrow

Building the C++ Market Making and Liquidity Taking Algorithms

In this chapter, we will implement a C++ market making algorithm on top of all the components we built in the previous chapters. This market making algorithm will connect to and send orders to the trading exchange we built previously. Additionally, we will implement a C++ liquidity taking algorithm in the same trading engine framework. This liquidity taking algorithm will also connect to and send orders to the trading exchange.

In this chapter, we will cover the following topics:

  • Understanding the behavior of our trading algorithms
  • Managing the passive liquidity provided in the order book
  • Opening and closing positions aggressively
  • Building the trade engine framework
  • Building and running the main trading application

Technical requirements

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

It is important that you have read and understood the design of the electronic trading ecosystem presented in the Designing Our Trading Ecosystem chapter, especially the Designing a framework for low latency C++ trading algorithms section. It is also expected that you are quite familiar with the previous two chapters – Processing Market Data and Sending Orders to the Exchange in C++ and Building the C++ Trading Algorithm Building Blocks, since we will be using every single component that we built in those two chapters in this chapter.

The specifications of the environment in which the source code for this book was developed are shown here. We present the details of this environment since all the C++ code presented in this book...

Understanding the behavior of our trading algorithms

In this section, we will discuss some additional details about the behavior and motivation behind the two trading strategies we will build in this chapter – the market making trading strategy and the liquidity taking trading strategy. With the use of a hypothetical example for each strategy, we will also try to understand the strategy order flow mechanics and try to further our understanding when we implement these trading strategies in our C++ system, towards the end of this chapter.

Understanding the market making trading algorithm

The market making trading strategies seek to make profits by seeking to capture the spread, which just means buying at the best bid price in the market passively and quickly selling at the best ask price in the market passively (or selling first and buying after). The market making strategies, profitability depends on the spread of the trading instrument, how many buy and sell trades the...

Managing the passive liquidity provided in the order book

At this point, we have all the sub-components we need to start building our trading strategies. The first strategy we will build will be the MM algorithm, which sends orders that are expected to rest passively in the order book. We discussed the details of this trading algorithm earlier in this chapter, so in this section, we will focus on the C++ implementation. All the source code for this MarketMaker trading algorithm can be found in the Chapter10/trading/strategy/market_maker.h and Chapter10/trading/strategy/market_maker.cpp source files.

Defining the data members in the MarketMaker algorithm

First, we need to define the data members that make up the MarketMaker class. The key members are the following:

  • A pointer to a constant FeatureEngine object called feature_engine_, which we will use to fetch the fair market price, using the FeatureEngine::getMktPrice() method we saw earlier
  • A pointer to an OrderManager...

Opening and closing positions aggressively

In this section, we will build a liquidity taking algorithm, whose behavior we covered in the first section of this chapter. This trading strategy does not send passive orders as the MM algorithm does; instead, it sends aggressive orders that trade against liquidity resting in the book. The source code for the LiquidityTaker algorithm is in the Chapter10/trading/strategy/liquidity_taker.h and Chapter10/trading/strategy/liquidity_taker.cpp source files. First, we will define the data members that make up the LiquidityTaker class in the next subsection.

Defining the data members in the LiquidityTaker algorithm

The LiquidityTaker trading strategy has the same data members as the MarketMaker algorithm we built in the previous section. Before we describe the data members themselves, we will present the header files we need to include in the liquidity_taker.h source file:

#pragma once
#include "common/macros.h"
#include "...

Building the trade engine framework

In this section, we will build the trade engine framework in the TradeEngine class. This framework ties all the different components we built together – the OrderGateway, MarketDataConsumer, MarketOrderBook, FeatureEngine, PositionKeeper, OrderManager, RiskManager, MarketMaker, and LiquidityTaker components. As a reminder of the trading engine component, we present a diagram of all the sub-components here. We have built all the sub-components; now, we will just build the trading engine framework in which these sub-components exist.

Figure 10.8 – The components of the trading engine in the client’s trading system

Figure 10.8 – The components of the trading engine in the client’s trading system

We will start this section by defining the data members of our class, as usual. All the source code for the basic TradeEngine framework is in the Chapter10/trading/strategy/trade_engine.h and Chapter10/trading/strategy/trade_engine.cpp source files.

Defining the data members in the trade...

Building and running the main trading application

In the last section of this chapter, we will finally build the main trading application using all the components we built in this chapter, as well as the previous two chapters. First, we will discuss the implementation of the trading_main binary application, which combines the MarketDataConsumer, OrderGateway, MarketOrderBook, and TradeEngine components. After that, we will run our complete electronic trading ecosystem – the electronic trading exchange (the exchange_main application) from the Communicating with Market Participants chapter and a few instances of the market participants (the trading_main application), which we will build next.

Building the main trading application

Now, let us build the executable trading_main binary that will initialize and run all the components on the market participant’s trading system. The source code for this application is in the Chapter10/trading/trading_main.cpp source file...

Summary

This chapter focused on using all the components we have built over the last two chapters and leveraging them to build our intelligent trading strategies – the MM trading strategy and the liquidity taking trading algorithm. We spent some time understanding the theory, motivation, and behavior of these two trading algorithms with some examples.

In the next two sections, we implemented the C++ MM trading algorithm, which manages passive orders, and the liquidity taking algorithm, which sends aggressive orders to the market.

Then, we built the trading engine framework that ties together the market data consumer, the order gateway, the feature engine, the position keeper, the order manager, and the risk manager together with the two trading algorithms. This framework is what we use to join all these components together and facilitate the flow of incoming and outgoing data streams and trading intelligence.

Finally, we built the main trading application, trading_main...

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 2023Publisher: PacktISBN-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.
undefined
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

Author (1)

author image
Sourav Ghosh

Sourav Ghosh has worked in several proprietary, high-frequency algorithmic trading firms over the last decade. He has built and deployed extremely low latency, high-throughput automated trading systems for trading exchanges around the world, across multiple asset classes. He specializes in statistical arbitrage market-making and pairs trading strategies with the most liquid global futures contracts. He is currently the vice president at an investment bank based in São Paulo, Brazil. He holds a master's in computer science from the University of Southern California. His areas of interest include computer architecture, FinTech, probability theory and stochastic processes, statistical learning and inference methods, and natural language processing.
Read more about Sourav Ghosh