libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType > Class Template Reference

A simple implementation of a price/time limit order book. More...

#include <order_book.hpp>

Classes

struct  cheapest_orders_by_time_t
 
struct  cheapest_t
 
class  most_expensive_by_time_t
 
struct  most_expensive_t
 

Public Types

using msg_details_t = MsgType
 
using price_t = typename msg_details_t::Price_t
 
using quantity_t = typename msg_details_t::Quantity_t
 
using side_t = typename msg_details_t::Side
 
using symbol_t = typename msg_details_t::SecurityID_t
 
using order_t = typename msg_details_t::NewOrder_t
 
using execution_t = typename msg_details_t::ExecutionReport
 
using lock_traits = libjmmcg::ppd::api_lock_traits< libjmmcg::ppd::platform_api, libjmmcg::ppd::heavyweight_threading >
 
using exception_type = lock_traits::exception_type
 
using trades_t = std::vector< execution_t >
 
using orders_by_time = std::list< internal_order >
 

Public Member Functions

 order_book ()
 
 ~order_book ()
 
bool empty () const noexcept(true)
 
bool is_crossed () const noexcept(true)
 
price_t agreed_spread (symbol_t const &symbol) const noexcept(true)
 
void place (order_t const &order) noexcept(false)
 Place the requested order in the book. More...
 
void cancel (order_t const &order) noexcept(false)
 Remove the specified order from the order book. More...
 
trades_t match ()
 Return all possible trades, also uncrossing the order book. More...
 

Friends

template<class MsgTypes1 >
std::ostream & operator<< (std::ostream &os, order_book< MsgTypes1 > const &o)
 

Detailed Description

template<class MsgType>
class isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >

A simple implementation of a price/time limit order book.

Exchange matching engines use an order book to match orders from buyers and sellers.

Note
A production-quality program would place this in shared memory (to be regularly flushed to disk) e.g. to be used in case the order book process crashes, so that the data is not lost, and can be recovered.
The algorithmic complexities all assume that the number of symbols is much smaller than the number of trades on the book. If this is not the case, then the log(num_symbols) lookup may dominate, but this is unlikely.

Definition at line 45 of file order_book.hpp.

Member Typedef Documentation

◆ exception_type

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::exception_type = lock_traits::exception_type

Definition at line 55 of file order_book.hpp.

◆ execution_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::execution_t = typename msg_details_t::ExecutionReport

Definition at line 53 of file order_book.hpp.

◆ lock_traits

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::lock_traits = libjmmcg::ppd::api_lock_traits<libjmmcg::ppd::platform_api, libjmmcg::ppd::heavyweight_threading>

Definition at line 54 of file order_book.hpp.

◆ msg_details_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::msg_details_t = MsgType

Definition at line 47 of file order_book.hpp.

◆ order_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::order_t = typename msg_details_t::NewOrder_t

Definition at line 52 of file order_book.hpp.

◆ orders_by_time

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::orders_by_time = std::list<internal_order>

We push_back(), so orders towards the beginning of the collection are older than those towards the end.

Todo:
Consider a faster collection, such as boost circular buffer for something similar.

Definition at line 83 of file order_book.hpp.

◆ price_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::price_t = typename msg_details_t::Price_t

Definition at line 48 of file order_book.hpp.

◆ quantity_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::quantity_t = typename msg_details_t::Quantity_t

Definition at line 49 of file order_book.hpp.

◆ side_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::side_t = typename msg_details_t::Side

Definition at line 50 of file order_book.hpp.

◆ symbol_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::symbol_t = typename msg_details_t::SecurityID_t

Definition at line 51 of file order_book.hpp.

◆ trades_t

template<class MsgType >
using isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::trades_t = std::vector<execution_t>

Definition at line 77 of file order_book.hpp.

Constructor & Destructor Documentation

◆ order_book()

template<class MsgType >
isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::order_book
inline

Definition at line 252 of file order_book_impl.hpp.

◆ ~order_book()

template<class MsgType >
isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::~order_book
inline

Definition at line 257 of file order_book_impl.hpp.

Member Function Documentation

◆ agreed_spread()

template<class MsgType >
order_book< MsgType >::price_t isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::agreed_spread ( symbol_t const &  symbol) const
inlinenoexcept

Algorithmic complexity: O(n) where n is the total number of orders in all the books.

Returns
The spread for the specified symbol, if there are prices on both sides, otherwise zero.

Definition at line 281 of file order_book_impl.hpp.

◆ cancel()

template<class MsgType >
void isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::cancel ( order_t const &  order)
inlinenoexcept

Remove the specified order from the order book.

Algorithmic complexity: O(n^2) where n is the total number of orders in all the books.

See also
place(), match()

Definition at line 321 of file order_book_impl.hpp.

References isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::empty().

◆ empty()

template<class MsgType >
bool isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::empty
inlinenoexcept

Algorithmic complexity: O(1)

Returns
False if there are any trades that have been placed, otherwise true.

Definition at line 262 of file order_book_impl.hpp.

Referenced by isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::cancel(), and isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::match().

◆ is_crossed()

template<class MsgType >
bool isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::is_crossed
inlinenoexcept

Algorithmic complexity: O(num_symbols*n) where num_symbols is the total number of symbols that have been placed and n is the total number of orders in all the books.

Returns
False if any there are no bids or no asks, otherwise the result of comparing the top prices.

Definition at line 268 of file order_book_impl.hpp.

Referenced by isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::match().

◆ match()

template<class MsgType >
order_book< MsgType >::trades_t isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::match
inline

Return all possible trades, also uncrossing the order book.

After calling place() this function may need to be called as the order book may be crossed and thus potential executions that only match() can discover.

Algorithmic complexity: O(num_executions*num_symbols*log(n)) where:

  1. num_executions is the total number of executions that would result,
  2. num_symbols is the total number of symbols that have been placed,
  3. n is the largest number of orders in all the books across the symbols.
Returns
All possible trades; non-empty if crossed, otherwise empty.
See also
place(), cancel()

Definition at line 343 of file order_book_impl.hpp.

References isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::empty(), and isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::is_crossed().

◆ place()

template<class MsgType >
void isimud::ISIMUD_VER_NAMESPACE::order_book< MsgType >::place ( order_t const &  order)
inlinenoexcept

Place the requested order in the book.

This may leave the order book in a crossed state, i.e. there may be available trades, so match() may have to be called after calling place().

Algorithmic complexity: O(log(n)) where n is the total number of orders in all the books.

See also
cancel(), is_crossed(), match()

Definition at line 294 of file order_book_impl.hpp.

References jmmcg::LIBJMMCG_VER_NAMESPACE::info::function::add_arg().

Friends And Related Function Documentation

◆ operator<<

template<class MsgType >
template<class MsgTypes1 >
std::ostream& operator<< ( std::ostream &  os,
order_book< MsgTypes1 > const &  o 
)
friend

Algorithmic complexity: O(n) where n is the total number of orders in all the books.


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