Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
order.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <string>
5#include <string_view>
6#include <chrono>
7#include <cstdint>
8#include <cstring>
9#include <type_traits>
10#include <nlohmann/json.hpp>
11#include <limits>
12#include "types.hpp"
14#include <utils/timestamp.hpp>
15
16using json = nlohmann::json;
17
18namespace slick::sim {
19
20enum Side : uint8_t {
24};
25
26inline constexpr Side opposite_side(Side side) {
27 switch(side) {
28 case Side::BUY:
29 return Side::SELL;
30 case Side::SELL:
31 return Side::BUY;
33 return Side::UNKNOWN_SIDE;
34 }
35 return Side::UNKNOWN_SIDE;
36}
37
38template<Side S>
39inline constexpr Side opposite_side() {
40 if constexpr (S == Side::BUY) {
41 return Side::SELL;
42 } else if constexpr (S == Side::SELL) {
43 return Side::BUY;
44 } else {
45 return Side::UNKNOWN_SIDE;
46 }
47}
48
49inline constexpr std::string_view to_string(Side side) {
50 switch(side)
51 {
52 case Side::BUY:
53 return "BUY";
54 case Side::SELL:
55 return "SELL";
57 break;
58 }
59 return "UNKNOWN";
60}
61
62enum class OrderType : char {
63 UNKNOWN = '0',
64 MARKET = '1',
65 LIMIT = '2',
66 STOP = '3',
68 BRACKET = '5',
69 TWAP = '6',
70 ROLL_OPEN = '7',
73 SCALED = 'A',
74 };
75
76 inline constexpr std::string_view to_string(OrderType type) {
77 switch(type) {
79 return "MARKET";
81 return "LIMIT";
82 case OrderType::STOP:
83 return "STOP";
85 return "STOP_LIMIT";
87 return "BRAKET";
88 case OrderType::TWAP:
89 return "TWAP";
91 return "ROLL_OPEN";
93 return "ROLL_CLOSE";
95 return "LIQUIDATION";
97 return "SCALED";
99 break;
100 }
101 return "UNKNOWN";
102}
103
104enum class ExecType : char {
105 NEW = '0',
106 CANCELED = '4',
107 REPLACED = '5',
109 REJECTED = '8',
110 EXPIRED = 'C',
112 TRADE = 'F',
116 RESTATED = 'D',
117};
118
119inline constexpr std::string_view to_string(ExecType exec_type) {
120 switch (exec_type) {
121 case ExecType::NEW:
122 return "NEW";
124 return "CANCELED";
126 return "REPLACED";
128 return "PENDING_CANCEL";
130 return "REJECTED";
132 return "EXPIRED";
134 return "PENDING_REPLACE";
135 case ExecType::TRADE:
136 return "TRADE";
138 return "TRADE_CORRECT";
140 return "TRADE_CANCEL";
142 return "ORDER_STATUS";
144 return "RESTATED";
145 }
146 return "UNKNOWN";
147}
148
166
167inline constexpr std::string_view to_string(OrderStatus status) {
168 switch (status) {
169 case OrderStatus::NEW:
170 return "NEW";
172 return "PARTIALLY_FILLED";
174 return "FILLED";
176 return "DONE_FOR_DAY";
178 return "CANCELLED";
180 return "REPLACED";
182 return "PENDING_CANCEL";
184 return "STOPPED";
186 return "REJECTED";
188 return "SUSPENDED";
190 return "PENDING_NEW";
192 return "CALCULATED";
194 return "EXPIRED";
196 return "ACCEPTED_FOR_BIDDING";
198 return "PENDING_REPLACE";
199 }
200 return "UNKNOWN";
201}
202
212
213inline std::string_view to_string(TimeInForce tif) {
214 switch (tif) {
215 case TimeInForce::DAY:
216 return "DAY";
218 return "GOOD_TILL_CANCEL";
220 return "AT_THE_OPENING";
222 return "IMMEDIATE_OR_CANCEL";
224 return "FILL_OR_KILL";
226 return "GOOD_TILL_CROSSING";
228 return "GOOD_TILL_DATE";
229 }
230 return "UNKNOWN";
231}
232
233enum class SelfMatchPreventionMode : uint8_t {
234 NONE = 0, // No SMP, allow self-matching
235 CANCEL_RESTING = 1, // Cancel the resting order on the book
236 CANCEL_NEWEST = 2, // Cancel/reject the incoming order
237};
238
239inline constexpr std::string_view to_string(SelfMatchPreventionMode mode) {
240 switch(mode) {
242 return "none";
244 return "cancel_resting";
246 return "cancel_newest";
247 }
248 return "none";
249}
250
253inline constexpr SelfMatchPreventionMode to_smp_mode(std::string_view name) {
254 if (name == "cancel_resting") {
256 }
257 if (name == "cancel_newest") {
259 }
261}
262
263enum class ProductType : uint8_t {
267};
268
269inline constexpr std::string_view to_string(ProductType type) {
270 switch(type) {
272 return "SPOT";
274 return "FUTURE";
276 break;
277 }
278 return "UNKNOWN_PRODUCT_TYPE";
279}
280
294
295static_assert(std::is_standard_layout_v<OrderIdentity>);
296static_assert(sizeof(OrderIdentity) == 32 + 37 + 37 + 37,
297 "OrderIdentity must stay gap-free: it is copied as one block onto the wire");
298
300 uint64_t id;
301 uint64_t priority = 0;
304 uint64_t request_tag = 0;
310 bool post_only = false;
311 bool pending_cancel = false;
312 bool size_in_quote = false;
313
322 uint32_t num_fills = 0;
323 double fee = 0;
324 double filled_value = 0;
325
326 int client_id = -1;
329 std::optional<uint64_t> last_fill_time;
330
331 // Cold: never on the order hot path, so they sit past the fields that are.
332 std::string end_time;
333 std::string reject_message;
334 std::string cancel_message;
335
336 json edit_history = json::array();
337
338 void resetFillAccounting(qty_t new_qty = 0) noexcept {
339 quantity = new_qty;
340 leaves_quantity = new_qty;
341 avg_fill_price = 0;
342 cum_quantity = 0;
343 cum_value = 0;
344 last_fill_price = 0;
345 last_fill_qty = 0;
346 num_fills = 0;
347 fee = 0.0;
348 filled_value = 0.0;
349 last_fill_time.reset();
350 }
351
352 Order() = default;
353
356 [[nodiscard]] bool restsRemainder() const noexcept {
357 return price != NULL_PRICE &&
360 }
361
362 double price_double() const noexcept {
363 return to_price_double(price);
364 }
365
366 double qty_double() const noexcept {
367 return to_qty_double(quantity);
368 }
369
370 double cum_qty_double() const noexcept {
372 }
373
374 double leaves_qty_double() const noexcept {
376 }
377
378 double last_fill_qty_double() const noexcept {
380 }
381
382 double last_fill_price_double() const noexcept {
384 }
385
386 double avg_fill_price_double() const noexcept {
388 }
389};
390
397
398} // namespace slick::sim::order_gateway
A null-terminated character buffer of fixed capacity, stored inline.
nlohmann::json json
Definition order.hpp:16
uint64_t get_current_time_ns()
Definition timestamp.hpp:93
constexpr Side opposite_side()
Definition order.hpp:39
constexpr price_t NULL_PRICE
Definition types.hpp:28
uint_fast64_t time_t
Definition types.hpp:23
@ UNKNOWN_SIDE
Definition order.hpp:23
constexpr double to_price_double(price_t price)
Definition types.hpp:46
SelfMatchPreventionMode
Definition order.hpp:233
constexpr SelfMatchPreventionMode to_smp_mode(std::string_view name)
Parses the self_match_prevention configuration value.
Definition order.hpp:253
boost::multiprecision::uint128_t cum_value_t
Definition types.hpp:25
constexpr const char * to_string(FeedState state) noexcept
@ TRADE
Reserved.
int_fast64_t qty_t
Definition types.hpp:22
constexpr double to_qty_double(qty_t qty)
Definition types.hpp:54
int_fast64_t price_t
Definition types.hpp:21
price_t filled_price
Definition order.hpp:393
std::string order_id
Definition order.hpp:392
time_t timestamp
Definition order.hpp:395
The four identity fields, stored inline in the order and width used by OrderResponse.
Definition order.hpp:288
utils::fixed_string< 32 > symbol
Definition order.hpp:289
utils::fixed_string< 37 > client_order_id
Definition order.hpp:291
utils::fixed_string< 37 > order_id
Definition order.hpp:292
utils::fixed_string< 37 > user_id
Definition order.hpp:290
price_t avg_fill_price
Definition order.hpp:317
OrderType type
Definition order.hpp:306
qty_t last_fill_qty
Definition order.hpp:321
bool restsRemainder() const noexcept
Whether what matching leaves of it rests on the book; otherwise it is cancelled.
Definition order.hpp:356
std::string reject_message
Definition order.hpp:333
cum_value_t cum_value
Definition order.hpp:319
double filled_value
Definition order.hpp:324
std::optional< uint64_t > last_fill_time
Definition order.hpp:329
uint64_t priority
Definition order.hpp:301
uint64_t created_time
Definition order.hpp:327
TimeInForce time_in_force
Definition order.hpp:308
double last_fill_qty_double() const noexcept
Definition order.hpp:378
uint64_t last_update_time
Definition order.hpp:328
double last_fill_price_double() const noexcept
Definition order.hpp:382
OrderStatus status
Definition order.hpp:307
std::string cancel_message
Definition order.hpp:334
uint32_t num_fills
Definition order.hpp:322
double avg_fill_price_double() const noexcept
Definition order.hpp:386
double qty_double() const noexcept
Definition order.hpp:366
qty_t leaves_quantity
Definition order.hpp:316
double leaves_qty_double() const noexcept
Definition order.hpp:374
uint64_t request_tag
Request::request_tag of the last request this order accepted, echoed on its responses.
Definition order.hpp:304
void resetFillAccounting(qty_t new_qty=0) noexcept
Definition order.hpp:338
double price_double() const noexcept
Definition order.hpp:362
std::string end_time
Definition order.hpp:332
price_t last_fill_price
Definition order.hpp:320
double cum_qty_double() const noexcept
Definition order.hpp:370
ProductType product_type
Definition order.hpp:309