Files
spark/src/lib/orderman/gateways/ICEExecutionGateway.cpp
T

524 lines
19 KiB
C++

#include "ICEExecutionGateway.h"
#include "lib/api/services/AdminService.pb.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/NewAdminDataEvent.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
#include "lib/orderman/orderman_utils/fix_order_codec.h"
#include "lib/tracing/bts_trace.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/helper_macros.h"
#include <atomic>
#include <set>
namespace bts {
namespace orderman {
using namespace jetfix;
using JFGw = JetFIXGateway<
fix::ICEExecutionReportMsg,
fix::BusinessMessageRejectMsg,
fix::RejectMsg,
fix::ICECancelRejectMsg,
fix::ICETraderLoginResponseMsg,
fix::ICETraderLogoutResponseMsg>;
struct ICEExecutionGateway::PImpl {
PImpl(
const config::JetFIX::Connection &cfg,
trace::PerfLog &l,
md::SecurityOracle o)
: gw(cfg, l), 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;
State state;
std::unordered_map<SecurityKey::Value, api::Security const *> securities;
std::unordered_map<int64_t, api::Security const *> securities_by_exchange_id;
GatewayName gw_name;
config::JetFIX::Connection cfg;
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;
trace::PerfLog &perf_log;
api::admin::AdminData admin_data;
std::atomic<int> nusers;
TimeStamp connection_time;
std::unordered_map<std::string, std::string>
exec_id_to_memo; // for the bullshit adjusts
md::SecurityOracle sec_oracle;
};
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<NewAdminDataEvent> &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<NewSecuritiesEvent> &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<fix::ICEExecutionReportMsg>() = [this](auto const &msg) {
this->on_exe_report(msg);
};
me->gw.on<fix::ICECancelRejectMsg>() = [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<fix::BusinessMessageRejectMsg>() = [this](auto const &m) {
this->on_business_reject(m);
};
me->gw.on<fix::RejectMsg>() = [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<fix::ICETraderLoginResponseMsg>() =
[&](const fix::ICETraderLoginResponseMsg &lmsg) {
std::string u = lmsg.get<fix::UserName>();
std::string m;
if (lmsg.has<fix::Text>())
m = lmsg.get<fix::Text>();
LOG(OrderMan, Info,
"Successfully logging trader {} into ICE {} {}", u, m,
me->gw_name.value());
if (--me->nusers == 0)
this->on_login();
};
me->gw.on<fix::ICETraderLogoutResponseMsg>() =
[&](const fix::ICETraderLogoutResponseMsg &lmsg) {
std::string u = lmsg.get<fix::UserName>();
std::string m;
if (lmsg.has<fix::Text>())
m = lmsg.get<fix::Text>();
LOG(OrderMan, Error, "Error logging trader {} into ICE {} {}", u,
m, me->gw_name.value());
if (--me->nusers == 0)
this->on_login();
};
std::set<std::string> 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<OutFIX>(me->cfg);
auto &stream = result->fix.back();
auto &buffer = stream.new_msg(MessageTypes::ICETraderLogin);
buffer.add<fix::CompanyID>(ice.company_id());
buffer.add<fix::UserName>(ice.authorized_trader());
buffer.add<fix::RawData>(ice.password());
if (ice.mifid_direct_electronic_access() >= 0)
buffer.add<fix::MifidDirectElectronicAccess>(
ice.mifid_direct_electronic_access());
if (ice.mifid_trading_capacity() >= 0)
buffer.add<fix::MifidTradingCapacity>(
ice.mifid_trading_capacity());
if (ice.mifid_liquidity_provision() >= 0)
buffer.add<fix::MifidLiquidityProvision>(
ice.mifid_liquidity_provision());
if (ice.mifid_commodity_deriv_indicator() >= 0)
buffer.add<fix::MifidCommodityDerivIndicator>(
ice.mifid_commodity_deriv_indicator());
if (ice.mifid_investment_decision() > 0)
buffer.add<fix::MifidInvestmentDecision>(
ice.mifid_investment_decision());
if (ice.mifid_execution_decision() > 0)
buffer.add<fix::MifidExecutionDecision>(
ice.mifid_execution_decision());
if (ice.mifid_client_id_code() > 0)
buffer.add<fix::MifidClientIDCode>(
ice.mifid_client_id_code());
if (ice.mifid_id() > 0)
buffer.add<fix::MifidId>(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;
}
OnExchangePositionState &ICEExecutionGateway::on_exchange_position_state() {
return me->on_exchange_position_state;
}
OnExchangeOpenOrdersState &ICEExecutionGateway::on_exchange_open_orders() {
return me->on_exchange_open_orders;
}
bool ICEExecutionGateway::process_memo(
const jetfix::fix::ICEExecutionReportMsg &msg,
OrderKey *ok,
SecurityKey *sk) {
if (msg.has<fix::Memo>()) {
decode_memo(msg.get<fix::Memo>(), ok, sk);
return true;
} else {
if (msg.get<fix::ExecType>() == api::orderman::EXEC_TYPE_TRADE_CORRECT &&
msg.has<fix::ExecRefID>()) {
std::string ref_exec_id = msg.get<fix::ExecRefID>();
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<fix::LastPx>())
new_price = Price(msg.get<fix::LastPx>());
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<fix::ExecID>())
execid = msg.get<fix::ExecID>();
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<fix::MultiLegReportingType>() &&
msg.get<fix::MultiLegReportingType>() == 2) {
auto it =
me->securities_by_exchange_id.find(msg.get<fix::Int_Symbol>());
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<fix::Int_Symbol>());
return;
}
leg_security = it->second;
}
std::string text_msg;
if (msg.has<fix::Text>())
text_msg = msg.get<fix::Text>();
if (msg.has<fix::OrdRejReason>())
text_msg += " Rejected OrdRejReason: " +
std::to_string(msg.get<fix::OrdRejReason>());
if (msg.has<fix::DeleteReason>())
text_msg += " Cancelled DeleteReason: " +
std::to_string(msg.get<fix::DeleteReason>());
// allocate from our pool
ExecutionReport er;
ExecutionReport *data = &er;
if (msg.has<fix::Memo>())
data->memo = msg.get<fix::Memo>();
data->exec_type = api::orderman::ExecType(msg.get<fix::ExecType>());
data->msg_seq_num = msg.get<fix::MsgSeqNum>();
// copy data from fix msg to our structure
if (msg.has<fix::ClearingAccount>())
str_to(msg.get<fix::ClearingAccount>(), &data->account_name);
if (msg.has<fix::TargetSubID>())
str_to(msg.get<fix::TargetSubID>(), &data->operator_id);
if (msg.has<fix::CompanyID>())
data->ice_company_id = msg.get<fix::CompanyID>();
data->order_key = ok;
data->security = sec;
data->leg_security = leg_security;
data->side = api::Side(msg.get<fix::Side>());
if (msg.has<fix::TimeInForce>())
data->time_in_force =
api::orderman::TimeInForce(msg.get<fix::TimeInForce>());
else
data->time_in_force = api::orderman::TIME_IN_FORCE_UNDEFINED;
if (msg.has<fix::OrdType>())
data->order_type =
api::orderman::NativeOrderType(msg.get<fix::OrdType>());
else
data->order_type = api::orderman::NATIVE_ORDER_TYPE_UNDEFINED;
data->order_status =
api::orderman::NativeOrderStatus(msg.get<fix::OrdStatus>());
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<ExchangeOrderID>(data->exchange_order_id));
data->ice_cl_ord_id = 0;
if (msg.has<fix::Int_ClOrdID>())
data->ice_cl_ord_id = msg.get<fix::Int_ClOrdID>();
if (msg.has<fix::Price>())
data->price = securities::decode_exchange_price(
*sec, Price(msg.get<fix::Price>()));
else
data->price = Price(os::nan());
if (msg.has<fix::StopPx>())
data->stop_price = securities::decode_exchange_price(
*sec, Price(msg.get<fix::StopPx>()));
else
data->stop_price = Price(os::nan());
if (msg.has<fix::MaxShow>())
data->max_show = Quantity(msg.get<fix::MaxShow>());
else
data->max_show = Quantity(0);
if (msg.has<fix::OrderQty>())
data->quantity = Quantity(msg.get<fix::OrderQty>());
else
data->quantity = Quantity(0);
data->filled_quantity = Quantity(msg.get<fix::CumQty>());
data->last_shares = msg.has<fix::LastShares>()
? Quantity(msg.get<fix::LastShares>())
: Quantity(0);
auto last_px_sec = data->leg_security ? data->leg_security : sec;
data->last_price = msg.has<fix::LastPx>()
? securities::decode_exchange_price(
*last_px_sec, Price(msg.get<fix::LastPx>()))
: Price(0);
data->avg_price = msg.has<fix::AvgPx>()
? securities::decode_exchange_price(
*last_px_sec, Price(msg.get<fix::AvgPx>()))
: Price(0);
if (msg.has<fix::ExecID>()) {
auto execid = msg.get<fix::ExecID>();
str_to(execid, &data->exchange_fill_id);
if (msg.has<fix::Memo>())
me->exec_id_to_memo[execid] = msg.get<fix::Memo>();
}
if (msg.has<fix::TransactTimeStr>()) {
std::string stamp = msg.get<fix::TransactTimeStr>();
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;
me->on_exe_report(
std::make_shared<ExecutionReport>(std::move(er)), msg.trace());
}
}
void ICEExecutionGateway::on_cancel_reject(
const jetfix::fix::ICECancelRejectMsg &msg) {
OrderKey ok(msg.get<fix::Int_ClOrdID>());
std::string reject_msg;
if (msg.has<fix::Text>())
reject_msg = msg.get<fix::Text>();
else if (msg.has<fix::CXlRejReason>())
reject_msg = fmt::format(
"Order cancel was rejected with error code {}",
msg.get<fix::CXlRejReason>());
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<fix::Text>() ? m.get<fix::Text>() : 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<fix::Text>() ? m.get<fix::Text>() : 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