#include "ICEExecutionGateway.h" #include "lib/jetfix/codec/MessageDefinitions.h" #include "lib/jetfix/codec/i32_toa.h" #include "lib/jetfix/common/OutFIX.h" #include "lib/jetfix/gateway/JetFIXGateway.h" #include "lib/logging/logging.h" #include "lib/orderman/events/CancelRejectEvent.h" #include "lib/orderman/events/GatewayStatusChangeEvent.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/orderman_utils/fix_order_codec.h" #include "lib/api/services/AdminService.pb.h" #include "lib/orderman/events/NewAdminDataEvent.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/helper_macros.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/bts_chrono.h" #include "lib/domain_model/Security.h" #include #include #include namespace bts { namespace orderman { using namespace jetfix; using JFGw = JetFIXGateway; struct ICEExecutionGateway::PImpl { PImpl(const config::JetFIX::Connection &cfg, trace::PerfLog &l, md::SecurityOracle o) : gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES), securities(), securities_by_exchange_id(), gw_name(cfg.name()), cfg(cfg), perf_log(l), admin_data(), nusers(-1), connection_time(), exec_id_to_memo(), sec_oracle(o) {} JFGw gw; boost::object_pool exe_report_pool; State state; std::unordered_map securities; std::unordered_map securities_by_exchange_id; GatewayName gw_name; config::JetFIX::Connection cfg; OnExeReport on_exe_report; OnGwStatusChange on_gw_status_change; OnCancelReject on_cancel_reject; trace::PerfLog &perf_log; api::admin::AdminData admin_data; std::atomic nusers; TimeStamp connection_time; std::unordered_map exec_id_to_memo; //for the bullshit adjusts md::SecurityOracle sec_oracle; }; void from_exchange_qty (const api::Security& sec, bts::orderman::ExecutionReport* exec_rep){ LOG(OrderMan, Info, "ICEExecutionGateway::from_exchange_qty " "sec:{} mult:{} " "leg_sec:{} leg_sec_mult:{}" "qty:{} filled_qty:{} last_shares:{} max_show:{} " "price:{} last_price:{} avg_price:{}", sec.id(), sec.ice().exchange_qty_mult(), exec_rep->leg_security == nullptr ? 0 : exec_rep->leg_security->id(), exec_rep->leg_security == nullptr ? os::nan() : exec_rep->leg_security->ice().exchange_qty_mult(), exec_rep->quantity, exec_rep->filled_quantity, exec_rep->last_shares, exec_rep->max_show, exec_rep->price, exec_rep->last_price, exec_rep->avg_price); exec_rep->quantity = bts::securities::from_exchange_qty( exec_rep->quantity,sec); exec_rep->filled_quantity = bts::securities::from_exchange_qty( exec_rep->filled_quantity,sec); // this is specific to the leg const api::Security& last_shr_sec = exec_rep->leg_security == nullptr ? sec : *exec_rep->leg_security; exec_rep->last_shares = bts::securities::from_exchange_qty( exec_rep->last_shares, last_shr_sec); exec_rep->max_show = bts::securities::from_exchange_qty( exec_rep->max_show,sec); } 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; } ICEExecutionGateway::~ICEExecutionGateway() {} void ICEExecutionGateway::on_admin_data(const mercury::Update& update) { if (update.events) { for (auto const &ev : *(update.events)) me->admin_data.CopyFrom(*ev.admin_data); if (me->state == State::WAITING_FOR_ADMIN) { LOG(OrderMan, Warn, "Received admin data. Waiting for securities {}", me->gw_name.value()); me->state = State::WAITING_FOR_SECURITIES; } } } void ICEExecutionGateway::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(sec_ptr->has_ice()) me->securities_by_exchange_id[sec_ptr->ice().exchange_id()] = sec_ptr; } if (me->state == State::WAITING_FOR_SECURITIES) { LOG(OrderMan, Warn, "Received first set of {} securities. Beginning wait 2s for trader login {}", update.events->size(), me->gw_name.value()); // got our security synopsis, ready to start the GW me->state = State::READY_TO_START_GW; // 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); }; } } } void ICEExecutionGateway::start() { me->gw.start(); bts_assert_msg(me->state == State::READY_TO_START_GW, "ICEExecutionGateway statemachine failure"); } void ICEExecutionGateway::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")); if(is_connected && me->state == State::READY_TO_START_GW) { me->state = State::TRADER_LOGIN_TIMEOUT; me->connection_time = Timer::now(); } if(me->state == State::READY) { me->on_gw_status_change(is_connected); if(!is_connected) //if we disconnect, set state so we log in again me->state = State::READY_TO_START_GW; } } void ICEExecutionGateway::on_trader_login_timeout_expired() { if (me->state == State::TRADER_LOGIN_TIMEOUT) { LOG(OrderMan, Warn, "Done waiting for login timeout, logging on traders {}", me->gw_name.value()); // got our security synopsis, ready to start the GW me->state = State::LOGIN_TIMEOUT_COMPLETE; me->gw.on() = [&](const fix::ICETraderLoginResponseMsg& lmsg) { std::string u = lmsg.get(); std::string m; if(lmsg.has()) m = lmsg.get(); LOG(OrderMan, Info, "Successfully logging trader {} into ICE {} {}", u, m, me->gw_name.value()); if(--me->nusers == 0) this->on_login(); }; me->gw.on() = [&](const fix::ICETraderLogoutResponseMsg& lmsg) { std::string u = lmsg.get(); std::string m; if(lmsg.has()) m = lmsg.get(); LOG(OrderMan, Error, "Error logging trader {} into ICE {} {}", u, m, me->gw_name.value()); if(--me->nusers == 0) this->on_login(); }; std::set auth_traders; for(const auto& u : me->admin_data.users()) { if(u.has_user_data() && u.user_data().has_exchange_user_data() && u.user_data().exchange_user_data().has_ice_data()) { if(!u.is_present()) continue; if(!u.is_active()) continue; const auto& ice = u.user_data().exchange_user_data().ice_data(); if(auth_traders.find(ice.authorized_trader()) == auth_traders.end()) { if(ice.authorized_trader() == "") continue; if(ice.company_id() <= 0) continue; if(me->nusers == -1) me->nusers = 0; me->nusers++; auto result = std::make_shared(me->cfg); auto &stream = result->fix.back(); auto &buffer = stream.new_msg(MessageTypes::ICETraderLogin); buffer.add(ice.company_id()); buffer.add(ice.authorized_trader()); buffer.add(ice.password()); if(ice.mifid_direct_electronic_access() >= 0) buffer.add(ice.mifid_direct_electronic_access()); if(ice.mifid_trading_capacity() >= 0) buffer.add(ice.mifid_trading_capacity()); if(ice.mifid_liquidity_provision() >= 0) buffer.add(ice.mifid_liquidity_provision()); if(ice.mifid_commodity_deriv_indicator() >= 0) buffer.add(ice.mifid_commodity_deriv_indicator()); if(ice.mifid_investment_decision() > 0) buffer.add(ice.mifid_investment_decision()); if(ice.mifid_execution_decision() > 0) buffer.add(ice.mifid_execution_decision()); if(ice.mifid_client_id_code() > 0) buffer.add(ice.mifid_client_id_code()); if(ice.mifid_id() > 0) buffer.add(ice.mifid_id()); stream.close(); trace::CorID trace; send_fix(result, trace); auth_traders.insert(ice.authorized_trader()); } } } LOG(OrderMan, Warn, "{} traders identified as ICE traders {}", auth_traders.size(), me->gw_name.value()); } } void ICEExecutionGateway::on_login() { if (me->state == State::LOGIN_TIMEOUT_COMPLETE) { LOG(OrderMan, Warn, "Successful login traders {}", me->gw_name.value()); me->state = State::READY; me->on_gw_status_change(true); } } OnExeReport &ICEExecutionGateway::on_exe_report() { return me->on_exe_report; } OnGwStatusChange &ICEExecutionGateway::on_gw_status_change() { return me->on_gw_status_change; } OnCancelReject &ICEExecutionGateway::on_cancel_reject() { return me->on_cancel_reject; } bool ICEExecutionGateway::process_memo(const jetfix::fix::ICEExecutionReportMsg &msg, OrderKey* ok, SecurityKey* sk) { if(msg.has()) { decode_memo(msg.get(), ok, sk); return true; } else { if (msg.get() == api::orderman::EXEC_TYPE_TRADE_CORRECT && msg.has()) { std::string ref_exec_id = msg.get(); auto found = me->exec_id_to_memo.find(ref_exec_id); if(found == me->exec_id_to_memo.end()) { auto new_price = Price(0); if(msg.has()) new_price = Price(msg.get()); LOG(OrderMan, Error, "Received unsupported trade correction msg. This correction will not be forwarded. The trader should be alerted. " "Partial original fill id: {}. New price: {}", ref_exec_id, new_price.value()); return false; } else { decode_memo(found->second, ok, sk); return true; } } } return false; } void ICEExecutionGateway::on_exe_report(const jetfix::fix::ICEExecutionReportMsg &msg) { OrderKey ok; SecurityKey sk; if(!process_memo(msg, &ok, &sk)) { std::string execid = ""; if(msg.has()) execid = msg.get(); LOG(OrderMan, Error, "Received execution report with no memo execid: {}. Will not be able to process this report. This is likely a bug that could lead " "to lost fills. Please investigate.", execid); return; } 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()]; if (!sec) { sec = me->sec_oracle.get_security(SecurityKey(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 { if(sec->has_ice()) me->securities_by_exchange_id[sec->ice().exchange_id()] = sec; api::Security const *leg_security = nullptr; if (msg.has() && msg.get() == 2) { auto it = me->securities_by_exchange_id.find(msg.get()); if (it == me->securities_by_exchange_id.end()) { LOG(OrderMan, Error, "Received execution report for an unknown spread leg" "security {}. Will not be able to process this report. " "This is likely a bug that could lead to " "lost leg fills. Please investigate. Spread sec_key: {} leg " "exchange id: {}", sk.value(), msg.get()); return; } leg_security = it->second; } std::string text_msg; if (msg.has()) text_msg = msg.get(); if (msg.has()) text_msg += " Rejected OrdRejReason: " + std::to_string(msg.get()); if (msg.has()) text_msg += " Cancelled DeleteReason: " + std::to_string(msg.get()); // allocate from our pool auto data = me->exe_report_pool.construct(); if(msg.has()) data->memo = msg.get(); data->exec_type = api::orderman::ExecType(msg.get()); data->msg_seq_num = msg.get(); // copy data from fix msg to our structure if (msg.has()) str_to(msg.get(), &data->account_name); if (msg.has()) str_to(msg.get(), &data->operator_id); if (msg.has()) data->ice_company_id = msg.get(); data->order_key = ok; data->security = sec; data->leg_security = leg_security; data->side = api::Side(msg.get()); if (msg.has()) data->time_in_force = api::orderman::TimeInForce(msg.get()); else data->time_in_force = api::orderman::TIME_IN_FORCE_UNDEFINED; if (msg.has()) data->order_type = api::orderman::NativeOrderType(msg.get()); else data->order_type = api::orderman::NATIVE_ORDER_TYPE_UNDEFINED; data->order_status = api::orderman::NativeOrderStatus(msg.get()); data->manual_order_indicator = api::orderman::MANUAL_ORDER_INDICATOR_NO; std::string oid_string = get_ice_order_id_string(msg); str_to(oid_string, & std::get( data->exchange_order_id)); data->ice_cl_ord_id = 0; if(msg.has()) data->ice_cl_ord_id = msg.get(); if (msg.has()) data->price = securities::decode_exchange_price(*sec, Price(msg.get())); else data->price = Price(os::nan()); if (msg.has()) 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()); else data->quantity = Quantity(0); data->filled_quantity = Quantity(msg.get()); data->last_shares = msg.has() ? Quantity(msg.get()) : Quantity(0); auto last_px_sec = data->leg_security ? data->leg_security : sec; data->last_price = msg.has() ? securities::decode_exchange_price(*last_px_sec, Price(msg.get())) : Price(0); data->avg_price = msg.has() ? securities::decode_exchange_price(*last_px_sec, Price(msg.get())) : Price(0); if (msg.has()) { auto execid = msg.get(); str_to(execid, &data->exchange_fill_id); if(msg.has()) me->exec_id_to_memo[execid] = msg.get(); } if (msg.has()) { std::string stamp = msg.get(); stamp.resize(sizeof(orderman::ExchangeTimeStampStr)); orderman::ExchangeTimeStampStr t; str_to(stamp, &t); data->transact_time = t; } data->cfg = &me->cfg; data->msg = text_msg; data->exchange = api::Venue::VENUE_ICE; from_exchange_qty(*sec,data); me->on_exe_report(*data, msg.trace()); } } void ICEExecutionGateway::on_cancel_reject(const jetfix::fix::ICECancelRejectMsg &msg) { OrderKey ok(msg.get()); 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()); me->on_cancel_reject(CancelReject {ok, CancelRejectReason::Other,reject_msg}); } void ICEExecutionGateway::on_business_reject(const jetfix::fix::BusinessMessageRejectMsg& 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 ICEExecutionGateway::on_session_reject(const jetfix::fix::RejectMsg& 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 ICEExecutionGateway::dispatch_incoming_fix_messages() { me->gw.dispatch_events(); if(me->state == State::TRADER_LOGIN_TIMEOUT && ms_diff(Timer::now(), me->connection_time) >= 2000) { on_trader_login_timeout_expired(); } } void ICEExecutionGateway::send_fix(const jetfix::OutFIXPtr& fix, trace::CorID trace) { auto scoped_log = trace::log_scope(me->perf_log,trace,"ICEExecutionGateway::send_fix"); me->gw.send(fix,trace); me->gw.dispatch_events(); } } // namespace orderman } // namespace bts