SPARK-513, Execution report processing is now idempotent.

This commit is contained in:
Pedro Pinto committed 2018-03-01 16:29:08 -05:00
1 parent 4db5399b13
commit 11e0c87ee4
5 files changed
+67 -55

No files matched your search

+1
View File
@@ -121,6 +121,7 @@ message Fill {
message NativeOrderState {
string exchange_order_id = 1;
uint64 last_msg_seq_num = 4;
enum SubError {
SUB_ERROR_NONE = 0;
SUB_ERROR_UNKNOWN_SECURITY = 1;
@@ -42,6 +42,7 @@ typedef FIXMsg<MessageTypes::ExecutionReport,
SecurityDesc,
Text,
TargetSubID,
MsgSeqNum,
TransactTimeStr>
ExecutionReportMsg;
@@ -132,6 +132,7 @@ void ExecutionGateway::on_exe_report(
// allocate from our pool
auto data = me->exe_report_pool.construct();
data->msg_seq_num = msg.get<fix::MsgSeqNum>();
// copy data from fix msg to our structure
if (msg.has<fix::Account>())
str_to(msg.get<fix::Account>(), &data->account_name);
@@ -13,6 +13,7 @@ public:
AccountName account_name;
OperatorID operator_id;
OrderKey order_key;
size_t msg_seq_num;
api::Security const*security;
api::Side side;
api::orderman::TimeInForce time_in_force;
@@ -35,17 +35,17 @@ struct NativeOrder::PImpl {
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> update_price_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> update_quantity_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
update_transact_time_bm;
update_transact_time_bm;
jetfix::FIXFragment create_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> create_price_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> create_quantity_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
create_transact_time_bm;
create_transact_time_bm;
jetfix::FIXFragment cancel_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
cancel_transact_time_bm;
cancel_transact_time_bm;
};
NativeOrder::NativeOrder() : me(new PImpl()) {}
@@ -106,12 +106,12 @@ void NativeOrder::on_init_recovered(OrderRecord const &rec,
case ORDER_STATUS_DISCONNECTED:
if (!me->sec) {
LOG(OrderMan, Error, "Unknown security {} referenced by recovered "
"order {}. This is a serious "
"problem which has to be corrected manually. ",
"order {}. This is a serious "
"problem which has to be corrected manually. ",
def.security_key(), rec.key());
transition_to_error(
NativeOrderState::SUB_ERROR_UNKNOWN_SECURITY,
fmt::format("Unknown security: {}", def.security_key()));
NativeOrderState::SUB_ERROR_UNKNOWN_SECURITY,
fmt::format("Unknown security: {}", def.security_key()));
} else {
init_fix();
if (me->state.status() != ORDER_STATUS_PENDING_WORKING)
@@ -149,7 +149,7 @@ void NativeOrder::on_init_recovered(OrderRecord const &rec,
}
void NativeOrder::on_init_from_exe_report(
ExecutionReport const &er, const PreActivationOrderServices &os) {
ExecutionReport const &er, const PreActivationOrderServices &os) {
auto k = er.order_key;
OrderRecord orecord;
@@ -207,8 +207,8 @@ void NativeOrder::on_init_from_exe_report(
auto &native_state = get_native_state(state);
auto ts = to_trim_str(er.exchange_order_id.value());
native_state.set_exchange_order_id(std::string(ts.first,ts.second));
native_state.set_exchange_order_id(std::string(ts.first, ts.second));
native_state.set_last_msg_seq_num(er.msg_seq_num);
on_init_recovered(orecord, os);
}
@@ -227,11 +227,11 @@ void NativeOrder::on_activate(Price p, Quantity q,
// subscribe to usefull events
services.exe_reports.sub(
key(), [this](auto const &r) { this->on_exe_report(r); });
key(), [this](auto const &r) { this->on_exe_report(r); });
services.cancel_rejects.sub(
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
services.gateway_status.sub(
[this](auto const &gw) { this->on_gateway_status(gw); });
[this](auto const &gw) { this->on_gateway_status(gw); });
// send out order
auto &encoding = me->create_encoding;
@@ -240,9 +240,9 @@ void NativeOrder::on_activate(Price p, Quantity q,
case CustomOrderDefinition::kLimit:
case CustomOrderDefinition::kStopLimit:
me->create_price_bm = encoding.overwrite(
me->create_price_bm,
securities::encode_exchange_price(*me->sec, p).value(),
price_sz());
me->create_price_bm,
securities::encode_exchange_price(*me->sec, p).value(),
price_sz());
break;
case CustomOrderDefinition::kStop:
case CustomOrderDefinition::kMarket:
@@ -259,7 +259,7 @@ void NativeOrder::on_activate(Price p, Quantity q,
q.value(), quantity_sz());
auto const &ts = services.get_time_str();
me->create_transact_time_bm = encoding.overwrite(
me->create_transact_time_bm, &ts.value()[0], ts.value().size());
me->create_transact_time_bm, &ts.value()[0], ts.value().size());
auto buffer = services.get_fix_buffer();
auto &stream = buffer->fix[0];
auto &f = stream.new_msg(jetfix::MessageTypes::NewOrderSingle);
@@ -272,20 +272,20 @@ void NativeOrder::on_activate(Price p, Quantity q,
key().value());
} else {
LOG(OrderMan, Warn, "Attempt to send order to a disconnected gateway. "
"Dropping order {}",
"Dropping order {}",
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK, "Exchange gateway is down");
}
break;
// all cases below are for orders recovered from disk
// all cases below are for orders recovered from disk
case ORDER_STATUS_WORKING:
services.exe_reports.sub(
key(), [this](auto const &r) { this->on_exe_report(r); });
key(), [this](auto const &r) { this->on_exe_report(r); });
services.cancel_rejects.sub(
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
services.gateway_status.sub(
[this](auto const &gw) { this->on_gateway_status(gw); });
[this](auto const &gw) { this->on_gateway_status(gw); });
if (!services.is_gateway_connected())
transition(ORDER_STATUS_DISCONNECTED);
@@ -293,11 +293,11 @@ void NativeOrder::on_activate(Price p, Quantity q,
case ORDER_STATUS_DISCONNECTED:
services.exe_reports.sub(
key(), [this](auto const &r) { this->on_exe_report(r); });
key(), [this](auto const &r) { this->on_exe_report(r); });
services.cancel_rejects.sub(
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
services.gateway_status.sub(
[this](auto const &gw) { this->on_gateway_status(gw); });
[this](auto const &gw) { this->on_gateway_status(gw); });
if (services.is_gateway_connected())
transition(ORDER_STATUS_WORKING);
@@ -305,11 +305,11 @@ void NativeOrder::on_activate(Price p, Quantity q,
case ORDER_STATUS_PENDING_WORKING:
services.exe_reports.sub(
key(), [this](auto const &r) { this->on_exe_report(r); });
key(), [this](auto const &r) { this->on_exe_report(r); });
services.cancel_rejects.sub(
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
key(), [this](auto const &cr) { this->on_cancel_reject(cr); });
services.gateway_status.sub(
[this](auto const &gw) { this->on_gateway_status(gw); });
[this](auto const &gw) { this->on_gateway_status(gw); });
break;
case ORDER_STATUS_REJECTED_BY_SPARK:
@@ -332,14 +332,14 @@ void NativeOrder::on_update(Price p, Quantity q) {
case ORDER_STATUS_WORKING: {
auto &encoding = me->update_encoding;
me->update_price_bm = encoding.overwrite(
me->update_price_bm,
securities::encode_exchange_price(*me->sec, p).value(), price_sz());
me->update_price_bm,
securities::encode_exchange_price(*me->sec, p).value(), price_sz());
me->update_quantity_bm =
encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz());
encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz());
auto const &ts = me->services->get_time_str();
me->update_transact_time_bm = encoding.overwrite(
me->update_transact_time_bm, &ts.value()[0], ts.value().size());
me->update_transact_time_bm, &ts.value()[0], ts.value().size());
auto buffer = me->services->get_fix_buffer();
auto &stream = buffer->fix[0];
auto &f = stream.new_msg(jetfix::MessageTypes::CancelReplace);
@@ -376,7 +376,7 @@ void NativeOrder::on_cancel() {
auto &encoding = me->cancel_encoding;
auto const &ts = me->services->get_time_str();
me->cancel_transact_time_bm = encoding.overwrite(
me->cancel_transact_time_bm, &ts.value()[0], ts.value().size());
me->cancel_transact_time_bm, &ts.value()[0], ts.value().size());
auto buffer = me->services->get_fix_buffer();
auto &stream = buffer->fix[0];
auto &f = stream.new_msg(jetfix::MessageTypes::Cancel);
@@ -406,14 +406,23 @@ void NativeOrder::on_cancel() {
void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
auto &state = me->state;
auto &native_state = get_native_state(state);
if (native_state.last_msg_seq_num() > er.data->msg_seq_num) {
LOG(OrderMan, Warn,
"Ignoring already seen execution report {} in order {}",
er.data->msg_seq_num, key().value());
return;
}
native_state.set_last_msg_seq_num(er.data->msg_seq_num);
state.set_price(er.data->price.value());
state.set_quantity(er.data->quantity.value());
auto external_status = map_external_status(er.data->order_status);
// filled quantity is reset back to 0 when the order is canceled
if (external_status != ORDER_STATUS_CANCELED) {
// filled quantity is reset back to 0 when the order is canceled
if (external_status != ORDER_STATUS_CANCELED) {
if (er.data->exec_type == api::orderman::EXEC_TYPE_PARTIAL_FILL ||
er.data->exec_type == api::orderman::EXEC_TYPE_FILL) {
@@ -422,34 +431,34 @@ void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
auto previous_sz = state.filled_quantity();
auto fill_price = er.data->last_price.value();
auto fill_sz = er.data->last_shares.value();
auto new_avg =
((previous_avg * previous_sz) + (fill_price * fill_sz)) / (previous_sz + fill_sz);
((previous_avg * previous_sz) + (fill_price * fill_sz)) /
(previous_sz + fill_sz);
state.set_avg_price(new_avg);
}
state.set_filled_quantity(er.data->filled_quantity.value());
}
}
state.set_last_price(er.data->last_price.value());
state.set_last_msg(er.data->msg);
auto &native_state = get_native_state(state);
native_state.set_native_status(er.data->order_status);
if (native_state.exchange_order_id().empty()) {
auto ts = to_trim_str(er.data->exchange_order_id.value());
native_state.set_exchange_order_id(std::string(ts.first,ts.second));
native_state.set_exchange_order_id(std::string(ts.first, ts.second));
add_exchange_order_id_to_fix();
}
switch (me->state.status()) {
// terminal states, nothing can change them
// terminal states, nothing can change them
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
break;
// receiving an execution report does does not mean
// we are connected
// receiving an execution report does does not mean
// we are connected
case ORDER_STATUS_DISCONNECTED:
break;
@@ -567,9 +576,9 @@ void NativeOrder::init_fix() {
create_encoding.add<fix::SecurityDesc>(sec.glbx().security_desc());
create_encoding.add<fix::ClOrdID>(clordid);
me->create_transact_time_bm = create_encoding.add<fix::TransactTimeStr>(
datetime::FIX_timestamp(datetime::now()));
datetime::FIX_timestamp(datetime::now()));
me->create_quantity_bm =
create_encoding.add<fix::OrderQty>(0, quantity_sz());
create_encoding.add<fix::OrderQty>(0, quantity_sz());
// initialize update FIX
auto &update_encoding = me->update_encoding;
@@ -586,9 +595,9 @@ void NativeOrder::init_fix() {
update_encoding.add<fix::CorrelationClOrdID>(clordid);
update_encoding.add<fix::InFlightMitigation>(true);
me->update_transact_time_bm = update_encoding.add<fix::TransactTimeStr>(
datetime::FIX_timestamp(datetime::now()));
datetime::FIX_timestamp(datetime::now()));
me->update_quantity_bm =
update_encoding.add<fix::OrderQty>(0, quantity_sz());
update_encoding.add<fix::OrderQty>(0, quantity_sz());
// initialize cancel FIX
auto &cancel_encoding = me->cancel_encoding;
@@ -602,7 +611,7 @@ void NativeOrder::init_fix() {
cancel_encoding.add<fix::OrigClOrdID>(clordid);
cancel_encoding.add<fix::CorrelationClOrdID>(clordid);
me->cancel_transact_time_bm = cancel_encoding.add<fix::TransactTimeStr>(
datetime::FIX_timestamp(datetime::now()));
datetime::FIX_timestamp(datetime::now()));
// special tags
switch (def.custom_def().custom_case()) {
@@ -633,7 +642,7 @@ void NativeOrder::init_fix() {
// stop px
auto stop_px = def.custom_def().stop().stop_price();
auto encoded_stop_px =
securities::encode_exchange_price(*me->sec, Price(stop_px)).value();
securities::encode_exchange_price(*me->sec, Price(stop_px)).value();
create_encoding.add<fix::StopPx>(encoded_stop_px);
update_encoding.add<fix::StopPx>(encoded_stop_px);
break;
@@ -648,7 +657,7 @@ void NativeOrder::init_fix() {
// stop px
auto stop_px = def.custom_def().stop_limit().stop_price();
auto encoded_stop_px =
securities::encode_exchange_price(*me->sec, Price(stop_px)).value();
securities::encode_exchange_price(*me->sec, Price(stop_px)).value();
create_encoding.add<fix::StopPx>(encoded_stop_px);
update_encoding.add<fix::StopPx>(encoded_stop_px);
@@ -734,10 +743,10 @@ void NativeOrder::transition(api::orderman::OrderStatus new_status) {
}
void NativeOrder::transition_to_error(
api::orderman::NativeOrderState::SubError sub_error,
std::string const &msg) {
api::orderman::NativeOrderState::SubError sub_error,
std::string const &msg) {
me->state.mutable_limit()->mutable_native_state()->set_sub_error_code(
sub_error);
sub_error);
transition(ORDER_STATUS_ERROR, msg);
}
@@ -765,4 +774,3 @@ NativeOrder::get_native_state(api::orderman::OrderState &st) {
}
}
}