396 lines
16 KiB
C++
396 lines
16 KiB
C++
#include "iLink2OrderEncoder.h"
|
|
#include "lib/api/services/OrderService.pb.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/orderman_cfg.pb.h"
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/jetfix/codec/FIXFragment.h"
|
|
#include "lib/jetfix/codec/FieldDefinitions.h"
|
|
#include "lib/orderman/OrderServices.h"
|
|
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
using namespace api::orderman;
|
|
|
|
struct iLink2OrderEncoder::PImpl {
|
|
PImpl() {}
|
|
|
|
CustomOrderDefinition::CustomCase order_type;
|
|
api::Security const *security;
|
|
|
|
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;
|
|
};
|
|
|
|
jetfix::FIXFragment::Bookmark<jetfix::fix::Text> static 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());
|
|
}
|
|
|
|
static std::string
|
|
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();
|
|
}
|
|
|
|
iLink2OrderEncoder::iLink2OrderEncoder() : me(new PImpl()) {}
|
|
iLink2OrderEncoder::~iLink2OrderEncoder() {}
|
|
|
|
void iLink2OrderEncoder::init(
|
|
OrderKey key,
|
|
api::orderman::RoutingRecord const &rr,
|
|
api::Security const &security,
|
|
api::orderman::OrderDefinition const &def,
|
|
int price_encoding_precision,
|
|
bool stop_order_has_been_triggered) {
|
|
|
|
me->security = &security;
|
|
me->order_type = def.custom_def().custom_case();
|
|
|
|
using namespace jetfix;
|
|
me->create_encoding = FIXFragment{};
|
|
me->update_encoding = FIXFragment{};
|
|
me->cancel_encoding = FIXFragment{};
|
|
|
|
auto const &sec = *me->security;
|
|
|
|
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::CustOrderHandlingInst>(
|
|
get_cust_order_handling_inst(rr));
|
|
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());
|
|
}
|
|
|
|
update_encoding.add<fix::OrigClOrdID>(clordid);
|
|
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()));
|
|
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, price_encoding_precision, price_sz());
|
|
me->update_price_bm = update_encoding.add<fix::Price>(
|
|
0, 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->security, Price(stop_px))
|
|
.value();
|
|
|
|
// 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, 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, price_encoding_precision, price_sz());
|
|
}
|
|
create_encoding.add<fix::StopPx>(
|
|
encoded_stop_px, price_encoding_precision, -1);
|
|
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->security, Price(stop_px))
|
|
.value();
|
|
|
|
// 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, 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, price_encoding_precision, price_sz());
|
|
me->update_price_bm = update_encoding.add<fix::Price>(
|
|
0, price_encoding_precision, price_sz());
|
|
me->update_stop_px_bm = update_encoding.add<fix::StopPx>(
|
|
0, price_encoding_precision, price_sz());
|
|
}
|
|
|
|
create_encoding.add<fix::StopPx>(
|
|
encoded_stop_px, price_encoding_precision, -1);
|
|
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, price_encoding_precision, price_sz());
|
|
me->update_price_bm = update_encoding.add<fix::Price>(
|
|
0, 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;
|
|
}
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_exchange_order_id(ExchangeOrderIDVar const &v) {
|
|
auto oid = to_trim_str(std::get<ExchangeOrderID>(v));
|
|
me->update_encoding.add<jetfix::fix::OrderID>(oid);
|
|
me->cancel_encoding.add<jetfix::fix::OrderID>(oid);
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_create_order_encoding(
|
|
Price p, int price_precision, Quantity q, OrderServices &services) {
|
|
auto &encoding = me->create_encoding;
|
|
|
|
switch (me->order_type) {
|
|
case CustomOrderDefinition::kLimit:
|
|
case CustomOrderDefinition::kStopLimit:
|
|
case CustomOrderDefinition::kGlbxIceberg:
|
|
me->create_price_bm = encoding.overwrite(
|
|
me->create_price_bm,
|
|
securities::encode_exchange_price(*me->security, p).value(),
|
|
price_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 &ts = services.get_time_str();
|
|
auto corid = services.corid();
|
|
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, corid);
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_replace_stop_px(
|
|
Price stop_px, int price_precision) {
|
|
auto &encoding = me->update_encoding;
|
|
me->update_stop_px_bm = encoding.overwrite(
|
|
me->update_stop_px_bm,
|
|
securities::encode_exchange_price(*me->security, stop_px).value(),
|
|
price_precision, price_sz());
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_replace_max_show(Quantity max_show) {
|
|
auto &encoding = me->update_encoding;
|
|
me->update_max_show_bm = encoding.overwrite(
|
|
me->update_max_show_bm, max_show.value(), quantity_sz());
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_replace_encoding(
|
|
Price p, int price_precision, Quantity q, OrderServices &services) {
|
|
auto &encoding = me->update_encoding;
|
|
|
|
me->update_price_bm = encoding.overwrite(
|
|
me->update_price_bm,
|
|
securities::encode_exchange_price(*me->security, p).value(),
|
|
price_precision, price_sz());
|
|
me->update_quantity_bm =
|
|
encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz());
|
|
|
|
auto &ts = services.get_time_str();
|
|
auto corid = services.corid();
|
|
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, corid);
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_replace_encoding(
|
|
Quantity q, OrderServices &services) {
|
|
auto &encoding = me->update_encoding;
|
|
me->update_quantity_bm =
|
|
encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz());
|
|
auto &ts = services.get_time_str();
|
|
auto corid = services.corid();
|
|
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, corid);
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_replace_encoding(
|
|
Price p, int price_precision, OrderServices &services) {
|
|
|
|
auto &encoding = me->update_encoding;
|
|
|
|
me->update_price_bm = encoding.overwrite(
|
|
me->update_price_bm,
|
|
securities::encode_exchange_price(*me->security, p).value(),
|
|
price_precision, price_sz());
|
|
auto &ts = services.get_time_str();
|
|
auto corid = services.corid();
|
|
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, corid);
|
|
}
|
|
|
|
void iLink2OrderEncoder::update_cancel_encoding(OrderServices &services) {
|
|
auto &encoding = me->cancel_encoding;
|
|
auto &ts = services.get_time_str();
|
|
me->cancel_transact_time_bm = encoding.overwrite(
|
|
me->cancel_transact_time_bm, &ts.value()[0], ts.value().size());
|
|
}
|
|
|
|
void iLink2OrderEncoder::send_create(OrderServices &s, SessionServices &os) {
|
|
s.queue_fix(os, jetfix::MessageTypes::NewOrderSingle, me->create_encoding);
|
|
}
|
|
|
|
void iLink2OrderEncoder::send_cancel_replace(
|
|
OrderServices &s, SessionServices &os) {
|
|
s.queue_fix(os, jetfix::MessageTypes::CancelReplace, me->update_encoding);
|
|
}
|
|
|
|
void iLink2OrderEncoder::send_cancel(OrderServices &s, SessionServices &os) {
|
|
s.queue_fix(os, jetfix::MessageTypes::Cancel, me->cancel_encoding);
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|