Leka
A low-latency C++20 price-time-priority limit order book and matching engine
▶ Replay viewer
Loading...
Searching...
No Matches
order_event.hpp
Go to the documentation of this file.
1#ifndef ORDER_EVENT_HPP
2#define ORDER_EVENT_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/timestamp.hpp"
10
11#include <variant>
12
13/**
14 * @file
15 * @brief Defines event-first order command payloads and dispatch types.
16 */
17
18namespace lob {
19
20/**
21 * @brief Operation selected before an event payload is interpreted.
22 *
23 * These are the three primitives a price-time-priority venue actually
24 * exposes. There is deliberately no in-place modify: a reprice or a size
25 * increase always forfeits time priority, so it is expressed as CANCEL
26 * followed by NEW, which also routes it through the matcher. REDUCE is the
27 * only change that keeps a resting order's queue position.
28 */
29enum class OrderEventType {
30 NEW,
31 CANCEL,
32 REDUCE
33};
34
35/** @brief Payload for accepting a new order. */
36struct NewOrder {
37 /** Unique identifier for the new order. */
39 /** Limit price, or an invalid price for a market order. */
41 /** Quantity submitted by the caller. */
43 /** Timestamp assigned to the order. */
45 /** Buy or sell direction. */
46 OrderSide orderSide;
47 /** Limit or market execution behavior. */
48 OrderType orderType;
49};
50
51/** @brief Payload for removing an existing resting order. */
53 /** Identifier of the order to remove. */
55};
56
57/**
58 * @brief Payload for shrinking a resting order without losing priority.
59 *
60 * The price cannot change, so the order never moves between levels and keeps
61 * its FIFO position. A reduction to zero is a cancellation and must be sent
62 * as CancelOrder instead.
63 */
65 /** Identifier of the order to shrink. */
67 /** Replacement remaining quantity; nonzero and not above the current one. */
69};
70
71/**
72 * @brief Type-safe event-first command for the matching engine.
73 *
74 * The active payload determines which fields are relevant. Accessing a
75 * payload for a different event type throws std::logic_error.
76 */
78 public:
79 /** Creates a NEW event. */
80 explicit OrderEvent(NewOrder order);
81 /** Creates a CANCEL event. */
82 explicit OrderEvent(CancelOrder order);
83 /** Creates a REDUCE event. */
84 explicit OrderEvent(ReduceOrder order);
85
86 /** Returns the operation represented by the active payload. */
88 /** Returns the NEW payload or throws when this is not a NEW event. */
89 const NewOrder& getNewOrder() const;
90 /** Returns the CANCEL payload or throws when this is not a CANCEL event. */
91 const CancelOrder& getCancelOrder() const;
92 /** Returns the REDUCE payload or throws when this is not a REDUCE event. */
93 const ReduceOrder& getReduceOrder() const;
94
95 private:
96 std::variant<NewOrder, CancelOrder, ReduceOrder> payload;
97};
98
99} // namespace lob
100
101#endif // ORDER_EVENT_HPP
const NewOrder & getNewOrder() const
Returns the NEW payload or throws when this is not a NEW event.
OrderEvent(NewOrder order)
Creates a NEW event.
const CancelOrder & getCancelOrder() const
Returns the CANCEL payload or throws when this is not a CANCEL event.
const ReduceOrder & getReduceOrder() const
Returns the REDUCE payload or throws when this is not a REDUCE event.
OrderEventType getEventType() const
Returns the operation represented by the active payload.
Type-safe identifier for an order; zero is reserved as invalid.
Definition order_id.hpp:10
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
Nanoseconds since the Unix epoch; zero is reserved as invalid.
Definition timestamp.hpp:10
OrderEventType
Operation selected before an event payload is interpreted.
Payload for removing an existing resting order.
OrderId orderId
Identifier of the order to remove.
Payload for accepting a new order.
Quantity quantity
Quantity submitted by the caller.
OrderType orderType
Limit or market execution behavior.
OrderSide orderSide
Buy or sell direction.
Timestamp timestamp
Timestamp assigned to the order.
OrderId orderId
Unique identifier for the new order.
Price price
Limit price, or an invalid price for a market order.
Payload for shrinking a resting order without losing priority.
Quantity newQuantity
Replacement remaining quantity; nonzero and not above the current one.
OrderId orderId
Identifier of the order to shrink.