FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
order.hpp
Go to the documentation of this file.
1#pragma once
2// Order: the OMS's 128-byte record for one order across its lifetime.
7
8#include <cstdint>
9#include <type_traits>
10
11namespace fastmm {
12
13struct Order {
14 enum Flags : std::uint8_t {
15 kPostOnly = 1U << 0,
16 kReduceOnly = 1U << 1,
17 kUnsolicitedCancel = 1U << 2, // venue cancelled it (not requested by us)
18 kReconciled = 1U << 3, // state fixed up by reconciliation
19 kSeenInReconcile = 1U << 4, // scratch flag during a reconcile pass
20 kSynthetic = 1U << 5, // created from reconcile (venue had it, we did not)
21 kReplaceOldCanceled = 1U << 6, // cancel-then-new replace: old leg already cancelled
22 };
25 VenueOrderId venue_order_id; // 41 -> 49
26 std::uint8_t pad0_[7]; // -> 56
29 Side side; // 1
31 TimeInForce tif; // 1 -> 64
34 std::uint8_t flags; // 1
35 std::uint8_t cancel_attempts; // 1
36 std::uint32_t user_tag; // 4 -> 72 (QuoteManager slot id etc.)
37 Price price; // 8 -> 80
38 Qty qty; // 8 -> 88
39 Qty cum_qty; // 8 -> 96
40 Timestamp created; // 8 -> 104
41 ClientOrderId pending_cl_ord_id; // 8 -> 112 replace: id of the replacement order
42 Price pending_price; // 8 -> 120
43 Qty pending_qty; // 8 -> 128
45 [[nodiscard]] constexpr Qty leaves_qty() const noexcept { return qty - cum_qty; }
46 [[nodiscard]] constexpr bool is_open() const noexcept { return !is_terminal(state); }
47 [[nodiscard]] constexpr bool is_working() const noexcept {
49 }
50 [[nodiscard]] constexpr bool has(Flags f) const noexcept { return (flags & f) != 0; }
51};
52static_assert(sizeof(Order) == 128 && std::is_trivially_copyable_v<Order>);
53
54// When the order's working leg (the order, or its latest replacement once acknowledged) was sent
55// and accepted. Kept by the OMS next to Order, not in it (Oms::times, OmsUpdate::times).
56struct OrderTimes {
57 Timestamp sent; // engine clock when the order or replace went out
58 Timestamp venue_ack; // the ack's exch_ts: the venue's accept time (Binance: whole ms)
59 Timestamp local_ack; // the ack's recv_ts: when it reached us
60};
62struct LimitOrder;
63
64struct NewOrderRequest {
67 // The account to send it from where the instrument's venue has a pool (core/account_pool.hpp):
68 // the primary or one of its members, else the order is refused (RejectReason::InvalidAccount).
69 // Invalid (the default): the engine picks the member by balance and remaining order window.
71 Side side;
74 bool post_only = false;
75 bool reduce_only = false;
78 std::uint32_t user_tag = 0;
80 // A GTC limit order, refined with the builder's methods:
81 // ctx.send(NewOrderRequest::limit(id, Side::Buy, px, qty).post_only().tag(7));
82 [[nodiscard]] static constexpr LimitOrder limit(InstrumentId instrument,
83 Side side,
85 Qty qty) noexcept;
86};
87
88// Chainable builder returned by NewOrderRequest::limit(); converts to NewOrderRequest.
89struct LimitOrder {
92 [[nodiscard]] constexpr LimitOrder post_only() const noexcept {
93 LimitOrder o = *this;
95 o.request.post_only = true;
96 return o;
97 }
98 [[nodiscard]] constexpr LimitOrder reduce_only() const noexcept {
99 LimitOrder o = *this;
101 return o;
102 }
103 [[nodiscard]] constexpr LimitOrder ioc() const noexcept {
104 LimitOrder o = *this;
106 return o;
107 }
108 // user_tag: echoed in the order record. Tags in the QuoteManager's range are rejected
109 // (RejectReason::InvalidTag).
110 [[nodiscard]] constexpr LimitOrder tag(std::uint32_t user_tag) const noexcept {
111 LimitOrder o = *this;
112 o.request.user_tag = user_tag;
113 return o;
114 }
115 // The account of the instrument's pool to send from (NewOrderRequest::account).
116 [[nodiscard]] constexpr LimitOrder account(VenueId venue) const noexcept {
117 LimitOrder o = *this;
118 o.request.account = venue;
119 return o;
120 }
121 // NOLINTNEXTLINE(google-explicit-constructor): passes straight to ctx.send().
122 constexpr operator NewOrderRequest() const noexcept { return request; }
124
125inline constexpr LimitOrder NewOrderRequest::limit(InstrumentId instrument,
126 Side side,
127 Price price,
128 Qty qty) noexcept {
129 LimitOrder o;
130 o.request.instrument = instrument;
131 o.request.side = side;
132 o.request.price = price;
133 o.request.qty = qty;
134 return o;
135}
136
137} // namespace fastmm
All engine enumerations.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Definition strategy.hpp:16
OrderType
Definition enums.hpp:23
OrderState
Definition enums.hpp:79
FixedString< 40 > VenueOrderId
Definition strong_id.hpp:42
RejectReason
Ordered exactly like RiskEngine::check_new evaluates them (5.7), followed by OMS / transport / venue ...
Definition enums.hpp:123
Side
Definition enums.hpp:12
constexpr bool is_terminal(OrderState s) noexcept
Definition enums.hpp:90
TimeInForce
Definition enums.hpp:36
Strongly typed identifiers.
Chainable builder returned by NewOrderRequest::limit(); converts to NewOrderRequest.
Definition order.hpp:91
constexpr LimitOrder reduce_only() const noexcept
Definition order.hpp:100
constexpr LimitOrder account(VenueId venue) const noexcept
The account of the instrument's pool to send from (NewOrderRequest::account).
Definition order.hpp:118
constexpr LimitOrder tag(std::uint32_t user_tag) const noexcept
user_tag: echoed in the order record.
Definition order.hpp:112
NewOrderRequest request
Definition order.hpp:92
constexpr LimitOrder ioc() const noexcept
Definition order.hpp:105
constexpr LimitOrder post_only() const noexcept
Definition order.hpp:94
Definition order.hpp:66
VenueId account
The account to send it from where the instrument's venue has a pool (core/account_pool....
Definition order.hpp:72
bool reduce_only
Definition order.hpp:77
Side side
Definition order.hpp:73
VenueId venue
Definition order.hpp:68
OrderType type
Definition order.hpp:74
bool post_only
Definition order.hpp:76
std::uint32_t user_tag
Definition order.hpp:80
Price price
Definition order.hpp:78
static constexpr LimitOrder limit(InstrumentId instrument, Side side, Price price, Qty qty) noexcept
A GTC limit order, refined with the builder's methods: ctx.send(NewOrderRequest::limit(id,...
Definition order.hpp:126
TimeInForce tif
Definition order.hpp:75
InstrumentId instrument
Definition order.hpp:67
Qty qty
Definition order.hpp:79
When the order's working leg (the order, or its latest replacement once acknowledged) was sent and ac...
Definition order.hpp:58
Timestamp sent
engine clock when the order or replace went out
Definition order.hpp:59
Timestamp local_ack
the ack's recv_ts: when it reached us
Definition order.hpp:61
Timestamp venue_ack
the ack's exch_ts: the venue's accept time (Binance: whole ms)
Definition order.hpp:60
Definition order.hpp:15
OrderState state
1
Definition order.hpp:34
TimeInForce tif
1 -> 64
Definition order.hpp:33
VenueOrderId venue_order_id
41 -> 49
Definition order.hpp:27
ClientOrderId cl_ord_id
8
Definition order.hpp:26
Qty qty
8 -> 88
Definition order.hpp:40
constexpr Qty leaves_qty() const noexcept
Definition order.hpp:47
std::uint8_t flags
1
Definition order.hpp:36
Price pending_price
8 -> 120
Definition order.hpp:44
constexpr bool has(Flags f) const noexcept
Definition order.hpp:52
std::uint8_t cancel_attempts
1
Definition order.hpp:37
ClientOrderId pending_cl_ord_id
8 -> 112 replace: id of the replacement order
Definition order.hpp:43
Timestamp created
8 -> 104
Definition order.hpp:42
constexpr bool is_open() const noexcept
Definition order.hpp:48
VenueId venue
1
Definition order.hpp:30
constexpr bool is_working() const noexcept
Definition order.hpp:49
Qty pending_qty
8 -> 128
Definition order.hpp:45
InstrumentId instrument
4 -> 60
Definition order.hpp:29
Side side
1
Definition order.hpp:31
std::uint32_t user_tag
4 -> 72 (QuoteManager slot id etc.)
Definition order.hpp:38
std::uint8_t pad0_[7]
-> 56
Definition order.hpp:28
RejectReason reject_reason
1
Definition order.hpp:35
Qty cum_qty
8 -> 96
Definition order.hpp:41
OrderType type
1
Definition order.hpp:32
Price price
8 -> 80
Definition order.hpp:39
Flags
Definition order.hpp:16
@ kReplaceOldCanceled
cancel-then-new replace: old leg already cancelled
Definition order.hpp:23
@ kSynthetic
created from reconcile (venue had it, we did not)
Definition order.hpp:22
@ kReconciled
state fixed up by reconciliation
Definition order.hpp:20
@ kReduceOnly
Definition order.hpp:18
@ kPostOnly
Definition order.hpp:17
@ kSeenInReconcile
scratch flag during a reconcile pass
Definition order.hpp:21
@ kUnsolicitedCancel
venue cancelled it (not requested by us)
Definition order.hpp:19
Definition time.hpp:45
Time primitives.