Leka
A low-latency C++20 price-time-priority limit order book and matching engine
▶ Replay viewer
Loading...
Searching...
No Matches
order.hpp
1#ifndef ORDER_HPP
2#define ORDER_HPP
3
4#include "lob/order/order_side.hpp"
5#include "lob/order/order_type.hpp"
6#include "lob/types/order_id.hpp"
7#include "lob/types/price.hpp"
8#include "lob/types/quantity.hpp"
9#include "lob/types/sequence_number.hpp"
10#include "lob/types/timestamp.hpp"
11
12namespace lob {
13
14class PriceLevel;
15
16/** @brief Represents a resting or incoming order and its lifecycle state. */
17class Order {
18 private:
19 OrderId orderId;
20 Price price;
21 Quantity originalQuantity;
22 Quantity remainingQuantity;
23 Timestamp timestamp;
24 OrderSide orderSide;
25 OrderType orderType;
26 SequenceNumber sequenceNumber;
27 Order* nextOrder{nullptr};
28 Order* previousOrder{nullptr};
29 PriceLevel* priceLevel{nullptr};
30
31 public:
32 /** Constructs an order with remaining quantity equal to the original quantity. */
33 Order(OrderId orderId, Price price, Quantity originalQuantity,
34 Timestamp timestamp, OrderSide orderSide, OrderType orderType,
35 SequenceNumber sequenceNumber);
36 /** Constructs an order with explicit original and remaining quantities. */
37 Order(OrderId orderId, Price price, Quantity originalQuantity,
38 Quantity remainingQuantity, Timestamp timestamp, OrderSide orderSide,
39 OrderType orderType, SequenceNumber sequenceNumber);
40
41 /** Returns the unique order identifier. */
42 OrderId getOrderId() const;
43 /** Returns the limit or reference price. */
44 Price getPrice() const;
45 /** Returns the quantity submitted when the order was created. */
47 /** Returns the quantity that has not yet executed. */
49 /** Returns the order timestamp. */
50 Timestamp getTimestamp() const;
51 /** Returns whether this is a buy or sell order. */
52 OrderSide getOrderSide() const;
53 /** Returns the order type. */
54 OrderType getOrderType() const;
55 /** Returns the deterministic processing sequence. */
57
58 /**
59 * @name Intrusive FIFO links
60 *
61 * The next/previous pointers live inside Order itself rather than in
62 * a separate list node, so joining or leaving a PriceLevel's queue
63 * allocates nothing and unlinking a *known* order is O(1). The
64 * priceLevel back-pointer is what makes cancellation O(1) end to
65 * end: OrderIndex resolves an OrderId to this Order, and the order
66 * already knows which level to unlink itself from, with no search.
67 *
68 * These are maintained by PriceLevel and OrderBook; callers outside
69 * the book should treat them as read-only.
70 * @{
71 */
72 /** Returns the next order in this price level's FIFO, or nullptr at the tail. */
73 Order* getNextOrder() const;
74 /** Sets the next order in the FIFO. Called by PriceLevel during linking. */
75 void setNextOrder(Order* nextOrder);
76 /** Returns the previous order in this price level's FIFO, or nullptr at the head. */
77 Order* getPreviousOrder() const;
78 /** Sets the previous order in the FIFO. Called by PriceLevel during linking. */
79 void setPreviousOrder(Order* previousOrder);
80 /** Returns the price level this order rests on, or nullptr when unlinked. */
82 /** Sets the owning price level; nullptr marks the order as unlinked. */
83 void setPriceLevel(PriceLevel* priceLevel);
84 /** @} */
85
86 /** Decreases remaining quantity; throws if the reduction would underflow. */
87 void reduceRemainingQuantity(Quantity quantity);
88 /** Returns true when remaining quantity is zero. */
89 bool isFullyFilled() const;
90 /** Returns whether the order state satisfies its structural invariants. */
91 bool isValid() const;
92 /** Updates quantity while the order remains at its current price level. */
93 void setRemainingQuantity(Quantity quantity);
94 /** Returns true for a buy order. */
95 bool isBuy() const;
96 /** Returns true for a sell order. */
97 bool isSell() const;
98 /** Returns true for a limit order. */
99 bool isLimit() const;
100 /** Returns true for a market order. */
101 bool isMarket() const;
102};
103
104} // namespace lob
105
106#endif // ORDER_HPP
Type-safe identifier for an order; zero is reserved as invalid.
Definition order_id.hpp:10
Order(OrderId orderId, Price price, Quantity originalQuantity, Timestamp timestamp, OrderSide orderSide, OrderType orderType, SequenceNumber sequenceNumber)
Constructs an order with remaining quantity equal to the original quantity.
Definition order.cpp:8
Quantity getOriginalQuantity() const
Returns the quantity submitted when the order was created.
Definition order.cpp:25
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
SequenceNumber getSequenceNumber() const
Returns the deterministic processing sequence.
Definition order.cpp:30
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
OrderId getOrderId() const
Returns the unique order identifier.
Definition order.cpp:23
OrderSide getOrderSide() const
Returns whether this is a buy or sell order.
Definition order.cpp:28
Price getPrice() const
Returns the limit or reference price.
Definition order.cpp:24
OrderType getOrderType() const
Returns the order type.
Definition order.cpp:29
bool isSell() const
Returns true for a sell order.
Definition order.cpp:73
PriceLevel * getPriceLevel() const
Returns the price level this order rests on, or nullptr when unlinked.
Definition order.cpp:36
bool isValid() const
Returns whether the order state satisfies its structural invariants.
Definition order.cpp:52
Timestamp getTimestamp() const
Returns the order timestamp.
Definition order.cpp:27
void reduceRemainingQuantity(Quantity quantity)
Decreases remaining quantity; throws if the reduction would underflow.
Definition order.cpp:40
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 setRemainingQuantity(Quantity quantity)
Updates quantity while the order remains at its current price level.
Definition order.cpp:62
bool isMarket() const
Returns true for a market order.
Definition order.cpp:75
void setPriceLevel(PriceLevel *priceLevel)
Sets the owning price level; nullptr marks the order as unlinked.
Definition order.cpp:37
bool isLimit() const
Returns true for a limit order.
Definition order.cpp:74
bool isBuy() const
Returns true for a buy order.
Definition order.cpp:72
Maintains FIFO resting orders at one price.
Type-safe nonzero price value used for price ordering.
Definition price.hpp:9
Type-safe unsigned order quantity; zero represents a filled state.
Definition quantity.hpp:8
Monotonic engine sequence used for deterministic processing order.
Nanoseconds since the Unix epoch; zero is reserved as invalid.
Definition timestamp.hpp:10