CBOE Emulator  1.0
csv.hpp
1 // A data feed handler that writes LOB::LimitOrderBook data in csv file format.
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_HANDLERS_CSV_HPP
19 #define DATA_FEED_HANDLERS_CSV_HPP
20 
21 #include "data_feed/receiver.hpp"
22 #include <ostream>
23 
25 namespace DataFeed {
26 
28 namespace Handlers {
29 
31 class CSV {
32  private:
34  std::ostream& stream;
35 
37  inline void print_header() {
38  stream
39  << "event_time,"
40  << "message_time,"
41  << "receipt_time,"
42  << "buy_volume,"
43  << "buy_size,"
44  << "buy_price,"
45  << "sell_volume,"
46  << "sell_size,"
47  << "sell_price,"
48  << "sell_adds,"
49  << "buy_adds,"
50  << "cancels,"
51  << "sell_trades,"
52  << "buy_trades"
53  << std::endl;
54  }
55 
60  inline void print(const Messages::Header& header) {
61  stream
62  << header.sequence << ","
63  << header.time << ","
64  << Clock::get_time() << ",";
65  }
66 
71  inline void print(const LOB::LimitOrderBook& book) {
72  stream
73  << book.volume_buy() << ","
74  << book.count_buy() << ","
75  << book.last_best_buy() << ","
76  << book.volume_sell() << ","
77  << book.count_sell() << ","
78  << book.last_best_sell();
79  }
80 
81  public:
86  CSV(std::ostream& stream_ = std::cout) : stream(stream_) { print_header(); }
87 
93  inline void did_receive(Receiver<CSV>* receiver, const Messages::StartOfSession& message) {
94  print(message.header);
95  print(receiver->get_book());
96  stream << ",0,0,0,0,0" << std::endl;
97  }
98 
104  inline void did_receive(Receiver<CSV>* receiver, const Messages::EndOfSession& message) {
105  print(message.header);
106  print(receiver->get_book());
107  stream << ",1,1,1,1,1" << std::endl;
108  }
109 
115  inline void did_receive(Receiver<CSV>* receiver, const Messages::Clear& message) {
116  std::cerr << "CSV::did_receive() - clear message" << std::endl;
117  }
118 
124  inline void did_receive(Receiver<CSV>* receiver, const Messages::AddOrder& message) {
125  if (not receiver->is_session_active())
126  return;
127  print(message.header);
128  print(receiver->get_book());
129  // add order
130  auto is_buy = DataFeed::side_to_bool(message.side);
131  stream << "," << !is_buy << "," << is_buy << ",0,0,0" << std::endl;
132  }
133 
139  inline void did_receive(Receiver<CSV>* receiver, const Messages::DeleteOrder& message) {
140  if (not receiver->is_session_active())
141  return;
142  print(message.header);
143  print(receiver->get_book());
144  // delete order
145  stream << ",0,0,1,0,0" << std::endl;
146 
147  }
148 
154  inline void did_receive(Receiver<CSV>* receiver, const Messages::Trade& message) {
155  if (not receiver->is_session_active())
156  return;
157  print(message.header);
158  print(receiver->get_book());
159  // trade
160  auto is_buy = DataFeed::side_to_bool(message.side);
161  stream << ",0,0,0," << !is_buy << "," << is_buy << std::endl;
162  }
163 };
164 
165 } // namespace Handlers
166 
167 } // namespace DataFeed
168 
169 #endif // DATA_FEED_HANDLERS_CSV_HPP
DataFeed::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::StartOfSession &message)
Handle a start of session message.
Definition: csv.hpp:93
DataFeed::Receiver
A multi-cast receiver for recreating a LOB::LimitOrderBook from network messages.
Definition: receiver.hpp:36
DataFeed::Messages::Trade
A message that indicates a market order matches with a limit order.
Definition: messages.hpp:387
DataFeed::LOB::LimitOrderBook::last_best_sell
Price last_best_sell() const
Return the last best sell price.
Definition: limit_order_book.hpp:262
DataFeed::Handlers::CSV
A Receiver handler that writes LOB::LimitOrderBook event data in csv format.
Definition: csv.hpp:31
DataFeed::Messages::DeleteOrder
A message that indicates a limit order was added to the book.
Definition: messages.hpp:332
DataFeed::Receiver::get_book
const LOB::LimitOrderBook & get_book()
Return the limit order book for this receiver.
Definition: receiver.hpp:214
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::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::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::DeleteOrder &message)
Handle a delete order message.
Definition: csv.hpp:139
DataFeed::LOB::LimitOrderBook
An order book for managing Limit / Order objects in a continuous double auction.
Definition: limit_order_book.hpp:41
DataFeed::Messages::StartOfSession
A message that indicates the start of a trading session.
Definition: messages.hpp:460
DataFeed::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::Clear &message)
Handle a clear book message.
Definition: csv.hpp:115
Clock::get_time
TimeStamp get_time()
Return the time in nanoseconds as a 64-bit unsigned integer (Timestamp).
Definition: clock.hpp:36
DataFeed::Messages::AddOrder::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:261
DataFeed::Handlers::CSV::CSV
CSV(std::ostream &stream_=std::cout)
Initialize a new data feed CSV handler.
Definition: csv.hpp:86
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::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::AddOrder &message)
Handle an add order message.
Definition: csv.hpp:124
DataFeed::Receiver::is_session_active
bool is_session_active() const
Return whether the trading session is active.
Definition: receiver.hpp:220
DataFeed::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::EndOfSession &message)
Handle an end of session message.
Definition: csv.hpp:104
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::LOB::LimitOrderBook::last_best_buy
Price last_best_buy() const
Return the last best buy price.
Definition: limit_order_book.hpp:268
DataFeed::LOB::LimitOrderBook::volume_buy
Volume volume_buy(Price price) const
Return the total volume for the buy side of the book.
Definition: limit_order_book.hpp:310
DataFeed::Handlers::CSV::did_receive
void did_receive(Receiver< CSV > *receiver, const Messages::Trade &message)
Handle a trade message.
Definition: csv.hpp:154
DataFeed::LOB::LimitOrderBook::count_buy
Count count_buy() const
Return the total number of orders on the buy-side of the book.
Definition: limit_order_book.hpp:348
DataFeed::Messages::DeleteOrder::header
const Header header
the message header that defines the template ID and length of message
Definition: messages.hpp:334
DataFeed::LOB::LimitOrderBook::count_sell
Count count_sell() const
Return the total number of orders on the sell-side of the book.
Definition: limit_order_book.hpp:345
DataFeed::LOB::LimitOrderBook::volume_sell
Volume volume_sell(Price price) const
Return the total volume for the sell side of the book.
Definition: limit_order_book.hpp:292
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::AddOrder
A message that indicates a limit order was added to the book.
Definition: messages.hpp:259
DataFeed::Messages::AddOrder::side
const Side side
the side of the order
Definition: messages.hpp:269