FastMM documentation
FastMM C++ API 0.14.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 <limits>
10#include <type_traits>
11
12namespace fastmm {
13
14struct Order {
15 enum Flags : std::uint8_t {
16 kPostOnly = 1U << 0,
17 kReduceOnly = 1U << 1,
18 kUnsolicitedCancel = 1U << 2, // venue cancelled it (not requested by us)
19 kReconciled = 1U << 3, // state fixed up by reconciliation
20 kSeenInReconcile = 1U << 4, // scratch flag during a reconcile pass
21 kSynthetic = 1U << 5, // created from reconcile (venue had it, we did not)
22 kReplaceOldCanceled = 1U << 6, // cancel-then-new replace: old leg already cancelled
23 };
26 VenueOrderId venue_order_id; // 41 -> 49
27 std::uint8_t arm; // 1 [ab] arm at its last new or replace: 0 none, k + 1 arm k
28 std::uint8_t pad0_[6]; // -> 56
31 Side side; // 1
33 TimeInForce tif; // 1 -> 64
36 std::uint8_t flags; // 1
37 std::uint8_t cancel_attempts; // 1
38 std::uint32_t user_tag; // 4 -> 72 (QuoteManager slot id etc.)
39 Price price; // 8 -> 80
40 Qty qty; // 8 -> 88
41 Qty cum_qty; // 8 -> 96
42 Timestamp created; // 8 -> 104
43 ClientOrderId pending_cl_ord_id; // 8 -> 112 replace: id of the replacement order
44 Price pending_price; // 8 -> 120
45 Qty pending_qty; // 8 -> 128
47 [[nodiscard]] constexpr Qty leaves_qty() const noexcept { return qty - cum_qty; }
48 [[nodiscard]] constexpr bool is_open() const noexcept { return !is_terminal(state); }
49 [[nodiscard]] constexpr bool is_working() const noexcept {
51 }
52 [[nodiscard]] constexpr bool has(Flags f) const noexcept { return (flags & f) != 0; }
53};
54static_assert(sizeof(Order) == 128 && std::is_trivially_copyable_v<Order>);
55
56// When the order's working leg (the order, or its latest replacement once acknowledged) was sent
57// and accepted. Kept by the OMS next to Order, not in it (Oms::times, OmsUpdate::times).
58struct OrderTimes {
59 Timestamp sent; // engine clock when the order or replace went out
60 Timestamp venue_ack; // the ack's exch_ts: the venue's accept time (Binance: whole ms)
61 Timestamp local_ack; // the ack's recv_ts: when it reached us
62};
64// The numbers a strategy publishes about its quotes on one side of an instrument
65// (StrategyContext::quote_context): the fair value it priced them from, a quote's distance from it,
66// a signal. NaN where nothing was published. The engine copies the side's context into each order
67// it sends or moves on that side, so the record of a fill can say what the strategy knew when it
68// placed the order and what it knew when the fill arrived (FillRecord).
69inline constexpr std::size_t kQuoteContextSize = 4;
70struct QuoteContext {
71 double v[kQuoteContextSize] = {std::numeric_limits<double>::quiet_NaN(),
72 std::numeric_limits<double>::quiet_NaN(),
73 std::numeric_limits<double>::quiet_NaN(),
74 std::numeric_limits<double>::quiet_NaN()};
75};
76static_assert(sizeof(QuoteContext) == 32 && std::is_trivially_copyable_v<QuoteContext>);
77
78// How an order came to be where it was when something happened to it: kept by the OMS next to
79// Order (Oms::trail), for an open order and for a recently terminal one, and copied into the
80// record of each fill (FillRecord). Engine clock throughout, so a replay reproduces it.
81struct OrderTrail {
82 OrderTimes times; // the working leg: sent (the order, or its last accepted replace)
83 Timestamp placed; // the first leg went out
84 Timestamp pending_sent; // the cancel or replace in flight went out; invalid: none. Kept on
85 // the terminal record: the cancel that ended the order.
86 Price placed_px; // the first leg's price
87 std::uint32_t amends = 0; // replaces the venue accepted
88 std::uint32_t pad_ = 0; //
89 QuoteContext context; // the side's context when the working leg went out
90 QuoteContext pending_context; // the side's context when the replace in flight went out
91};
92static_assert(std::is_trivially_copyable_v<OrderTrail>);
93
94// What an order or a quote level is worth when the strategy did not say ([capacity],
95// core/capacity_price.hpp).
96inline constexpr float kNoValue = std::numeric_limits<float>::quiet_NaN();
97[[nodiscard]] constexpr bool value_stated(float v) noexcept {
98 return v == v; // NOLINT(misc-redundant-expression): false for NaN only
100
101struct LimitOrder;
102
103struct NewOrderRequest {
106 // The account to send it from where the instrument's venue has a pool (core/account_pool.hpp):
107 // the primary or one of its members, else the order is refused (RejectReason::InvalidAccount).
108 // Invalid (the default): the engine picks the member by balance and remaining order window.
110 Side side;
113 bool post_only = false;
114 bool reduce_only = false;
117 std::uint32_t user_tag = 0;
118 // What the order is worth, in the quote currency: [capacity] sends it only if this covers the
119 // price of the order slot it takes. kNoValue: not priced.
120 float value = kNoValue;
121
122 // A GTC limit order, refined with the builder's methods:
123 // ctx.send(NewOrderRequest::limit(id, Side::Buy, px, qty).post_only().tag(7));
124 [[nodiscard]] static constexpr LimitOrder limit(InstrumentId instrument,
125 Side side,
126 Price price,
127 Qty qty) noexcept;
128};
129
130// Chainable builder returned by NewOrderRequest::limit(); converts to NewOrderRequest.
131struct LimitOrder {
134 [[nodiscard]] constexpr LimitOrder post_only() const noexcept {
135 LimitOrder o = *this;
137 o.request.post_only = true;
138 return o;
139 }
140 [[nodiscard]] constexpr LimitOrder reduce_only() const noexcept {
141 LimitOrder o = *this;
143 return o;
144 }
145 [[nodiscard]] constexpr LimitOrder ioc() const noexcept {
146 LimitOrder o = *this;
148 return o;
149 }
150 // user_tag: echoed in the order record. Tags in the QuoteManager's range are rejected
151 // (RejectReason::InvalidTag).
152 [[nodiscard]] constexpr LimitOrder tag(std::uint32_t user_tag) const noexcept {
153 LimitOrder o = *this;
154 o.request.user_tag = user_tag;
155 return o;
156 }
157 // What the order is worth ([capacity], NewOrderRequest::value).
158 [[nodiscard]] constexpr LimitOrder value(float v) const noexcept {
159 LimitOrder o = *this;
161 return o;
162 }
163 // The account of the instrument's pool to send from (NewOrderRequest::account).
164 [[nodiscard]] constexpr LimitOrder account(VenueId venue) const noexcept {
165 LimitOrder o = *this;
166 o.request.account = venue;
167 return o;
168 }
169 // NOLINTNEXTLINE(google-explicit-constructor): passes straight to ctx.send().
170 constexpr operator NewOrderRequest() const noexcept { return request; }
172
173inline constexpr LimitOrder NewOrderRequest::limit(InstrumentId instrument,
174 Side side,
175 Price price,
176 Qty qty) noexcept {
177 LimitOrder o;
178 o.request.instrument = instrument;
179 o.request.side = side;
180 o.request.price = price;
181 o.request.qty = qty;
182 return o;
183}
184
185} // namespace fastmm
All engine enumerations.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Definition strategy.hpp:23
constexpr std::size_t kQuoteContextSize
The numbers a strategy publishes about its quotes on one side of an instrument (StrategyContext::quot...
Definition order.hpp:71
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
constexpr float kNoValue
What an order or a quote level is worth when the strategy did not say ([capacity],...
Definition order.hpp:98
StrongId< struct InstrumentTag, std::uint32_t > InstrumentId
Dense index into the engine's instrument table.
Definition strong_id.hpp:38
constexpr bool value_stated(float v) noexcept
Definition order.hpp:99
TimeInForce
Definition enums.hpp:36
Strongly typed identifiers.
Chainable builder returned by NewOrderRequest::limit(); converts to NewOrderRequest.
Definition order.hpp:133
constexpr LimitOrder reduce_only() const noexcept
Definition order.hpp:142
constexpr LimitOrder value(float v) const noexcept
What the order is worth ([capacity], NewOrderRequest::value).
Definition order.hpp:160
constexpr LimitOrder account(VenueId venue) const noexcept
The account of the instrument's pool to send from (NewOrderRequest::account).
Definition order.hpp:166
constexpr LimitOrder tag(std::uint32_t user_tag) const noexcept
user_tag: echoed in the order record.
Definition order.hpp:154
NewOrderRequest request
Definition order.hpp:134
constexpr LimitOrder ioc() const noexcept
Definition order.hpp:147
constexpr LimitOrder post_only() const noexcept
Definition order.hpp:136
Definition order.hpp:105
VenueId account
The account to send it from where the instrument's venue has a pool (core/account_pool....
Definition order.hpp:111
bool reduce_only
Definition order.hpp:116
Side side
Definition order.hpp:112
VenueId venue
Definition order.hpp:107
OrderType type
Definition order.hpp:113
bool post_only
Definition order.hpp:115
std::uint32_t user_tag
Definition order.hpp:119
Price price
Definition order.hpp:117
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:174
TimeInForce tif
Definition order.hpp:114
float value
What the order is worth, in the quote currency: [capacity] sends it only if this covers the price of ...
Definition order.hpp:122
InstrumentId instrument
Definition order.hpp:106
Qty qty
Definition order.hpp:118
When the order's working leg (the order, or its latest replacement once acknowledged) was sent and ac...
Definition order.hpp:60
Timestamp sent
engine clock when the order or replace went out
Definition order.hpp:61
Timestamp local_ack
the ack's recv_ts: when it reached us
Definition order.hpp:63
Timestamp venue_ack
the ack's exch_ts: the venue's accept time (Binance: whole ms)
Definition order.hpp:62
OrderTimes times
the working leg: sent (the order, or its last accepted replace)
Definition order.hpp:84
Price placed_px
the first leg's price
Definition order.hpp:88
std::uint32_t pad_
Definition order.hpp:90
Timestamp pending_sent
the cancel or replace in flight went out; invalid: none.
Definition order.hpp:86
std::uint32_t amends
replaces the venue accepted
Definition order.hpp:89
Timestamp placed
the first leg went out
Definition order.hpp:85
QuoteContext pending_context
the side's context when the replace in flight went out
Definition order.hpp:92
QuoteContext context
the side's context when the working leg went out
Definition order.hpp:91
Definition order.hpp:16
OrderState state
1
Definition order.hpp:36
TimeInForce tif
1 -> 64
Definition order.hpp:35
VenueOrderId venue_order_id
41 -> 49
Definition order.hpp:28
ClientOrderId cl_ord_id
8
Definition order.hpp:27
Qty qty
8 -> 88
Definition order.hpp:42
constexpr Qty leaves_qty() const noexcept
Definition order.hpp:49
std::uint8_t arm
1 [ab] arm at its last new or replace: 0 none, k + 1 arm k
Definition order.hpp:29
std::uint8_t flags
1
Definition order.hpp:38
Price pending_price
8 -> 120
Definition order.hpp:46
constexpr bool has(Flags f) const noexcept
Definition order.hpp:54
std::uint8_t cancel_attempts
1
Definition order.hpp:39
ClientOrderId pending_cl_ord_id
8 -> 112 replace: id of the replacement order
Definition order.hpp:45
Timestamp created
8 -> 104
Definition order.hpp:44
constexpr bool is_open() const noexcept
Definition order.hpp:50
VenueId venue
1
Definition order.hpp:32
constexpr bool is_working() const noexcept
Definition order.hpp:51
Qty pending_qty
8 -> 128
Definition order.hpp:47
InstrumentId instrument
4 -> 60
Definition order.hpp:31
Side side
1
Definition order.hpp:33
std::uint8_t pad0_[6]
-> 56
Definition order.hpp:30
std::uint32_t user_tag
4 -> 72 (QuoteManager slot id etc.)
Definition order.hpp:40
RejectReason reject_reason
1
Definition order.hpp:37
Qty cum_qty
8 -> 96
Definition order.hpp:43
OrderType type
1
Definition order.hpp:34
Price price
8 -> 80
Definition order.hpp:41
Flags
Definition order.hpp:17
@ kReplaceOldCanceled
cancel-then-new replace: old leg already cancelled
Definition order.hpp:24
@ kSynthetic
created from reconcile (venue had it, we did not)
Definition order.hpp:23
@ kReconciled
state fixed up by reconciliation
Definition order.hpp:21
@ kReduceOnly
Definition order.hpp:19
@ kPostOnly
Definition order.hpp:18
@ kSeenInReconcile
scratch flag during a reconcile pass
Definition order.hpp:22
@ kUnsolicitedCancel
venue cancelled it (not requested by us)
Definition order.hpp:20
Definition order.hpp:72
double v[kQuoteContextSize]
Definition order.hpp:73
Definition time.hpp:45
Time primitives.