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
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
18
namespace
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
*/
29
enum class
OrderEventType
{
30
NEW,
31
CANCEL,
32
REDUCE
33
};
34
35
/** @brief Payload for accepting a new order. */
36
struct
NewOrder
{
37
/** Unique identifier for the new order. */
38
OrderId
orderId
;
39
/** Limit price, or an invalid price for a market order. */
40
Price
price
;
41
/** Quantity submitted by the caller. */
42
Quantity
quantity
;
43
/** Timestamp assigned to the order. */
44
Timestamp
timestamp
;
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. */
52
struct
CancelOrder
{
53
/** Identifier of the order to remove. */
54
OrderId
orderId
;
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
*/
64
struct
ReduceOrder
{
65
/** Identifier of the order to shrink. */
66
OrderId
orderId
;
67
/** Replacement remaining quantity; nonzero and not above the current one. */
68
Quantity
newQuantity
;
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
*/
77
class
OrderEvent
{
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. */
87
OrderEventType
getEventType
()
const
;
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
lob::OrderEvent::getNewOrder
const NewOrder & getNewOrder() const
Returns the NEW payload or throws when this is not a NEW event.
Definition
order_event.cpp:32
lob::OrderEvent::OrderEvent
OrderEvent(NewOrder order)
Creates a NEW event.
Definition
order_event.cpp:14
lob::OrderEvent::getCancelOrder
const CancelOrder & getCancelOrder() const
Returns the CANCEL payload or throws when this is not a CANCEL event.
Definition
order_event.cpp:41
lob::OrderEvent::getReduceOrder
const ReduceOrder & getReduceOrder() const
Returns the REDUCE payload or throws when this is not a REDUCE event.
Definition
order_event.cpp:50
lob::OrderEvent::getEventType
OrderEventType getEventType() const
Returns the operation represented by the active payload.
Definition
order_event.cpp:23
lob::OrderId
Type-safe identifier for an order; zero is reserved as invalid.
Definition
order_id.hpp:10
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
lob::Timestamp
Nanoseconds since the Unix epoch; zero is reserved as invalid.
Definition
timestamp.hpp:10
lob::OrderEventType
OrderEventType
Operation selected before an event payload is interpreted.
Definition
order_event.hpp:29
lob::CancelOrder
Payload for removing an existing resting order.
Definition
order_event.hpp:52
lob::CancelOrder::orderId
OrderId orderId
Identifier of the order to remove.
Definition
order_event.hpp:54
lob::NewOrder
Payload for accepting a new order.
Definition
order_event.hpp:36
lob::NewOrder::quantity
Quantity quantity
Quantity submitted by the caller.
Definition
order_event.hpp:42
lob::NewOrder::orderType
OrderType orderType
Limit or market execution behavior.
Definition
order_event.hpp:48
lob::NewOrder::orderSide
OrderSide orderSide
Buy or sell direction.
Definition
order_event.hpp:46
lob::NewOrder::timestamp
Timestamp timestamp
Timestamp assigned to the order.
Definition
order_event.hpp:44
lob::NewOrder::orderId
OrderId orderId
Unique identifier for the new order.
Definition
order_event.hpp:38
lob::NewOrder::price
Price price
Limit price, or an invalid price for a market order.
Definition
order_event.hpp:40
lob::ReduceOrder
Payload for shrinking a resting order without losing priority.
Definition
order_event.hpp:64
lob::ReduceOrder::newQuantity
Quantity newQuantity
Replacement remaining quantity; nonzero and not above the current one.
Definition
order_event.hpp:68
lob::ReduceOrder::orderId
OrderId orderId
Identifier of the order to shrink.
Definition
order_event.hpp:66
include
lob
order
order_event.hpp
Generated by
1.18.0