#include "ExecutionGateway.h" #include "lib/utils/TimeStamp.h" #include "server/sparkd/jetfix/codec/MessageDefinitions.h" #include "server/sparkd/jetfix/gateway/JetFIXGateway.h" #include "server/sparkd/orderman2/events/CancelRejectEvent.h" #include "server/sparkd/orderman2/events/GatewayStatusChangeEvent.h" #include "server/sparkd/orderman2/events/NewExecutionReportEvent.h" #include "server/sparkd/orderman2/events/NewSecuritiesEvent.h" #include "server/sparkd/orderman2/orderman2_utils/fix_order_codec.h" #include "lib/logging/logging.h" #include namespace bts { namespace orderman2 { using namespace jetfix; using JFGw = JetFIXGateway; struct ExecutionGateway::PImpl { PImpl(config::JetFIX::Connection const &cfg) : gw(cfg), exe_report_pool(), state(State::WAITING_FOR_SECURITIES), securities(), exe_report_stream(), gw_status_change_stream(), cancel_reject_stream(), gw_name(cfg.name()), cfg(cfg) {} JFGw gw; boost::object_pool exe_report_pool; State state; std::unordered_map securities; mercury::Stream exe_report_stream; mercury::Stream gw_status_change_stream; mercury::Stream cancel_reject_stream; GatewayName gw_name; config::JetFIX::Connection cfg; }; ExecutionGateway::ExecutionGateway(config::JetFIX::Connection const &cfg) : me(new PImpl(cfg)) {} ExecutionGateway::~ExecutionGateway() {} void ExecutionGateway::init() { me->state = State::WAITING_FOR_SECURITIES; subscribe( [this](auto const &sec_update) { this->on_securities(sec_update); }); subscribe( me->gw_name, [this](auto const &fix) { this->on_outgoing_fix(fix); }); } void ExecutionGateway::on_securities( const mercury::Update &update) { if (update.events) { for (auto const &ev : *(update.events)) { auto sec_ptr = ev.security; me->securities[sec_ptr->id()] = sec_ptr; } if (me->state == State::WAITING_FOR_SECURITIES) { LOG(OrderMan, Warn, "Received first set of {} securities. Proceeding " "with initialization of execution gateway {}", update.events->size(), me->gw_name.value()); // got our security synopsis, ready to start the GW me->state = State::READY; // subscribe to gateway events me->gw.on() = [this](auto const &msg) { this->on_exe_report(msg); }; me->gw.on() = [this](auto const &msg) { this->on_cancel_reject(msg); }; me->gw.on_ready() = [this]() { this->on_connection_status_change(true); }; me->gw.on_disconnect() = [this]() { this->on_connection_status_change(false); }; me->gw.on() = [this](auto const &m) { this->on_business_reject(m); }; me->gw.on() = [this](auto const &m) { this->on_session_reject(m); }; // stream of execution reports me->exe_report_stream = make_stream(me->gw_name); // stream for gateway status me->gw_status_change_stream = make_stream(me->gw_name); // stream for cancel rejects me->cancel_reject_stream = make_stream(me->gw_name); // start the gateway me->gw.start(); } } } void ExecutionGateway::on_exe_report( const jetfix::fix::ExecutionReportMsg &msg) { OrderKey ok; SecurityKey sk; decode_clorid(msg.get(), &ok, &sk); auto sec = me->securities[sk.value()]; 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 " "to" " lost fills. Please investigate.", sk.value()); else { // ignore leg fills, we care only about the actual security fill if (msg.has() && msg.get() == 2) return; std::string text_msg; if (msg.has()) text_msg = msg.get(); // allocate from our pool auto data = me->exe_report_pool.construct(); // copy data from fix msg to our structure str_to(msg.get(), &data->account_name); str_to(msg.get(), &data->operator_id); data->order_key = ok; data->security = sec; data->side = api::Side(msg.get()); data->time_in_force = api::orderman2::TimeInForce(msg.get()); data->order_type = api::orderman2::NativeOrderType(msg.get()); data->exec_type = api::orderman2::ExecType(msg.get()); data->order_status = api::orderman2::NativeOrderStatus(msg.get()); data->manual_order_indicator = msg.get() ? api::orderman2::MANUAL_ORDER_INDICATOR_YES : api::orderman2::MANUAL_ORDER_INDICATOR_YES; str_to(msg.get(), &data->exchange_order_id); data->price = securities::decode_exchange_price(*sec, Price(msg.get())); data->quantity = Quantity(msg.get()); data->filled_quantity = Quantity(msg.get()); data->last_shares = msg.has() ? Quantity(msg.get()) : Quantity(0); data->last_price = msg.has() ? securities::decode_exchange_price( *sec, Price(msg.get())) : Price(0); data->avg_price = msg.has() ? securities::decode_exchange_price( *sec, Price(msg.get())) : Price(0); str_to(msg.get(), &data->exchange_fill_id); str_to(msg.get(), &data->transact_time); data->cfg = &me->cfg; data->msg = text_msg; publish(me->exe_report_stream, data); } } void ExecutionGateway::on_cancel_reject( const jetfix::fix::CancelRejectMsg &msg) { OrderKey ok; SecurityKey sk; decode_clorid(msg.get(), &ok, &sk); std::string reject_msg; if (msg.has()) reject_msg = msg.get(); else if (msg.has()) reject_msg = fmt::format("Order cancel was rejected with error code {}", msg.get()); publish(me->cancel_reject_stream, ok, reject_msg); } void ExecutionGateway::on_connection_status_change(bool is_connected) { LOG(OrderMan, Info, "Connection state of {} changed to {}", me->gw_name.value(), is_connected ? std::string("connected") : std::string("disconnected")); publish(me->gw_status_change_stream, is_connected); } void ExecutionGateway::on_business_reject( jetfix::fix::BusinessMessageRejectMsg const &m) { LOG(OrderMan, Error, "Unexpected business reject in {} gateway . Reason: " "{}. This should be investigated", me->gw_name.value(), m.has() ? m.get() : std::string("Unknown")); } void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const &m) { LOG(OrderMan, Error, "Unexpected reject message in {} gateway . Reason: {}. " "This should be investigated", me->gw_name.value(), m.has() ? m.get() : std::string("Unknown")); } void ExecutionGateway::on_poll() { me->gw.dispatch_events(); } void ExecutionGateway::on_outgoing_fix( mercury::Update const &msg) { if (msg.events) for (auto const &ev : *(msg.events)) { LOG(Latency, Info, "Gateway {} {}", ev.cid, utils::TimeStamp::now().to_uint64()); me->gw.send(ev.cid, ev.msg); } } } }