diff --git a/src/lib/orderman/actors/GLBXiLink3ExecutionGateway.cpp b/src/lib/orderman/actors/GLBXiLink3ExecutionGateway.cpp index b1d617b04..bd05811c3 100644 --- a/src/lib/orderman/actors/GLBXiLink3ExecutionGateway.cpp +++ b/src/lib/orderman/actors/GLBXiLink3ExecutionGateway.cpp @@ -11,6 +11,7 @@ #include "lib/orderman/actors/ilink3/SessionStore.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/orderman_utils/fix_order_codec.h" +#include "lib/orderman/actors/ilink3/IL3SecDB.h" namespace bts { namespace orderman { @@ -28,8 +29,7 @@ struct GLBXiLink3ExecutionGateway::PImpl { PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &log, md::SecurityOracle o) : state(), cfg(cfg), reactor(), start_of_pause(), on_exe_report(), - on_gw_status_change(), on_cancel_reject(), perf_log(log), - sec_oracle(o), securities(), securities_by_descriptor(), + on_gw_status_change(), on_cancel_reject(), perf_log(log), db(o), gw_name(cfg.name()), session_store(), gw(), connection(nullptr), connection_failures(0), started(false) { TCPEndpoint endpoint{cfg.socket_connect_host_ip(), @@ -47,10 +47,7 @@ struct GLBXiLink3ExecutionGateway::PImpl { OnGwStatusChange on_gw_status_change; orderman::OnCancelReject on_cancel_reject; trace::PerfLog &perf_log; - md::SecurityOracle sec_oracle; - std::unordered_map securities; - std::unordered_map - securities_by_descriptor; + IL3SecDB db; GatewayName gw_name; std::optional session_store; std::optional gw; @@ -133,13 +130,13 @@ void GLBXiLink3ExecutionGateway::connect(jetfix::SessionName const &sn) { c.firm = me->cfg.sender_compid().substr(3); c.failover_on_disconnect = false; - me->session_store.emplace(me->sec_oracle, sn, me->cfg); + me->session_store.emplace(me->db, sn, me->cfg); c.raw_message_logger = [store = &me->session_store.value()](auto d, auto const &msg) { store->async_store_raw_msg(d, msg); }; - me->gw.emplace(*me->connection, me->framer, &me->sec_oracle, + me->gw.emplace(*me->connection, me->framer, &me->db, me->session_store->starting_session_descriptor(), c, me->cfg); me->gw->subscribe_cancel_reject([this](auto k, auto const &msg) { @@ -205,9 +202,7 @@ void GLBXiLink3ExecutionGateway::on_securities( for (auto const &ev : *(update.events)) { auto sec_ptr = ev.security; - me->securities[sec_ptr->id()] = sec_ptr; - me->securities_by_descriptor[securities::descriptor(*sec_ptr)] = - sec_ptr; + me->db.add_security(*sec_ptr); } if (me->state == State::WAITING_FOR_SECURITIES) { diff --git a/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.cpp b/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.cpp index 576f6337e..5f44775cd 100644 --- a/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.cpp +++ b/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.cpp @@ -1,4 +1,6 @@ + #include "IL3ExecutionReportParser.h" + #include "IL3Dispatcher.h" #include "IL3GeneratedFull.h" #include "ILink3Utils.h" @@ -6,37 +8,33 @@ #include "lib/md/IntPrice.h" #include "lib/orderman/orderman_utils/fix_order_codec.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/orderman/actors/ilink3/IL3SecDB.h" namespace bts { namespace orderman { namespace ilink3 { -IL3ExecutionReportParser::IL3ExecutionReportParser( - config::JetFIX::Connection const &cfg, md::SecurityOracle *o) - : oracle(o), most_recent_account(), cfg(cfg) {} - -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)); - } -} +IL3ExecutionReportParser::IL3ExecutionReportParser(config::JetFIX::Connection const &cfg, IL3SecDB *db) + : most_recent_account(), cfg(cfg) ,db(db) {} 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; + if (db) + return db->get_security(id); + else + return nullptr; +} + +const api::Security * IL3ExecutionReportParser::get_security(ExchangeSecurityKey id) { + if (db) + return db->get_security(id); + else + return nullptr; } template -void IL3ExecutionReportParser::common(const T &msg, ExecutionReport *data) { +void IL3ExecutionReportParser::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; @@ -50,8 +48,6 @@ void IL3ExecutionReportParser::common(const T &msg, ExecutionReport *data) { 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()); auto &price = const_cast(msg).price(); data->price = securities::decode_exchange_price( @@ -66,9 +62,43 @@ void IL3ExecutionReportParser::common(const T &msg, ExecutionReport *data) { data->cfg = &cfg; } +template +void IL3ExecutionReportParser::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(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 IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportNew522 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); data->avg_price = Price{0.0}; data->cust_order_handling_inst = ""; data->exchange_fill_id = ExchangeFillID{}; @@ -80,85 +110,101 @@ bool IL3ExecutionReportParser::parse( data->memo = ""; data->msg = ""; data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus()); - // data->order_status = - // api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_NEW; - return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportReject523 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus()); data->msg = msg.getTextAsStringView(); - // data->order_status = - // api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_REJECTED; return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportElimination524 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus()); return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportTradeOutright525 &msg, ExecutionReport *data) { - common(msg, 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(msg).lastPx(); - data->last_price = securities::decode_exchange_price( - *data->security, Price{price.mantissa() * md::pow10(price.exponent())}); - data->filled_quantity = Quantity{msg.cumQty()}; + parse_common(msg, data); + parse_trade(msg, data); return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportTradeSpread526 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); + parse_trade(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; + 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(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 IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportModify531 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus()); return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportStatus532 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); // data->order_status = msg. return true; } bool IL3ExecutionReportParser::parse( const iLinkBinary::ExecutionReportCancel534 &msg, ExecutionReport *data) { - common(msg, data); + parse_common(msg, data); data->order_status = api::orderman::NativeOrderStatus(*msg.ordStatus()); return true; } diff --git a/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.h b/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.h index e1c715953..49af61895 100644 --- a/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.h +++ b/src/lib/orderman/actors/ilink3/IL3ExecutionReportParser.h @@ -11,18 +11,17 @@ namespace bts { namespace orderman { namespace ilink3 { +class IL3SecDB; + class IL3ExecutionReportParser { - btree::btree_map security_cache; - md::SecurityOracle* oracle; AccountName most_recent_account; config::JetFIX::Connection const cfg; - + IL3SecDB *db; const api::Security* get_security(SecurityKey); + const api::Security* get_security(ExchangeSecurityKey); public: - IL3ExecutionReportParser(config::JetFIX::Connection const &, md::SecurityOracle*); - void cache(const api::Security*); - + IL3ExecutionReportParser(config::JetFIX::Connection const &, IL3SecDB *sec_db); bool parse(const iLinkBinary::ExecutionReportNew522&, ExecutionReport*); bool parse(const iLinkBinary::ExecutionReportReject523&, ExecutionReport*); bool parse(const iLinkBinary::ExecutionReportElimination524&, ExecutionReport*); @@ -34,7 +33,8 @@ public: bool parse(const iLinkBinary::ExecutionReportCancel534&, ExecutionReport*); void set_account(const iLinkBinary::PartyDetailsDefinitionRequestAck519&); - template void common(const T&, ExecutionReport*); + template void parse_common(const T&, ExecutionReport*); + template void parse_trade(const T&, ExecutionReport*); }; bool parse_report(const std::string&, IL3ExecutionReportParser&, ExecutionReport*); diff --git a/src/lib/orderman/actors/ilink3/IL3SecDB.h b/src/lib/orderman/actors/ilink3/IL3SecDB.h new file mode 100644 index 000000000..82a87b809 --- /dev/null +++ b/src/lib/orderman/actors/ilink3/IL3SecDB.h @@ -0,0 +1,40 @@ +#pragma once + +#include "lib/domain_model/Security.pb.h" +#include "lib/domain_model/SecurityKey.h" +#include "lib/md/ExchangeSecurityKey.h" +#include "lib/md/SecurityOracle.h" + +namespace bts { +namespace orderman { +namespace ilink3 { + +class IL3SecDB { +public: + IL3SecDB(md::SecurityOracle& oracle) : oracle(oracle) {} + + void add_security(const api::Security &sec) { + auto id = SecurityKey{sec.id()}; + security_by_key[id] = &sec; + auto ex_id = ExchangeSecurityKey{sec.glbx().exchange_id()}; + security_by_exchange_key[ex_id] = &sec; + } + const api::Security *get_security(SecurityKey sk) { + auto &sec = security_by_key[sk]; + if (!sec) + sec = oracle.get_security(sk); + return sec; + } + const api::Security *get_security(ExchangeSecurityKey k) { + auto &sec = security_by_exchange_key[k]; + if (!sec) + sec = oracle.get_security(k); + return sec; + } + +private: + md::SecurityOracle &oracle; + std::unordered_map security_by_key; + std::unordered_map security_by_exchange_key; +}; +}}} diff --git a/src/lib/orderman/actors/ilink3/ILinkGW.cpp b/src/lib/orderman/actors/ilink3/ILinkGW.cpp index 27682872e..0a5e18203 100644 --- a/src/lib/orderman/actors/ilink3/ILinkGW.cpp +++ b/src/lib/orderman/actors/ilink3/ILinkGW.cpp @@ -37,8 +37,8 @@ log::CharPtrWrapper wrap(const char* str) { return log::CharPtrWrapper{ str }; } ILinkGW::ILinkGW( IL3Connection& con, - IL3MessageFramer& buffer, - md::SecurityOracle* oracle, + IL3MessageFramer& buffer, + IL3SecDB *db, const Session& session, const Config& conf, config::JetFIX::Connection const & cfg) : @@ -53,7 +53,7 @@ ILinkGW::ILinkGW( _msg_count(0), _time(), _pool(), - _exe_parser(cfg,oracle), + _exe_parser(cfg,db), _retransmit() { _clorid_buffer.resize(clordid_sz()); @@ -281,8 +281,6 @@ void ILinkGW::send_order(const CreateOrderMsg& create) { msg.liquidityFlag(iLinkBinary::BooleanNULL::NULL_VALUE); msg.managedOrder(iLinkBinary::BooleanNULL::NULL_VALUE); msg.shortSaleType(iLinkBinary::ShortSaleType::NULL_VALUE); - - _exe_parser.cache(create.security); log_out(msg); } diff --git a/src/lib/orderman/actors/ilink3/ILinkGW.h b/src/lib/orderman/actors/ilink3/ILinkGW.h index 4553bdf0d..f332be735 100644 --- a/src/lib/orderman/actors/ilink3/ILinkGW.h +++ b/src/lib/orderman/actors/ilink3/ILinkGW.h @@ -23,6 +23,8 @@ namespace orderman { class ExecutionReport; namespace ilink3 { +class IL3SecDB; + struct Session { uint64_t uuid; // bts->cme @@ -84,8 +86,8 @@ class ILinkGW : IL3ConnectionHandler { public: ILinkGW( IL3Connection&, - IL3MessageFramer&, - md::SecurityOracle*, + IL3MessageFramer&, + IL3SecDB*, const Session&, const Config&, config::JetFIX::Connection const &); diff --git a/src/lib/orderman/actors/ilink3/SessionStore.cpp b/src/lib/orderman/actors/ilink3/SessionStore.cpp index bce9aaa34..d7e7e580e 100644 --- a/src/lib/orderman/actors/ilink3/SessionStore.cpp +++ b/src/lib/orderman/actors/ilink3/SessionStore.cpp @@ -6,6 +6,7 @@ #include "lib/utils/bts_chrono.h" #include "lib/utils/file_utils.h" #include "lib/utils/thread_manager.h" +#include "lib/orderman/actors/ilink3/IL3SecDB.h" #include #include @@ -45,7 +46,7 @@ static std::string session_path(std::string const &store_path) { } static std::vector -load_replay(md::SecurityOracle &, config::JetFIX::Connection const &cfg, +load_replay(IL3SecDB &,config::JetFIX::Connection const &cfg, std::string const &path) { std::vector result; @@ -110,12 +111,12 @@ Session load_session(jetfix::SessionName const &sn, std::string const &path) { } struct SessionStore::PImpl { - PImpl(md::SecurityOracle &oracle, jetfix::SessionName const &session_name, + PImpl(IL3SecDB &db,jetfix::SessionName const &session_name, config::JetFIX::Connection const &cfg, std::string const &base_path) : cfg(cfg), store_path(base_store_path(base_path, cfg.name(), session_name.as_string)), replay_messages( - load_replay(oracle, cfg, raw_msg_log_path(store_path))), + load_replay(db, cfg, raw_msg_log_path(store_path))), starting_session_descriptor( load_session(session_name, session_path(store_path))), raw_msg_store() { @@ -132,11 +133,11 @@ struct SessionStore::PImpl { std::ofstream raw_msg_store; }; -SessionStore::SessionStore(md::SecurityOracle &oracle, +SessionStore::SessionStore(IL3SecDB &db, jetfix::SessionName const &session_name, config::JetFIX::Connection const &cfg, std::string base_path) - : me(new PImpl(oracle, session_name, cfg, base_path)) { + : me(new PImpl(db,session_name, cfg, base_path)) { me->storage_thread = threading::make_thread_from_function( "il3_store", [this]() { do_work(); }); } diff --git a/src/lib/orderman/actors/ilink3/SessionStore.h b/src/lib/orderman/actors/ilink3/SessionStore.h index 391ebceda..f9459f054 100644 --- a/src/lib/orderman/actors/ilink3/SessionStore.h +++ b/src/lib/orderman/actors/ilink3/SessionStore.h @@ -9,7 +9,6 @@ // fwds namespace bts { namespace jetfix {struct SessionName;} -namespace md { class SecurityOracle;} namespace orderman {class ExecutionReport;} } @@ -18,9 +17,11 @@ namespace orderman { namespace ilink3 { struct Session; +class IL3SecDB; + class SessionStore { public: - SessionStore(md::SecurityOracle &, + SessionStore(IL3SecDB &, jetfix::SessionName const&session_name, config::JetFIX::Connection const&, std::string base_path=""); diff --git a/src/lib/orderman/orders/GLBXNativeOrder.h b/src/lib/orderman/orders/GLBXNativeOrder.h index 507e187b8..b6d6ad046 100644 --- a/src/lib/orderman/orders/GLBXNativeOrder.h +++ b/src/lib/orderman/orders/GLBXNativeOrder.h @@ -50,7 +50,6 @@ class GLBXNativeOrder : public NativeOrderBase { OrderOpResult _on_activate(log::Level reject_level, log::Level transition_level, Price p, Quantity q, bts::orderman::OrderServices &services); - void init_fix(); void add_exchange_order_id_to_fix(); void transition(log::Level, diff --git a/testing/manual/orders/order_entry.txt b/testing/manual/orders/order_entry.txt index 33596769a..7f15d4f51 100644 --- a/testing/manual/orders/order_entry.txt +++ b/testing/manual/orders/order_entry.txt @@ -10,7 +10,7 @@ ** Partial fills - Enter a limit order that you can cause to be partially filled either on entry or by an aggressor - Verify the partial fill causes the filled quantity to update and that you have a correct average price -- Cause the order to be completely filled and verify its updated orrectly +- Cause the order to be completely filled and verify its updated correctly * Spreads - Enter a spread trade - Modify its price and quantity, verify they are modified