100% found this document useful (1 vote)
928 views5 pages

Implementing An Order Book in C++

Uploaded by

bnjangale13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
928 views5 pages

Implementing An Order Book in C++

Uploaded by

bnjangale13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Implementing an Order Book in C++

AARON DE LA ROSA

An Order Book is a data structure used in financial markets to keep track of all buy and sell orders for
a particular asset, such as stocks, bonds, or cryptocurrencies. It helps market participants know the
available orders and their prices, facilitating the matching of buyers with sellers.
The order book is divided into two parts:

• Buy orders: Requests to buy at or below a specific price.

• Sell orders: Requests to sell at or above a specific price.

Order books handle various order types:

• Market orders: Execute immediately at the best available price.


• Limit orders: Execute only when a specific price is reached.

• Stop orders: Execute after the asset reaches a certain price level.
• Good-Till-Canceled (GTC): Remain active until canceled.

• Fill-Or-Kill (FOK): Execute fully or cancel immediately.

In C++, we can implement an order book by creating a class structure that maintains a collection of
orders. Each order contains details such as the order ID, type, side (buy/sell), price, and quantity.

Key Components of the C++ Implementation

1. Order Class: Represents an individual order, storing attributes like ID, type, side (buy/sell),
price, and quantity.

2. OrderBook Class: Maintains the list of orders, provides methods to add, cancel, and match
orders.

3. Matching Logic: Matches buy and sell orders based on price and quantity.

4. Execution Logic: Handles the execution of orders and prints the matched results.

5. Data Structures:

o std::vector: Stores the list of orders.

o std::unordered_map: Used for fast lookups to cancel orders by ID.

• Adding Orders: Orders are added to the book using the addOrder() method.
• Matching Orders: Orders are matched based on their type and side using matchOrders().
• Executing Orders: When orders are matched, the executeOrder() function prints the trade
details and reduces the quantity of the matched orders.
• Cancelling Orders: Orders can be removed using their ID with the cancelOrder() method.
• Printing the Order Book: The printOrders() method shows the current state of all orders in
the book.
This implementation demonstrates a basic simulation of a financial order book in C++.

It ensures:

• Market orders are filled immediately.


• Limit orders wait until they meet price conditions.
• Fill-Or-Kill orders are executed fully or canceled.
• Orders can be partially matched, making the system more realistic.

This design makes use of OOP (Object-Oriented Programming) to encapsulate the order and order
book functionality, ensuring modularity, maintainability, and scalability.

You might also like