Towards a persistent book.

This commit is contained in:
Pedro Pinto committed 2018-03-23 16:35:53 -04:00
1 parent bf01d8254e
commit 9392ba47e6
11 files changed
+230 -147

No files matched your search

@@ -3,7 +3,7 @@
#include "IDGenerator.h"
#include "lib/config/config.h"
#include "lib/config/protos/ExchangeSim.pb.h"
#include "lib/config/protos/ExchangeSimCfg.pb.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "lib/md/MDConsumer.h"
@@ -16,6 +16,7 @@
#include "server/sparkd/exchange_sim/book/Order.h"
#include "server/sparkd/exchange_sim/book/OrderMatcher.h"
#include "server/sparkd/exchange_sim/book/book_transform.h"
#include "server/sparkd/persistence/SqliteStore.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <fmt/format.h>
@@ -100,7 +101,9 @@ struct ExchangeSimApplication::PImpl {
consumer(api::VENUE_GLBX, co2::PLATFORM_MDP3ONIXS),
timer(utils::Dispatcher::background(), 200,
[=]() { producer.heartbeat(); }),
do_merge_with_md(config::get<config::ExchangeSim>().merge_with_md()) {}
do_merge_with_md(config::get<config::ExchangeSim>().merge_with_md()),
store(new persistence::SqliteStore("order_book"))
{}
IDGenerator m_generator;
md::MDProducer producer;
@@ -111,6 +114,7 @@ struct ExchangeSimApplication::PImpl {
std::map<std::string, ex_sim::OrderMatcher> matchers;
bool do_merge_with_md;
TimeStamp last_scan;
std::unique_ptr<persistence::IStore> store;
std::mutex lock;
};
@@ -185,23 +189,23 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
throw std::logic_error("Duplicate order");
ex_sim::Order order;
order.clordid = clOrdID;
order.sec_desc = sec_desc;
order.account = account;
order.owner = senderCompID;
order.target = targetCompID;
order.sender_sub_id = senderSubID;
order.side = convert(side);
order.type = convert(ordType);
order.price = Price(price);
order.quantity = Quantity(orderQty);
order.moi = convert(moi);
order.open_quantity = order.quantity;
order.executed_quantity = Quantity(0);
order.avg_executed_price = Price(0);
order.last_executed_price = Price(0);
order.last_executed_quantity = Quantity(0);
order.create_time = Timer::now();
order.set_clordid (clOrdID);
order.set_sec_desc(sec_desc);
order.set_account(account);
order.set_owner(senderCompID);
order.set_target(targetCompID);
order.set_sender_sub_id(senderSubID);
order.set_side(convert(side));
order.set_type(convert(ordType));
order.set_price(price);
order.set_quantity(orderQty);
order.set_moi(convert(moi));
order.set_open_quantity(order.quantity());
order.set_executed_quantity(0);
order.set_avg_executed_price(0);
order.set_last_executed_price(0);
order.set_last_executed_quantity(0);
*order.mutable_create_time() = utils::pb::pb_now();
updateOrder(order, FIX::OrdStatus_NEW);
std::vector<Order> updates;
@@ -246,8 +250,8 @@ void ExchangeSimApplication::onMessage(
throw std::logic_error("Unknown order");
else {
auto order_cpy = *order;
order_cpy.price = Price(price);
order_cpy.quantity = Quantity(orderQty);
order_cpy.set_price (price);
order_cpy.set_quantity(orderQty);
updateOrder(order_cpy, FIX::OrdStatus_REPLACED);
std::vector<Order> updates;
@@ -280,7 +284,7 @@ void ExchangeSimApplication::onMessage(const FIX42::OrderCancelRequest &message,
throw std::logic_error("Unknown order");
else {
auto order_cpy = *order;
order_cpy.open_quantity = Quantity(0);
order_cpy.set_open_quantity(0);
updateOrder(order_cpy, FIX::OrdStatus_CANCELED);
matcher.cancel(origClOrdID);
update_md(sec_desc);
@@ -295,15 +299,15 @@ void ExchangeSimApplication::rejectOrder(const Order &order) {
void ExchangeSimApplication::fillOrder(const Order &order) {
updateOrder(order,
order.open_quantity.value() == 0
order.open_quantity() == 0
? FIX::OrdStatus_FILLED
: FIX::OrdStatus_PARTIALLY_FILLED);
}
void ExchangeSimApplication::updateOrder(const Order &order, char status) {
FIX::TargetCompID targetCompID(order.owner);
FIX::SenderCompID senderCompID(order.target);
FIX::TargetSubID(order.sender_sub_id);
FIX::TargetCompID targetCompID(order.owner());
FIX::SenderCompID senderCompID(order.target());
FIX::TargetSubID(order.sender_sub_id());
FIX42::ExecutionReport fixOrder;
fixOrder.set(FIX::OrderID("12345678901234567"));
@@ -311,20 +315,20 @@ void ExchangeSimApplication::updateOrder(const Order &order, char status) {
fixOrder.set(FIX::ExecTransType(FIX::ExecTransType_NEW));
fixOrder.set(FIX::ExecType(status));
fixOrder.set(FIX::OrdStatus(status));
fixOrder.set(FIX::Account(order.account));
fixOrder.set(FIX::SecurityDesc(order.sec_desc));
fixOrder.set(FIX::Side(convert(order.side)));
fixOrder.set(FIX::LeavesQty(order.open_quantity.value()));
fixOrder.set(FIX::CumQty(order.executed_quantity.value()));
fixOrder.set(FIX::AvgPx(order.avg_executed_price.value()));
fixOrder.set(FIX::ClOrdID(order.clordid));
fixOrder.set(FIX::OrderQty(order.quantity.value()));
fixOrder.set(FIX::Account(order.account()));
fixOrder.set(FIX::SecurityDesc(order.sec_desc()));
fixOrder.set(FIX::Side(convert(order.side())));
fixOrder.set(FIX::LeavesQty(order.open_quantity()));
fixOrder.set(FIX::CumQty(order.executed_quantity()));
fixOrder.set(FIX::AvgPx(order.avg_executed_price()));
fixOrder.set(FIX::ClOrdID(order.clordid()));
fixOrder.set(FIX::OrderQty(order.quantity()));
fixOrder.set(FIX::TimeInForce(FIX::TimeInForce_DAY));
fixOrder.set(FIX::OrdType(convert(order.type)));
fixOrder.set(FIX::Price(order.price.value()));
fixOrder.set(FIX::OrderQty(order.quantity.value()));
fixOrder.set(FIX::OrdType(convert(order.type())));
fixOrder.set(FIX::Price(order.price()));
fixOrder.set(FIX::OrderQty(order.quantity()));
fixOrder.setField(convert(order.moi));
fixOrder.setField(convert(order.moi()));
boost::posix_time::ptime now =
boost::posix_time::second_clock::universal_time();
@@ -335,11 +339,11 @@ void ExchangeSimApplication::updateOrder(const Order &order, char status) {
true));
if (status == FIX::OrdStatus_FILLED ||
status == FIX::OrdStatus_PARTIALLY_FILLED) {
fixOrder.set(FIX::LastShares(order.last_executed_quantity.value()));
fixOrder.set(FIX::LastPx(order.last_executed_price.value()));
fixOrder.set(FIX::LastShares(order.last_executed_quantity()));
fixOrder.set(FIX::LastPx(order.last_executed_price()));
}
fixOrder.getHeader().set(FIX::TargetSubID(order.sender_sub_id));
fixOrder.getHeader().set(FIX::TargetSubID(order.sender_sub_id()));
try {
FIX::Session::sendToTarget(fixOrder, senderCompID, targetCompID);
} catch (FIX::SessionNotFound &) {
@@ -518,34 +522,34 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
switch (op.op) {
case BookOp::ORDER: {
ex_sim::Order order;
order.clordid = fmt::format("synth-{}", me->m_generator.genOrderID());
order.sec_desc = sec_desc;
order.account = "SYNTH";
order.owner = "SYNTH";
order.target = "SYNTH";
order.sender_sub_id = "SYNTH";
order.side = op.side;
order.type = api::orderman::NATIVE_ORDER_TYPE_LIMIT;
order.price = op.price;
order.quantity = op.quantity;
order.moi = api::orderman::MANUAL_ORDER_INDICATOR_NO;
order.open_quantity = order.quantity;
order.executed_quantity = Quantity(0);
order.avg_executed_price = Price(0);
order.last_executed_price = Price(0);
order.last_executed_quantity = Quantity(0);
order.create_time = Timer::now();
order.set_clordid (fmt::format("synth-{}", me->m_generator.genOrderID()));
order.set_sec_desc(sec_desc);
order.set_account("SYNTH");
order.set_owner("SYNTH");
order.set_target("SYNTH");
order.set_sender_sub_id("SYNTH");
order.set_side(op.side);
order.set_type(api::orderman::NATIVE_ORDER_TYPE_LIMIT);
order.set_price(op.price.value());
order.set_quantity(op.quantity.value());
order.set_moi(api::orderman::MANUAL_ORDER_INDICATOR_NO);
order.set_open_quantity(order.quantity());
order.set_executed_quantity(0);
order.set_avg_executed_price(0);
order.set_last_executed_price(0);
order.set_last_executed_quantity(0);
*order.mutable_create_time() = utils::pb::pb_now();
std::vector<Order> updates;
matcher.add(order, &updates);
for (auto const &o : updates)
if (o.account != "SYNTH") {
if (o.account() != "SYNTH") {
LOG(ExSim, Diag,
"Generating fill in response to md change for {}: {} {}@{}",
sec_desc,
o.side == api::SIDE_BUY ? std::string("Buy")
o.side() == api::SIDE_BUY ? std::string("Buy")
: std::string("Sell"),
o.open_quantity.value(), o.price.value());
o.open_quantity(), o.price());
fillOrder(o);
}
break;
@@ -557,8 +561,23 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
}
update_md(sec_desc);
auto order_book = matcher.get_order_book("SYNTH");
order_book.set_sec_desc(sec_desc);
persist_book(order_book);
}
void ExchangeSimApplication::persist_book(OrderBook const &b)
{
auto existing = me->store->retrieve_one("books",b.sec_desc());
auto val = utils::pb::to_string(b);
if (existing.first)
me->store->update("books", b.sec_desc(), val);
else
me->store->insert("books", b.sec_desc(), val);
}
GCC_DIAG_RESTORE()
GCC_DIAG_RESTORE()
}