#include "SimExecutionGateway.h" #include "lib/api/services/AdminService.pb.h" #include "lib/api/services/OrderService.pb.h" #include "lib/domain_model/Security.pb.h" #include "lib/domain_model/common.h" #include "lib/logging/logging.h" #include "lib/md/MDClient.h" #include "lib/orderman/CancelOrderMsg.h" #include "lib/orderman/CancelReplaceOrderMsg.h" #include "lib/orderman/CreateOrderMsg.h" #include "lib/orderman/OrderMsg.h" #include "lib/orderman/events/CancelRejectEvent.h" #include "lib/orderman/events/GatewayStatusChangeEvent.h" #include "lib/orderman/events/NewAdminDataEvent.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/gateways/IExecutionGateway.h" #include "lib/orderman/orderman_utils/ExecutionReport.h" #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/bts_assert.h" #include "lib/utils/bts_chrono.h" #include "lib/utils/helper_macros.h" #include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h" #include #include #include #include #include #include #include #include namespace bts { namespace orderman { struct SimExecutionGateway::PImpl { PImpl(md::SecurityOracle o, bts::md::MDClient &md_client) : admin_data(), connection_time(), sec_oracle(o), md_client(md_client) { LOG(OrderMan, Info, "SimExecutionGateway::PImpl constructor"); } std::unordered_map securities; std::unordered_map securities_by_exchange_id; OnExeReport on_exe_report; OnGwStatusChange on_gw_status_change; OnCancelReject on_cancel_reject; orderman::OnExchangePositionState on_exchange_position_state; orderman::OnExchangeOpenOrdersState on_exchange_open_orders; api::admin::AdminData admin_data; TimeStamp connection_time; md::SecurityOracle sec_oracle; bts::md::MDClient &md_client; std::vector buffered_msgs = {}; unsigned long exchange_oid = 0; std::vector exe_reports = {}; IEMOrderMuxer *_order_mux = nullptr; }; SimExecutionGateway::SimExecutionGateway( md::SecurityOracle o, bts::md::MDClient &md_client) : IExecutionGateway(), me(new PImpl(o, md_client)) { LOG(OrderMan, Info, "SimExecutionGateway::SimExecutionGateway constructor"); } SimExecutionGateway::~SimExecutionGateway() {} void SimExecutionGateway::start() { LOG(OrderMan, Info, "SimExecutionGateway::start"); } // this is a periodic timer void SimExecutionGateway::dispatch_incoming_fix_messages() { if (me->buffered_msgs.size() == 0) { return; } std::vector aux_msgs = me->buffered_msgs; me->buffered_msgs.clear(); for (OrderMsg m : aux_msgs) { if (std::holds_alternative(m)) { CreateOrderMsg cr_msg = std::get(m); LOG(OrderMan, Info, "SimExecutionGateway::dispatch_incoming_fix_message key: {} sec: " "{} p: {} q: {}", cr_msg.key, cr_msg.security->id(), cr_msg.price, cr_msg.quantity); ExecutionReport er = {}; std::string account_name = "ACCA1"; std::array an_array = {}; size_t i = 0; for (const char c : account_name) { an_array[i++] = c; } er.account_name = AccountName(an_array); to_array(cr_msg.routing_record->account(), &er.account_name.value()); to_array( cr_msg.routing_record->operator_id(), &er.operator_id.value()); er.order_key = cr_msg.key; er.msg_seq_num = 0; er.security = cr_msg.security; er.leg_security = nullptr; er.side = cr_msg.side; er.time_in_force = cr_msg.time_in_force; er.order_type = cr_msg.order_type; er.exec_type = api::orderman::EXEC_TYPE_NEW; er.order_status = api::orderman::NATIVE_ORDER_STATUS_NEW; er.manual_order_indicator = api::orderman::MANUAL_ORDER_INDICATOR_NO; ExchangeOrderID64 eoid = ExchangeOrderID64(me->exchange_oid++); ExchangeOrderIDVar oid = eoid; er.exchange_order_id = oid; er.price = cr_msg.price; er.quantity = cr_msg.quantity; er.filled_quantity = Quantity(0); er.last_shares = Quantity(0); er.msg = "Message Placed"; er.exchange = api::VENUE_SIM; trace::CorID cid = {}; me->exe_reports.push_back(er); const auto &er_pheap = me->exe_reports.back(); LOG(OrderMan, Info, "SimExecutionGateway::dispatch_incoming_fix_messages er: {}", er_pheap.to_string()); me->on_exe_report(er_pheap, cid); } else if (std::holds_alternative(m)) { CancelOrderMsg cxl_m = std::get(m); for (auto &er : me->exe_reports) { if (er.order_key == cxl_m.key) { er.order_status = api::orderman::NATIVE_ORDER_STATUS_CANCELED; er.msg = "Cancelled"; me->on_exe_report(er, {}); LOG(OrderMan, Info, "SimExecutionGateway::dispatch_incoming_fix_messages er: " "{}", er.to_string()); return; } } } else if (std::holds_alternative(m)) { CancelReplaceOrderMsg cxl_r_msg = std::get(m); CreateOrderMsg cr_msg = cxl_r_msg.create_fields; for (auto &er : me->exe_reports) { if (er.order_key == cr_msg.key) { er.order_status = api::orderman::NATIVE_ORDER_STATUS_REPLACED; er.msg = "Replaced"; er.price = cr_msg.price; er.quantity = cr_msg.quantity; me->on_exe_report(er, {}); return; } } } } } void SimExecutionGateway::on_securities( const mercury::Update &) { LOG(OrderMan, Info, "SimExecutionGateway::on_securities"); } void SimExecutionGateway::on_admin_data( const mercury::Update &update) { LOG(OrderMan, Info, "SimExecutionGateway::on_admin_data"); if (update.events) { for (auto const &ev : *(update.events)) me->admin_data.CopyFrom(*ev.admin_data); } } void SimExecutionGateway::send_fix(const jetfix::OutFIXPtr &, trace::CorID) { // only deals with messages bts_assert(false); } void SimExecutionGateway::send_msgs( std::vector const &msg_vec, trace::CorID) { // this is where it will be sent to the exchange LOG(OrderMan, Info, "SimExecutionGateway::send_msgs count: {}", msg_vec.size()); for (auto &m : msg_vec) { me->buffered_msgs.emplace_back(m); } } OnExeReport &SimExecutionGateway::on_exe_report() { return me->on_exe_report; } OnGwStatusChange &SimExecutionGateway::on_gw_status_change() { return me->on_gw_status_change; } OnCancelReject &SimExecutionGateway::on_cancel_reject() { return me->on_cancel_reject; } OnExchangePositionState &SimExecutionGateway::on_exchange_position_state() { return me->on_exchange_position_state; } OnExchangeOpenOrdersState &SimExecutionGateway::on_exchange_open_orders() { return me->on_exchange_open_orders; } } // namespace orderman } // namespace bts