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.cpp
1
#include "lob/book/price_level.hpp"
2
3
#include <limits>
4
#include <stdexcept>
5
6
namespace
lob {
7
8
/** Appends an eligible order and updates FIFO and aggregate state. */
9
void
PriceLevel::addOrder
(
Order
* order) {
10
if
(order ==
nullptr
) {
11
throw
std::invalid_argument(
"Cannot add a null order"
);
12
}
13
if
((order->
getPrice
() <=> price) != 0) {
14
throw
std::invalid_argument(
"Order price does not match price level"
);
15
}
16
if
(order->
getPriceLevel
() !=
nullptr
) {
17
throw
std::invalid_argument(
"Order already belongs to a price level"
);
18
}
19
if
(order->
isFullyFilled
()) {
20
throw
std::invalid_argument(
"Cannot add a fully filled order"
);
21
}
22
if
(order->
getRemainingQuantity
().
getQuantity
() >
23
std::numeric_limits<std::uint64_t>::max() - totalQuantity.getQuantity()) {
24
throw
std::overflow_error(
"Price level quantity overflow"
);
25
}
26
27
order->
setPreviousOrder
(tailOrder);
// Add the order to the end of the list
28
order->
setNextOrder
(
nullptr
);
// Set the next order to nullptr since it's the last order in the list
29
if
(tailOrder !=
nullptr
) {
30
tailOrder->setNextOrder(order);
31
}
else
{
32
headOrder = order;
33
}
34
tailOrder = order;
35
++count;
36
totalQuantity = totalQuantity + order->
getRemainingQuantity
();
37
order->
setPriceLevel
(
this
);
38
}
39
40
/** Unlinks an order in constant time while it is still alive. */
41
void
PriceLevel::removeOrder
(
Order
* order) {
42
if
(order ==
nullptr
|| order->
getPriceLevel
() !=
this
) {
43
throw
std::invalid_argument(
"Order does not belong to this price level"
);
44
}
45
46
Order
* previousOrder = order->
getPreviousOrder
();
47
Order
* nextOrder = order->
getNextOrder
();
48
// Update the previous and next orders to bypass the removed order
49
if
(previousOrder !=
nullptr
) {
50
previousOrder->
setNextOrder
(nextOrder);
51
}
else
{
52
headOrder = nextOrder;
53
}
54
55
if
(nextOrder !=
nullptr
) {
56
nextOrder->
setPreviousOrder
(previousOrder);
57
}
else
{
58
tailOrder = previousOrder;
59
}
60
61
totalQuantity =
Quantity
{totalQuantity.getQuantity() -
62
order->
getRemainingQuantity
().
getQuantity
()};
63
--count;
64
order->
setPreviousOrder
(
nullptr
);
65
order->
setNextOrder
(
nullptr
);
66
order->
setPriceLevel
(
nullptr
);
67
}
68
69
/** Applies a partial execution to the aggregate quantity. */
70
void
PriceLevel::reduceTotalQuantity
(
Quantity
quantity) {
71
if
(quantity > totalQuantity) {
72
throw
std::invalid_argument(
"Cannot reduce price level quantity below zero"
);
73
}
74
75
totalQuantity =
Quantity
{totalQuantity.getQuantity() - quantity.
getQuantity
()};
76
}
77
78
bool
PriceLevel::isEmpty
()
const
{
79
return
count == 0;
80
}
81
82
}
// namespace lob
lob::Order
Represents a resting or incoming order and its lifecycle state.
Definition
order.hpp:17
lob::Order::getPreviousOrder
Order * getPreviousOrder() const
Returns the previous order in this price level's FIFO, or nullptr at the head.
Definition
order.cpp:34
lob::Order::setNextOrder
void setNextOrder(Order *nextOrder)
Sets the next order in the FIFO.
Definition
order.cpp:33
lob::Order::getNextOrder
Order * getNextOrder() const
Returns the next order in this price level's FIFO, or nullptr at the tail.
Definition
order.cpp:32
lob::Order::setPreviousOrder
void setPreviousOrder(Order *previousOrder)
Sets the previous order in the FIFO.
Definition
order.cpp:35
lob::Order::getPrice
Price getPrice() const
Returns the limit or reference price.
Definition
order.cpp:24
lob::Order::getPriceLevel
PriceLevel * getPriceLevel() const
Returns the price level this order rests on, or nullptr when unlinked.
Definition
order.cpp:36
lob::Order::isFullyFilled
bool isFullyFilled() const
Returns true when remaining quantity is zero.
Definition
order.cpp:49
lob::Order::getRemainingQuantity
Quantity getRemainingQuantity() const
Returns the quantity that has not yet executed.
Definition
order.cpp:26
lob::Order::setPriceLevel
void setPriceLevel(PriceLevel *priceLevel)
Sets the owning price level; nullptr marks the order as unlinked.
Definition
order.cpp:37
lob::PriceLevel::reduceTotalQuantity
void reduceTotalQuantity(Quantity quantity)
Decreases aggregate quantity after a partial execution.
Definition
price_level.cpp:70
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::Quantity
Type-safe unsigned order quantity; zero represents a filled state.
Definition
quantity.hpp:8
lob::Quantity::getQuantity
std::uint64_t getQuantity() const
Returns the underlying numeric quantity.
Definition
quantity.hpp:20
src
book
price_level.cpp
Generated by
1.18.0