diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index 9f28434..88ba087 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -1,4 +1,6 @@ add_library(engine STATIC + csv-engine-listener.cc + csv-engine-listener.hh engine-listener.cc engine-listener.hh ) diff --git a/src/engine/csv-engine-listener.cc b/src/engine/csv-engine-listener.cc new file mode 100644 index 0000000..fb04801 --- /dev/null +++ b/src/engine/csv-engine-listener.cc @@ -0,0 +1,66 @@ +#include "csv-engine-listener.hh" + +namespace kraken::engine { + +CsvEngineListener::CsvEngineListener() = default; + +CsvEngineListener::~CsvEngineListener() = default; + +void CsvEngineListener::on_acknowledgement(User user, UserOrderId id) { + output_.emplace_back(csv::csv_line_type{ + "A", + std::to_string(int(user)), + std::to_string(int(id)), + }); +} + +void CsvEngineListener::on_bad_order(User, UserOrderId) { + // Nothing to do +} + +void CsvEngineListener::on_top_of_book_change(Side side, Price price, + Quantity quantity) { + output_.emplace_back(csv::csv_line_type{ + "B", + side == Side::BID ? "B" : "S", + std::to_string(int(price)), + std::to_string(int(quantity)), + }); +} + +void CsvEngineListener::on_top_of_book_change(Side side) { + output_.emplace_back(csv::csv_line_type{ + "B", + side == Side::BID ? "B" : "S", + "-", + "-", + }); +} + +void CsvEngineListener::on_rejection(User user, UserOrderId id) { + output_.emplace_back(csv::csv_line_type{ + "R", + std::to_string(int(user)), + std::to_string(int(id)), + }); +} + +void CsvEngineListener::on_match(User user_buy, UserOrderId id_bid, + User user_sell, UserOrderId id_ask, + Price price, Quantity quantity) { + output_.emplace_back(csv::csv_line_type{ + "T", + std::to_string(int(user_buy)), + std::to_string(int(id_bid)), + std::to_string(int(user_sell)), + std::to_string(int(id_ask)), + std::to_string(int(price)), + std::to_string(int(quantity)), + }); +} + +void CsvEngineListener::on_flush() { + // Nothing to do +} + +} // namespace kraken::engine diff --git a/src/engine/csv-engine-listener.hh b/src/engine/csv-engine-listener.hh new file mode 100644 index 0000000..593876b --- /dev/null +++ b/src/engine/csv-engine-listener.hh @@ -0,0 +1,42 @@ +#pragma once + +#include + +#include "csv/csv.hh" + +#include "engine-listener.hh" + +namespace kraken::engine { + +struct CsvEngineListener : EngineListener { + CsvEngineListener(); + + virtual ~CsvEngineListener(); + + /// Called when a new trade or cancel order has been acknowledged. + void on_acknowledgement(User user, UserOrderId id) override; + + /// Called when an order is invalid. + void on_bad_order(User user, UserOrderId id) override; + + /// Called when the top of the book changes. + void on_top_of_book_change(Side side, Price price, + Quantity total_quantity) override; + /// Called when the top of the book changes, no orders remaining on a side. + void on_top_of_book_change(Side side) override; + + /// Called when an order is rejected by the engine for crossing the book. + void on_rejection(User user, UserOrderId id) override; + + /// Called when an order is matched by the engine. + void on_match(User user_buy, UserOrderId id_bid, User user_sell, + UserOrderId id_ask, Price price, Quantity quantity) override; + + /// Called when a flush order is processed. + void on_flush() override; + +private: + csv::csv_type output_{}; +}; + +}; // namespace kraken::engine