CppTrader  1.0.4.0
C++ Trader
order.h
Go to the documentation of this file.
1 
9 #ifndef CPPTRADER_MATCHING_ORDER_H
10 #define CPPTRADER_MATCHING_ORDER_H
11 
12 #include "errors.h"
13 
14 #include "containers/list.h"
15 #include "utility/iostream.h"
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <limits>
20 
21 namespace CppTrader {
22 namespace Matching {
23 
25 
33 enum class OrderSide : uint8_t
34 {
35  BUY,
36  SELL
37 };
38 
39 template <class TOutputStream>
40 TOutputStream& operator<<(TOutputStream& stream, OrderSide side);
41 
43 
81 enum class OrderType : uint8_t
82 {
83  MARKET,
84  LIMIT,
85  STOP,
86  STOP_LIMIT,
89 };
90 
91 template <class TOutputStream>
92 TOutputStream& operator<<(TOutputStream& stream, OrderType type);
93 
95 
109 enum class OrderTimeInForce : uint8_t
110 {
111  GTC,
112  IOC,
113  FOK,
114  AON
115 };
116 
117 template <class TOutputStream>
118 TOutputStream& operator<<(TOutputStream& stream, OrderTimeInForce tif);
119 
121 
127 struct Order
128 {
130  uint64_t Id;
132  uint32_t SymbolId;
138  uint64_t Price;
140  uint64_t StopPrice;
141 
143  uint64_t Quantity;
147  uint64_t LeavesQuantity;
148 
151 
153 
164  uint64_t HiddenQuantity() const noexcept { return (LeavesQuantity > MaxVisibleQuantity) ? (LeavesQuantity - MaxVisibleQuantity) : 0; }
166  uint64_t VisibleQuantity() const noexcept { return std::min(LeavesQuantity, MaxVisibleQuantity); }
167 
169 
179  uint64_t Slippage;
180 
182 
191 
198  int64_t TrailingStep;
199 
200  Order() noexcept = default;
201  Order(uint64_t id, uint32_t symbol, OrderType type, OrderSide side, uint64_t price, uint64_t stop_price, uint64_t quantity,
203  uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max(),
204  uint64_t slippage = std::numeric_limits<uint64_t>::max(),
205  int64_t trailing_distance = 0,
206  int64_t trailing_step = 0) noexcept;
207  Order(const Order&) noexcept = default;
208  Order(Order&&) noexcept = default;
209  ~Order() noexcept = default;
210 
211  Order& operator=(const Order&) noexcept = default;
212  Order& operator=(Order&&) noexcept = default;
213 
214  template <class TOutputStream>
215  friend TOutputStream& operator<<(TOutputStream& stream, const Order& order);
216 
218  bool IsMarket() const noexcept { return Type == OrderType::MARKET; }
220  bool IsLimit() const noexcept { return Type == OrderType::LIMIT; }
222  bool IsStop() const noexcept { return Type == OrderType::STOP; }
224  bool IsStopLimit() const noexcept { return Type == OrderType::STOP_LIMIT; }
226  bool IsTrailingStop() const noexcept { return Type == OrderType::TRAILING_STOP; }
228  bool IsTrailingStopLimit() const noexcept { return Type == OrderType::TRAILING_STOP_LIMIT; }
229 
231  bool IsBuy() const noexcept { return Side == OrderSide::BUY; }
233  bool IsSell() const noexcept { return Side == OrderSide::SELL; }
234 
236  bool IsGTC() const noexcept { return TimeInForce == OrderTimeInForce::GTC; }
238  bool IsIOC() const noexcept { return TimeInForce == OrderTimeInForce::IOC; }
240  bool IsFOK() const noexcept { return TimeInForce == OrderTimeInForce::FOK; }
242  bool IsAON() const noexcept { return TimeInForce == OrderTimeInForce::AON; }
243 
245  bool IsHidden() const noexcept { return MaxVisibleQuantity == 0; }
247  bool IsIceberg() const noexcept { return MaxVisibleQuantity < std::numeric_limits<uint64_t>::max(); }
248 
250  bool IsSlippage() const noexcept { return Slippage < std::numeric_limits<uint64_t>::max(); }
251 
253  ErrorCode Validate() const noexcept;
254 
256  static Order Market(uint64_t id, uint32_t symbol, OrderSide side, uint64_t quantity, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
258  static Order BuyMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
260  static Order SellMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
261 
263  static Order Limit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
265  static Order BuyLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
267  static Order SellLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
268 
270  static Order Stop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
272  static Order BuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
274  static Order SellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
275 
277  static Order StopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
279  static Order BuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
281  static Order SellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
282 
284  static Order TrailingStop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
286  static Order TrailingBuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
288  static Order TrailingSellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t slippage = std::numeric_limits<uint64_t>::max()) noexcept;
289 
291  static Order TrailingStopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
293  static Order TrailingBuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
295  static Order TrailingSellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step = 0, OrderTimeInForce tif = OrderTimeInForce::GTC, uint64_t max_visible_quantity = std::numeric_limits<uint64_t>::max()) noexcept;
296 };
297 
298 struct LevelNode;
299 
301 struct OrderNode : public Order, public CppCommon::List<OrderNode>::Node
302 {
304 
305  OrderNode(const Order& order) noexcept;
306  OrderNode(const OrderNode&) noexcept = default;
307  OrderNode(OrderNode&&) noexcept = default;
308  ~OrderNode() noexcept = default;
309 
310  OrderNode& operator=(const Order& order) noexcept;
311  OrderNode& operator=(const OrderNode&) noexcept = default;
312  OrderNode& operator=(OrderNode&&) noexcept = default;
313 };
314 
315 } // namespace Matching
316 } // namespace CppTrader
317 
318 #include "order.inl"
319 
320 #endif // CPPTRADER_MATCHING_ORDER_H
Errors definition.
OrderSide
Order side.
Definition: order.h:34
ErrorCode
Error code.
Definition: errors.h:21
OrderTimeInForce
Order Time in Force.
Definition: order.h:110
OrderType
Order type.
Definition: order.h:82
TOutputStream & operator<<(TOutputStream &stream, ErrorCode error)
Definition: errors.inl:13
C++ Trader project definitions.
Definition: errors.h:16
Price level node.
Definition: level.h:79
bool IsHidden() const noexcept
Is the 'Hidden' order?
Definition: order.h:245
bool IsFOK() const noexcept
Is the 'Fill-Or-Kill' order?
Definition: order.h:240
uint64_t VisibleQuantity() const noexcept
Order visible quantity.
Definition: order.h:166
ErrorCode Validate() const noexcept
Validate order parameters.
Definition: order.cpp:14
bool IsIceberg() const noexcept
Is the 'Iceberg' order?
Definition: order.h:247
bool IsStopLimit() const noexcept
Is the stop-limit order?
Definition: order.h:224
static Order SellLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell limit order.
Definition: order.inl:153
static Order BuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy stop-limit order.
Definition: order.inl:178
static Order BuyMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy market order.
Definition: order.inl:133
Order() noexcept=default
static Order TrailingBuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing buy stop order.
Definition: order.inl:193
static Order Stop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new stop order.
Definition: order.inl:158
bool IsAON() const noexcept
Is the 'All-Or-None' order?
Definition: order.h:242
OrderSide Side
Order side.
Definition: order.h:136
uint64_t Price
Order price.
Definition: order.h:138
uint64_t MaxVisibleQuantity
Order max visible quantity.
Definition: order.h:162
bool IsTrailingStop() const noexcept
Is the trailing stop order?
Definition: order.h:226
int64_t TrailingStep
Order trailing step.
Definition: order.h:198
bool IsSlippage() const noexcept
Is the order have slippage?
Definition: order.h:250
uint64_t ExecutedQuantity
Order executed quantity.
Definition: order.h:145
static Order TrailingStop(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing stop order.
Definition: order.inl:188
static Order Market(uint64_t id, uint32_t symbol, OrderSide side, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new market order.
Definition: order.inl:128
static Order SellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell stop-limit order.
Definition: order.inl:183
bool IsGTC() const noexcept
Is the 'Good-Till-Cancelled' order?
Definition: order.h:236
static Order TrailingStopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing stop-limit order.
Definition: order.inl:203
bool IsBuy() const noexcept
Is the order with buy side?
Definition: order.h:231
static Order StopLimit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t stop_price, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new stop-limit order.
Definition: order.inl:173
static Order Limit(uint64_t id, uint32_t symbol, OrderSide side, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new limit order.
Definition: order.inl:143
uint64_t LeavesQuantity
Order leaves quantity.
Definition: order.h:147
uint64_t StopPrice
Order stop price.
Definition: order.h:140
bool IsLimit() const noexcept
Is the limit order?
Definition: order.h:220
OrderType Type
Order type.
Definition: order.h:134
static Order SellMarket(uint64_t id, uint32_t symbol, uint64_t quantity, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell market order.
Definition: order.inl:138
OrderTimeInForce TimeInForce
Time in Force.
Definition: order.h:150
static Order SellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new sell stop order.
Definition: order.inl:168
uint64_t HiddenQuantity() const noexcept
Order hidden quantity.
Definition: order.h:164
static Order BuyLimit(uint64_t id, uint32_t symbol, uint64_t price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy limit order.
Definition: order.inl:148
bool IsMarket() const noexcept
Is the market order?
Definition: order.h:218
int64_t TrailingDistance
Order trailing distance to market.
Definition: order.h:189
static Order TrailingBuyStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing buy stop-limit order.
Definition: order.inl:208
uint64_t Id
Order Id.
Definition: order.h:130
bool IsIOC() const noexcept
Is the 'Immediate-Or-Cancel' order?
Definition: order.h:238
bool IsStop() const noexcept
Is the stop order?
Definition: order.h:222
uint32_t SymbolId
Symbol Id.
Definition: order.h:132
bool IsTrailingStopLimit() const noexcept
Is the trailing stop-limit order?
Definition: order.h:228
static Order BuyStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new buy stop order.
Definition: order.inl:163
uint64_t Slippage
Market order slippage.
Definition: order.h:179
static Order TrailingSellStop(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t slippage=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing sell stop order.
Definition: order.inl:198
uint64_t Quantity
Order quantity.
Definition: order.h:143
bool IsSell() const noexcept
Is the order with sell side?
Definition: order.h:233
static Order TrailingSellStopLimit(uint64_t id, uint32_t symbol, uint64_t stop_price, uint64_t price, uint64_t quantity, int64_t trailing_distance, int64_t trailing_step=0, OrderTimeInForce tif=OrderTimeInForce::GTC, uint64_t max_visible_quantity=std::numeric_limits< uint64_t >::max()) noexcept
Prepare a new trailing sell stop-limit order.
Definition: order.inl:213
OrderNode(const OrderNode &) noexcept=default
OrderNode(OrderNode &&) noexcept=default