Leka
A low-latency C++20 price-time-priority limit order book and matching engine
▶ Replay viewer
Loading...
Searching...
No Matches
price_level.cpp
1#include "lob/book/price_level.hpp"
2
3#include <limits>
4#include <stdexcept>
5
6namespace lob {
7
8/** Appends an eligible order and updates FIFO and aggregate state. */
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. */
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() -
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. */
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
78bool PriceLevel::isEmpty() const {
79 return count == 0;
80}
81
82} // namespace lob
Represents a resting or incoming order and its lifecycle state.
Definition order.hpp:17
Order * getPreviousOrder() const
Returns the previous order in this price level's FIFO, or nullptr at the head.
Definition order.cpp:34
void setNextOrder(Order *nextOrder)
Sets the next order in the FIFO.
Definition order.cpp:33
Order * getNextOrder() const
Returns the next order in this price level's FIFO, or nullptr at the tail.
Definition order.cpp:32
void setPreviousOrder(Order *previousOrder)
Sets the previous order in the FIFO.
Definition order.cpp:35
Price getPrice() const
Returns the limit or reference price.
Definition order.cpp:24
PriceLevel * getPriceLevel() const
Returns the price level this order rests on, or nullptr when unlinked.
Definition order.cpp:36
bool isFullyFilled() const
Returns true when remaining quantity is zero.
Definition order.cpp:49
Quantity getRemainingQuantity() const
Returns the quantity that has not yet executed.
Definition order.cpp:26
void setPriceLevel(PriceLevel *priceLevel)
Sets the owning price level; nullptr marks the order as unlinked.
Definition order.cpp:37
void reduceTotalQuantity(Quantity quantity)
Decreases aggregate quantity after a partial execution.
void removeOrder(Order *order)
Unlinks an order and updates aggregate state.
bool isEmpty() const
Returns true when no orders are resting at this price.
void addOrder(Order *order)
Appends an order to the FIFO and updates aggregate state.
Type-safe unsigned order quantity; zero represents a filled state.
Definition quantity.hpp:8
std::uint64_t getQuantity() const
Returns the underlying numeric quantity.
Definition quantity.hpp:20