Orderbook Simulation
OrderbookSim is a C++ application simulating a financial market order book. It efficiently manages and matches buy and sell orders while calculating the Volume-Weighted Average Price (VWAP).
Classes | Public Member Functions | List of all members
Orderbook Class Reference

Manages and matches orders in a trading environment. More...

#include <Orderbook.hpp>

Public Member Functions

 Orderbook ()
 Default constructor, initializes the Good For Day order pruning thread. More...
 
 Orderbook (const Orderbook &)=delete
 
void operator= (const Orderbook &)=delete
 
 Orderbook (Orderbook &&)=delete
 
void operator= (Orderbook &&)=delete
 
 ~Orderbook ()
 Destructor, joins the pruning thread and signals shutdown. More...
 
void changeExecutionType (int type)
 Changes the execution type for order matching. More...
 
Trades addOrder (OrderPtr order)
 Adds an order to the orderbook. More...
 
Trades addOrder (OrderType type, Side side, Price price, Quantity qty)
 Adds a new order to the orderbook with specified parameters. More...
 
Trades addOrder (OrderType type, OrderId orderId, Side side, Price price, Quantity qty)
 Adds a new order with a specified ID. More...
 
void addOrderViaPython (int type, int id, int side, int price, int qty)
 Adds an order to the orderbook via Python interface. More...
 
void CancelOrder (OrderId orderId)
 Cancels an order by its ID. More...
 
Trades ModifyOrder (OrderModify order)
 Modifies an existing order. More...
 
void setPriceScale (Price _price)
 Sets the price scale for the orderbook. More...
 
Price getPriceScale ()
 Retrieves the current price scale of the orderbook. More...
 
void printAllOrders ()
 Prints all orders and orderbook details. More...
 
void saveToJson (const std::string &buyfilename="BuyHistory.json", const std::string &sellfilename="SellHistory.json", const std::string &purchasefilename="PurchaseHistory.json", const std::string &buyLivefilename="BuyLiveHistory.json", const std::string &sellLivefilename="SellLiveHistory.json")
 Saves orderbook data to JSON files. More...
 
std::size_t Size () const
 Retrieves the number of orders in the orderbook. More...
 
OrderbookLevelInfos getOrderInfos () const
 Retrieves aggregated orderbook level information. More...
 
void EndOfDay ()
 Handles end-of-day processing for the orderbook. More...
 

Detailed Description

Manages and matches orders in a trading environment.

The Orderbook class maintains separate collections for bids and asks, as well as provides functions for adding, canceling, modifying, and matching orders. It also includes features for managing Good For Day orders and saving the orderbook state to JSON.

Constructor & Destructor Documentation

◆ Orderbook() [1/3]

Orderbook::Orderbook ( )
inline

Default constructor, initializes the Good For Day order pruning thread.

◆ Orderbook() [2/3]

Orderbook::Orderbook ( const Orderbook )
delete

◆ Orderbook() [3/3]

Orderbook::Orderbook ( Orderbook &&  )
delete

◆ ~Orderbook()

Orderbook::~Orderbook ( )
inline

Destructor, joins the pruning thread and signals shutdown.

Member Function Documentation

◆ addOrder() [1/3]

Trades Orderbook::addOrder ( OrderPtr  order)

Adds an order to the orderbook.

Parameters
orderThe order to add.
Returns
A vector of Trades resulting from the addition.

Adds the provided order to the orderbook, updates internal data structures, and attempts to match orders.

Parameters
orderA shared pointer to the Order to add.
Returns
A vector of Trades resulting from the addition.

◆ addOrder() [2/3]

Trades Orderbook::addOrder ( OrderType  type,
OrderId  id,
Side  side,
Price  price,
Quantity  qty 
)

Adds a new order with a specified ID.

Adds a new order to the orderbook with a specified ID.

Parameters
typeThe type of the order.
orderIdThe ID of the order.
sideThe side of the order (Buy or Sell).
priceThe price of the order.
qtyThe quantity of the order.
Returns
A vector of Trades resulting from the addition.

Creates and adds a new order with the specified ID and parameters.

Parameters
typeThe type of the order.
idThe ID of the order.
sideThe side of the order (Buy or Sell).
priceThe price of the order.
qtyThe quantity of the order.
Returns
A vector of Trades resulting from the addition.

◆ addOrder() [3/3]

Trades Orderbook::addOrder ( OrderType  type,
Side  side,
Price  price,
Quantity  qty 
)

Adds a new order to the orderbook with specified parameters.

Adds a new order to the orderbook.

Parameters
typeThe type of the order.
sideThe side of the order (Buy or Sell).
priceThe price of the order.
qtyThe quantity of the order.
Returns
A vector of Trades resulting from the addition.

Creates and adds a new order to the orderbook with the specified parameters.

Parameters
typeThe type of the order.
sideThe side of the order (Buy or Sell).
priceThe price of the order.
qtyThe quantity of the order.
Returns
A vector of Trades resulting from the addition.

◆ addOrderViaPython()

void Orderbook::addOrderViaPython ( int  type,
int  id,
int  side,
int  price,
int  qty 
)

Adds an order to the orderbook via Python interface.

Parameters
typeThe type of the order (as integer).
idThe ID of the order.
sideThe side of the order (1 for Buy, 2 for Sell).
priceThe price of the order.
qtyThe quantity of the order.

Exposed to Python via Pybind11, allows adding orders from Python code.

Parameters
typeThe type of the order (as integer).
idThe ID of the order.
sideThe side of the order (1 for Buy, 2 for Sell).
priceThe price of the order.
qtyThe quantity of the order.

◆ CancelOrder()

void Orderbook::CancelOrder ( OrderId  orderId)

Cancels an order by its ID.

Parameters
orderIdThe ID of the order to cancel.

Public interface to cancel an order.

Parameters
orderIdThe ID of the order to cancel.

◆ changeExecutionType()

void Orderbook::changeExecutionType ( int  type)

Changes the execution type for order matching.

Parameters
typeThe execution type (0: BuyersPrice, 1: SellersPrice, 2: MidPrice).

Adjusts how the execution price is determined during order matching.

Parameters
typeThe execution type (0: BuyersPrice, 1: SellersPrice, 2: MidPrice).

◆ EndOfDay()

void Orderbook::EndOfDay ( )

Handles end-of-day processing for the orderbook.

Cancels GoodForDay orders and cleans up orders and order entries.

◆ getOrderInfos()

OrderbookLevelInfos Orderbook::getOrderInfos ( ) const

Retrieves aggregated orderbook level information.

Provides information about the bid and ask levels in the orderbook, including prices and quantities.

Returns
An OrderbookLevelInfos object containing level information.

◆ getPriceScale()

Price Orderbook::getPriceScale ( )

Retrieves the current price scale of the orderbook.

Returns
The current price scale.

◆ ModifyOrder()

Trades Orderbook::ModifyOrder ( OrderModify  order)

Modifies an existing order.

Parameters
orderThe OrderModify object containing new order details.
Returns
A vector of Trades resulting from the modification.

Replaces the existing order with a new one based on the modification details.

Parameters
orderThe OrderModify object containing new order details.
Returns
A vector of Trades resulting from the modification.

◆ operator=() [1/2]

void Orderbook::operator= ( const Orderbook )
delete

◆ operator=() [2/2]

void Orderbook::operator= ( Orderbook &&  )
delete

◆ printAllOrders()

void Orderbook::printAllOrders ( )

Prints all orders and orderbook details.

Outputs order histories and statistics to the console.

◆ saveToJson()

void Orderbook::saveToJson ( const std::string &  buyfilename = "BuyHistory.json",
const std::string &  sellfilename = "SellHistory.json",
const std::string &  purchasefilename = "PurchaseHistory.json",
const std::string &  buyLivefilename = "BuyLiveHistory.json",
const std::string &  sellLivefilename = "SellLiveHistory.json" 
)

Saves orderbook data to JSON files.

Exports order histories and live orders to specified JSON files.

Parameters
buyfilenameFilename for buy order history.
sellfilenameFilename for sell order history.
purchasefilenameFilename for purchase history.
buyLivefilenameFilename for live buy orders.
sellLivefilenameFilename for live sell orders.

◆ setPriceScale()

void Orderbook::setPriceScale ( Price  _price)

Sets the price scale for the orderbook.

Parameters
_priceThe new price scale to set.

Adjusts the price scale used in the orderbook's internal calculations.

Parameters
_priceThe new price scale to set.

◆ Size()

std::size_t Orderbook::Size ( ) const

Retrieves the number of orders in the orderbook.

Returns
The size of the orderbook.

The documentation for this class was generated from the following files: