Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
order_event.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <type_traits>
7#include <string_view>
8#include <cstring>
9#include <algorithm>
10
11namespace slick::trader {
12
13constexpr size_t MAX_ORDER_ID_LENGTH = 64;
14constexpr size_t MAX_REJECT_REASON_LENGTH = 128;
15
16#pragma pack(push, 1)
17struct OrderEventBase : public Event {
20
21 // Base event for order-related events
23 : Event(t)
25 , side(Side::BUY)
26 {}
27
28 void set_client_order_id(std::string_view id) {
29 size_t len = std::min(id.size(), size_t(63));
30 std::memcpy(client_order_id, id.data(), len);
31 client_order_id[len] = '\0';
32 }
33};
34
35struct OrderEvent : public OrderEventBase {
37
42
43 void set_order_id(std::string_view id) {
44 size_t len = std::min(id.size(), size_t(63));
45 std::memcpy(order_id, id.data(), len);
46 order_id[len] = '\0';
47 }
48};
49
52 price_t price; // Limit price (or take-profit for BRACKET)
53 price_t stop_price; // Stop price (for STOP, STOP_LIMIT, BRACKET)
57
67};
68
85
91
92struct OrderAckEvent : public OrderEvent {
94 price_t price; // Limit price (or take-profit for BRACKET orders)
95 price_t stop_price; // Stop price (for STOP, STOP_LIMIT, BRACKET orders)
100
111};
112
113// What a fill moves. An ordinary fill moves both the order - how much of it is done - and
114// the position in the order's instrument. An order for a multi-leg instrument, a futures
115// spread say, is different: the exchange fills it at the spread's own price, but what is
116// then held is a position in each leg, at the leg's price. Its broker reports that as one
117// ORDER fill followed by a LEG fill per leg.
118enum class FillScope : uint8_t {
119 ORDER_AND_POSITION, // an ordinary fill
120 ORDER, // a multi-leg order's fill, at the order's own price: the order moves, no position is booked
121 LEG, // one leg's share of it: `instrument`, `side`, price and quantity are the leg's, and only
122 // the position moves. cumulative_filled and remaining_quantity are not set.
123};
124
125// OrderFillEvent
126struct OrderFillEvent : public OrderEvent {
132 price_t fee_amount{0}; // actual fee for this fill (0 if broker doesn't report it)
134 // The number a broker gave the fill when it wrote it to a journal that outlives the
135 // process, or 0 for a fill that was not journaled. Increasing per broker and never
136 // reused. A broker that journals its fills replays them after a restart, and the
137 // strategy manager drops those the strategies had before it.
138 uint64_t journal_seq{0};
139 // Replayed from the broker's journal at startup: the fill may be one the strategies had
140 // before the restart.
141 bool replayed{false};
142 // The instrument's contract multiplier as the broker knew it when the fill was made; 0
143 // when it does not say. A fill that carries one is valued with it even before the
144 // instrument's definition has arrived - as one replayed at startup is.
146
149 , fill_id{}
150 , fill_price(0)
151 , fill_quantity(0)
154 , fee_amount(0)
156 , journal_seq(0)
157 , replayed(false)
159 {}
160
161 void set_fill_id(std::string_view id) {
162 size_t len = std::min(id.size(), MAX_ORDER_ID_LENGTH - 1);
163 std::memcpy(fill_id, id.data(), len);
164 fill_id[len] = '\0';
165 }
166};
167
168// OrderRejectEvent
174
177 , reason{}
178 , price(0)
179 , stop_price(0)
180 , quantity(0) {}
181
182 void set_reason(std::string_view r) {
183 size_t len = std::min(r.size(), MAX_REJECT_REASON_LENGTH - 1);
184 std::memcpy(reason, r.data(), len);
185 reason[len] = '\0';
186 }
187};
188
189// OrderCancelEvent
193
198
199 void set_reason(std::string_view r) {
200 size_t len = std::min(r.size(), MAX_REJECT_REASON_LENGTH - 1);
201 std::memcpy(reason, r.data(), len);
202 reason[len] = '\0';
203 }
204};
205#pragma pack(pop)
206
207static_assert(std::is_trivially_copyable_v<NewOrderEvent>, "NewOrderEvent must be trivially copyable");
208static_assert(std::is_trivially_copyable_v<CancelOrderEvent>, "CancelOrderEvent must be trivially copyable");
209static_assert(std::is_trivially_copyable_v<OrderAckEvent>, "OrderAckEvent must be trivially copyable");
210static_assert(std::is_trivially_copyable_v<OrderFillEvent>, "OrderFillEvent must be trivially copyable");
211static_assert(std::is_trivially_copyable_v<OrderRejectEvent>, "OrderRejectEvent must be trivially copyable");
212static_assert(std::is_trivially_copyable_v<OrderCancelEvent>, "OrderCancelEvent must be trivially copyable");
213
214} // namespace slick::trader
constexpr size_t MAX_REJECT_REASON_LENGTH
constexpr price_t NULL_PRICE
Definition types.hpp:19
constexpr size_t MAX_ORDER_ID_LENGTH
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16
Event(EventType t=EventType::TRADE)
Definition event.hpp:63
void set_reason(std::string_view r)
char reason[MAX_REJECT_REASON_LENGTH]
void set_client_order_id(std::string_view id)
char order_id[MAX_ORDER_ID_LENGTH]
void set_order_id(std::string_view id)
char fill_id[MAX_ORDER_ID_LENGTH]
void set_fill_id(std::string_view id)
void set_reason(std::string_view r)
char reason[MAX_REJECT_REASON_LENGTH]