From 618c41ce976c0eb61dcbe6308babbd5ecb5c0074 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 7 Jun 2019 17:49:33 -0400 Subject: [PATCH] synthesized Pedro's cme changes for ice --- src/lib/jetfix/codec/MessageDefinitions.h | 1 + .../orderman/actors/ICEExecutionGateway.cpp | 25 +++- src/lib/orderman/actors/ICEExecutionGateway.h | 3 +- src/lib/orderman/actors/IExecutionGateway.cpp | 6 +- src/lib/orderman/actors/IExecutionGateway.h | 3 +- src/lib/orderman/orders/ICENativeOrder.cpp | 129 +++++++++--------- src/lib/orderman/orders/ICENativeOrder.h | 1 + 7 files changed, 95 insertions(+), 73 deletions(-) diff --git a/src/lib/jetfix/codec/MessageDefinitions.h b/src/lib/jetfix/codec/MessageDefinitions.h index 61976f274..fe849c027 100644 --- a/src/lib/jetfix/codec/MessageDefinitions.h +++ b/src/lib/jetfix/codec/MessageDefinitions.h @@ -90,6 +90,7 @@ typedef FIXMsg nusers; TimeStamp connection_time; std::unordered_map exec_id_to_memo; //for the bullshit adjusts + md::SecurityOracle sec_oracle; }; -ICEExecutionGateway::ICEExecutionGateway(const config::JetFIX::Connection &cfg, trace::PerfLog &log) - : IExecutionGateway(), me(new PImpl(cfg, log)) +ICEExecutionGateway::ICEExecutionGateway(const config::JetFIX::Connection &cfg, trace::PerfLog &log, md::SecurityOracle o) + : IExecutionGateway(), me(new PImpl(cfg, log, o)) { me->state = State::WAITING_FOR_ADMIN; } @@ -304,8 +307,13 @@ void ICEExecutionGateway::on_exe_report(const jetfix::fix::ICEExecutionReportMsg auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), "ICEExecutionGateway::on_exe_report", ok.value()); LOG(Latency, Info, "Started handling of execution report for order {}", ok.value()); - auto sec = me->securities[sk.value()]; - + auto &sec = me->securities[sk.value()]; + if (!sec) + { + sec = me->sec_oracle.get_security(SecurityKey(sk.value())); + if(sec->has_ice()) + me->securities_by_exchange_id[sec->ice().exchange_id()] = sec; + } if (!sec) LOG(OrderMan, Error, "Received execution report for an unknown security {}. Will not be able to process this report. This is likely a bug that could lead " @@ -387,6 +395,11 @@ void ICEExecutionGateway::on_exe_report(const jetfix::fix::ICEExecutionReportMsg data->stop_price = securities::decode_exchange_price(*sec, Price(msg.get())); else data->stop_price = Price(os::nan()); + + if (msg.has()) + data->max_show = Quantity(msg.get()); + else + data->max_show = Quantity(0); if (msg.has()) data->quantity = Quantity(msg.get()); diff --git a/src/lib/orderman/actors/ICEExecutionGateway.h b/src/lib/orderman/actors/ICEExecutionGateway.h index 8c453ab71..c885a90af 100644 --- a/src/lib/orderman/actors/ICEExecutionGateway.h +++ b/src/lib/orderman/actors/ICEExecutionGateway.h @@ -8,7 +8,8 @@ namespace orderman { // fix execution gateway for a single FIX session class ICEExecutionGateway : public IExecutionGateway { public: - ICEExecutionGateway(const config::JetFIX::Connection&, trace::PerfLog &); + ICEExecutionGateway(const config::JetFIX::Connection&, trace::PerfLog &, + md::SecurityOracle); virtual ~ICEExecutionGateway(); void start() override; diff --git a/src/lib/orderman/actors/IExecutionGateway.cpp b/src/lib/orderman/actors/IExecutionGateway.cpp index 7bfe8efec..ebe2a9e2b 100644 --- a/src/lib/orderman/actors/IExecutionGateway.cpp +++ b/src/lib/orderman/actors/IExecutionGateway.cpp @@ -7,13 +7,13 @@ namespace bts { namespace orderman { std::unique_ptr -ExecutionGatewayFactory::create_gateway(const config::JetFIX::Connection& conn, trace::PerfLog& pl) +ExecutionGatewayFactory::create_gateway(const config::JetFIX::Connection& conn, trace::PerfLog& pl, md::SecurityOracle o) { if(conn.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_ICE || conn.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_SIMICE) - return std::make_unique(conn, pl); + return std::make_unique(conn, pl, o); else - return std::make_unique(conn, pl); + return std::make_unique(conn, pl, o); } } // namespace orderman diff --git a/src/lib/orderman/actors/IExecutionGateway.h b/src/lib/orderman/actors/IExecutionGateway.h index 4d486b964..14eff752d 100644 --- a/src/lib/orderman/actors/IExecutionGateway.h +++ b/src/lib/orderman/actors/IExecutionGateway.h @@ -3,6 +3,7 @@ #include "lib/config/protos/jetfix.pb.h" #include "lib/jetfix/codec/MessageDefinitions.h" #include "lib/jetfix/common/OutFIXPtr.h" +#include "lib/md/SecurityOracle.h" #include "lib/mercury/events/Update.h" #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/utils/bts_no_copy.h" @@ -43,7 +44,7 @@ private: class ExecutionGatewayFactory { public: - static std::unique_ptr create_gateway(const config::JetFIX::Connection&, trace::PerfLog&); + static std::unique_ptr create_gateway(const config::JetFIX::Connection&, trace::PerfLog&, md::SecurityOracle); }; diff --git a/src/lib/orderman/orders/ICENativeOrder.cpp b/src/lib/orderman/orders/ICENativeOrder.cpp index e19826954..fcbe1dc26 100644 --- a/src/lib/orderman/orders/ICENativeOrder.cpp +++ b/src/lib/orderman/orders/ICENativeOrder.cpp @@ -58,6 +58,7 @@ struct ICENativeOrder::PImpl jetfix::FIXFragment update_encoding; jetfix::FIXFragment::Bookmark update_price_bm; jetfix::FIXFragment::Bookmark update_quantity_bm; + jetfix::FIXFragment::Bookmark update_stop_px_bm; jetfix::FIXFragment::Bookmark update_transact_time_bm; jetfix::FIXFragment::Bookmark update_clordid_bm; jetfix::FIXFragment::Bookmark update_orig_clordid_bm; @@ -89,6 +90,14 @@ void ICENativeOrder::on_init_new(const OrderDefinition& def, const PreActivation { // order was just created. me->definition = def; + me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); + if (!me->sec) + { + auto msg = fmt::format("Unknown security: {}", def.security_key()); + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } switch (def.custom_def().custom_case()) { case CustomOrderDefinition::kMarket: @@ -108,32 +117,21 @@ void ICENativeOrder::on_init_new(const OrderDefinition& def, const PreActivation break; } - me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); - - if (!me->sec) + auto rq = serv.order_router.get_routing_record( UserID(def.user_id()), AccountID(def.account_id()), *me->sec ); + if (!rq.has_route) { - auto msg = fmt::format("Unknown security: {}", def.security_key()); + auto msg = fmt::format("Could not route order: {}", rq.error_msg); + LOG(OrderMan, Error, "{}", msg); me->state.set_last_msg(msg); transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; } - else - { - auto rq = serv.order_router.get_routing_record(UserID(def.user_id()), AccountID(def.account_id()), *me->sec); - if (rq.has_route) - { - transition(ORDER_STATUS_INACTIVE); - auto &native_state = get_native_state(me->state); - *native_state.mutable_routing_record() = rq.routing_record; - setup_ids(rq.routing_record.sender_compid()); - init_fix(); - } - else - { - auto msg = fmt::format("Could not route order: {}", rq.error_msg); - LOG(OrderMan, Error, "{}", msg); - transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); - } - } + + transition(ORDER_STATUS_INACTIVE); + auto &native_state = get_native_state(me->state); + *native_state.mutable_routing_record() = rq.routing_record; + setup_ids(rq.routing_record.sender_compid()); + init_fix(); } void ICENativeOrder::init_recovered_common(const api::orderman::OrderRecord& rec, const PreActivationOrderServices& serv) @@ -376,7 +374,7 @@ ICENativeOrder::_on_activate(log::Level reject_level, log::Level transition_leve // can we trade? auto lqr = me->services->limiter().can_trade( UserID(me->definition.user_id()), AccountID(me->definition.account_id()) ); - if (!lqr.ok) + if (!lqr.ok()) { LOG(OrderMan, reject_level, "Unauthorized order attempted. User: {} on " @@ -392,7 +390,7 @@ ICENativeOrder::_on_activate(log::Level reject_level, log::Level transition_leve // can we trade this security, with this size? lqr = me->services->limiter().can_send_new_order( AccountID(me->definition.account_id()), *me->sec, me->definition.side(), q ); - if (!lqr.ok) + if (!lqr.ok()) { LOG(OrderMan, reject_level, "New order would breach limit. User: {} Account: " @@ -474,6 +472,7 @@ OrderOpResult ICENativeOrder::on_activate(Price p, Quantity q, bts::orderman::Or // Early return so we skip the expensive report_state() // call. It is OK to spkip it because we are bound to report // our state once the excange aknowledges the order. + update_working_position(); return OrderOpResult{}; } @@ -540,6 +539,16 @@ OrderOpResult ICENativeOrder::on_activate(Price p, Quantity q, bts::orderman::Or OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) { + api::orderman::Update update; + update.set_price(p.value()); + update.set_quantity(q.value()); + return on_update(update); +} + +OrderOpResult ICENativeOrder::on_update(api::orderman::Update const &update) +{ + auto p = Price(update.price()); + auto q = Quantity(update.quantity()); auto scoped_log = me->services->log_scope("ICENativeOrder::on_update", key().value()); auto result = OrderOpResult{}; @@ -560,24 +569,13 @@ OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) Quantity(current_working_qty)); // are we under the position limits? - if (!lqr.ok) + if (!lqr.ok()) { auto msg = fmt::format("Cannot update order due to limit breach: {}", lqr.error_msg); LOG(OrderMan, Error, "Order update failed due to limit breach. Order: {} Msg: {}", key().value(), lqr.error_msg); - - // if we are not being driven by an algo we just signal the - // error but keep running. otherwise we stop. - if (parent_key() == null_order_key()) - transition(me->state.status(), msg); - else - { - me->state.set_last_msg(msg); - on_cancel(); - } - result.error = api::orderman::ORDER_ERROR_LIMIT_BREACH; - result.error_msg = lqr.error_msg; + ignore_or_cancel(msg, ORDER_ERROR_LIMIT_BREACH, &result); break; } @@ -586,23 +584,24 @@ OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) { LOG(OrderMan, Warn, "Cannot update order {} because of messaging limits. ", key().value()); auto msg = "Messaging threshold breached"; - - // if we are not being driven by an algo we just signal the - // error but keep running. otherwise we stop. - if (parent_key() == null_order_key()) - transition(me->state.status(), msg); - else - { - me->state.set_last_msg(msg); - on_cancel(); - } - result.error = api::orderman::ORDER_ERROR_MESSAGING_LIMIT_BREACH; - result.error_msg = msg; + ignore_or_cancel(msg, ORDER_ERROR_MESSAGING_LIMIT_BREACH, &result); break; } - // all good auto &encoding = me->update_encoding; + + if (update.has_custom()) + { + bts_assert(update.custom().custom_case() == me->definition.custom_def().custom_case()); + if (update.custom().custom_case() == CustomOrderDefinition::kStopLimit) + { + auto const &stop_limit = update.custom().stop_limit(); + auto stop_px = Price(stop_limit.stop_price()); + me->update_stop_px_bm = encoding.overwrite(me->update_stop_px_bm, + securities::encode_exchange_price(*me->sec, stop_px).value(), + me->price_encoding_precision, price_sz()); + } + } me->update_price_bm = encoding.overwrite(me->update_price_bm, securities::encode_exchange_price(*me->sec, p).value(), me->price_encoding_precision, price_sz()); me->update_quantity_bm = encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz()); @@ -647,11 +646,6 @@ OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) return result; } -OrderOpResult ICENativeOrder::on_update(api::orderman::Update const &update) -{ - return on_update(Price(update.price()),Quantity(update.quantity())); -} - OrderOpResult ICENativeOrder::on_detach() { return OrderOpResult{}; } void ICENativeOrder::on_attached(api::orderman::OrderDefinition const &new_parent) @@ -684,7 +678,7 @@ void ICENativeOrder::on_attached(api::orderman::OrderDefinition const &new_paren // init_fix(); // if (has_exchange_order_id()) // add_exchange_order_id_to_fix(); - //TODO ICE is a little different with some of these tags, there is no manual order reported and the user is really + //ICE is a little different with some of these tags, there is no manual order reported and the user is really //not the user we are updating here. So we can update the BTS data but not reinit the fix //We should not reinit the fix because we need to correctly cache the orig_clordid for cancels and updates subsequent to //the first update. @@ -983,8 +977,7 @@ void ICENativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) LOG(OrderMan, Warn, "Applying buffered update ({}@{}) to order {}, on " "re-connection", - q.value(),p.value(), - key().value()); + q.value(),p.value(), key().value()); on_update(p, q); } } @@ -1033,12 +1026,12 @@ void ICENativeOrder::update_working_position() if (current_position.value() > previous_position.value()) { me->services->limiter().add( *me->sec, AccountID(me->definition.account_id()), - me->definition.side(), Quantity(current_position.value() - previous_position.value())); + me->definition.side(), Quantity(current_position.value() - previous_position.value()), true); } else if (previous_position.value() > current_position.value()) { me->services->limiter().remove( *me->sec, AccountID(me->definition.account_id()), - me->definition.side(), Quantity(previous_position.value() - current_position.value())); + me->definition.side(), Quantity(previous_position.value() - current_position.value()), true); } } else @@ -1227,7 +1220,6 @@ void ICENativeOrder::init_fix() auto stop_px = def.custom_def().stop().stop_price(); auto encoded_stop_px = securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - update_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); break; } @@ -1242,7 +1234,7 @@ void ICENativeOrder::init_fix() auto stop_px = def.custom_def().stop_limit().stop_price(); auto encoded_stop_px = securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - update_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); + me->update_stop_px_bm = update_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); // price me->create_price_bm = create_encoding.add(0, me->price_encoding_precision, price_sz()); @@ -1373,5 +1365,18 @@ api::orderman::NativeOrderState& ICENativeOrder::get_native_state(api::orderman: bts_assert(false); return *st.mutable_stop_limit()->mutable_native_state(); } + +void ICENativeOrder::ignore_or_cancel(std::string const &msg, api::orderman::OrderError error, OrderOpResult *out) +{ + if (parent_key() == null_order_key()) + transition(me->state.status(), msg); + else + { + me->state.set_last_msg(msg); + on_cancel(); + } + *out = {error, msg}; +} + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/ICENativeOrder.h b/src/lib/orderman/orders/ICENativeOrder.h index 060b18527..cc62f47ef 100644 --- a/src/lib/orderman/orders/ICENativeOrder.h +++ b/src/lib/orderman/orders/ICENativeOrder.h @@ -32,6 +32,7 @@ class ICENativeOrder : public NativeOrder { api::orderman::OrderDefinition on_init_from_exe_report(const ExecutionReport &, const PreActivationOrderServices &) override; + void ignore_or_cancel(const std::string&, api::orderman::OrderError, OrderOpResult* out); protected: void warm_up_activate(OrderServicesData &, OrderServices &) override;