Also moved a few logging deps from lib_spark -> system: * bts_chrono.h * bts_no_copy.h * defer.h * dispatcher.h/cpp * file_utils.h/cpp * type_name.h
709 lines
28 KiB
C++
709 lines
28 KiB
C++
#include "EMExecutionGateway.h"
|
|
#include "logging/logging.h"
|
|
#include "spark/api/service_helpers/AdminServiceUtils.h"
|
|
#include "spark/api/services/AdminService.pb.h"
|
|
#include "spark/api/services/OrderService.pb.h"
|
|
#include "spark/domain_model/Security.pb.h"
|
|
#include "spark/domain_model/SecurityKey.h"
|
|
#include "spark/domain_model/common.h"
|
|
#include "spark/md/SecurityOracle.h"
|
|
#include "spark/orderman/CancelOrderMsg.h"
|
|
#include "spark/orderman/CancelReplaceOrderMsg.h"
|
|
#include "spark/orderman/CreateOrderMsg.h"
|
|
#include "spark/orderman/OrderMsg.h"
|
|
#include "spark/orderman/RFQMsg.h"
|
|
#include "spark/orderman/events/NewAdminDataEvent.h"
|
|
#include "spark/orderman/events/NewExecutionReportEvent.h"
|
|
#include "spark/orderman/events/NewSecuritiesEvent.h"
|
|
#include "spark/orderman/orderman_utils/CancelReject.h"
|
|
#include "spark/orderman/orderman_utils/ExchangeOpenOrders.h"
|
|
#include "spark/orderman/orderman_utils/ExchangePosition.h"
|
|
#include "spark/orderman/orderman_utils/ExecutionReport.h"
|
|
#include "spark/orderman/orderman_utils/orderman_common.h"
|
|
#include "spark/os/os.h"
|
|
#include "spark/tracing/bts_trace.h"
|
|
#include "spark/utils/bts_assert.h"
|
|
#include "spark/utils/datetime.h"
|
|
//#include "spark/utils/helper_macros.h"
|
|
#include "sparkd/md/platforms/emumba/EmMDListener.h"
|
|
#include "sparkd/md/platforms/emumba/IEMOrderMuxer.h"
|
|
|
|
#include "spark/api/service_helpers/AdminServiceUtils.h"
|
|
#include "spark/config/config.h"
|
|
#include "spark/config/protos/toggles.pb.h"
|
|
#include "spark/domain_model/Security.h"
|
|
#include "spark/utils/PBUtils.h"
|
|
#include "spark/utils/Uuid.h"
|
|
#include <atomic>
|
|
#include <bits/stdint-uintn.h>
|
|
#include <boost/container/small_vector.hpp>
|
|
#include <boost/pool/object_pool.hpp>
|
|
#include <boost/proto/transform/impl.hpp>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <emumba-deribit-connector/market_data_common.h>
|
|
#include <emumba-deribit-connector/order_entry_common.h>
|
|
#include <emumba-deribit-connector/price.h>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <tbb/concurrent_queue.h>
|
|
#include <unordered_map>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
using EMOrderKey = std::int64_t;
|
|
|
|
// Order callbacks from order_mux are copied here and placed in the tbb queue
|
|
struct EMCallbackMsg {
|
|
// didnt make it const since tbb::queue needs a copy cons
|
|
emumba::deribit_connector::data_types::error ec;
|
|
emumba::order_entry::order_key order_key;
|
|
EMOrderState order_state;
|
|
};
|
|
|
|
// Position callbacks from order_mux are copied here and placed in the tbb queue
|
|
struct EMExchangePositionCallbackMsg {
|
|
std::vector<EMPosition> positions;
|
|
};
|
|
|
|
struct EMExchangeOpenOrdersCallbackMsg {
|
|
std::vector<EMOpenOrder> orders;
|
|
};
|
|
|
|
struct EMExecutionGateway::PImpl {
|
|
PImpl(md::SecurityOracle o) : sec_oracle(o) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::PImpl constructor");
|
|
}
|
|
admin::AdminServiceUtils::ExchangeSubAccountResolver subaccount_resolver =
|
|
{};
|
|
// callback cashed data
|
|
api::admin::AdminData admin_data = {};
|
|
md::SecurityOracle sec_oracle;
|
|
// interface (IExecutionGateway) callbacks
|
|
OnExeReport on_exe_report;
|
|
OnGwStatusChange on_gw_status_change;
|
|
OnCancelReject on_cancel_reject;
|
|
OnExchangePositionState on_exchange_position_state;
|
|
OnExchangeOpenOrdersState on_exchange_open_orders;
|
|
// orders are sent out and callbacks received via IEMOrderMuxer
|
|
IEMOrderMuxer *order_mux = nullptr;
|
|
size_t msg_seq_num = 0;
|
|
boost::object_pool<ExecutionReport> exe_report_pool;
|
|
// bts key to em keys
|
|
std::unordered_map<OrderKey, EMOrderKey> key_map = {};
|
|
// em's key vs order, we keep the create message because it has
|
|
// information that we need to build and fire the ExecutionReport (data
|
|
// that is not provided by the EM order callback)
|
|
std::unordered_map<EMOrderKey, CreateOrderMsg> order_map = {};
|
|
// this is updated as we get exchange positions
|
|
ExchangePosition exchange_position;
|
|
ExchangeOpenOrders exchange_open_orders;
|
|
// since we get callbacks from IEMOrderMuxer in a different thread we
|
|
// queue them here and then dequeue on the dispatch_incoming_fix_messages
|
|
// call
|
|
tbb::concurrent_queue<EMCallbackMsg> order_cb_queue =
|
|
tbb::concurrent_queue<EMCallbackMsg>();
|
|
tbb::concurrent_queue<EMExchangePositionCallbackMsg> exch_pos_queue =
|
|
tbb::concurrent_queue<EMExchangePositionCallbackMsg>();
|
|
tbb::concurrent_queue<EMExchangeOpenOrdersCallbackMsg>
|
|
exch_open_orders_queue =
|
|
tbb::concurrent_queue<EMExchangeOpenOrdersCallbackMsg>();
|
|
// periodic query (temporary)
|
|
bts::datetime::PTime last_pos_fire_time = bts::datetime::now();
|
|
uint32_t pos_fire_period_ms = 5000;
|
|
};
|
|
|
|
// helper functions
|
|
// ///////////////////////////////////////////////////////////
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
api::orderman::NativeOrderStatus native_order_status(
|
|
const EMOrderState &state) {
|
|
switch (state.status) {
|
|
case emumba::order_entry::status::working:
|
|
// TODO: check partially filled
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_NEW;
|
|
case emumba::order_entry::status::completed:
|
|
// maybe check fills
|
|
// if (state.total_fills.value() > 0)
|
|
// return
|
|
// api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_FILLED;
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_FILLED;
|
|
case emumba::order_entry::status::canceled:
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_CANCELED;
|
|
case emumba::order_entry::status::rejected:
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_REJECTED;
|
|
case emumba::order_entry::status::untriggered:
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNDEFINED;
|
|
}
|
|
bts_assert(false);
|
|
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNDEFINED;
|
|
}
|
|
|
|
bool terminal_status(emumba::order_entry::status status) {
|
|
switch (status) {
|
|
case emumba::order_entry::status::working:
|
|
return false;
|
|
case emumba::order_entry::status::completed:
|
|
case emumba::order_entry::status::canceled:
|
|
case emumba::order_entry::status::rejected:
|
|
return true;
|
|
case emumba::order_entry::status::untriggered:
|
|
return false;
|
|
}
|
|
bts_assert(false);
|
|
return true;
|
|
}
|
|
|
|
// get em's qty
|
|
double get_quantity(const emumba::market_data::quantity &q) {
|
|
const auto &v = q.value();
|
|
if (std::holds_alternative<double>(v))
|
|
return std::get<double>(v);
|
|
return static_cast<double>(std::get<uint32_t>(v));
|
|
}
|
|
|
|
// gets the exchange's (not em's) order id
|
|
std::string get_exchange_oid(const emumba::order_entry::order_state &state) {
|
|
using namespace emumba::order_entry;
|
|
const auto &exch_state = state.exchange_order_state;
|
|
bts_assert(std::holds_alternative<deribit_order_state>(exch_state));
|
|
const auto &deribit_state = std::get<deribit_order_state>(exch_state);
|
|
return deribit_state.exchange_order_id;
|
|
}
|
|
|
|
emumba::order_entry::time_in_force convert_tif(api::orderman::TimeInForce tif) {
|
|
using namespace emumba::order_entry;
|
|
using namespace bts::api::orderman;
|
|
switch (tif) {
|
|
case TimeInForce::TIME_IN_FORCE_UNDEFINED:
|
|
return time_in_force::day;
|
|
case TimeInForce::TIME_IN_FORCE_DAY:
|
|
return time_in_force::day;
|
|
case TimeInForce::TIME_IN_FORCE_GTC:
|
|
return time_in_force::gtc;
|
|
case TimeInForce::TIME_IN_FORCE_IOC:
|
|
return time_in_force::ioc;
|
|
case TimeInForce::TIME_IN_FORCE_DATE:
|
|
return time_in_force::date;
|
|
default:
|
|
bts_assert(false);
|
|
return time_in_force::date;
|
|
}
|
|
}
|
|
|
|
emumba::order_entry::order_type convert_order_type(
|
|
api::orderman::NativeOrderType t) {
|
|
using namespace emumba::order_entry;
|
|
using namespace bts::api::orderman;
|
|
switch (t) {
|
|
case NativeOrderType::NATIVE_ORDER_TYPE_LIMIT:
|
|
return order_type::limit_order;
|
|
case NativeOrderType::NATIVE_ORDER_TYPE_MARKET:
|
|
return order_type::market_order;
|
|
case NativeOrderType::NATIVE_ORDER_TYPE_MARKET_LIMIT:
|
|
return order_type::market_limit_order;
|
|
case NativeOrderType::NATIVE_ORDER_TYPE_STOP:
|
|
return order_type::stop_market_order;
|
|
case NativeOrderType::NATIVE_ORDER_TYPE_STOP_LIMIT:
|
|
return order_type::stop_limit_order;
|
|
default:
|
|
bts_assert(false);
|
|
return order_type::limit_order;
|
|
}
|
|
}
|
|
|
|
EMExecutionGateway::EMExecutionGateway(md::SecurityOracle o)
|
|
: IExecutionGateway(), me(new PImpl(o)) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::EMExecutionGateway constructor t_id: {} "
|
|
"t_name: {}",
|
|
os::get_thread_id(), os::get_application_name());
|
|
}
|
|
|
|
EMExecutionGateway::~EMExecutionGateway() {}
|
|
|
|
void EMExecutionGateway::start() {
|
|
_init();
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::start order_mux aquired: {} t_id: {} "
|
|
"t_name: {}",
|
|
me->order_mux != nullptr, os::get_thread_id(), os::get_thread_name());
|
|
}
|
|
|
|
// this is a periodic timer
|
|
void EMExecutionGateway::dispatch_incoming_fix_messages() {
|
|
EMCallbackMsg cb_msg = {};
|
|
// keep popping messages until no more
|
|
while (me->order_cb_queue.try_pop(cb_msg)) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::dispatch_incoming_fix_messages "
|
|
"dispatched cb for key: {}",
|
|
cb_msg.order_key.value());
|
|
_process_cb_and_fire_er(cb_msg);
|
|
}
|
|
EMExchangePositionCallbackMsg pos_cm_msg = {};
|
|
while (me->exch_pos_queue.try_pop(pos_cm_msg)) {
|
|
_process_pos_cb_and_fire(pos_cm_msg);
|
|
}
|
|
EMExchangeOpenOrdersCallbackMsg open_order_cb_msg = {};
|
|
while (me->exch_open_orders_queue.try_pop(open_order_cb_msg)) {
|
|
_process_open_order_cb_and_fire(open_order_cb_msg);
|
|
}
|
|
_send_position_request();
|
|
}
|
|
|
|
void EMExecutionGateway::on_securities(
|
|
const mercury::Update<NewSecuritiesEvent> &) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::on_securities t_id: {} t_name: {}",
|
|
os::get_thread_id(), os::get_thread_name());
|
|
}
|
|
|
|
void EMExecutionGateway::on_admin_data(
|
|
const mercury::Update<NewAdminDataEvent> &update) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::on_admin_data t_id: {} t_name: {}",
|
|
os::get_thread_id(), os::get_thread_name());
|
|
if (update.events) {
|
|
for (auto const &ev : *(update.events))
|
|
me->admin_data.CopyFrom(*ev.admin_data);
|
|
}
|
|
}
|
|
|
|
void EMExecutionGateway::send_fix(const jetfix::OutFIXPtr &, trace::CorID) {
|
|
bts_assert_msg(false, "EMExecutionGateway does not support fix");
|
|
}
|
|
|
|
void EMExecutionGateway::send_msgs(
|
|
std::vector<OrderMsg> const &msg_vec, trace::CorID) {
|
|
if (!_init()) {
|
|
LOG(OrderMan, Error, "EMExecutionGateway::send_msgs order_mux not set");
|
|
}
|
|
LOG(OrderMan, Info, "EMExecutionGateway::send_msgs count: {}",
|
|
msg_vec.size());
|
|
for (const auto &m : msg_vec) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::send_msgs {}", m);
|
|
if (std::holds_alternative<CreateOrderMsg>(m)) {
|
|
const CreateOrderMsg &cr_msg = std::get<CreateOrderMsg>(m);
|
|
_send_create(cr_msg);
|
|
} else if (std::holds_alternative<CancelReplaceOrderMsg>(m)) {
|
|
const CancelReplaceOrderMsg &cxl_repl_msg =
|
|
std::get<CancelReplaceOrderMsg>(m);
|
|
_send_cxl_replace(cxl_repl_msg);
|
|
} else if (std::holds_alternative<CancelOrderMsg>(m)) {
|
|
const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(m);
|
|
_send_cxl(cxl_msg);
|
|
} else if (std::holds_alternative<RFQMsg>(m)) {
|
|
const RFQMsg &rfq_msg = std::get<RFQMsg>(m);
|
|
_send_rfq(rfq_msg);
|
|
}
|
|
}
|
|
}
|
|
|
|
OnExeReport &EMExecutionGateway::on_exe_report() { return me->on_exe_report; }
|
|
OnGwStatusChange &EMExecutionGateway::on_gw_status_change() {
|
|
return me->on_gw_status_change;
|
|
}
|
|
OnCancelReject &EMExecutionGateway::on_cancel_reject() {
|
|
return me->on_cancel_reject;
|
|
}
|
|
OnExchangePositionState &EMExecutionGateway::on_exchange_position_state() {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::on_exchange_position_state subscribed");
|
|
return me->on_exchange_position_state;
|
|
}
|
|
OnExchangeOpenOrdersState &EMExecutionGateway::on_exchange_open_orders() {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::on_exchange_open_orders subscribed");
|
|
return me->on_exchange_open_orders;
|
|
}
|
|
|
|
bool EMExecutionGateway::_init() {
|
|
if (me->order_mux == nullptr) {
|
|
me->order_mux = md::EmMDListener::order_mux();
|
|
if (me->order_mux != nullptr) {
|
|
me->order_mux->order_update_cb() =
|
|
[this](const auto &er, const auto &key, const auto &state) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway received order callback for key: {} "
|
|
"state: {}",
|
|
key.value(), state);
|
|
me->order_cb_queue.push({er, key, state});
|
|
};
|
|
me->order_mux->exchange_position_update_cb() =
|
|
[this](const auto &exch_pos) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway exchange_position_update_cb() "
|
|
"positions: {}",
|
|
exch_pos.size());
|
|
// lots of copying going on, but this is periodic
|
|
EMExchangePositionCallbackMsg pos_cb = {exch_pos};
|
|
me->exch_pos_queue.push(pos_cb);
|
|
};
|
|
me->order_mux->exchange_open_orders_update_cb() =
|
|
[this](const auto &orders) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway exchange_open_orders_update_cb() "
|
|
"open_orders: {}",
|
|
orders.size());
|
|
for (const auto &oord : orders) {
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway exchange_open_orders_update_cb {}",
|
|
oord);
|
|
}
|
|
EMExchangeOpenOrdersCallbackMsg open_order_cb = {orders};
|
|
me->exch_open_orders_queue.push(open_order_cb);
|
|
};
|
|
}
|
|
}
|
|
return me->order_mux != nullptr;
|
|
}
|
|
|
|
// converts the CreateOrderMsg to emumba::order_entry::new_order and
|
|
// sends it to ExchangePosition
|
|
void EMExecutionGateway::_send_create(const CreateOrderMsg &c) {
|
|
// TODO: check if sub account exists
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::_send_create key: {} p: {} q: {} account: {} "
|
|
"operator_id: {} sub_account: {}",
|
|
c.key, c.price, c.quantity, c.routing_record->account(),
|
|
c.routing_record->operator_id(), c.routing_record->account_code());
|
|
emumba::order_entry::new_order new_order;
|
|
// only assuming deribit for now, as we add exchanges we fix it here
|
|
bts_assert_msg(
|
|
c.security->has_deribit(), "EMExecutionGateway only handles Deribit");
|
|
std::string desc = c.security->deribit().security_desc();
|
|
new_order.order_def.sec_id.value() = desc;
|
|
new_order.order_def.order_side = c.side == bts::api::SIDE_BUY
|
|
? emumba::market_data::side::buy
|
|
: emumba::market_data::side::sell;
|
|
new_order.order_def.time_in_frc = convert_tif(c.time_in_force);
|
|
// price, qty
|
|
new_order.px = emumba::market_data::price(c.price.value());
|
|
new_order.qty.value() = c.quantity.full_value();
|
|
// order_def.exchange_order
|
|
emumba::order_entry::deribit_order_definition deribit_def;
|
|
emumba::order_entry::deribit_order_def_optional deribit_def_optionals;
|
|
deribit_def.ord_type = convert_order_type(c.order_type);
|
|
deribit_def_optionals.mmp = false;
|
|
deribit_def.optional_fields = deribit_def_optionals;
|
|
new_order.order_def.exchange_order = deribit_def;
|
|
new_order.order_def.account = c.routing_record->account_code();
|
|
// place order now
|
|
auto op_res = me->order_mux->launch_new_order(new_order);
|
|
if (op_res.is_success) {
|
|
auto new_order_key = op_res.ok;
|
|
EMOrderKey em_key = new_order_key.value();
|
|
me->order_map.insert({em_key, c});
|
|
me->key_map.insert({c.key, em_key});
|
|
} else {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_send_create launch_new_order rejected, "
|
|
"error_msg: {}",
|
|
op_res.err_msg);
|
|
}
|
|
}
|
|
|
|
void EMExecutionGateway::_send_cxl_replace(
|
|
const CancelReplaceOrderMsg &cxl_rpl) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_send_cxl_replace key: {}",
|
|
cxl_rpl.create_fields.key);
|
|
auto it = me->key_map.find(cxl_rpl.create_fields.key);
|
|
if (it == me->key_map.end()) {
|
|
// TODO: should this be an ER ?
|
|
LOG(OrderMan, Error, "Could not modify order key: {}, could not find key",
|
|
cxl_rpl.create_fields.key);
|
|
return;
|
|
}
|
|
EMOrderKey em_key = it->second;
|
|
auto cr_it = me->order_map.find(em_key);
|
|
auto &cr_msg = cr_it->second;
|
|
// this is controversial: because we update the price qty right away
|
|
cr_msg = cxl_rpl.create_fields;
|
|
// how do we handle the case of partial fills here, or does EMOrder
|
|
// does it ?
|
|
const emumba::order_entry::order_update update = {
|
|
emumba::order_entry::order_key(em_key),
|
|
emumba::market_data::price(cxl_rpl.create_fields.price.value()),
|
|
emumba::market_data::quantity(
|
|
cxl_rpl.create_fields.quantity.full_value())};
|
|
me->order_mux->update_order(update);
|
|
}
|
|
|
|
void EMExecutionGateway::_send_rfq(const RFQMsg &rfq_msg) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_send_rfq rfq: {}",
|
|
utils::pb::to_string(rfq_msg.rfq));
|
|
me->order_mux->send_rfq(rfq_msg);
|
|
}
|
|
|
|
void EMExecutionGateway::_send_cxl(const CancelOrderMsg &cxl) {
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_send_cxl key: {}", cxl.key);
|
|
auto it = me->key_map.find(cxl.key);
|
|
if (it == me->key_map.end()) {
|
|
LOG(OrderMan, Error,
|
|
"Could not cancel order key: {}, could not find order key", cxl.key);
|
|
me->on_cancel_reject(
|
|
{cxl.key, CancelRejectReason::UnknownOrder, "Could not find order"});
|
|
return;
|
|
}
|
|
auto key = it->second;
|
|
me->order_mux->cancel_order(emumba::order_entry::order_key(key));
|
|
}
|
|
// f(EMCallbackMsg) -> ExecutionReport and calls on_exe_report(ExecutionReport)
|
|
void EMExecutionGateway::_process_cb_and_fire_er(const EMCallbackMsg &em_cb) {
|
|
// this is the only tricky part here because ...
|
|
EMOrderKey key = em_cb.order_key.value();
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_process_cb_and_fire_er key: {} {}",
|
|
key, em_cb.order_state);
|
|
auto it = me->order_map.find(key);
|
|
if (it == me->order_map.end()) {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_process_cb_and_fire_er key: {} not found", key);
|
|
}
|
|
const auto &cr_msg = it->second;
|
|
// cant create one on the stack
|
|
ExecutionReport *er_p = me->exe_report_pool.construct();
|
|
ExecutionReport &er = *er_p;
|
|
// length of these have to be <= 12
|
|
to_array(cr_msg.routing_record->account(), &er.account_name.value());
|
|
to_array(cr_msg.routing_record->operator_id(), &er.operator_id.value());
|
|
er.order_key = cr_msg.key;
|
|
er.msg_seq_num =
|
|
me->msg_seq_num++; // this is needed for fix, here we just ++ it
|
|
er.security = cr_msg.security;
|
|
er.leg_security = nullptr;
|
|
er.side = cr_msg.side;
|
|
er.time_in_force = cr_msg.time_in_force;
|
|
er.order_type = cr_msg.order_type;
|
|
// TODO
|
|
er.exec_type = api::orderman::ExecType::EXEC_TYPE_NEW; // TODO
|
|
er.order_status = native_order_status(em_cb.order_state);
|
|
er.manual_order_indicator =
|
|
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO;
|
|
std::string exch_oid = std::to_string(em_cb.order_key.value());
|
|
// TODO, should we put EM order or Deribit ?
|
|
// (this is not used by EMNativeOrder so we are ok)
|
|
auto &er_exch_oid = std::get<ExchangeOrderID>(er.exchange_order_id).value();
|
|
to_array(exch_oid, &er_exch_oid);
|
|
er.price.value() = em_cb.order_state.order_px;
|
|
er.quantity = em_cb.order_state.order_qty;
|
|
// fractional qty
|
|
// fills
|
|
er.avg_price.value() = em_cb.order_state.avg_filled_price;
|
|
er.filled_quantity = em_cb.order_state.total_fills;
|
|
if (em_cb.ec.value() && !em_cb.ec.message().empty()) {
|
|
// Deribit sends error codes, at rather benign things (like error 13777,
|
|
// https://docs.deribit.com/#rpc-error-codes) when there is no no evasive
|
|
// action that needs to be taken. However there are algos, like spreader
|
|
// that are super sensitive to messages and cancel themselves. To avoid
|
|
// this we only pass on the error messages when the order has terminated.
|
|
// The assumption being that if the order has not terminated it is OK to
|
|
// ignore the message.
|
|
// Alternatively we can change the spreader to not do that. Right not this
|
|
// seems to be the better choice
|
|
bool is_terminal = terminal_status(em_cb.order_state.status);
|
|
if (is_terminal)
|
|
er.msg = fmt::format(
|
|
"error: {} ({} {})", em_cb.ec.value(), em_cb.ec.message(),
|
|
em_cb.ec.details());
|
|
else {
|
|
LOG(OrderMan, Warn,
|
|
"EMExecutionGateway::_process_cb_and_fire_er received error, "
|
|
"code: {} msg: {} details: {} . This is being ignored since it "
|
|
"did not terminate the order",
|
|
em_cb.ec.value(), em_cb.ec.message(), em_cb.ec.details());
|
|
}
|
|
}
|
|
if (!em_cb.order_state.trades.empty()) {
|
|
er.exec_type = api::orderman::ExecType::EXEC_TYPE_PARTIAL_FILL;
|
|
if (em_cb.order_state.status == emumba::order_entry::status::completed) {
|
|
er.exec_type = api::orderman::ExecType::EXEC_TYPE_FILL;
|
|
}
|
|
er.last_price = Price(em_cb.order_state.last_filled_price);
|
|
er.last_shares = em_cb.order_state.last_filled_qty;
|
|
// TODO: this should be part of the update
|
|
utils::Uuid guid;
|
|
std::string fill_id = guid.to_text_string();
|
|
to_array(fill_id, &er.exchange_fill_id.value());
|
|
} else {
|
|
er.last_shares = Quantity(0);
|
|
}
|
|
er.max_show = Quantity(0);
|
|
er.exchange = cr_msg.security->exchange();
|
|
ExchangeTimeStamp64 now = ExchangeTimeStamp64(datetime::unix_nano_now());
|
|
er.transact_time = now;
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::_process_cb_and_fire_er firing er: {}",
|
|
er.to_string());
|
|
me->on_exe_report(er, {});
|
|
_fire_leg_er(em_cb, er);
|
|
}
|
|
|
|
void EMExecutionGateway::_fire_leg_er(
|
|
const EMCallbackMsg &em, const ExecutionReport &er) {
|
|
const auto &ord_state = em.order_state;
|
|
if (ord_state.leg_trades.empty()) {
|
|
return;
|
|
}
|
|
auto sec_ptr = me->sec_oracle.get_security(SecurityKey(ord_state.sec_key));
|
|
if (sec_ptr == nullptr) {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_fire_leg_er could not get security for "
|
|
"sec_key: {}, this should be investigated",
|
|
ord_state.sec_key);
|
|
return;
|
|
}
|
|
if (!sec_ptr->has_spread()) {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_fire_leg_er sec_key: {} is not a spread, this "
|
|
"should be investigated",
|
|
ord_state.sec_key);
|
|
return;
|
|
}
|
|
const auto &spread = sec_ptr->spread();
|
|
if (spread.legs_size() > (int)ord_state.leg_trades.size()) {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_fire_leg_er sec_key: {} missing leg fills, "
|
|
"this should be investigated",
|
|
ord_state.sec_key);
|
|
return;
|
|
}
|
|
for (const auto &lf : ord_state.leg_trades) {
|
|
auto ls_ptr = me->sec_oracle.get_security(SecurityKey(lf.sec_key));
|
|
if (ls_ptr != nullptr) {
|
|
ExecutionReport *er_p = me->exe_report_pool.construct();
|
|
ExecutionReport &l_er = *er_p;
|
|
l_er = er;
|
|
l_er.leg_security = ls_ptr;
|
|
l_er.side = lf.side;
|
|
l_er.price = Price(lf.last_filled_price);
|
|
l_er.quantity = Quantity::create_new(lf.last_filled_qty);
|
|
l_er.last_price = Price(lf.last_filled_price);
|
|
l_er.last_shares = Quantity::create_new(lf.last_filled_qty);
|
|
l_er.avg_price = l_er.last_price;
|
|
l_er.msg_seq_num = me->msg_seq_num++;
|
|
utils::Uuid guid;
|
|
std::string fill_id = guid.to_text_string();
|
|
to_array(fill_id, &l_er.exchange_fill_id.value());
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_fire_leg_er firing er: {}",
|
|
l_er.to_string());
|
|
me->on_exe_report(l_er, {});
|
|
} else {
|
|
LOG(OrderMan, Error,
|
|
"EMExecutionGateway::_fire_leg_er sec_key: {} could not resolve "
|
|
"leg: {}",
|
|
ord_state.sec_key, lf.sec_key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// converts the EMPosition received in the callback from IEMOrderMuxer, to
|
|
// ExchangePosition (resolved security and account)
|
|
void EMExecutionGateway::_process_pos_cb_and_fire(
|
|
const EMExchangePositionCallbackMsg &pos_cb_msg) {
|
|
me->exchange_position.total_exchange_positions.clear();
|
|
// TODO: need to fix this whem EM connects to multiple exchanges
|
|
me->exchange_position.exchange = api::VENUE_DERIBIT;
|
|
for (const auto &pos : pos_cb_msg.positions) {
|
|
const std::string &exch_sec_desc =
|
|
std::get<std::string>(pos.position.sec_id.value());
|
|
auto sec = _get_security(exch_sec_desc);
|
|
double pos_qty = std::get<double>(pos.position.qty.value());
|
|
auto bts_account_ptr = _get_account(sec, pos.sub_account, exch_sec_desc);
|
|
auto account_name = [bts_account_ptr]() {
|
|
return bts_account_ptr == nullptr ? "" : bts_account_ptr->name();
|
|
};
|
|
std::optional<uint32_t> account_id =
|
|
bts_account_ptr == nullptr
|
|
? std::nullopt
|
|
: std::optional<uint32_t>(bts_account_ptr->id());
|
|
me->exchange_position.total_exchange_positions.push_back(
|
|
{exch_sec_desc, sec, account_name(), account_id, pos_qty,
|
|
Price(pos.position.average_price.to_double())});
|
|
}
|
|
LOG(OrderMan, Info, "EMExecutionGateway::_process_pos_cb_and_fire n_pos: {}",
|
|
me->exchange_position.total_exchange_positions.size());
|
|
me->on_exchange_position_state(me->exchange_position);
|
|
}
|
|
|
|
void EMExecutionGateway::_process_open_order_cb_and_fire(
|
|
const EMExchangeOpenOrdersCallbackMsg &open_order_cb) {
|
|
using namespace emumba::market_data;
|
|
me->exchange_open_orders.all_open_orders.clear();
|
|
me->exchange_open_orders.exchange = api::VENUE_DERIBIT;
|
|
for (const EMOpenOrder &i : open_order_cb.orders) {
|
|
const auto &o = i.open_order;
|
|
const std::string &exch_sec_desc =
|
|
std::get<std::string>(o.sec_id.value());
|
|
auto sec = _get_security(exch_sec_desc);
|
|
auto bts_account_ptr = _get_account(sec, i.sub_account, exch_sec_desc);
|
|
Quantity qty = Quantity::create_new(std::get<double>(o.qty.value()));
|
|
Quantity filled_qty =
|
|
Quantity::create_new(std::get<double>(o.filled_qty.value()));
|
|
me->exchange_open_orders.all_open_orders.push_back(
|
|
{exch_sec_desc, i.sub_account, sec, o.exchange_order_id,
|
|
bts_account_ptr == nullptr ? "" : bts_account_ptr->name(),
|
|
bts_account_ptr == nullptr
|
|
? std::nullopt
|
|
: std::optional<uint32_t>(bts_account_ptr->id()),
|
|
bts::api::orderman::TIME_IN_FORCE_DAY,
|
|
o.order_side == side::buy ? bts::api::SIDE_BUY : bts::api::SIDE_SELL,
|
|
Price(o.px.to_double()), qty, Price(o.average_price.to_double()),
|
|
filled_qty, o.creation_timestamp.get_timestamp(),
|
|
o.last_update_timestamp.get_timestamp(), o.api, o.web});
|
|
}
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::_process_open_order_cb_and_fire n_open_orders: {}",
|
|
me->exchange_open_orders.all_open_orders.size());
|
|
me->on_exchange_open_orders(me->exchange_open_orders);
|
|
}
|
|
|
|
void EMExecutionGateway::_send_position_request() {
|
|
if (me->order_mux == nullptr)
|
|
return;
|
|
auto msecs = bts::datetime::msecs_since(me->last_pos_fire_time);
|
|
if (msecs < me->pos_fire_period_ms) {
|
|
return;
|
|
}
|
|
me->last_pos_fire_time = bts::datetime::now();
|
|
LOG(OrderMan, Info,
|
|
"EMExecutionGateway::_send_position_request requesting positions");
|
|
me->order_mux->get_positions();
|
|
me->order_mux->get_open_orders();
|
|
}
|
|
|
|
api::admin::BTSAccount *EMExecutionGateway::_get_account(
|
|
api::Security const *sec,
|
|
const std::string &exch_account,
|
|
const std::string &exchange_sec_desc) {
|
|
auto *entry = me->subaccount_resolver.find_entry(sec, exch_account, false);
|
|
if (entry == nullptr) {
|
|
LOG(OrderMan, Error,
|
|
"Could not resolve internal account for sec: {}, sub_account: {}",
|
|
sec != nullptr ? utils::pb::to_string(*sec) : exchange_sec_desc,
|
|
exch_account);
|
|
return nullptr;
|
|
}
|
|
auto account_name = entry->bts_account_name();
|
|
auto bts_account_ptr =
|
|
admin::AdminServiceUtils::find_account(me->admin_data, account_name);
|
|
return bts_account_ptr;
|
|
}
|
|
|
|
const api::Security *EMExecutionGateway::_get_security(
|
|
const std::string &exch_sec_desc) {
|
|
for (auto sec : me->sec_oracle.get_securities()) {
|
|
if (sec->has_deribit()) {
|
|
if (sec->deribit().security_desc() == exch_sec_desc) {
|
|
return sec;
|
|
}
|
|
}
|
|
}
|
|
return static_cast<const api::Security *>(nullptr);
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|