Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
order_store.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common/messages.hpp>
4#include <common/order.hpp>
5#include <slick/queue.hpp>
6#include <cstring>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
12
24public:
28 OrderStore() = default;
29
32 explicit OrderStore(slick::queue<OrderResponse> &response_queue)
33 : response_queue_(&response_queue)
34 , cursor_(response_queue.initial_reading_index()) {}
35
41 void drain(int max_batch = 4096) {
42 if (response_queue_ == nullptr) {
43 return;
44 }
45 for (int i = 0; i < max_batch; ++i) {
46 auto [data, size] = response_queue_->read(cursor_);
47 if (data == nullptr) {
48 break;
49 }
50 apply(*data);
51 }
52 }
53
55 std::vector<const Order*> orders_of(std::string_view user_id) const {
56 std::vector<const Order*> result;
57 auto it = ids_by_user_.find(std::string(user_id));
58 if (it == ids_by_user_.end()) {
59 return result;
60 }
61 result.reserve(it->second.size());
62 // ids_by_user_ is append-ordered, so walking it backwards yields newest
63 // first without having to sort on a timestamp that ties across a batch.
64 for (auto id = it->second.rbegin(); id != it->second.rend(); ++id) {
65 auto order = by_order_id_.find(*id);
66 if (order != by_order_id_.end()) {
67 result.push_back(&order->second);
68 }
69 }
70 return result;
71 }
72
73 const Order* find(std::string_view order_id) const {
74 auto it = by_order_id_.find(std::string(order_id));
75 return it == by_order_id_.end() ? nullptr : &it->second;
76 }
77
78 size_t size() const noexcept { return by_order_id_.size(); }
79
82 void apply(const OrderResponse &response) {
83 std::string order_id(response.order_id,
84 ::strnlen(response.order_id, sizeof(response.order_id)));
85 if (order_id.empty()) {
86 return;
87 }
88
89 auto [it, inserted] = by_order_id_.try_emplace(order_id);
90 Order &order = it->second;
91
92 if (inserted) {
93 // Identity and creation are fixed at first sight; every later report for
94 // this order repeats them, and PENDING_NEW is always the first.
95 order.order_id = order_id;
96 order.symbol = response.symbol;
97 order.user_id = response.user_id;
98 order.client_order_id = response.client_order_id;
99 order.created_time = response.creation_time;
101 ids_by_user_[order.user_id.str()].push_back(order_id);
102 }
103
104 order.side = response.side;
105 order.type = response.order_type;
106 order.time_in_force = response.time_in_force;
107 order.status = response.order_status;
108 order.post_only = response.post_only;
109 order.price = response.price;
110 order.quantity = response.qty;
111 order.leaves_quantity = response.leaves_qty;
112 order.cum_quantity = response.cum_qty;
113 order.avg_fill_price = response.avg_fill_price;
114 order.last_update_time = response.timestamp;
116
117 if (response.exec_type == ExecType::TRADE) {
118 // num_fills is counted here rather than read off the report, because the
119 // report has no such field - the fill count is a property of the stream.
120 // Each response is consumed once, so this cannot double count.
121 order.last_fill_price = response.last_fill_price;
122 order.last_fill_qty = response.last_qty;
123 // Copy first: optional's operator= binds a reference, and timestamp is a packed,
124 // misaligned field (UBSan halts on the reference).
125 order.last_fill_time = uint64_t{response.timestamp};
126 ++order.num_fills;
127 }
128
130 * to_qty_double(response.cum_qty);
131
132 if (response.order_status == OrderStatus::REJECTED) {
133 order.reject_message = error_message_of(response);
134 } else if (response.order_status == OrderStatus::CANCELED) {
135 order.cancel_message = error_message_of(response);
136 }
137 }
138
139private:
143 static std::string error_message_of(const OrderResponse &response) {
144 return std::string(response.error_message,
145 strnlen(response.error_message, sizeof(response.error_message)));
146 }
147
148 slick::queue<OrderResponse> *response_queue_ = nullptr;
149 uint64_t cursor_ = 0;
150
151 std::unordered_map<std::string, Order> by_order_id_;
153 std::unordered_map<std::string, std::vector<std::string>> ids_by_user_;
154};
155
156} // end namespace slick::sim::order_gateway
void drain(int max_batch=4096)
Applies every response published since the last call.
OrderStore()=default
Projection only.
const Order * find(std::string_view order_id) const
void apply(const OrderResponse &response)
Folds one execution report into the order it describes, creating it on first sight.
std::vector< const Order * > orders_of(std::string_view user_id) const
The orders belonging to one user, most recently created first.
OrderStore(slick::queue< OrderResponse > &response_queue)
Self-draining, with its own cursor.
constexpr double to_price_double(price_t price)
Definition types.hpp:46
constexpr double to_qty_double(qty_t qty)
Definition types.hpp:54
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
std::string reject_message
Definition order.hpp:333
double filled_value
Definition order.hpp:324
std::optional< uint64_t > last_fill_time
Definition order.hpp:329
uint64_t created_time
Definition order.hpp:327
TimeInForce time_in_force
Definition order.hpp:308
uint64_t last_update_time
Definition order.hpp:328
OrderStatus status
Definition order.hpp:307
std::string cancel_message
Definition order.hpp:334
uint32_t num_fills
Definition order.hpp:322
qty_t leaves_quantity
Definition order.hpp:316
price_t last_fill_price
Definition order.hpp:320
ProductType product_type
Definition order.hpp:309