334 lines
12 KiB
C++
334 lines
12 KiB
C++
|
|
#include "IL3Parser.h"
|
|
|
|
#include "IL3Dispatcher.h"
|
|
#include "IL3GeneratedFull.h"
|
|
#include "ILink3Utils.h"
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/md/IntPrice.h"
|
|
#include "lib/orderman/actors/ilink3/IL3SecDB.h"
|
|
#include "lib/orderman/orderman_utils/CancelReject.h"
|
|
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
|
#include "lib/orderman/orderman_utils/orderman_common.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
namespace ilink3 {
|
|
|
|
IL3Parser::IL3Parser(config::JetFIX::Connection const *cfg, IL3SecDB *db)
|
|
: most_recent_account(), cfg(cfg), db(db) {}
|
|
|
|
const api::Security *IL3Parser::get_security(SecurityKey id) {
|
|
if (db)
|
|
return db->get_security(id);
|
|
else
|
|
return nullptr;
|
|
}
|
|
|
|
const api::Security *IL3Parser::get_security(ExchangeSecurityKey id) {
|
|
if (db)
|
|
return db->get_security(id);
|
|
else
|
|
return nullptr;
|
|
}
|
|
|
|
template <class T>
|
|
void IL3Parser::parse_common(const T &msg, ExecutionReport *data) {
|
|
data->account_name = most_recent_account;
|
|
static_assert(sizeof(OperatorID) == T::senderIDLength());
|
|
memcpy(&data->operator_id.value(), msg.senderID(), msg.senderIDLength());
|
|
data->exchange = api::VENUE_GLBX;
|
|
data->exchange_order_id = ExchangeOrderID64{msg.orderID()};
|
|
data->leg_security = nullptr;
|
|
data->manual_order_indicator =
|
|
map_manual_order_indicator(msg.manualOrderIndicator());
|
|
data->msg_seq_num = msg.seqNum();
|
|
SecurityKey sk;
|
|
decode_clorid(msg.clOrdID(), &data->order_key, &sk);
|
|
auto sec = get_security(sk);
|
|
bts_assert(sec);
|
|
data->security = sec;
|
|
data->order_type = map_order_type(msg.ordType());
|
|
auto &price = const_cast<T &>(msg).price();
|
|
data->price = securities::decode_exchange_price(
|
|
*sec, Price{price.mantissa() * md::pow10(price.exponent())});
|
|
data->quantity = Quantity{msg.orderQty()};
|
|
data->side = map_side(msg.side());
|
|
auto &stop_price = const_cast<T &>(msg).stopPx();
|
|
data->stop_price = securities::decode_exchange_price(
|
|
*sec, Price{stop_price.mantissa() * md::pow10(stop_price.exponent())});
|
|
data->time_in_force = map_time_in_force(msg.timeInForce());
|
|
data->transact_time = ExchangeTimeStamp64{msg.transactTime()};
|
|
data->ice_cl_ord_id = 0;
|
|
data->ice_company_id = 0;
|
|
data->cfg = cfg;
|
|
}
|
|
|
|
template <class T>
|
|
void IL3Parser::parse_trade(const T &msg, ExecutionReport *data) {
|
|
orderman::to_array(msg.getExecIDAsStringView(),
|
|
&data->exchange_fill_id.value());
|
|
switch (msg.ordStatus()) {
|
|
case iLinkBinary::OrdStatusTrd::Filled:
|
|
data->order_status =
|
|
api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_FILLED;
|
|
data->exec_type = api::orderman::EXEC_TYPE_FILL;
|
|
break;
|
|
case iLinkBinary::OrdStatusTrd::PartiallyFilled:
|
|
data->order_status = api::orderman::NativeOrderStatus::
|
|
NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
|
|
data->exec_type = api::orderman::EXEC_TYPE_PARTIAL_FILL;
|
|
break;
|
|
default:
|
|
data->order_status =
|
|
api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNDEFINED;
|
|
}
|
|
data->last_shares = Quantity{msg.lastQty()};
|
|
auto &price = const_cast<T &>(msg).lastPx();
|
|
if (data->leg_security) {
|
|
data->last_price = securities::decode_exchange_price(
|
|
*data->leg_security,
|
|
Price{price.mantissa() * md::pow10(price.exponent())});
|
|
} else {
|
|
data->last_price = securities::decode_exchange_price(
|
|
*data->security,
|
|
Price{price.mantissa() * md::pow10(price.exponent())});
|
|
}
|
|
data->filled_quantity = Quantity{msg.cumQty()};
|
|
}
|
|
|
|
bool IL3Parser::has_account() const {
|
|
return strlen(most_recent_account.value().data());
|
|
}
|
|
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportNew522 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
data->avg_price = Price{0.0};
|
|
data->cust_order_handling_inst = "";
|
|
data->exchange_fill_id = ExchangeFillID{};
|
|
data->exec_type = api::orderman::EXEC_TYPE_NEW;
|
|
data->filled_quantity = Quantity{0};
|
|
data->last_price = Price{0.0};
|
|
data->last_shares = Quantity{0};
|
|
data->max_show = Quantity{0};
|
|
data->memo = "";
|
|
data->msg = "";
|
|
data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus());
|
|
return true;
|
|
}
|
|
|
|
template <class ER>
|
|
bool try_parse_exe_report_helper(IL3Parser &parser, std::string_view const &msg,
|
|
ExecutionReport *out) {
|
|
|
|
iLinkBinary::MessageHeader mheader{
|
|
(char *)msg.data(), 0, msg.size(),
|
|
iLinkBinary::MessageHeader::sbeSchemaVersion()};
|
|
|
|
if (mheader.templateId() == ER::sbeTemplateId()) {
|
|
return parser.parse(
|
|
ER{(char *)msg.data(), iLinkBinary::MessageHeader::encodedLength(),
|
|
msg.size(), mheader.blockLength(), mheader.version()},
|
|
out);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::optional<ExecutionReport>
|
|
IL3Parser::try_parse_exe_report(std::string_view const &msg) {
|
|
using namespace iLinkBinary;
|
|
|
|
if (has_account()) {
|
|
ExecutionReport report;
|
|
if (try_parse_exe_report_helper<ExecutionReportNew522>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportReject523>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportElimination524>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportTradeOutright525>(
|
|
*this, msg, &report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportTradeSpread526>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportTradeSpreadLeg527>(
|
|
*this, msg, &report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportModify531>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportStatus532>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
if (try_parse_exe_report_helper<ExecutionReportCancel534>(*this, msg,
|
|
&report))
|
|
return {report};
|
|
}
|
|
|
|
iLinkBinary::MessageHeader mheader{
|
|
(char *)msg.data(), 0, msg.size(),
|
|
iLinkBinary::MessageHeader::sbeSchemaVersion()};
|
|
|
|
if (mheader.templateId() ==
|
|
PartyDetailsDefinitionRequestAck519::sbeTemplateId())
|
|
set_account(PartyDetailsDefinitionRequestAck519{
|
|
(char *)msg.data(), iLinkBinary::MessageHeader::encodedLength(),
|
|
msg.size(), mheader.blockLength(), mheader.version()});
|
|
return {};
|
|
}
|
|
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportReject523 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus());
|
|
data->msg = msg.getTextAsStringView();
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportElimination524 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus());
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportTradeOutright525 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
parse_trade(msg, data);
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportTradeSpread526 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
parse_trade(msg, data);
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportTradeSpreadLeg527 &msg,
|
|
ExecutionReport *data) {
|
|
data->account_name = most_recent_account;
|
|
data->avg_price = Price{0.0};
|
|
static_assert(sizeof(OperatorID) ==
|
|
iLinkBinary::ExecutionReportTradeSpread526::senderIDLength());
|
|
memcpy(&data->operator_id.value(), msg.senderID(), msg.senderIDLength());
|
|
data->exchange = api::VENUE_GLBX;
|
|
data->exchange_order_id = ExchangeOrderID64{msg.orderID()};
|
|
|
|
data->msg_seq_num = msg.seqNum();
|
|
SecurityKey sk;
|
|
decode_clorid(msg.clOrdID(), &data->order_key, &sk);
|
|
auto sec = get_security(sk);
|
|
bts_assert(sec);
|
|
data->security = sec;
|
|
|
|
data->leg_security =
|
|
get_security(ExchangeSecurityKey{(uint64_t)msg.securityID()});
|
|
if (data->leg_security == nullptr) {
|
|
LOG(iLink3, 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 "
|
|
"security id: {}",
|
|
sk.value(), msg.securityID());
|
|
return false;
|
|
}
|
|
data->order_type = map_order_type(msg.ordType());
|
|
auto &price =
|
|
const_cast<iLinkBinary::ExecutionReportTradeSpreadLeg527 &>(msg)
|
|
.lastPx();
|
|
data->price = securities::decode_exchange_price(
|
|
*data->leg_security,
|
|
Price{price.mantissa() * md::pow10(price.exponent())});
|
|
data->quantity = Quantity{0};
|
|
data->side = map_side(msg.side());
|
|
data->stop_price = Price{0.0};
|
|
data->time_in_force = api::orderman::TimeInForce::TIME_IN_FORCE_UNDEFINED;
|
|
data->transact_time = ExchangeTimeStamp64{msg.transactTime()};
|
|
data->ice_cl_ord_id = 0;
|
|
data->ice_company_id = 0;
|
|
data->cfg = cfg;
|
|
parse_trade(msg, data);
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportModify531 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus());
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportStatus532 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
return true;
|
|
}
|
|
bool IL3Parser::parse(const iLinkBinary::ExecutionReportCancel534 &msg,
|
|
ExecutionReport *data) {
|
|
parse_common(msg, data);
|
|
data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus());
|
|
return true;
|
|
}
|
|
|
|
bool IL3Parser::parse(const iLinkBinary::OrderCancelReject535 &msg,
|
|
CancelReject *cr) {
|
|
SecurityKey sk;
|
|
OrderKey ok;
|
|
decode_clorid(msg.clOrdID(), &ok, &sk);
|
|
cr->order_key = ok;
|
|
cr->reject_msg = msg.getTextAsStringView();
|
|
// https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+Reject+Codes
|
|
if (msg.cxlRejReason() == 2045)
|
|
cr->reason = CancelRejectReason::UnknownOrder;
|
|
else
|
|
cr->reason = CancelRejectReason::Other;
|
|
return true;
|
|
}
|
|
|
|
void IL3Parser::set_account(
|
|
const iLinkBinary::PartyDetailsDefinitionRequestAck519 &msg) {
|
|
// When using on-demand format the PartyDetailsDefinitionRequestAck519
|
|
// is sent back immediately preceding the execution report on the wire.
|
|
auto details =
|
|
const_cast<iLinkBinary::PartyDetailsDefinitionRequestAck519 &>(msg)
|
|
.noPartyDetails();
|
|
while (details.hasNext()) {
|
|
details.next();
|
|
if (details.partyDetailRole() ==
|
|
iLinkBinary::PartyDetailRole::CustomerAccount) {
|
|
const char *p_account = details.partyDetailID();
|
|
strncpy(most_recent_account.value().data(), p_account,
|
|
most_recent_account.value().size());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ParseHandler {
|
|
IL3Parser &parser;
|
|
ExecutionReport *result;
|
|
bool success = false;
|
|
template <class T> void handle(const T &t) {
|
|
success = parser.parse(t, result);
|
|
}
|
|
void handle(iLinkBinary::PartyDetailsDefinitionRequestAck519 &t) {
|
|
parser.set_account(t);
|
|
}
|
|
};
|
|
|
|
bool parse_report(const std::string &data, IL3Parser &parser,
|
|
ExecutionReport *result) {
|
|
ParseHandler handler{parser, result};
|
|
consume_messages(handler, data.c_str(), data.size(),
|
|
dispatch_execution_reports<ParseHandler>);
|
|
return handler.success;
|
|
}
|
|
|
|
} // namespace ilink3
|
|
} // namespace orderman
|
|
} // namespace bts
|