JayBeams  0.1
Another project to have fun coding.
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
jb::itch5::array_based_book_side< compare_t > Class Template Reference

Represent one side of the book. More...

#include <array_based_order_book.hpp>

Classes

struct  side
 template specialization struct to handle differences between BUY and SELL version SELL side. More...
 
struct  side< std::greater< std::size_t >, DUMMY >
 version BUY side More...
 

Public Member Functions

 array_based_book_side (array_based_order_book::config const &cfg)
 Constructor, initialize a book side from its configuration. More...
 
half_quote best_quote () const
 
half_quote worst_quote () const
 
std::size_t count () const
 
bool add_order (price4_t px, int qty)
 Add a price and quantity to the side order book. More...
 
bool reduce_order (price4_t px, int qty)
 Reduce the quantity for a given price. More...
 
bool is_ascending () const
 Testing hook. More...
 

Static Public Member Functions

static std::size_t price_levels_empty_quote ()
 Return the number of price levels for the empty quote. More...
 

Static Public Attributes

static std::size_t const tk_empty_quote
 Cache the number of levels for the empty quote. More...
 

Private Member Functions

std::size_t relative_worst_top_level () const
 
std::size_t top_levels_count () const
 
void move_top_to_bottom (std::size_t const tk_max)
 Move prices from tk_begin_top_ to tk_max (excluded) out of top_levels_ to bottom_levels_. More...
 
void move_top_to_bottom_ranged (std::size_t N)
 Move the bottom N levels from the top vector to the bottom map. More...
 
void move_bottom_to_top ()
 Move relative prices from best price to tk_begin_top* (included) out of bottom_levels_ to top_levels_. More...
 
std::size_t next_best_price_level () const
 

Static Private Member Functions

static auto get_limits (std::size_t const tk_px, std::size_t const rel)
 

Private Attributes

std::size_t max_size_
 top_levels_ max size More...
 
std::vector< int > top_levels_
 the best relative prices and quantity More...
 
std::map< std::size_t, int, compare_t > bottom_levels_
 the worst (tail) price level and quantity More...
 
std::size_t tk_inside_
 price level the inside More...
 
std::size_t tk_begin_top_
 worst price level on the top_levels_ range More...
 
std::size_t tk_end_top_
 one price level past-the-best price in top_levels_ range More...
 

Detailed Description

template<typename compare_t>
class jb::itch5::array_based_book_side< compare_t >

Represent one side of the book.

This implementation uses a top_levels_ fix sized vector<price4_t> to keep the prices around the inside. We are hoping this makes a faster order book side (instead of map based).

We have observed that most of the changes to an order book happen in the levels closer to the inside. We are trying to exploit this observation by using a vector<> for the N levels closer to the inside, as updates in a vector should be O(1) instead of O(log N) for tree-based implementations, and O(1) (with a larger constant) for hash-based implementations. This introduces some complexity: maintaining all the levels in the vector would consume too much memory, so we use a vector for the N levels closer to the inside, and a map for all the other levels.

Template Parameters
compare_tfunction object class type to sort the side

Definition at line 78 of file array_based_order_book.hpp.

Constructor & Destructor Documentation

◆ array_based_book_side()

template<typename compare_t >
jb::itch5::array_based_book_side< compare_t >::array_based_book_side ( array_based_order_book::config const &  cfg)
inlineexplicit

Constructor, initialize a book side from its configuration.

Definition at line 131 of file array_based_order_book.hpp.

Member Function Documentation

◆ add_order()

template<typename compare_t >
bool jb::itch5::array_based_book_side< compare_t >::add_order ( price4_t  px,
int  qty 
)
inline

Add a price and quantity to the side order book.

  • if px is worse than the px_begin_top_ then px goes to bottom_levels
  • if px is better than the inside then changes inside, redefines limits if needed (moving the tail to bottom_levels if any)
  • finally, updates qty at the relative(px)
Parameters
pxthe price of the new order
qtythe quantity of the new order
Returns
true if the inside changed
Exceptions
feed_errorpx out of valid range, or qty <= 0

Definition at line 192 of file array_based_order_book.hpp.

References jb::itch5::price_levels(), and jb::itch5::detail::validate_operation_params().

Referenced by BOOST_AUTO_TEST_CASE().

◆ best_quote()

template<typename compare_t >
half_quote jb::itch5::array_based_book_side< compare_t >::best_quote ( ) const
inline
Returns
the best bid price and quantity. The inside is always at the top_levels_

Definition at line 142 of file array_based_order_book.hpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count()

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::count ( ) const
inline
Returns
the number of levels with non-zero quantity for the order side. This is the size of bottom_levels_ plus all non zero top_levels values

Definition at line 175 of file array_based_order_book.hpp.

Referenced by BOOST_AUTO_TEST_CASE().

◆ get_limits()

template<typename compare_t >
static auto jb::itch5::array_based_book_side< compare_t >::get_limits ( std::size_t const  tk_px,
std::size_t const  rel 
)
inlinestaticprivate
Returns
pair of price levels that are rel worse and better than tk_px, limited to valid price levels if tk_px is at the limit, return and empty quote values

Definition at line 509 of file array_based_order_book.hpp.

References jb::itch5::empty_offer_price(), and jb::itch5::price_levels().

◆ is_ascending()

template<typename compare_t >
bool jb::itch5::array_based_book_side< compare_t >::is_ascending ( ) const
inline

Testing hook.

Returns
true is side is ascending To test different implementations for buy and sell sides

Definition at line 333 of file array_based_order_book.hpp.

◆ move_bottom_to_top()

template<typename compare_t >
void jb::itch5::array_based_book_side< compare_t >::move_bottom_to_top ( )
inlineprivate

Move relative prices from best price to tk_begin_top* (included) out of bottom_levels_ to top_levels_.

*tk_begin_top_ is updated before calling this function.

Definition at line 464 of file array_based_order_book.hpp.

◆ move_top_to_bottom()

template<typename compare_t >
void jb::itch5::array_based_book_side< compare_t >::move_top_to_bottom ( std::size_t const  tk_max)
inlineprivate

Move prices from tk_begin_top_ to tk_max (excluded) out of top_levels_ to bottom_levels_.

Parameters
tk_maxfirst limit price level that is not moved out.

Inserts the prices into bottom_levels_ Shift top_levels_ prices in order for rel_max to become relative 0 Clear (value = 0) former relative position of moved prices

Definition at line 394 of file array_based_order_book.hpp.

References JB_ASSERT_THROW.

◆ move_top_to_bottom_ranged()

template<typename compare_t >
void jb::itch5::array_based_book_side< compare_t >::move_top_to_bottom_ranged ( std::size_t  N)
inlineprivate

Move the bottom N levels from the top vector to the bottom map.

This function "copies" the bottom N levels from the top vector, and then zeroes out that portion of the vector ...

Parameters
Nthe number of elements to move.

Definition at line 431 of file array_based_order_book.hpp.

◆ next_best_price_level()

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::next_best_price_level ( ) const
inlineprivate
Returns
the best next price level from the inside. (return empty quote price if none available).

Definition at line 489 of file array_based_order_book.hpp.

◆ price_levels_empty_quote()

template<typename compare_t >
static std::size_t jb::itch5::array_based_book_side< compare_t >::price_levels_empty_quote ( )
inlinestatic

Return the number of price levels for the empty quote.

This is mostly used to initialize tk_empty_quote, because otherwise the template definitions get too gnarly.

Definition at line 351 of file array_based_order_book.hpp.

References jb::itch5::price_levels().

◆ reduce_order()

template<typename compare_t >
bool jb::itch5::array_based_book_side< compare_t >::reduce_order ( price4_t  px,
int  qty 
)
inline

Reduce the quantity for a given price.

Parameters
pxthe price of the order that was reduced
qtythe quantity reduced in the order
Returns
true if the inside changed
Exceptions
feed_errorqty <= 0
feed_errorpx should be in bottom_levels_ but is empty
feed_errortrying to reduce a non-existing bottom_levels price
feed_errortrying to reduce a non-existing top_levels price
feed_errortrying to reduce a price better than px_inside_

Checks and handles if px is a bottom_level_ price Checks if top_levels_ is empty after reducing If so, limits have to be redefined, and tail moved into top_levels

Definition at line 249 of file array_based_order_book.hpp.

References JB_LOG, jb::itch5::price_levels(), jb::itch5::detail::raise_invalid_reduce(), jb::itch5::detail::validate_operation_params(), and jb::warning.

Referenced by BOOST_AUTO_TEST_CASE().

◆ relative_worst_top_level()

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::relative_worst_top_level ( ) const
inlineprivate
Returns
relative position of the worst valid price at top_levels.
Exceptions
feed_errortop_levels_ is empty.

Definition at line 360 of file array_based_order_book.hpp.

◆ top_levels_count()

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::top_levels_count ( ) const
inlineprivate
Returns
number of valid prices (>0) at top_levels_

Definition at line 369 of file array_based_order_book.hpp.

◆ worst_quote()

template<typename compare_t >
half_quote jb::itch5::array_based_book_side< compare_t >::worst_quote ( ) const
inline
Returns
the worst bid price and quantity. Check if the side is empty first (return empty_quote in this case) The worst price is at the bottom_levels (if not empty), at the top_levels otherwise.

Definition at line 155 of file array_based_order_book.hpp.

Referenced by BOOST_AUTO_TEST_CASE().

Member Data Documentation

◆ bottom_levels_

template<typename compare_t >
std::map<std::size_t, int, compare_t> jb::itch5::array_based_book_side< compare_t >::bottom_levels_
private

the worst (tail) price level and quantity

Definition at line 619 of file array_based_order_book.hpp.

◆ max_size_

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::max_size_
private

top_levels_ max size

Definition at line 613 of file array_based_order_book.hpp.

◆ tk_begin_top_

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::tk_begin_top_
private

worst price level on the top_levels_ range

Definition at line 625 of file array_based_order_book.hpp.

◆ tk_empty_quote

template<typename compare_t >
std::size_t const jb::itch5::array_based_book_side< compare_t >::tk_empty_quote
static
Initial value:

Cache the number of levels for the empty quote.

This value is used numerous times in the critical path, and we want to avoid recomputing it over and over.

Definition at line 343 of file array_based_order_book.hpp.

◆ tk_end_top_

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::tk_end_top_
private

one price level past-the-best price in top_levels_ range

Definition at line 628 of file array_based_order_book.hpp.

◆ tk_inside_

template<typename compare_t >
std::size_t jb::itch5::array_based_book_side< compare_t >::tk_inside_
private

price level the inside

Definition at line 622 of file array_based_order_book.hpp.

◆ top_levels_

template<typename compare_t >
std::vector<int> jb::itch5::array_based_book_side< compare_t >::top_levels_
private

the best relative prices and quantity

Definition at line 616 of file array_based_order_book.hpp.


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