Brought back the ilink3/ilink2 encoding abstraction.
This commit is contained in:
1 parent
b2471d45b2
commit
45aa75e8bb
10 files changed
+202
-379
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/orderman_cfg.pb.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/jetfix/codec/FIXFragment.h"
|
||||
@@ -12,10 +13,12 @@
|
||||
#include "lib/orderman/orderman_utils/ExecutionReport.h"
|
||||
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
||||
#include "lib/orderman/orders/PreActivationOrderServices.h"
|
||||
#include "lib/orderman/orders/glbx/IGLBXOrderEncoder.h"
|
||||
#include "lib/orderman/orders/glbx/iLink2OrderEncoder.h"
|
||||
#include "lib/orderman/orders/glbx/iLink3OrderEncoder.h"
|
||||
#include "lib/orderman/securities/ISecurityDB.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/config/protos/orderman_cfg.pb.h"
|
||||
|
||||
#include "lib/orderman/actors/order_manager/OrderServices.h"
|
||||
|
||||
@@ -49,6 +52,8 @@ struct NativeOrder::PImpl {
|
||||
SessionServices *session_services;
|
||||
|
||||
const api::Security *sec;
|
||||
std::unique_ptr<IGLBXOrderEncoder> encoder;
|
||||
|
||||
Quantity previous_working_position;
|
||||
|
||||
OrderDefinition definition;
|
||||
@@ -56,42 +61,15 @@ struct NativeOrder::PImpl {
|
||||
int price_encoding_precision;
|
||||
std::string exchange_order_id;
|
||||
|
||||
jetfix::FIXFragment update_encoding;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> update_price_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> update_quantity_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::MaxShow> update_max_show_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::StopPx> update_stop_px_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
|
||||
update_transact_time_bm;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::Text> update_corid_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;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::Text> create_corid_bm;
|
||||
|
||||
jetfix::FIXFragment cancel_encoding;
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
|
||||
cancel_transact_time_bm;
|
||||
|
||||
OrderServices::ExeReportSubID exe_report_sid;
|
||||
OrderServices::CancelRejectSubID cancel_reject_sid;
|
||||
|
||||
bool buffered_cancel = false;
|
||||
std::optional<api::orderman::Update> buffered_update;
|
||||
RoutingRecord routing_record;
|
||||
bool stop_order_still_has_stop_px = true;
|
||||
};
|
||||
|
||||
jetfix::FIXFragment::Bookmark<jetfix::fix::Text>
|
||||
update_corid(jetfix::FIXFragment::Bookmark<jetfix::fix::Text> const &bm,
|
||||
jetfix::FIXFragment &encoding, trace::CorID const &cid) {
|
||||
std::array<char, corid_sz()+1> buffer;
|
||||
encode_corid(cid.channel, cid.sn, buffer);
|
||||
return encoding.overwrite(bm, &buffer[0], corid_sz());
|
||||
}
|
||||
|
||||
NativeOrder::NativeOrder() : me(new PImpl()) {}
|
||||
|
||||
OrderDefinition const &NativeOrder::definition() const {
|
||||
@@ -163,7 +141,7 @@ void NativeOrder::on_init_new(OrderDefinition const &def,
|
||||
me->routing_record = rq.routing_record;
|
||||
*native_state.mutable_routing_record() = rq.routing_record;
|
||||
|
||||
init_fix();
|
||||
init_encodings();
|
||||
}
|
||||
|
||||
void NativeOrder::on_init_recovered(OrderRecord const &rec,
|
||||
@@ -199,22 +177,25 @@ void NativeOrder::on_init_recovered(OrderRecord const &rec,
|
||||
}
|
||||
|
||||
auto rq = serv.order_router.get_routing_record(
|
||||
UserID(def.user_id()), AccountID(def.account_id()), *me->sec);
|
||||
UserID(def.user_id()), AccountID(def.account_id()), *me->sec);
|
||||
if (!rq.has_route) {
|
||||
auto msg = fmt::format("Could not route order: {}", rq.error_msg);
|
||||
LOG(OrderMan, Error, "Unable to route order {} recovered from disk. Message: {}",
|
||||
LOG(OrderMan, Error,
|
||||
"Unable to route order {} recovered from disk. Message: {}",
|
||||
rec.key(), msg);
|
||||
transition_to_error(NativeOrderState::SUB_ERROR_UNKNOWN_FIX_SESSION,
|
||||
"Could not route order");
|
||||
return;
|
||||
}
|
||||
// if we made it this far we are good.
|
||||
me->routing_record = rq.routing_record;
|
||||
me->routing_record = rq.routing_record;
|
||||
*native_state.mutable_routing_record() = rq.routing_record;
|
||||
|
||||
if (me->state.status() != ORDER_STATUS_PENDING_WORKING)
|
||||
|
||||
init_encodings();
|
||||
if (me->state.status() != ORDER_STATUS_PENDING_WORKING) {
|
||||
me->exchange_order_id = native_state.exchange_order_id();
|
||||
init_fix();
|
||||
me->encoder->update_exchange_order_id(me->exchange_order_id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -318,12 +299,12 @@ NativeOrder::on_init_from_exe_report(ExecutionReport const &er,
|
||||
}
|
||||
|
||||
auto &native_state = get_native_state(state);
|
||||
native_state.set_exchange_order_id(to_trim_str(
|
||||
std::get<ExchangeOrderID>(er.exchange_order_id)));
|
||||
native_state.set_exchange_order_id(
|
||||
to_trim_str(std::get<ExchangeOrderID>(er.exchange_order_id)));
|
||||
native_state.set_last_msg_seq_num(er.msg_seq_num);
|
||||
native_state.set_native_type(er.order_type);
|
||||
native_state.set_stop_px(er.stop_price.value());
|
||||
|
||||
|
||||
auto &rr = *native_state.mutable_routing_record();
|
||||
rr.set_session(er.cfg->name());
|
||||
rr.set_account(to_trim_str(er.account_name));
|
||||
@@ -366,7 +347,7 @@ void NativeOrder::warm_up_activate(OrderServicesData &data,
|
||||
OrderOpResult
|
||||
NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
|
||||
Price p, Quantity q,
|
||||
bts::orderman::OrderServices &services) {
|
||||
bts::orderman::OrderServices &order_services) {
|
||||
me->state.set_price(p.value());
|
||||
me->state.set_quantity(q.value());
|
||||
me->state.set_filled_quantity(0);
|
||||
@@ -445,34 +426,8 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
|
||||
*me->session_services,
|
||||
[this](auto const &gw) { this->on_gateway_status(gw); });
|
||||
|
||||
// send out order
|
||||
auto &encoding = me->create_encoding;
|
||||
|
||||
switch (me->definition.custom_def().custom_case()) {
|
||||
case CustomOrderDefinition::kLimit:
|
||||
case CustomOrderDefinition::kStopLimit:
|
||||
case CustomOrderDefinition::kGlbxIceberg:
|
||||
me->create_price_bm = encoding.overwrite(
|
||||
me->create_price_bm,
|
||||
securities::encode_exchange_price(*me->sec, p).value(),
|
||||
me->price_encoding_precision, price_sz());
|
||||
break;
|
||||
case CustomOrderDefinition::kStop:
|
||||
case CustomOrderDefinition::kMarket:
|
||||
break;
|
||||
default:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
me->create_quantity_bm =
|
||||
encoding.overwrite(me->create_quantity_bm, 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_corid_bm =
|
||||
update_corid(me->create_corid_bm,encoding, services.corid());
|
||||
me->encoder->update_create_order_encoding(p, me->price_encoding_precision, q,
|
||||
order_services);
|
||||
|
||||
return OrderOpResult{};
|
||||
}
|
||||
@@ -485,16 +440,14 @@ OrderOpResult NativeOrder::on_activate(Price p, Quantity q,
|
||||
switch (me->state.status()) {
|
||||
|
||||
case ORDER_STATUS_INACTIVE: {
|
||||
// this is a brand new order
|
||||
|
||||
auto scoped_log =
|
||||
services.log_scope("NativeOrder::on_activate", key().value());
|
||||
// this is a brand new order
|
||||
|
||||
result = _on_activate(Warn, Info, p, q, services);
|
||||
if (result.ok()) {
|
||||
me->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::NewOrderSingle,
|
||||
me->create_encoding);
|
||||
|
||||
me->encoder->send_create(*me->services, *me->session_services);
|
||||
me->services->governor(*me->session_services).record_sent();
|
||||
me->state.set_status(ORDER_STATUS_PENDING_WORKING);
|
||||
|
||||
@@ -636,8 +589,6 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto &encoding = me->update_encoding;
|
||||
|
||||
if (update.has_custom()) {
|
||||
bts_assert(update.custom().custom_case() ==
|
||||
me->definition.custom_def().custom_case());
|
||||
@@ -651,10 +602,8 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
|
||||
->mutable_stop()
|
||||
->set_stop_price(stop_px.value());
|
||||
me->services->report_definition(me->definition);
|
||||
me->update_stop_px_bm = encoding.overwrite(
|
||||
me->update_stop_px_bm,
|
||||
securities::encode_exchange_price(*me->sec, stop_px).value(),
|
||||
me->price_encoding_precision, price_sz());
|
||||
me->encoder->update_cancel_replace_stop_px(
|
||||
stop_px, me->price_encoding_precision);
|
||||
}
|
||||
} else if (update.custom().custom_case() ==
|
||||
CustomOrderDefinition::CustomOrderDefinition::kStopLimit) {
|
||||
@@ -665,10 +614,9 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
|
||||
->mutable_stop_limit()
|
||||
->set_stop_price(stop_px.value());
|
||||
me->services->report_definition(me->definition);
|
||||
me->update_stop_px_bm = encoding.overwrite(
|
||||
me->update_stop_px_bm,
|
||||
securities::encode_exchange_price(*me->sec, stop_px).value(),
|
||||
me->price_encoding_precision, price_sz());
|
||||
|
||||
me->encoder->update_cancel_replace_stop_px(
|
||||
stop_px, me->price_encoding_precision);
|
||||
}
|
||||
} else if (update.custom().custom_case() ==
|
||||
CustomOrderDefinition::kGlbxIceberg) {
|
||||
@@ -683,32 +631,20 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
|
||||
ignore_or_cancel(msg, ORDER_ERROR_INVALID_PARAMETER, &result);
|
||||
break;
|
||||
}
|
||||
me->update_max_show_bm = encoding.overwrite(
|
||||
me->update_max_show_bm, ib.max_show_quantity(), quantity_sz());
|
||||
me->encoder->update_cancel_replace_max_show(
|
||||
Quantity(ib.max_show_quantity()));
|
||||
}
|
||||
}
|
||||
|
||||
// All good
|
||||
if (is_stop_limit_order() || !is_stop_order() || stop_order_has_been_triggered()) {
|
||||
if (is_stop_limit_order() || !is_stop_order() ||
|
||||
stop_order_has_been_triggered()) {
|
||||
// Stop orders become limit orders when triggered
|
||||
me->update_price_bm = encoding.overwrite(
|
||||
me->update_price_bm,
|
||||
securities::encode_exchange_price(*me->sec, p).value(),
|
||||
me->price_encoding_precision, price_sz());
|
||||
me->encoder->update_cancel_replace_encoding(
|
||||
p, me->price_encoding_precision, *me->services);
|
||||
}
|
||||
me->update_quantity_bm =
|
||||
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_corid_bm =
|
||||
update_corid(me->update_corid_bm,encoding, me->services->corid());
|
||||
|
||||
me->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::CancelReplace,
|
||||
me->update_encoding);
|
||||
me->encoder->update_cancel_replace_encoding(q, *me->services);
|
||||
me->encoder->send_cancel_replace(*me->services, *me->session_services);
|
||||
|
||||
me->services->governor(*me->session_services).record_sent();
|
||||
update_working_position();
|
||||
@@ -769,7 +705,9 @@ void NativeOrder::on_attached(
|
||||
|
||||
auto &native_state = get_native_state(me->state);
|
||||
*native_state.mutable_routing_record() = rq.routing_record;
|
||||
init_fix();
|
||||
init_encodings();
|
||||
if (has_exchange_order_id())
|
||||
me->encoder->update_exchange_order_id(me->exchange_order_id);
|
||||
|
||||
// Update user, account and manual order indicator based on new parent
|
||||
me->definition.set_manual_order_indicator(
|
||||
@@ -805,15 +743,8 @@ OrderOpResult NativeOrder::on_cancel() {
|
||||
switch (me->state.status()) {
|
||||
|
||||
case ORDER_STATUS_WORKING: {
|
||||
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->services->queue_fix(*me->session_services,
|
||||
jetfix::MessageTypes::Cancel,
|
||||
me->cancel_encoding);
|
||||
|
||||
me->encoder->update_cancel_encoding(*me->services);
|
||||
me->encoder->send_cancel(*me->services, *me->session_services);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -862,16 +793,14 @@ bool NativeOrder::is_old_exe_report(
|
||||
sizeof(mercury::TimeStampStr))
|
||||
return false;
|
||||
|
||||
auto& ts = std::get<mercury::TimeStampStr>(er.data->transact_time);
|
||||
auto &ts = std::get<mercury::TimeStampStr>(er.data->transact_time);
|
||||
if (std::lexicographical_compare(native_state.last_transact_time().begin(),
|
||||
native_state.last_transact_time().end(),
|
||||
ts.value().begin(),
|
||||
ts.value().end()))
|
||||
ts.value().begin(), ts.value().end()))
|
||||
return false;
|
||||
|
||||
if (std::equal(native_state.last_transact_time().begin(),
|
||||
native_state.last_transact_time().end(),
|
||||
ts.value().begin(),
|
||||
native_state.last_transact_time().end(), ts.value().begin(),
|
||||
ts.value().end()))
|
||||
return native_state.last_msg_seq_num() >= er.data->msg_seq_num;
|
||||
|
||||
@@ -903,8 +832,8 @@ void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
|
||||
}
|
||||
|
||||
native_state.set_last_msg_seq_num(er.data->msg_seq_num);
|
||||
native_state.set_last_transact_time(to_str(
|
||||
std::get<mercury::TimeStampStr>(er.data->transact_time)));
|
||||
native_state.set_last_transact_time(
|
||||
to_str(std::get<mercury::TimeStampStr>(er.data->transact_time)));
|
||||
|
||||
auto external_status = map_external_status(er.data->order_status);
|
||||
|
||||
@@ -934,21 +863,23 @@ void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) {
|
||||
native_state.set_native_status(er.data->order_status);
|
||||
native_state.set_native_type(er.data->order_type);
|
||||
native_state.set_stop_px(er.data->stop_price.value());
|
||||
bool got_first_exchange_order_id = !has_exchange_order_id();
|
||||
|
||||
if (got_first_exchange_order_id) {
|
||||
me->exchange_order_id = to_trim_str(
|
||||
std::get<ExchangeOrderID>(er.data->exchange_order_id));
|
||||
native_state.set_exchange_order_id(me->exchange_order_id);
|
||||
add_exchange_order_id_to_fix();
|
||||
}
|
||||
|
||||
// The FIX message we send for updates has to change once a stop
|
||||
// order is triggered. Post triggering we no longer can update the
|
||||
// stop px. Instead the stop order turns into a regular limit
|
||||
// order, which means we can now update the actual price.
|
||||
if (is_stop_order() && stop_order_has_been_triggered() && fix_has_stop_px())
|
||||
init_fix();
|
||||
if (is_stop_order() && stop_order_has_been_triggered() && me->stop_order_still_has_stop_px) {
|
||||
me->stop_order_still_has_stop_px = false;
|
||||
init_encodings();
|
||||
}
|
||||
|
||||
bool got_first_exchange_order_id = !has_exchange_order_id();
|
||||
if (got_first_exchange_order_id) {
|
||||
me->exchange_order_id =
|
||||
to_trim_str(std::get<ExchangeOrderID>(er.data->exchange_order_id));
|
||||
native_state.set_exchange_order_id(me->exchange_order_id);
|
||||
me->encoder->update_exchange_order_id(me->exchange_order_id);
|
||||
}
|
||||
|
||||
if (!terminal_state(me->state.status())) {
|
||||
state.set_price(er.data->price.value());
|
||||
@@ -1130,248 +1061,33 @@ void NativeOrder::report_state() {
|
||||
me->services->report_state(me->state);
|
||||
}
|
||||
|
||||
std::string NativeOrder::get_cust_order_handling_inst(api::orderman::RoutingRecord const&rr) {
|
||||
std::string NativeOrder::get_cust_order_handling_inst(
|
||||
api::orderman::RoutingRecord const &rr) {
|
||||
if (rr.cust_order_handling_inst().size())
|
||||
return rr.cust_order_handling_inst();
|
||||
else
|
||||
return config::get<config::OrdermanCfg>().fallback_cust_order_handling_inst();
|
||||
return config::get<config::OrdermanCfg>()
|
||||
.fallback_cust_order_handling_inst();
|
||||
}
|
||||
|
||||
void NativeOrder::init_fix() {
|
||||
bts_assert(me->sec);
|
||||
|
||||
using namespace jetfix;
|
||||
me->create_encoding = FIXFragment{};
|
||||
me->update_encoding = FIXFragment{};
|
||||
me->cancel_encoding = FIXFragment{};
|
||||
|
||||
auto const &def = me->definition;
|
||||
auto const &sec = *me->sec;
|
||||
auto const &rr = me->routing_record;
|
||||
|
||||
std::string clordid(clordid_sz(), ' ');
|
||||
encode_clorid(key(), SecurityKey(sec.id()), &clordid);
|
||||
|
||||
// initialize create FIX
|
||||
auto &create_encoding = me->create_encoding;
|
||||
create_encoding.add<fix::SenderSubID>(rr.operator_id());
|
||||
create_encoding.add<fix::SenderLocationID>(rr.location());
|
||||
create_encoding.add<fix::Account>(rr.account());
|
||||
create_encoding.add<fix::Side>(def.side());
|
||||
create_encoding.add<fix::TimeInForce>(def.time_in_force());
|
||||
create_encoding.add<fix::ManualOrderIndicator>(def.manual_order_indicator());
|
||||
create_encoding.add<fix::CustomerOrFirm>(rr.customer_or_firm());
|
||||
create_encoding.add<fix::CtiCode>(rr.cti_code());
|
||||
create_encoding.add<fix::SecurityDesc>(sec.glbx().security_desc());
|
||||
create_encoding.add<fix::ClOrdID>(clordid);
|
||||
create_encoding.add<fix::CustOrderHandlingInst> (get_cust_order_handling_inst(rr));
|
||||
|
||||
if (rr.smp_id().size()) {
|
||||
create_encoding.add<fix::SelfMatchPreventionID>(rr.smp_id());
|
||||
if (rr.smp_instruction() != SELF_MATCH_PREVENTION_INSTRUCTION_UNDEFINED)
|
||||
create_encoding.add<fix::SelfMatchPreventionInstruction>(
|
||||
rr.smp_instruction());
|
||||
}
|
||||
|
||||
me->create_transact_time_bm = create_encoding.add<fix::TransactTimeStr>(
|
||||
datetime::FIX_timestamp(datetime::now()));
|
||||
me->create_quantity_bm =
|
||||
create_encoding.add<fix::OrderQty>(0, quantity_sz());
|
||||
me->create_corid_bm =
|
||||
create_encoding.add<fix::Text>(std::string(corid_sz(), ' '));
|
||||
|
||||
// initialize update FIX
|
||||
auto &update_encoding = me->update_encoding;
|
||||
update_encoding.add<fix::SenderSubID>(rr.operator_id());
|
||||
update_encoding.add<fix::SenderLocationID>(rr.location());
|
||||
update_encoding.add<fix::Account>(rr.account());
|
||||
update_encoding.add<fix::Side>(def.side());
|
||||
update_encoding.add<fix::TimeInForce>(def.time_in_force());
|
||||
update_encoding.add<fix::ManualOrderIndicator>(def.manual_order_indicator());
|
||||
update_encoding.add<fix::CtiCode>(rr.cti_code());
|
||||
update_encoding.add<fix::CustomerOrFirm>(rr.customer_or_firm());
|
||||
update_encoding.add<fix::SecurityDesc>(sec.glbx().security_desc());
|
||||
update_encoding.add<fix::ClOrdID>(clordid);
|
||||
update_encoding.add<fix::OrigClOrdID>(clordid);
|
||||
update_encoding.add<fix::CorrelationClOrdID>(clordid);
|
||||
update_encoding.add<fix::InFlightMitigation>(true);
|
||||
update_encoding.add<fix::CustOrderHandlingInst> (get_cust_order_handling_inst(rr));
|
||||
// Self Match Protection
|
||||
if (rr.smp_id().size()) {
|
||||
update_encoding.add<fix::SelfMatchPreventionID>(rr.smp_id());
|
||||
if (rr.smp_instruction() != SELF_MATCH_PREVENTION_INSTRUCTION_UNDEFINED)
|
||||
update_encoding.add<fix::SelfMatchPreventionInstruction>(
|
||||
rr.smp_instruction());
|
||||
}
|
||||
me->update_transact_time_bm = update_encoding.add<fix::TransactTimeStr>(
|
||||
datetime::FIX_timestamp(datetime::now()));
|
||||
me->update_quantity_bm =
|
||||
update_encoding.add<fix::OrderQty>(0, quantity_sz());
|
||||
me->update_corid_bm =
|
||||
update_encoding.add<fix::Text>(std::string(corid_sz(), ' '));
|
||||
|
||||
// initialize cancel FIX
|
||||
auto &cancel_encoding = me->cancel_encoding;
|
||||
cancel_encoding.add<fix::SenderSubID>(rr.operator_id());
|
||||
cancel_encoding.add<fix::SenderLocationID>(rr.location());
|
||||
cancel_encoding.add<fix::Account>(rr.account());
|
||||
cancel_encoding.add<fix::Side>(def.side());
|
||||
cancel_encoding.add<fix::TimeInForce>(def.time_in_force());
|
||||
cancel_encoding.add<fix::ManualOrderIndicator>(def.manual_order_indicator());
|
||||
cancel_encoding.add<fix::SecurityDesc>(sec.glbx().security_desc());
|
||||
cancel_encoding.add<fix::ClOrdID>(clordid);
|
||||
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()));
|
||||
|
||||
// special tags
|
||||
switch (def.custom_def().custom_case()) {
|
||||
case CustomOrderDefinition::kMarket:
|
||||
// type
|
||||
create_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_MARKET);
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_MARKET);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_MARKET);
|
||||
break;
|
||||
|
||||
case CustomOrderDefinition::kLimit:
|
||||
// type
|
||||
create_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
|
||||
// price
|
||||
me->create_price_bm = create_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_price_bm = update_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
break;
|
||||
|
||||
case CustomOrderDefinition::kStop: {
|
||||
// type
|
||||
create_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP);
|
||||
|
||||
// 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();
|
||||
create_encoding.add<fix::StopPx>(encoded_stop_px,
|
||||
me->price_encoding_precision, -1);
|
||||
|
||||
// Stop orders go through two stages. Before the stop price is
|
||||
// triggered they are stop orders and have no price field, only
|
||||
// stop px. After being triggered they become regular limit
|
||||
// orders and have only price, not stop px fields.
|
||||
if (stop_order_has_been_triggered()) {
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
me->update_price_bm = update_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_stop_px_bm = {};
|
||||
} else {
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP);
|
||||
me->update_price_bm = {};
|
||||
me->update_stop_px_bm = update_encoding.add<fix::StopPx>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case CustomOrderDefinition::kStopLimit: {
|
||||
// type
|
||||
create_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP_LIMIT);
|
||||
|
||||
// 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();
|
||||
create_encoding.add<fix::StopPx>(encoded_stop_px,
|
||||
me->price_encoding_precision, -1);
|
||||
|
||||
// Like stop orders, stop limits go through two stages. Before
|
||||
// the stop price is triggered they are stop limit orders and
|
||||
// have both a price and a stop px price field. After being
|
||||
// triggered they become regular limit orders and have only
|
||||
// price, not stop px fields.
|
||||
if (stop_order_has_been_triggered()) {
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
me->update_price_bm = update_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_stop_px_bm = {};
|
||||
} else {
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP_LIMIT);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_STOP_LIMIT);
|
||||
me->create_price_bm = create_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_price_bm = update_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_stop_px_bm = update_encoding.add<fix::StopPx>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case CustomOrderDefinition::kGlbxIceberg: {
|
||||
|
||||
// type, still a limit order but we use a show quantity
|
||||
create_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
update_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
cancel_encoding.add<fix::OrdType>(NATIVE_ORDER_TYPE_LIMIT);
|
||||
|
||||
// price
|
||||
me->create_price_bm = create_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
me->update_price_bm = update_encoding.add<fix::Price>(
|
||||
0, me->price_encoding_precision, price_sz());
|
||||
|
||||
// max show qty
|
||||
auto max_show_qty = def.custom_def().glbx_iceberg().max_show_quantity();
|
||||
create_encoding.add<fix::MaxShow>(max_show_qty);
|
||||
me->update_max_show_bm =
|
||||
update_encoding.add<fix::MaxShow>(max_show_qty, max_show_sz());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
if (has_exchange_order_id())
|
||||
add_exchange_order_id_to_fix();
|
||||
}
|
||||
|
||||
bool NativeOrder::is_stop_order() {
|
||||
bool NativeOrder::is_stop_order() {
|
||||
auto const &def = me->definition;
|
||||
return (def.custom_def().custom_case() == CustomOrderDefinition::kStop ||
|
||||
def.custom_def().custom_case() == CustomOrderDefinition::kStopLimit);
|
||||
}
|
||||
|
||||
bool NativeOrder::is_stop_limit_order() {
|
||||
bool NativeOrder::is_stop_limit_order() {
|
||||
auto const &def = me->definition;
|
||||
return def.custom_def().custom_case() == CustomOrderDefinition::kStopLimit;
|
||||
}
|
||||
|
||||
bool NativeOrder::stop_order_has_been_triggered() {
|
||||
bool NativeOrder::stop_order_has_been_triggered() {
|
||||
bts_assert(is_stop_order());
|
||||
auto const &native_state = get_native_state(me->state);
|
||||
return has_exchange_order_id() &&
|
||||
native_state.native_type() == NATIVE_ORDER_TYPE_LIMIT;
|
||||
}
|
||||
|
||||
bool NativeOrder::fix_has_stop_px() {
|
||||
bts_assert(is_stop_order());
|
||||
return me->update_stop_px_bm.valid();
|
||||
}
|
||||
|
||||
void NativeOrder::add_exchange_order_id_to_fix() {
|
||||
bts_assert(has_exchange_order_id());
|
||||
me->update_encoding.add<jetfix::fix::OrderID>(me->exchange_order_id);
|
||||
me->cancel_encoding.add<jetfix::fix::OrderID>(me->exchange_order_id);
|
||||
}
|
||||
|
||||
OrderStatus map_external_status(NativeOrderStatus status) {
|
||||
switch (status) {
|
||||
case NATIVE_ORDER_STATUS_NEW:
|
||||
@@ -1537,5 +1253,16 @@ void NativeOrder::ignore_or_cancel(std::string const &msg,
|
||||
*out = {error, msg};
|
||||
}
|
||||
|
||||
void NativeOrder::init_encodings() {
|
||||
auto rr = get_native_state(me->state).routing_record();
|
||||
if (use_ilink3(rr.session()))
|
||||
me->encoder = std::make_unique<iLink3OrderEncoder>();
|
||||
else
|
||||
me->encoder = std::make_unique<iLink2OrderEncoder>();
|
||||
me->encoder->init(key(), rr, *me->sec, definition(),
|
||||
me->price_encoding_precision,
|
||||
stop_order_has_been_triggered());
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
Reference in new issue
Block a user