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++ Trading Algorithm’s Building Blocks

In this chapter, we will build components that make up the intelligence in our trading applications. These are the components that the trading strategies will rely on very heavily to make decisions, send and manage orders, track and manage positions, profits and losses (PnLs), and manage risk. Not only do the trading strategies need to track the trading PnLs since the goal is to make money, but these components also need to track the PnLs to decide when to stop trading if needed. We will learn how to compute complex features from market data updates, track trading performance based on order executions and market updates, send and manage live strategy orders in the market, and manage market risk. In this chapter, we will cover the following topics:

  • Reacting to executions and managing positions, PnLs, and risk
  • Building the feature engine and computing complex features
  • Using executions and updating positions and...

Technical requirements

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

You must have read and understood the design of the electronic trading ecosystem that was presented in Chapter, Designing Our Trading Ecosystem, especially the Designing a framework for low-latency C++ trading algorithms section. As before, we will use the building blocks we built in Chapter, Building the C++ Building Blocks for Low-Latency Applications.

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

  • OS: Linux 5.19.0-41-generic #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC...

Reacting to executions and managing positions, PnLs, and risk

We need to build a few basic building blocks that will build and support our trading strategies. We discussed the need for these components in Chapter, Designing Our Trading Ecosystem, in the Designing a framework for low-latency C++ trading algorithms section. We have already implemented a major component – the limit order book – but in this section, we will build the remaining components we need, namely the following:

  • A FeatureEngine, which will be used to compute simple and complex features/signals that drive the trading strategy decisions
  • A PositionKeeper, which will receive executions and compute important measures such as position, PnLs, traded volumes, and more
  • An OrderManager, which will be used by the strategies to send orders, manage them, and update these orders when there are updates
  • A RiskManager to compute and check the market risk that a trading strategy is attempting to take...

Building the feature and computing complex features

In this section, we will build a minimal version of a feature engine. We will only compute two simple features – one (market price) that computes fair market prices based on the top of book prices and quantity and another (aggressive trade qty ratio) that computes how big a trade is compared to the top of book quantities. We will use these feature values to drive our market-making and liquidity-taking trading algorithms later in this chapter. The source code for the FeatureEngine class we will build here can be found in the Chapter9/trading/strategy/feature_engine.h file on GitHub. We discussed the details of this component in Chapter, Designing Our Trading Ecosystem, in the Designing a framework for low-latency C++ trading algorithms section.

Defining the data members in the feature engine

First, we need to declare the FeatureEngine class and define the data members inside this class. First, we will include the required...

Using executions to update positions and PnLs

Now, we will build a PositionKeeper class that will be responsible for processing executions on a strategy’s orders and computing and tracking positions and PnLs. This component is used by the strategy as well as the risk manager to compute positions and PnLs for different purposes. All the source code for the PositionKeeper class is in the Chapter9/trading/strategy/position_keeper.h file on GitHub. Before we build the PositionKeeper class, which manages positions for all trading instruments, we will need to build a PositionInfo struct, which is also present in the same source file. The PositionInfo struct is the lower-level struct for managing the positions and PnLs for a single trading instrument; we will cover it in more detail in the next few subsections. We discussed the details of this component in Chapter, Designing Our Trading Ecosystem, in the Designing a framework for low-latency C++ trading algorithms section.

Declaring...

Sending and managing orders

In Chapter, Designing Our Trading Ecosystem, we discussed the purpose of the trading system’s order manager component (the Designing a framework for low-latency C++ trading algorithms section). In this section, we will implement an OrderManager class to encapsulate the order management logic inside this class and thus make it easy for trading strategies to manage their orders. Before we build the OrderManager class itself, we will need to define a basic building block called the OMOrder structure.

Defining the OMOrder struct and its related types

In this first subsection, we will define some enumerations and types to be used in the OrderManager class and its sub-components. All the source code for this subsection is in the Chapter9/trading/strategy/om_order.h source file on GitHub.

First, we must provide the include files that the om_order.h file needs:

#pragma once
#include <array>
#include <sstream>
#include "common...

Computing and managing risk

The final component we still need to build before we can build our trading strategies is RiskManager. The RiskManager component tracks the active order quantities that a trading strategy has in the market through the same OrderManager instance that a trading strategy uses. It also tracks the positions and realized and unrealized PnLs using the PositionKeeper instance, which tracks the trading strategy’s positions and PnLs. It checks that the strategy stays within its assigned risk limits. If the trading strategy goes past its risk limits, such as if it loses more money than it’s allowed, tries to send an order larger than it’s allowed, or builds a position larger than it’s allowed, it prevents it from trading. To keep our RiskManager simple, we will only implement risk checks on the maximum allowed order size, the maximum allowed position, and the maximum allowed loss for each trading instrument in the client’s trading...

Summary

In this chapter, our primary focus was on adding intelligence and sophistication to the market participants’ trading systems. First, we discussed our market-making and liquidity-taking trading strategies. We discussed the motivation behind these strategies, how they seek to profit in the markets, and the trading dynamics of these algorithms.

We implemented the important components that make up the intelligence around our trading strategies. The first one was the feature engine that’s used to compute trading features/signals from the market data so that they can be used by the trading strategies to make informed trading decisions. The next one was the position keeper, which is in charge of tracking a trading strategy’s positions and PnLs as the strategy’s orders are executed in the market. After, we looked at the order manager component, which sends and manages live orders in the market to simplify the trading strategy’s implementation....

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