Towards il3 new

This commit is contained in:
Pedro Pinto committed 2021-02-04 13:49:17 -05:00
1 parent b8f146364b
commit e892b8b3e5
14 files changed
+111 -54

No files matched your search

@@ -1,3 +1,6 @@
* CME Risk Controls
** [[https://www.cmegroup.com/tools-information/webhelp/account-manager-service/Content/calculating-icc.html?Highlight=icc][ICC]]
** GC2
* MDP3
** Templates
- [[ftp://ftp.cmegroup.com/SBEFix/][Templates and Config FTP Site]]
@@ -8,6 +8,6 @@
namespace bts::spartan::mdp3 {
using MarketSegmentID =
utils::ValueWrapper<struct MarketSegmentIDTag, int32_t>;
utils::ValueWrapper<struct MarketSegmentIDTag, uint8_t>;
}
@@ -15,6 +15,7 @@ template <class M> void SecurityDefinition::init(M m) {
security_group_ =
str_from_fixed_array(m.securityGroup(), m.securityGroupEncodingLength());
asset_ = str_from_fixed_array(m.asset(), m.assetEncodingLength());
market_segment_id_.value() = m.marketSegmentID();
display_factor_ =
Price(m.displayFactor().mantissa(), m.displayFactor().exponent())
.to_double();
@@ -14,7 +14,7 @@ public:
virtual OrderHandle queue(NewOrder const&) = 0;
virtual void queue(OrderHandle,CancelReplace const&) = 0;
virtual void queue(OrderHandle,Cancel const&) = 0;
virtual void send() = 0;
virtual void send_queued() = 0;
};
}
@@ -7,6 +7,7 @@ struct OrderUpdate;
class IGatewayObserver {
public:
virtual void on_gateway_ready() = 0;
virtual void on_error(GatewayError const&) = 0;
virtual void on_order_update(OrderUpdate const&) = 0;
};
@@ -1,15 +1,17 @@
#pragma once
#include "server/sparkd/cmds/spartan/domain/Price.h"
#include "server/sparkd/cmds/spartan/domain/Quantity.h"
#include "server/sparkd/cmds/spartan/domain/Side.h"
#include "server/sparkd/cmds/spartan/domain/SecurityID.h"
#include "domain/Price.h"
#include "domain/Quantity.h"
#include "domain/Side.h"
#include "domain/SecurityID.h"
#include "domain/MarketSegmentID.h"
namespace bts::spartan {
class NewOrder {
public:
const mdp3::SecurityID security_id;
const mdp3::MarketSegmentID segment_id;
const mdp3::Price price;
const mdp3::Quantity quantity;
const Side side;
@@ -58,10 +58,10 @@ void IL3Gateway::on_connect() {
void IL3Gateway::on_disconnect() {
switch (me->state) {
case State::CONNECTING:
on_error({GatewayError::FAILED_TO_CONNECT, "Failed to connect"});
on_error({GatewayError::FAILED_TO_CONNECT});
break;
default:
on_error({GatewayError::FAILED_TO_CONNECT, "Unexpected disconnect"});
on_error({GatewayError::UNEXPECTED_DISCONNECT});
break;
}
}
@@ -94,17 +94,32 @@ void IL3Gateway::on_data(std::string_view buffer) {
});
}
OrderHandle IL3Gateway::queue(NewOrder const &) { return {}; }
void IL3Gateway::queue(OrderHandle, CancelReplace const &) {}
void IL3Gateway::queue(OrderHandle, Cancel const &) {}
void IL3Gateway::send() { IL3MsgSender::flush(); }
OrderHandle IL3Gateway::queue(NewOrder const &o) {
bts_assert(me->state == State::READY);
return std::get<IL3RealTimeHandler>(me->handler).queue(o);
}
void IL3Gateway::queue(OrderHandle h, CancelReplace const &cr) {
bts_assert(me->state == State::READY);
std::get<IL3RealTimeHandler>(me->handler).queue(h,cr);
}
void IL3Gateway::queue(OrderHandle h, Cancel const &c) {
bts_assert(me->state == State::READY);
std::get<IL3RealTimeHandler>(me->handler).queue(h,c);
}
void IL3Gateway::send_queued() {
bts_assert(me->state == State::READY);
IL3MsgSender::flush();
}
void IL3Gateway::on_init_success(IL3InitRec const &rec) {
bts_assert(me->state == State::INITIALIZING);
transition(State::READY);
auto rec_cpy = rec;
me->handler.emplace<IL3RealTimeHandler>();
// std::get<RealTimeHandler>(me->handler).start(rec_cpy);
std::get<IL3RealTimeHandler>(me->handler)
.start(me->cfg, rec, *this, *this, me->clock);
}
void IL3Gateway::on_init_failure(GatewayError const &error) {
@@ -124,8 +139,7 @@ void IL3Gateway::on_error(GatewayError const &e) {
case State::READY:
case State::INITIALIZING:
LOG(Spartan, Error, "Shutting down gateway {} Error: {}", segment, e);
me->connection.disconnect();
transition(State::ERROR);
terminate();
break;
case State::CONNECTING:
LOG(Spartan, Error,
@@ -176,9 +190,27 @@ void IL3Gateway::on_realtime_error(GatewayError const &e) {
on_error(e);
}
void IL3Gateway::on_order_update(OrderUpdate const &update) {
void IL3Gateway::on_realtime_order_update(OrderUpdate const &update) {
bts_assert_d(me->state == State::READY);
me->observer.on_order_update(update);
}
void IL3Gateway::on_realtime_ready() {
bts_assert_d(me->state == State::READY);
me->observer.on_gateway_ready();
}
void IL3Gateway::terminate() {
bts_assert_d(me->state == State::READY ||
me->state == State::INITIALIZING);
auto terminate = enqueue<::il3::Terminate507>();
terminate.uUID(me->cfg.uuid.value());
terminate.requestTimestamp(me->clock.now().value());
terminate.errorCodes(0);
terminate.splitMsg(::il3::SplitMsg::NULL_VALUE);
flush();
transition(State::ERROR);
}
} // namespace bts::spartan::il3
@@ -51,7 +51,7 @@ class IL3Gateway : public IGateway, // Order gateway abstraction
OrderHandle queue(NewOrder const &) override;
void queue(OrderHandle, CancelReplace const &) override;
void queue(OrderHandle, Cancel const &) override;
void send() override;
void send_queued() override;
private:
// IIL3InitObserver
@@ -59,8 +59,9 @@ class IL3Gateway : public IGateway, // Order gateway abstraction
void on_init_failure(GatewayError const &) override;
// IIL3RealTimeObserver
void on_realtime_ready() override;
void on_realtime_error(GatewayError const &) override;
void on_order_update(OrderUpdate const &) override;
void on_realtime_order_update(OrderUpdate const &) override;
// IL3MsgSender
void on_flush(std::string_view bytes) override;
@@ -68,6 +69,7 @@ class IL3Gateway : public IGateway, // Order gateway abstraction
enum class State;
struct PImpl;
void on_error(GatewayError const &);
void terminate();
void transition(State new_state);
static const char *state_name(State);
std::unique_ptr<PImpl> me;
@@ -16,10 +16,10 @@ struct IL3GatewayCfg {
const size_t party_details_list_req_id_1505;
const std::string sender_id_5392;
const std::string location_9357;
const std::string segment;
const uint32_t to_cme;
const uint32_t from_cme;
bool is_new_session() const {return to_cme == 0 && from_cme == 0;};
};
@@ -8,17 +8,18 @@ class IL3MsgSender{
public:
// Allocate and enqueue (but do not yet send) a IL3 message. For
// messages types that have repeating groups or variable size
// fields the caller must manually calculate the extra bytes needed
// beyond the SBE root block size and use the parameter to request
// the extra size. An example of this calculation can be found in
// PartyDetailsHandler::send_party_details()
// fields, the caller must manually calculate the extra bytes
// needed beyond the SBE root block size and use the parameter to
// request the extra size. An example of this calculation can be
// found in PartyDetailsHandler::send_party_details()
template <class Msg> Msg enqueue(size_t extra = 0);
// Send all pending messages
inline void flush();
protected:
// Implementers can override this to process outgoing bytes
// Implementers can override this to actualy forward outgoing bytes
// to a network connection or other type of sink.
virtual void on_flush(std::string_view bytes) = 0;
private:
@@ -7,8 +7,8 @@
namespace bts::spartan::il3 {
struct IL3InitRec {
size_t from_cme = 0;
size_t to_cme = 0;
uint32_t from_cme = 0;
uint32_t to_cme = 0;
uint32_t party_reg;
std::vector<IL3MsgBox> queued_messages = {};
};
@@ -7,8 +7,9 @@ struct OrderUpdate;
class IIL3RealTimeObserver {
public:
virtual void on_realtime_ready() = 0;
virtual void on_realtime_error(GatewayError const &) = 0;
virtual void on_order_update(OrderUpdate const &) = 0;
virtual void on_realtime_order_update(OrderUpdate const &) = 0;
};
} // namespace bts::spartan
@@ -28,6 +28,7 @@
#include "execution/il3/gen/src/OrderCancelRequest516.h"
#include "execution/il3/gen/src/Sequence506.h"
#include "execution/il3/gen/src/Terminate507.h"
#include "execution/il3/handlers/init/IL3InitRec.h"
#include "lib/jetfix/codec/i32_toa.h"
#include "lib/logging/logging.h"
#include "system/Clock.h"
@@ -56,8 +57,10 @@ struct IL3RealTimeHandler::PImpl {
IL3MsgSender &sender;
Clock &clock;
uint32_t next_sn;
std::array<char, 4> sender_id_5392;
std::array<char, 4> location_9357;
// Small String optimization should kick in to avoid indirection in
// these strings
std::string sender_id_5392;
std::string location_9357;
boost::container::small_vector<WorkingOrder, 10> working_orders;
IIL3RealTimeObserver &obs;
IL3GatewayCfg const cfg;
@@ -74,8 +77,7 @@ inline size_t IL3RealTimeHandler::alloc_order() {
return n;
}
inline size_t
IL3RealTimeHandler::get_order(std::string_view clorid_str) const {
inline size_t IL3RealTimeHandler::get_order(std::string_view clorid_str) const {
uint64_t clorid_v = 0;
for (auto c : clorid_str)
clorid_v = (clorid_v * 10) + c - '0';
@@ -87,8 +89,7 @@ IL3RealTimeHandler::get_order(std::string_view clorid_str) const {
return 0;
}
inline size_t
IL3RealTimeHandler::get_order(mdp3::OrderKey exchange_k) const {
inline size_t IL3RealTimeHandler::get_order(mdp3::OrderKey exchange_k) const {
for (size_t i = 0; i < me->working_orders.size(); i++)
if (me->working_orders[i].ok == exchange_k)
return i;
@@ -99,6 +100,27 @@ IL3RealTimeHandler::get_order(mdp3::OrderKey exchange_k) const {
IL3RealTimeHandler::IL3RealTimeHandler() {}
IL3RealTimeHandler::~IL3RealTimeHandler() {}
void IL3RealTimeHandler::start(
IL3GatewayCfg const &cfg,
IL3InitRec const &init_rec,
IL3MsgSender &sender,
IIL3RealTimeObserver &obs,
Clock &c) {
bts_assert(!me);
me.reset(new PImpl{
init_rec.party_reg,
sender,
c,
init_rec.to_cme,
cfg.sender_id_5392,
cfg.location_9357,
{},
obs,
cfg});
me->obs.on_realtime_ready();
}
OrderHandle IL3RealTimeHandler::queue(NewOrder const &o) {
auto now = me->clock.now().value();
auto m = me->sender.enqueue<::il3::NewOrderSingle514>();
@@ -232,8 +254,6 @@ void IL3RealTimeHandler::queue(OrderHandle h, Cancel const &) {
w = {};
}
void IL3RealTimeHandler::send() { me->sender.flush(); }
void IL3RealTimeHandler::operator()(::il3::Sequence506) {
auto msg = me->sender.enqueue<::il3::Sequence506>();
msg.uUID(me->cfg.uuid.value());
@@ -258,7 +278,7 @@ void IL3RealTimeHandler::operator()(::il3::ExecutionReportNew522 m) {
auto &o = me->working_orders[oidx];
bts_assert_d(o.ok == mdp3::OrderKey{});
o.ok = ok;
me->obs.on_order_update(
me->obs.on_realtime_order_update(
{OrderHandle{oidx}, OrderStatus::WORKING, o.avg_fill_price,
o.total_fills});
}
@@ -273,13 +293,12 @@ void IL3RealTimeHandler::operator()(::il3::ExecutionReportReject523 m) {
me->obs.on_realtime_error(error);
}
void IL3RealTimeHandler::operator()(
::il3::ExecutionReportElimination524 m) {
void IL3RealTimeHandler::operator()(::il3::ExecutionReportElimination524 m) {
auto ok = mdp3::OrderKey{m.orderID()};
auto oidx = get_order(ok);
auto &o = me->working_orders[oidx];
o.clear();
me->obs.on_order_update(
me->obs.on_realtime_order_update(
{OrderHandle{oidx}, OrderStatus::CANCELED, o.avg_fill_price,
o.total_fills});
}
@@ -312,16 +331,14 @@ void IL3RealTimeHandler::on_trade(M m) {
o.total_fills = new_qty;
}
me->obs.on_order_update(
me->obs.on_realtime_order_update(
{OrderHandle{oidx}, new_status, new_avg_fill_price, new_qty});
}
void IL3RealTimeHandler::operator()(
::il3::ExecutionReportTradeOutright525 m) {
void IL3RealTimeHandler::operator()(::il3::ExecutionReportTradeOutright525 m) {
on_trade(m);
}
void IL3RealTimeHandler::operator()(
::il3::ExecutionReportTradeSpread526 m) {
void IL3RealTimeHandler::operator()(::il3::ExecutionReportTradeSpread526 m) {
on_trade(m);
}
@@ -337,13 +354,12 @@ void IL3RealTimeHandler::operator()(::il3::ExecutionReportCancel534 m) {
auto avg_p = o.avg_fill_price;
auto total_fills = o.total_fills;
o.clear();
me->obs.on_order_update(
me->obs.on_realtime_order_update(
{OrderHandle{oidx}, OrderStatus::CANCELED, avg_p, total_fills});
}
// Ignore these
void IL3RealTimeHandler::operator()(
::il3::ExecutionReportTradeSpreadLeg527) {}
void IL3RealTimeHandler::operator()(::il3::ExecutionReportTradeSpreadLeg527) {}
void IL3RealTimeHandler::operator()(::il3::Terminate507 m) {
auto error =
@@ -356,8 +372,7 @@ void IL3RealTimeHandler::operator()(::il3::OrderCancelReject535 m) {
me->obs.on_realtime_error(error);
}
void IL3RealTimeHandler::operator()(
::il3::OrderCancelReplaceReject536 m) {
void IL3RealTimeHandler::operator()(::il3::OrderCancelReplaceReject536 m) {
auto error = GatewayError::il3_cancel_replace_reject(
m.getTextAsString(), m.cxlRejReason());
me->obs.on_realtime_error(error);
@@ -6,7 +6,7 @@
namespace bts::spartan {
class Clock;
class IRealTimeExecutionObserver;
class IIL3RealTimeObserver;
class GatewayError;
class NewOrder;
class CancelReplace;
@@ -16,8 +16,8 @@ class Cancel;
namespace bts::spartan::il3 {
struct IL3GatewayCfg;
struct SuccessfullInitRec;
class IL3MsgSender;
struct IL3InitRec;
// Handles the happy path of an ilink3 connection: sending, canceling
// and updating orders and processing execution reports
@@ -28,15 +28,14 @@ class IL3RealTimeHandler {
void start(
IL3GatewayCfg const &cfg,
SuccessfullInitRec const &,
IL3InitRec const &,
IL3MsgSender &sender,
IRealTimeExecutionObserver&,
IIL3RealTimeObserver&,
Clock &c);
OrderHandle queue(NewOrder const &);
void queue(OrderHandle, CancelReplace const &);
void queue(OrderHandle, Cancel const &);
void send();
void operator()(::il3::Sequence506);
void operator()(::il3::BusinessReject521);