Files
spark/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.cpp
T
2020-07-02 07:26:43 -04:00

162 lines
6.0 KiB
C++

#include "IL3ExecutionReportParser.h"
#include "IL3GeneratedFull.h"
#include "ILink3Utils.h"
#include "IL3Dispatcher.h"
#include "lib/md/IntPrice.h"
#include "lib/orderman/orderman_utils/fix_order_codec.h"
namespace bts {
namespace orderman {
namespace ilink3 {
IL3ExecutionReportParser::IL3ExecutionReportParser(md::SecurityOracle* o):
oracle(o),
most_recent_account()
{}
void IL3ExecutionReportParser::cache(const api::Security* security) {
auto id = SecurityKey { security->id() };
if (security_cache.find(id) == security_cache.end()) {
security_cache.insert(std::make_pair(id, security));
}
}
const api::Security* IL3ExecutionReportParser::get_security(SecurityKey id) {
auto iter = security_cache.find(id);
if (iter == security_cache.end()) {
if (oracle) {
return oracle->get_security(id);
}
else return nullptr;
}
return iter->second;
}
template <class T>
void IL3ExecutionReportParser::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());
auto& price = const_cast<T&>(msg).price();
data->exchange = api::VENUE_GLBX;
data->exchange_order_id = ExchangeOrderIDu64 { 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->security = get_security(ExchangeSecurityKey { (uint64_t) msg.securityID() });
data->order_type = map_order_type(msg.ordType());
data->price = Price { price.mantissa() * md::pow10(price.exponent()) };
data->quantity = Quantity { msg.orderQty() };
data->side = map_side(msg.side());
data->stop_price = Price { 0.0 };
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;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportNew522& msg, ExecutionReport* data) {
common(msg, data);
data->avg_price = Price { 0.0 };
data->cfg = nullptr;
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::NATIVE_ORDER_STATUS_NEW;
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportReject523& msg, ExecutionReport* data) {
common(msg, data);
data->order_status = api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_REJECTED;
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportElimination524& msg, ExecutionReport* data) {
common(msg, data);
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportTradeOutright525& msg, ExecutionReport* data) {
common(msg, data);
switch(msg.ordStatus()) {
case iLinkBinary::OrdStatusTrd::Filled:
data->order_status = api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_FILLED;
break;
case iLinkBinary::OrdStatusTrd::PartiallyFilled:
data->order_status = api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
break;
default:
data->order_status = api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNDEFINED;
}
data->last_shares = Quantity { msg.lastQty() };
data->filled_quantity = Quantity { msg.cumQty() };
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportTradeSpread526& msg, ExecutionReport* data) {
common(msg, data);
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportTradeSpreadLeg527& msg, ExecutionReport* data) {
// determine spread security from order key?
(void)msg;
(void)data;
return false;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportModify531& msg, ExecutionReport* data) {
common(msg, data);
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportStatus532& msg, ExecutionReport* data) {
common(msg, data);
// data->order_status = msg.
return true;
}
bool IL3ExecutionReportParser::parse(const iLinkBinary::ExecutionReportCancel534& msg, ExecutionReport* data) {
common(msg, data);
data->order_status = api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_CANCELED;
return true;
}
void IL3ExecutionReportParser::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 {
IL3ExecutionReportParser& 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, IL3ExecutionReportParser& parser, ExecutionReport* result) {
ParseHandler handler { parser, result };
consume_messages(handler, data.c_str(), data.size(), dispatch_execution_reports<ParseHandler>);
return handler.success;
}
}}}//namespace