CBOE Emulator  1.0
messages.hpp
1 // Structures for limit order book depth messages.
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef DATA_FEED_MESSAGES_HPP
19 #define DATA_FEED_MESSAGES_HPP
20 
21 #include <array>
22 #include <chrono>
23 #include <deque>
24 #include <iostream>
25 #include <sstream>
26 #include "limit_order_book/structures.hpp"
27 #include "clock.hpp"
28 
30 namespace DataFeed {
31 
34 
36 typedef uint32_t SequenceNumber;
37 
39 typedef uint64_t OrderID;
40 
42 enum class Side : char { Sell = 'S', Buy = 'B' };
43 
50 inline std::ostream& operator<<(std::ostream& stream, const Side& side) {
51  stream << static_cast<char>(side);
52  return stream;
53 }
54 
60 inline constexpr double side_to_double(Side side) {
61  // 'B' is 66 and 'S' is larger. subtracting 66 and performing a boolean
62  // condition allows fast conversion from char to double
63  return (-2 * static_cast<bool>(static_cast<char>(side) - 'B')) + 1;
64 }
65 
71 inline constexpr bool side_to_bool(Side side) {
72  // 'B' is 66 and 'S' is larger. subtracting 66 and performing a boolean
73  // condition allows fast conversion from char to double
74  return not (static_cast<char>(side) - 'B');
75 }
76 
82 inline constexpr LOB::Side side_to_LOB_side(Side side) {
83  return static_cast<LOB::Side>(side_to_bool(side));
84 }
85 
91 inline constexpr Side bool_to_side(bool side) {
92  // B is ASCII 66
93  // S is ASCII 83
94  // start with ASCII S, which is larger than ASCII B, if side is true remove
95  // the difference between ASCII S and B to get ASCII B
96  return static_cast<Side>('S' - side * ('S' - 'B'));
97 }
98 
100 typedef uint32_t Quantity;
101 
103 typedef uint64_t Price;
104 
106 namespace Messages {
107 
110 typedef std::array<char, 40> Packet;
111 
113 typedef std::deque<Packet> PacketQueue;
114 
116 enum class MessageID : char {
117  Clear = 'c', // clear the book
118  AddOrder = 'a', // add an order
119  DeleteOrder = 'd', // delete an order
120  Trade = 't', // a trade occurred
121  StartOfSession = 's', // start of trading
122  EndOfSession = 'e', // end of trading
123 };
124 
131 inline std::ostream& operator<<(std::ostream& stream, const MessageID& uid) {
132  stream << static_cast<char>(uid);
133  return stream;
134 }
135 
136 // ---------------------------------------------------------------------------
137 // MARK: Header
138 // ---------------------------------------------------------------------------
139 
145 struct Header {
147  const uint16_t length;
149  const MessageID uid;
151  const uint8_t padding = 0;
156 
165  uint16_t length_,
166  MessageID uid_,
167  SequenceNumber sequence_ = 0,
168  TimeStamp time_ = 0
169  ) :
170  length(length_),
171  uid(uid_),
172  sequence(sequence_),
173  time(time_) { }
174 
181  inline friend std::ostream& operator<<(
182  std::ostream& stream,
183  const Header& header
184  ) {
185  stream << "Header("
186  << "length=" << static_cast<int>(header.length) << ","
187  << "uid='" << header.uid << "',"
188  << "sequence=" << header.sequence << ","
189  << "time=" << header.time << ")";
190  return stream;
191  }
192 
197  inline std::string to_string() const {
198  std::ostringstream stream;
199  stream << this;
200  return stream.str();
201  }
202 } __attribute__ ((packed));
203 
204 // ---------------------------------------------------------------------------
205 // MARK: Clear
206 // ---------------------------------------------------------------------------
207 
213 struct Clear {
215  const Header header;
216 
222  Clear(SequenceNumber sequence = 0, TimeStamp time = 0) :
223  header{sizeof(Clear), MessageID::Clear, sequence, time} { }
224 
231  inline friend std::ostream& operator<<(
232  std::ostream& stream,
233  const Clear& message
234  ) {
235  stream << "Clear(" << message.header << ")";
236  return stream;
237  }
238 
243  inline std::string to_string() const {
244  std::ostringstream stream;
245  stream << this;
246  return stream.str();
247  }
248 } __attribute__ ((packed));
249 
250 // ---------------------------------------------------------------------------
251 // MARK: AddOrder
252 // ---------------------------------------------------------------------------
253 
259 struct AddOrder {
261  const Header header;
263  const OrderID uid;
265  const Price price;
269  const Side side;
270 
281  OrderID uid_,
282  Price price_,
283  Quantity quantity_,
284  Side side_,
285  SequenceNumber sequence = 0,
286  TimeStamp time = 0
287  ) :
288  header{sizeof(AddOrder), MessageID::AddOrder, sequence, time},
289  uid(uid_),
290  price(price_),
291  quantity(quantity_),
292  side(side_) { }
293 
300  inline friend std::ostream& operator<<(
301  std::ostream& stream,
302  const AddOrder& message
303  ) {
304  stream << "AddOrder(" << message.header << ","
305  << "uid=" << message.uid << ","
306  << "price=" << message.price << ","
307  << "quantity=" << message.quantity << ","
308  << "side='" << message.side << "')";
309  return stream;
310  }
311 
316  inline std::string to_string() const {
317  std::ostringstream stream;
318  stream << this;
319  return stream.str();
320  }
321 } __attribute__ ((packed));
322 
323 // ---------------------------------------------------------------------------
324 // MARK: DeleteOrder
325 // ---------------------------------------------------------------------------
326 
332 struct DeleteOrder {
334  const Header header;
336  const OrderID uid;
337 
344  explicit DeleteOrder(
345  OrderID uid_,
346  SequenceNumber sequence = 0,
347  TimeStamp time = 0
348  ) :
349  header{sizeof(DeleteOrder), MessageID::DeleteOrder, sequence, time},
350  uid(uid_) { }
351 
358  inline friend std::ostream& operator<<(
359  std::ostream& stream,
360  const DeleteOrder& message
361  ) {
362  stream << "DeleteOrder(" << message.header << ","
363  << "uid=" << message.uid << ")";
364  return stream;
365  }
366 
371  inline std::string to_string() const {
372  std::ostringstream stream;
373  stream << this;
374  return stream.str();
375  }
376 } __attribute__ ((packed));
377 
378 // ---------------------------------------------------------------------------
379 // MARK: Trade
380 // ---------------------------------------------------------------------------
381 
387 struct Trade {
389  const Header header;
391  const OrderID uid;
393  const Price price;
397  const Side side;
398 
409  OrderID uid_,
410  Price price_,
411  Quantity quantity_,
412  Side side_,
413  SequenceNumber sequence = 0,
414  TimeStamp time = 0
415  ) :
416  header{sizeof(Trade), MessageID::Trade, sequence, time},
417  uid(uid_),
418  price(price_),
419  quantity(quantity_),
420  side(side_) { }
421 
428  inline friend std::ostream& operator<<(
429  std::ostream& stream,
430  const Trade& message
431  ) {
432  stream << "Trade(" << message.header << ","
433  << "uid=" << message.uid << ","
434  << "price=" << message.price << ","
435  << "quantity=" << message.quantity << ","
436  << "side='" << message.side << "')";
437  return stream;
438  }
439 
444  inline std::string to_string() const {
445  std::ostringstream stream;
446  stream << this;
447  return stream.str();
448  }
449 } __attribute__ ((packed));
450 
451 // ---------------------------------------------------------------------------
452 // MARK: StartOfSession
453 // ---------------------------------------------------------------------------
454 
462  const Header header;
463 
469  StartOfSession(SequenceNumber sequence = 0, TimeStamp time = 0) :
470  header{sizeof(StartOfSession), MessageID::StartOfSession, sequence, time} { }
471 
478  inline friend std::ostream& operator<<(
479  std::ostream& stream,
480  const StartOfSession& message
481  ) {
482  stream << "StartOfSession(" << message.header << ")";
483  return stream;
484  }
485 
490  inline std::string to_string() const {
491  std::ostringstream stream;
492  stream << this;
493  return stream.str();
494  }
495 } __attribute__ ((packed));
496 
497 // ---------------------------------------------------------------------------
498 // MARK: EndOfSession
499 // ---------------------------------------------------------------------------
500 
506 struct EndOfSession {
508  const Header header;
509 
515  EndOfSession(SequenceNumber sequence = 0, TimeStamp time = 0) :
516  header{sizeof(EndOfSession), MessageID::EndOfSession, sequence, time} { }
517 
524  inline friend std::ostream& operator<<(
525  std::ostream& stream,
526  const EndOfSession& message
527  ) {
528  stream << "EndOfSession(" << message.header << ")";
529  return stream;
530  }
531 
536  inline std::string to_string() const {
537  std::ostringstream stream;
538  stream << this;
539  return stream.str();
540  }
541 } __attribute__ ((packed));
542 
543 } // namespace Messages
544 
545 } // namespace DataFeed
546 
547 #endif // DATA_FEED_MESSAGES_HPP
DataFeed::Messages::DeleteOrder::operator<<
friend std::ostream & operator<<(std::ostream &stream, const DeleteOrder &message)
Write the data from the message to a stream.
Definition: messages.hpp:358
DataFeed::Messages::Header::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:197
DataFeed::Messages::Trade::Trade
Trade(OrderID uid_, Price price_, Quantity quantity_, Side side_, SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new trade message.
Definition: messages.hpp:408
DataFeed::Messages::Header::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Header &header)
Write the data from the header to a stream.
Definition: messages.hpp:181
DataFeed::Messages::AddOrder::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:316
DataFeed::Messages::Trade
A message that indicates a market order matches with a limit order.
Definition: messages.hpp:387
DataFeed::LOB::Side
Side
the possible sides for the LimitTree
Definition: structures.hpp:32
DataFeed::TimeStamp
Clock::TimeStamp TimeStamp
A forward a declaration for the Clock timestamp.
Definition: messages.hpp:33
DataFeed::operator<<
std::ostream & operator<<(std::ostream &stream, const Side &side)
Write the order side to a stream.
Definition: messages.hpp:50
DataFeed::Messages::Trade::quantity
const Quantity quantity
the quantity of the trade
Definition: messages.hpp:395
DataFeed::Messages::Trade::uid
const OrderID uid
the MessageID for the limit order that matched
Definition: messages.hpp:391
DataFeed::Messages::Header::sequence
const SequenceNumber sequence
sequence number of the message (event time)
Definition: messages.hpp:153
DataFeed::Messages::Header::Header
Header(uint16_t length_, MessageID uid_, SequenceNumber sequence_=0, TimeStamp time_=0)
Initialize a new session header.
Definition: messages.hpp:164
DataFeed::Messages::DeleteOrder
A message that indicates a limit order was added to the book.
Definition: messages.hpp:332
DataFeed::Messages::Trade::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:389
DataFeed::Messages::Trade::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Trade &message)
Write the data from the message to a stream.
Definition: messages.hpp:428
DataFeed::Messages::EndOfSession
A message that indicates the end of a trading session.
Definition: messages.hpp:506
DataFeed::Messages::Clear
A message that indicates to clear all orders in the order book.
Definition: messages.hpp:213
DataFeed::OrderID
uint64_t OrderID
A type for order prices.
Definition: messages.hpp:39
DataFeed::SequenceNumber
uint32_t SequenceNumber
A type for sequence numbers.
Definition: messages.hpp:36
DataFeed::Messages::StartOfSession::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:490
DataFeed::Messages::EndOfSession::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:536
DataFeed::Messages::StartOfSession
A message that indicates the start of a trading session.
Definition: messages.hpp:460
DataFeed::Messages::AddOrder::AddOrder
AddOrder(OrderID uid_, Price price_, Quantity quantity_, Side side_, SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new add order message.
Definition: messages.hpp:280
DataFeed::Messages::DeleteOrder::uid
const OrderID uid
the day specific ID for the order to delete
Definition: messages.hpp:336
DataFeed::Messages::AddOrder::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:261
DataFeed::Messages::AddOrder::uid
const OrderID uid
the day specific ID for the order
Definition: messages.hpp:263
DataFeed::Messages::Header
A header containing type information and metadata for a message.
Definition: messages.hpp:145
DataFeed
Logic for sending and receiving messages on a financial data feed.
Definition: heartbeat.hpp:28
DataFeed::side_to_bool
constexpr bool side_to_bool(Side side)
Convert an order side character to a boolean value.
Definition: messages.hpp:71
DataFeed::Messages::PacketQueue
std::deque< Packet > PacketQueue
A type for queuing packet buffers.
Definition: messages.hpp:113
DataFeed::Price
uint64_t Price
A type for order prices.
Definition: messages.hpp:103
DataFeed::Messages::StartOfSession::operator<<
friend std::ostream & operator<<(std::ostream &stream, const StartOfSession &message)
Write the data from the message to a stream.
Definition: messages.hpp:478
DataFeed::Quantity
uint32_t Quantity
A type for order quantities.
Definition: messages.hpp:100
DataFeed::Messages::Trade::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:444
Clock::TimeStamp
uint64_t TimeStamp
a type for timestamps
Definition: clock.hpp:28
DataFeed::Messages::operator<<
std::ostream & operator<<(std::ostream &stream, const MessageID &uid)
Definition: messages.hpp:131
DataFeed::Messages::DeleteOrder::DeleteOrder
DeleteOrder(OrderID uid_, SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new delete order message.
Definition: messages.hpp:344
DataFeed::Messages::MessageID
MessageID
Message IDs for messages in the protocol.
Definition: messages.hpp:116
DataFeed::Messages::Clear::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:215
DataFeed::side_to_LOB_side
constexpr LOB::Side side_to_LOB_side(Side side)
Convert a side character to a LOB side value.
Definition: messages.hpp:82
DataFeed::Messages::StartOfSession::StartOfSession
StartOfSession(SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new start of session message.
Definition: messages.hpp:469
DataFeed::Side
Side
The side of an order.
Definition: messages.hpp:42
DataFeed::Messages::Trade::side
const Side side
the side of the market order
Definition: messages.hpp:397
DataFeed::Messages::EndOfSession::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:508
DataFeed::Messages::DeleteOrder::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:371
DataFeed::bool_to_side
constexpr Side bool_to_side(bool side)
Convert a boolean to an order side.
Definition: messages.hpp:91
DataFeed::Messages::Header::uid
const MessageID uid
the template ID for the message
Definition: messages.hpp:149
DataFeed::Messages::Header::time
const TimeStamp time
the time for the message
Definition: messages.hpp:155
DataFeed::Messages::Clear::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Clear &message)
Write the data from the message to a stream.
Definition: messages.hpp:231
DataFeed::Messages::AddOrder::price
const Price price
the price of the order
Definition: messages.hpp:265
DataFeed::Messages::DeleteOrder::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:334
DataFeed::Messages::Trade::price
const Price price
the execution price for the trade
Definition: messages.hpp:393
DataFeed::Messages::AddOrder::operator<<
friend std::ostream & operator<<(std::ostream &stream, const AddOrder &message)
Write the data from the message to a stream.
Definition: messages.hpp:300
DataFeed::Messages::Header::length
const uint16_t length
the length of the message in bytes
Definition: messages.hpp:147
DataFeed::Messages::AddOrder::quantity
const Quantity quantity
the quantity of the order
Definition: messages.hpp:267
DataFeed::side_to_double
constexpr double side_to_double(Side side)
Convert an order side character to a double.
Definition: messages.hpp:60
DataFeed::Messages::EndOfSession::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EndOfSession &message)
Write the data from the message to a stream.
Definition: messages.hpp:524
DataFeed::Messages::StartOfSession::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:462
DataFeed::Messages::EndOfSession::EndOfSession
EndOfSession(SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new end of session message.
Definition: messages.hpp:515
DataFeed::Messages::Header::padding
const uint8_t padding
arbitrary padding to align the sequence number in memory
Definition: messages.hpp:151
DataFeed::Messages::AddOrder
A message that indicates a limit order was added to the book.
Definition: messages.hpp:259
DataFeed::Messages::Clear::Clear
Clear(SequenceNumber sequence=0, TimeStamp time=0)
Initialize a new clear book message.
Definition: messages.hpp:222
DataFeed::Messages::Packet
std::array< char, 40 > Packet
Definition: messages.hpp:110
DataFeed::Messages::Clear::to_string
std::string to_string() const
Convert the object to an STL string.
Definition: messages.hpp:243
DataFeed::Messages::AddOrder::side
const Side side
the side of the order
Definition: messages.hpp:269