From 0cf4b5c2c8597f7bac1e9ea970c8cf20535fa592 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 9 Apr 2019 15:07:10 -0400 Subject: [PATCH] SPARK-2224 3/3 basic piq stream (unblocks jake) --- src/lib/api/services/MBOService.proto | 4 +- src/lib/md/ExchangeOrderKey.h | 7 + src/lib/md/MBOProcessor.h | 5 +- src/lib/md/MBOServiceServer.cpp | 156 ++++++++++++++++-- src/lib/md/relay/QueryServer.cpp | 19 --- .../md/platforms/mdp3_onixs/MDP3Listener.cpp | 4 +- 6 files changed, 156 insertions(+), 39 deletions(-) create mode 100644 src/lib/md/ExchangeOrderKey.h diff --git a/src/lib/api/services/MBOService.proto b/src/lib/api/services/MBOService.proto index 0a00fc22e..4a8383f8b 100644 --- a/src/lib/api/services/MBOService.proto +++ b/src/lib/api/services/MBOService.proto @@ -5,11 +5,11 @@ option cc_enable_arenas=true; import "lib/api/apioptions.proto"; import "lib/api/apicore.proto"; -message OrderPiq { +message OrderPIQ { uint32 order_id = 1 [(bts.api.key)=true]; uint32 position = 2; } service MBOService { - rpc piq (bts.api.VoidMessage) returns (stream OrderPiq); + rpc piq (bts.api.VoidMessage) returns (stream OrderPIQ); } \ No newline at end of file diff --git a/src/lib/md/ExchangeOrderKey.h b/src/lib/md/ExchangeOrderKey.h new file mode 100644 index 000000000..b3d59e459 --- /dev/null +++ b/src/lib/md/ExchangeOrderKey.h @@ -0,0 +1,7 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +namespace bts { +namespace md { +using ExchangeOrderKey = utils::ValueWrapper; +using SparkOrderKey = utils::ValueWrapper; +} } \ No newline at end of file diff --git a/src/lib/md/MBOProcessor.h b/src/lib/md/MBOProcessor.h index bbb8c7e21..17246fd21 100644 --- a/src/lib/md/MBOProcessor.h +++ b/src/lib/md/MBOProcessor.h @@ -4,17 +4,16 @@ #include "lib/utils/TimeStamp.h" #include "lib/utils/ValueWrapper.h" #include "IntPrice.h" +#include "ExchangeOrderKey.h" #include #include namespace bts { namespace md { - -using OrderKey = utils::ValueWrapper; struct SnapshotOrder { - OrderKey id; + ExchangeOrderKey id; IntPrice price; int32_t size; }; diff --git a/src/lib/md/MBOServiceServer.cpp b/src/lib/md/MBOServiceServer.cpp index 2841633d3..6a9218a80 100644 --- a/src/lib/md/MBOServiceServer.cpp +++ b/src/lib/md/MBOServiceServer.cpp @@ -5,11 +5,17 @@ #include "lib/utils/Dispatcher.h" #include "lib/utils/TimeStamp.h" #include "MDClient.h" +#include "ExchangeOrderKey.h" +#include "MBOProcessor.h" #include "lib/utils/LockBox.h" #include #include +#include + + +using namespace bts::api::orderman; namespace bts { namespace md { @@ -61,10 +67,36 @@ private: } }; -struct ActiveOrder { - uint32_t id; - int64_t ex_id; - int32_t position; +class ActiveOrder { + SecurityKey _security; + SparkOrderKey _id; + ExchangeOrderKey _ex_id; + int32_t _position; + +public: + ActiveOrder(): _security(), _id(), _ex_id(), _position(-1) { bts_assert(false); } + ActiveOrder(SecurityKey sec, SparkOrderKey s, ExchangeOrderKey ex): _security(sec), _id(s), _ex_id(ex), _position(-1) {} + ActiveOrder(SecurityKey sec, SparkOrderKey s, const std::string& ex): _security(sec), _id(s), _ex_id(), _position(-1) { + _ex_id = ExchangeOrderKey(std::stoul(ex)); + } + + SparkOrderKey id() const { return _id; } + ExchangeOrderKey exchange_id() const { return _ex_id; } + int32_t& position() { return _position; }; + int32_t position() const { return _position; }; + bool set_position(int32_t pos) { + if (pos != _position) { + _position = pos; + return true; + } + return false; + } + OrderPIQ to_piq() const { + OrderPIQ result; + result.set_order_id(_id.value()); + result.set_position(_position); + return result; + } }; // @@ -81,33 +113,102 @@ struct MBOServiceImpl { client(c), orders(ctx.locate()), handle_index(c), - publisher(pub), + publish(pub), timer(d, 200, [&]() {timer_tick();}), orders_sub() {} utils::Dispatcher& dispatcher; md::MDClient& client; - api::orderman::OrderService orders; + OrderService orders; HandleIndex handle_index; - MBOServiceBase::PiqPublisher publisher; + MBOServiceBase::PiqPublisher publish; utils::DispatcherTimer timer; rxcpp::composite_subscription orders_sub; + std::unordered_map active; void timer_tick(); void flush(); }; void MBOServiceImpl::timer_tick() { - // for each active + //Dispatcher thread + std::vector rows; - // publisher.flush(); + publish(nullptr, &rows, nullptr); } + /////////////////////// +std::optional search_orders(const ExchangeOrderKey oid, const std::vector& orders) { + IntPrice price = {}; + size_t position = 0; + for (size_t x = 0; x < orders.size(); ++x) { + const auto& oo = orders[x]; + if (oo.id == oid) { + return { position }; + } + else if (price == oo.price) { + position += 1; + } + else { + price = oo.price; + position = 0; + } + } + return std::nullopt; +} + +bool update_position(const HandleIndex& , ActiveOrder& order) { + order.set_position(-2); + return true; +} + +const NativeOrderState* native_order_state(const OrderRecord& record) { + switch(record.state().custom_case()) { + case OrderState::kMarket: + return &record.state().market().native_state(); + case OrderState::kLimit: + return &record.state().limit().native_state(); + case OrderState::kStop: + return &record.state().stop().native_state(); + case OrderState::kStopLimit: + return &record.state().stop_limit().native_state(); + default: + return nullptr; + } +} + +bool is_active_exchange_order(const OrderRecord& record) { + return record.state().status() == ORDER_STATUS_WORKING && + native_order_state(record) != nullptr; +} + +void try_remove(MBOServiceImpl& me, std::set& to_remove, const OrderRecord& record) { + auto key = SparkOrderKey(record.key()); + auto iter = me.active.find(key); + if (iter != me.active.end()) { + me.active.erase(key); + to_remove.insert(key.value()); + } +} +void try_add(MBOServiceImpl& me, std::vector& to_add, const OrderRecord& record) { + if (is_active_exchange_order(record)) { + auto native = native_order_state(record); + bts_assert(native); + ActiveOrder order( + SecurityKey(record.def().security_key()), + SparkOrderKey(record.key()), + native->exchange_order_id()); + update_position(me.handle_index, order); + to_add.push_back(order.to_piq()); + me.active[order.id()] = order; + } +} + MBOServiceServer::MBOServiceServer( api::ServiceContext& ctx, utils::Dispatcher& d, @@ -121,12 +222,41 @@ MBOServiceServer::MBOServiceServer( // subscribe to orders => stash active orders dict _me->orders_sub = _me->orders.subscribe_orders().subscribe([=](const auto& update) { - const auto& added = update.added(); - const auto& removed = update.removed(); // api thread - _me->dispatcher.post([this,added,removed]() { + const auto& added = update.added(); + const auto& changed = update.changed(); + const auto& removed = update.removed(); + _me->dispatcher.post([this,added,changed,removed]() { // dispatcher thread - // handle add/remove/change + std::vector to_add, to_change; + std::set to_remove; + + // Remove + for(const auto& r : removed) { + // If it was active remove it and add to to_remove + try_remove(*_me, to_remove, *r); + } + // Added + for(const auto& add : added) { + // If active add look up the position and add to the state + try_add(*_me, to_add, *add); + } + // Changed + for(const auto& ch : changed) { + // new order is active and old is not look up piq and add to state + bool is_active = is_active_exchange_order(*ch.current()); + bool was_active = is_active_exchange_order(*ch.previous()); + if (is_active && !was_active) { + try_add(*_me, to_add, *ch.current()); + } + else if (was_active && !is_active) { + try_remove(*_me, to_remove, *ch.current()); + } + // is_active && was_active => don't care + // !is_active && !was_active => don't care + } + + _me->publish(&to_add, &to_change, &to_remove); }); }); } diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index f7f8c9b84..4dc971d81 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -393,25 +393,6 @@ public: } } } - - static std::optional search_orders(const OrderKey oid, const std::vector& orders) { - IntPrice price = {}; - size_t position = 0; - for (size_t x = 0; x < orders.size(); ++x) { - const auto& oo = orders[x]; - if (oo.id == oid) { - return { position }; - } - else if (price == oo.price) { - position += 1; - } - else { - price = oo.price; - position = 0; - } - } - return std::nullopt; - } }; struct MDRelayServer : bts::co2::AuthenticatedMarketDataRelayBase { diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp index bd943250f..85867b973 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp @@ -129,10 +129,10 @@ void MDP3Listener::onBookUpdate(onixs::Handler&, const onixs::Security& sec, con auto* snapshot = _mbo->allocate_snapshot(); // copy from onixs book for (const auto& bid : book.bids()) { - snapshot->bids.push_back({ OrderKey(bid.id()), convert_price(bid.price(), 0), bid.quantity() }); + snapshot->bids.push_back({ ExchangeOrderKey(bid.id()), convert_price(bid.price(), 0), bid.quantity() }); } for (const auto& ask : book.offers()) { - snapshot->asks.push_back({ OrderKey(ask.id()), convert_price(ask.price(),0), ask.quantity() }); + snapshot->asks.push_back({ ExchangeOrderKey(ask.id()), convert_price(ask.price(),0), ask.quantity() }); } _mbo->publish_snapshot(SecurityKey(key), snapshot); }