Skip to content
Daniel Esambu
Work
Work
Projects
Things I've built
Leka
A C++20 matching engine
Latency Lab
(opens in a new tab)
A C++ course I wrote
Resume
The formal version
Writing
Writing
Poetry
Poems, shelved by what they're about
Essays
Notes and essays
Math
Math
Notes
Notes I've written up
IB papers
Worked exam questions
Calculus lessons
(opens in a new tab)
On YouTube
Life
Life
Photos
Things I stopped to look at
Training
Reps, not phases
Modeling
Measurements and photos
Resume
Say hi
Say hi
Resume
Menu
Leka
A low-latency C++20 price-time-priority limit order book and matching engine
Replay viewer
Toggle main menu visibility
Loading...
Searching...
No Matches
price_level.hpp
1
#ifndef PRICE_LEVEL_HPP
2
#define PRICE_LEVEL_HPP
3
#include "lob/types/price.hpp"
4
#include "lob/order/order.hpp"
5
#include "lob/types/quantity.hpp"
6
#include <cstddef>
7
8
namespace
lob {
9
/**
10
* @brief Maintains FIFO resting orders at one price.
11
*
12
* Orders are linked intrusively, so appending and removing a known order are
13
* constant-time operations. The level also tracks order count and aggregate
14
* remaining quantity.
15
*/
16
class
PriceLevel
{
17
private
:
18
Price
price;
19
Order
* headOrder{
nullptr
};
20
Order
* tailOrder{
nullptr
};
21
22
std::size_t count{0};
// Counts of orders we currently have - init at 0
23
24
Quantity
totalQuantity{0};
// Total quantity of orders at this price level - init at 0
25
26
public
:
27
/** Creates an empty level for a single price. */
28
explicit
PriceLevel
(
Price
price) : price(price) {}
29
30
/** Returns the level price. */
31
Price
getPrice
()
const
{
return
price; }
32
/** Returns the first resting order, or nullptr when empty. */
33
Order
*
getHeadOrder
()
const
{
return
headOrder; }
34
/** Returns the last resting order, or nullptr when empty. */
35
Order
*
getTailOrder
()
const
{
return
tailOrder; }
36
/** Returns the number of resting orders. */
37
std::size_t
getOrderCount
()
const
{
return
count; }
38
/** Returns the aggregate remaining quantity. */
39
Quantity
getTotalQuantity
()
const
{
return
totalQuantity; }
40
41
/** Appends an order to the FIFO and updates aggregate state. */
42
void
addOrder
(
Order
* order);
43
/** Unlinks an order and updates aggregate state. */
44
void
removeOrder
(
Order
* order);
45
46
/** Decreases aggregate quantity after a partial execution. */
47
void
reduceTotalQuantity
(
Quantity
quantity);
48
49
/** Returns true when no orders are resting at this price. */
50
bool
isEmpty
()
const
;
51
};
52
}
// namespace lob
53
#endif
// PRICE_LEVEL_HPP
lob::Order
Represents a resting or incoming order and its lifecycle state.
Definition
order.hpp:17
lob::PriceLevel::PriceLevel
PriceLevel(Price price)
Creates an empty level for a single price.
Definition
price_level.hpp:28
lob::PriceLevel::getTailOrder
Order * getTailOrder() const
Returns the last resting order, or nullptr when empty.
Definition
price_level.hpp:35
lob::PriceLevel::reduceTotalQuantity
void reduceTotalQuantity(Quantity quantity)
Decreases aggregate quantity after a partial execution.
Definition
price_level.cpp:70
lob::PriceLevel::getPrice
Price getPrice() const
Returns the level price.
Definition
price_level.hpp:31
lob::PriceLevel::getHeadOrder
Order * getHeadOrder() const
Returns the first resting order, or nullptr when empty.
Definition
price_level.hpp:33
lob::PriceLevel::getOrderCount
std::size_t getOrderCount() const
Returns the number of resting orders.
Definition
price_level.hpp:37
lob::PriceLevel::removeOrder
void removeOrder(Order *order)
Unlinks an order and updates aggregate state.
Definition
price_level.cpp:41
lob::PriceLevel::isEmpty
bool isEmpty() const
Returns true when no orders are resting at this price.
Definition
price_level.cpp:78
lob::PriceLevel::addOrder
void addOrder(Order *order)
Appends an order to the FIFO and updates aggregate state.
Definition
price_level.cpp:9
lob::PriceLevel::getTotalQuantity
Quantity getTotalQuantity() const
Returns the aggregate remaining quantity.
Definition
price_level.hpp:39
lob::Price
Type-safe nonzero price value used for price ordering.
Definition
price.hpp:9
lob::Quantity
Type-safe unsigned order quantity; zero represents a filled state.
Definition
quantity.hpp:8
include
lob
book
price_level.hpp
Generated by
1.18.0