JayBeams  0.1
Another project to have fun coding.
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
jb::itch5::compute_book< book_type > Class Template Reference

Compute the book and call a user-defined callback on each change. More...

#include <compute_book.hpp>

Public Types

Type traits
using clock_type = jb::itch5::clock_type
 clock_type is used as a compute_book<book_type> type in some modules More...
 
using time_point = jb::itch5::time_point
 time_point is used as a compute_book<book_type> type in some modules More...
 
using book_type_config = typename book_type::config
 config type is used to construct the order_book More...
 
using callback_type = std::function< void(message_header const &header, order_book< book_type > const &updated_book, book_update const &update)>
 Define the callback type. More...
 

Public Member Functions

 compute_book (callback_type &&cb, book_type_config const &cfg)
 Constructor. More...
 
 compute_book (callback_type const &cb, book_type_config const &cfg)
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, add_order_message const &msg)
 Handle a new order message. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, add_order_mpid_message const &msg)
 Handle a new order with MPID. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, order_executed_message const &msg)
 Handle an order execution. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, order_executed_price_message const &msg)
 Handle an order execution with a different price than the order's. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, order_cancel_message const &msg)
 Handle a partial cancel. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, order_delete_message const &msg)
 Handle a full cancel. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, order_replace_message const &msg)
 Handle an order replace. More...
 
void handle_message (time_point recvts, long msgcnt, std::size_t msgoffset, stock_directory_message const &msg)
 Pre-populate the books based on the symbol directory. More...
 
template<typename message_type >
void handle_message (time_point, long, std::size_t, message_type const &)
 Ignore all other message types. More...
 
void handle_unknown (time_point recvts, unknown_message const &msg)
 Log any unknown message types. More...
 
std::vector< stock_tsymbols () const
 Return the symbols known in the order book. More...
 
time_point now () const
 Return the current timestamp for delay measurements. More...
 

Private Types

using books_by_security = std::unordered_map< stock_t, order_book< book_type >, boost::hash< stock_t > >
 Represent the collection of order books. More...
 
using orders_by_id = std::unordered_map< std::uint64_t, order_data >
 Represent the collection of all orders. More...
 
using orders_iterator = typename orders_by_id::iterator
 

Private Member Functions

void handle_order_reduction (time_point recvts, long msgcnt, std::size_t msgoffset, message_header const &header, std::uint64_t order_reference_number, std::uint32_t shares)
 Refactor code to handle order reductions, i.e., cancels and executions. More...
 
book_update do_reduce (orders_iterator position, order_book< book_type > &book, time_point recvts, long msgcnt, std::size_t msgoffset, message_header const &header, std::uint64_t order_reference_number, std::uint32_t shares)
 Refactor code common to handle_order_reduction() and handle_message(order_replace_message). More...
 

Private Attributes

callback_type callback_
 Store the callback function, this is invoked on each event that changes a book. More...
 
books_by_security books_
 The order books indexed by security. More...
 
orders_by_id orders_
 The live orders indexed by the "order reference number". More...
 
book_type_config const & cfg_
 reference to the order book config More...
 

Detailed Description

template<typename book_type>
class jb::itch5::compute_book< book_type >

Compute the book and call a user-defined callback on each change.

Keep a collection of all the order books, indexed by symbol, and forward the updates to them. Only process the relevant messages types in ITCH-5.0 that are necessary to keep the book.

Template Parameters
book_typethe type used to define order_book<book_type>, must be compatible with jb::itch5::map_price

Definition at line 107 of file compute_book.hpp.

Member Typedef Documentation

◆ book_type_config

template<typename book_type>
using jb::itch5::compute_book< book_type >::book_type_config = typename book_type::config

config type is used to construct the order_book

Definition at line 122 of file compute_book.hpp.

◆ books_by_security

template<typename book_type>
using jb::itch5::compute_book< book_type >::books_by_security = std::unordered_map<stock_t, order_book<book_type>, boost::hash<stock_t> >
private

Represent the collection of order books.

Definition at line 402 of file compute_book.hpp.

◆ callback_type

template<typename book_type>
using jb::itch5::compute_book< book_type >::callback_type = std::function<void( message_header const& header, order_book<book_type> const& updated_book, book_update const& update)>

Define the callback type.

A callback of this type is required in the constructor of this class. After each book update the user-provided callback is invoked.

Parameters
headerthe header of the raw ITCH-5.0 message
updatea representation of the update just applied to the book
updated_bookthe order_book after the update was applied

Definition at line 138 of file compute_book.hpp.

◆ clock_type

template<typename book_type>
using jb::itch5::compute_book< book_type >::clock_type = jb::itch5::clock_type

clock_type is used as a compute_book<book_type> type in some modules

Definition at line 115 of file compute_book.hpp.

◆ orders_by_id

template<typename book_type>
using jb::itch5::compute_book< book_type >::orders_by_id = std::unordered_map<std::uint64_t, order_data>
private

Represent the collection of all orders.

Definition at line 405 of file compute_book.hpp.

◆ orders_iterator

template<typename book_type>
using jb::itch5::compute_book< book_type >::orders_iterator = typename orders_by_id::iterator
private

Definition at line 406 of file compute_book.hpp.

◆ time_point

template<typename book_type>
using jb::itch5::compute_book< book_type >::time_point = jb::itch5::time_point

time_point is used as a compute_book<book_type> type in some modules

Definition at line 119 of file compute_book.hpp.

Constructor & Destructor Documentation

◆ compute_book() [1/2]

template<typename book_type>
jb::itch5::compute_book< book_type >::compute_book ( callback_type &&  cb,
book_type_config const &  cfg 
)
inlineexplicit

Constructor.

Definition at line 142 of file compute_book.hpp.

◆ compute_book() [2/2]

template<typename book_type>
jb::itch5::compute_book< book_type >::compute_book ( callback_type const &  cb,
book_type_config const &  cfg 
)
inlineexplicit

Definition at line 149 of file compute_book.hpp.

Member Function Documentation

◆ do_reduce()

template<typename book_type>
book_update jb::itch5::compute_book< book_type >::do_reduce ( orders_iterator  position,
order_book< book_type > &  book,
time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
message_header const &  header,
std::uint64_t  order_reference_number,
std::uint32_t  shares 
)
inlineprivate

Refactor code common to handle_order_reduction() and handle_message(order_replace_message).

Parameters
positionthe location of the order matching order_reference_number
bookthe order_book matching the symbol for the given order
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
headerthe header of the message that triggered this event
order_reference_numberthe id of the order being reduced
sharesthe number of shares to reduce, if 0 reduce all shares

Definition at line 460 of file compute_book.hpp.

References jb::itch5::order_book< book_type >::handle_order_reduced(), JB_LOG, jb::itch5::book_update::qty, jb::itch5::book_update::recvts, and jb::warning.

◆ handle_message() [1/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
add_order_message const &  msg 
)
inline

Handle a new order message.

New orders are added to the list of known orders and their qty is added to the right book at the order's price level.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing a new order

Definition at line 164 of file compute_book.hpp.

References JB_LOG, jb::trace, and jb::warning.

◆ handle_message() [2/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
add_order_mpid_message const &  msg 
)
inline

Handle a new order with MPID.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing a new order

Definition at line 206 of file compute_book.hpp.

◆ handle_message() [3/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
order_executed_message const &  msg 
)
inline

Handle an order execution.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing the execution

Definition at line 222 of file compute_book.hpp.

References JB_LOG, and jb::trace.

◆ handle_message() [4/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
order_executed_price_message const &  msg 
)
inline

Handle an order execution with a different price than the order's.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing the execution

Definition at line 239 of file compute_book.hpp.

◆ handle_message() [5/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
order_cancel_message const &  msg 
)
inline

Handle a partial cancel.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing the cancelation

Definition at line 256 of file compute_book.hpp.

References JB_LOG, and jb::trace.

◆ handle_message() [6/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
order_delete_message const &  msg 
)
inline

Handle a full cancel.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing the cancelation

Definition at line 273 of file compute_book.hpp.

References JB_LOG, and jb::trace.

◆ handle_message() [7/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
order_replace_message const &  msg 
)
inline

Handle an order replace.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing the cancel/replace

Definition at line 289 of file compute_book.hpp.

References JB_ASSERT_THROW, JB_LOG, jb::trace, and jb::warning.

◆ handle_message() [8/9]

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_message ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
stock_directory_message const &  msg 
)
inline

Pre-populate the books based on the symbol directory.

ITCH-5.0 sends the list of expected securities to be traded on a given day as a sequence of messages. We use these messages to pre-populate the map of books and avoid hash map updates during the critical path.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
msgthe message describing a known symbol for the feed

Definition at line 354 of file compute_book.hpp.

References JB_LOG, and jb::trace.

◆ handle_message() [9/9]

template<typename book_type>
template<typename message_type >
void jb::itch5::compute_book< book_type >::handle_message ( time_point  ,
long  ,
std::size_t  ,
message_type const &   
)
inline

Ignore all other message types.

We are only interested in a handful of message types, anything else is captured by this template function and ignored.

Definition at line 369 of file compute_book.hpp.

◆ handle_order_reduction()

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_order_reduction ( time_point  recvts,
long  msgcnt,
std::size_t  msgoffset,
message_header const &  header,
std::uint64_t  order_reference_number,
std::uint32_t  shares 
)
inlineprivate

Refactor code to handle order reductions, i.e., cancels and executions.

Parameters
recvtsthe timestamp when the message was received
msgcntthe number of messages received before this message
msgoffsetthe number of bytes received before this message
headerthe header of the message that triggered this event
order_reference_numberthe id of the order being reduced
sharesthe number of shares to reduce, if 0 reduce all shares

Definition at line 419 of file compute_book.hpp.

References JB_ASSERT_THROW, JB_LOG, and jb::warning.

◆ handle_unknown()

template<typename book_type>
void jb::itch5::compute_book< book_type >::handle_unknown ( time_point  recvts,
unknown_message const &  msg 
)
inline

Log any unknown message types.

Parameters
recvtsthe timestamp when the message was received
msgthe unknown message location and contents

Definition at line 378 of file compute_book.hpp.

References jb::itch5::unknown_message::buf(), jb::itch5::unknown_message::count(), jb::error, JB_LOG, and jb::itch5::unknown_message::offset().

◆ now()

template<typename book_type>
time_point jb::itch5::compute_book< book_type >::now ( ) const
inline

Return the current timestamp for delay measurements.

Definition at line 395 of file compute_book.hpp.

◆ symbols()

template<typename book_type>
std::vector<stock_t> jb::itch5::compute_book< book_type >::symbols ( ) const
inline

Return the symbols known in the order book.

Definition at line 386 of file compute_book.hpp.

Member Data Documentation

◆ books_

template<typename book_type>
books_by_security jb::itch5::compute_book< book_type >::books_
private

The order books indexed by security.

Definition at line 496 of file compute_book.hpp.

◆ callback_

template<typename book_type>
callback_type jb::itch5::compute_book< book_type >::callback_
private

Store the callback function, this is invoked on each event that changes a book.

Definition at line 493 of file compute_book.hpp.

◆ cfg_

template<typename book_type>
book_type_config const& jb::itch5::compute_book< book_type >::cfg_
private

reference to the order book config

Definition at line 502 of file compute_book.hpp.

◆ orders_

template<typename book_type>
orders_by_id jb::itch5::compute_book< book_type >::orders_
private

The live orders indexed by the "order reference number".

Definition at line 499 of file compute_book.hpp.


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