Files
spark/src/app_sparkd/sparkd/execution/py_execution_gateway_adapter.cpp
T
John Colvin a26918efe9 refactor: Rename a few TitleCase .h files snake_case
* EnumMacros.h -> enum_macros.h
* TimeSpan.h ---> time_span.h
* TimeStamp.h --> time_stamp.h
2026-03-26 16:20:40 -04:00

735 lines
28 KiB
C++

#include "py_execution_gateway_adapter.h"
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/execution_gateway_bridge_impl.h"
#include "python_interop/execution/py_cancel_order_request.h"
#include "python_interop/execution/py_cancel_replace_order_request.h"
#include "python_interop/execution/py_create_order_request.h"
#include "python_interop/execution/py_execution_gateway.h"
#include "python_interop/execution/py_execution_report.h"
#include "python_interop/execution/py_position_info.h"
#include "python_interop/plugin_manager.h"
#include "python_interop/py_contract_price_info.h"
#include "python_interop/py_contract_quantity_info.h"
#include "python_interop/py_contract_specification.h"
#include "python_interop/py_security.h"
#include "python_interop/python_object.h"
#include "spark/api/domain/Venue.h"
#include "spark/api/service_helpers/AdminServiceUtils.h"
#include "spark/api/services/AdminService.pb.h"
#include "spark/api/services/OrderService.h"
#include "spark/api/services/exec_type.h"
#include "spark/api/services/native_order_status.h"
#include "spark/api/services/native_order_type.h"
#include "spark/api/services/time_in_force.h"
#include "spark/config/protos/jetfix.pb.h"
#include "spark/core/time_stamp.h"
#include "spark/core/start_stop_state_machine.h"
#include "spark/domain/tick_utils.h"
#include "spark/domain_model/Security.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/domain_model/SecurityType.h"
#include "spark/domain_model/Side.h"
#include "spark/domain_model/Venue.h"
#include "spark/io/json_formatter.h"
#include "spark/logging/logging.h"
#include "spark/logging/trace_log.h"
#include "spark/md/SecurityOracle.h"
#include "spark/mercury/events/Update.h"
#include "spark/orderman/CancelOrderMsg.h"
#include "spark/orderman/CancelReplaceOrderMsg.h"
#include "spark/orderman/CreateOrderMsg.h"
#include "spark/orderman/RFQMsg.h"
#include "spark/orderman/events/NewAdminDataEvent.h"
#include "spark/orderman/events/NewSecuritiesEvent.h"
#include "spark/orderman/gateways/IExecutionGateway.h"
#include "spark/orderman/log_messages/cancel_order_log.h"
#include "spark/orderman/log_messages/create_order_log.h"
#include "spark/orderman/log_messages/execution_report_log.h"
#include "spark/orderman/log_messages/modify_order_log.h"
#include "spark/orderman/orderman_utils/ExchangePosition.h"
#include "spark/orderman/orderman_utils/ExecutionReport.h"
#include "spark/orderman/orderman_utils/execution_report_store.h"
#include "spark/orderman/orderman_utils/orderman_common.h"
#include "spark/orderman/orderman_utils/simple_validation.h"
#include "spark/utils/PBUtils.h"
#include "spark/utils/bts_lock_guard.h"
#include "spark/utils/overloaded.h"
#include "spark/utils/stopwatch.h"
#include "spark/utils/string_utils.h"
#include "sparkd/execution/py_execution_gateway_config.h"
#include <fmt/format.h>
#include <future>
#include <limits>
#include <nlohmann/json.hpp>
#include <string>
#include <variant>
#include <vector>
namespace bts::orderman {
#define ASSERT_FN(test, fn, ...) \
bts_assert_msgf(test, "PyExecutionGatewayAdapter::" fn ". " __VA_ARGS__)
#define LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "PyExecutionGatewayAdapter::" fn ". " __VA_ARGS__)
#define DIAG_FN(fn, ...) LOG_FN(bts::Diag, fn, __VA_ARGS__)
#define INFO_FN(fn, ...) LOG_FN(bts::Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) LOG_FN(bts::Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) LOG_FN(bts::Error, fn, __VA_ARGS__)
#define _LOG(level, ...) LOG(OrderMan, level, __VA_ARGS__)
#define INFO(...) _LOG(bts::Info, __VA_ARGS__)
#define ERROR(...) _LOG(bts::Error, __VA_ARGS__)
#define _LOG_TRACE(level, name) \
LOG_TRACE(OrderMan, level, std::string("py_exe_gw_adapt"), std::string(name))
#define INFO_TRACE(name) _LOG_TRACE(bts::Info, name)
#define WARN_TRACE(name) _LOG_TRACE(bts::Warn, name)
#define ERROR_TRACE(name) _LOG_TRACE(bts::Error, name)
#define _LOG_TRACE_PARAMS(level, name, params) \
LOG_TRACE_PARAMS( \
OrderMan, level, std::string("py_exe_gw_adapt"), std::string(name), \
params)
#define DIAG_TRACE_PARAMS(name, params) \
_LOG_TRACE_PARAMS(bts::Diag, name, params)
#define WARN_TRACE_PARAMS(name, params) \
_LOG_TRACE_PARAMS(bts::Warn, name, params)
#define ERROR_TRACE_PARAMS(name, params) \
_LOG_TRACE_PARAMS(bts::Error, name, params)
#define INFO_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
OrderMan, bts::Info, std::string("py_exe_gw_adapt"), std::string(name), \
params)
using bts::Quantity;
using bts::api::security_type_parse_smart;
using bts::api::SecurityType;
using bts::api::Venue;
using bts::api::venue_parse_smart;
using bts::api::admin::BTSAccount;
using bts::api::orderman::exec_type_parse_smart;
using bts::api::orderman::native_order_status_parse_smart;
using bts::api::orderman::time_in_force_parse_smart;
using bts::core::Delegate;
using bts::core::StartStopStateMachine;
using bts::core::TimeStamp;
using bts::md::SecurityOracle;
using bts::orderman::CancelOrderMsg;
using bts::orderman::CancelReplaceOrderMsg;
using bts::orderman::CreateOrderMsg;
using bts::orderman::ExchangeOrderID64;
using bts::orderman::ExchangePosition;
using bts::orderman::ExecutionReport;
using bts::orderman::ExecutionReportStore;
using bts::orderman::NewAdminDataEvent;
using bts::orderman::NewSecuritiesEvent;
using bts::orderman::OrderKey;
using bts::orderman::OrderMsg;
using bts::orderman::RFQMsg;
using bts::system::string_utils::parse_uint;
using nlohmann::json;
using python_execution::ExecutionGateway;
using python_execution::ExecutionGatewayBridgeImpl;
using python_execution::PyCancelOrderRequest;
using python_execution::PyCancelReplaceOrderRequest;
using python_execution::PyCreateOrderRequest;
using python_execution::PyPositionInfo;
using python_interop::PluginManager;
using python_interop::PythonObjectT;
using std::int64_t;
using std::optional;
using std::shared_ptr;
using std::size_t;
using std::string;
using std::thread;
using std::uint32_t;
using std::uint64_t;
using std::vector;
struct ExecutionGatewayCallbacks {
bts::orderman::OnExeReport exe_report;
bts::orderman::OnGwStatusChange gw_status_change;
bts::orderman::OnCancelReject cancel_reject;
bts::orderman::OnExchangePositionState exchange_position_state;
bts::orderman::OnExchangeOpenOrdersState exchange_open_orders;
};
struct PyExecutionGatewayAdapter::PImpl {
explicit PImpl(
bts::md::SecurityOracle sec_oracle,
PyExecutionGatewayConfig config,
bts::config::JetFIX::Connection jetfix_connection,
shared_ptr<PluginManager> plugin_man)
: sec_oracle(std::move(sec_oracle)), config(std::move(config)),
name(fmt::format("{}_py_gw", string{jetfix_connection.name()})),
bridge_ptr(std::make_shared<ExecutionGatewayBridgeImpl>(
jetfix_connection.name())),
bridge(*bridge_ptr), plugin_man(std::move(plugin_man)),
jetfix_connection(std::make_shared<bts::config::JetFIX_Connection>(
std::move(jetfix_connection))),
orders(this->config.account, this->jetfix_connection) {
bts_assert(this->jetfix_connection->venue() != api::VENUE_UNSET);
bts_assert(!this->jetfix_connection->name().empty());
pos.exchange = this->jetfix_connection->venue();
sm.set_name(name);
sm.set_subsystem(bts::log::OrderMan);
sm.start_core_impl.bind<&PImpl::start_impl>(*this);
sm.stop_core_impl.bind<&PImpl::stop_impl>(*this);
}
~PImpl() { stop(); }
enum RequestType {
ORDER,
POSITION,
};
SecurityOracle sec_oracle;
PyExecutionGatewayConfig config;
std::string name;
std::shared_ptr<ExecutionGatewayBridgeImpl> bridge_ptr;
ExecutionGatewayBridgeImpl &bridge;
shared_ptr<PluginManager> plugin_man;
shared_ptr<bts::config::JetFIX_Connection> jetfix_connection;
optional<thread> pos_snapshot_thread;
optional<thread> py_thread;
bool is_ready = false;
bool is_ready_dirty = false;
volatile bool py_keep_going = false;
uint32_t n_active = 0;
core::TimeStamp last_unresolved_account_log_time;
StartStopStateMachine sm;
// SimpleOrderKeyMap key_map;
ExecutionReportStore orders;
ExecutionGatewayCallbacks callbacks;
ExchangePosition pos;
BTSAccount account;
OnExeReport exe_report_callback;
void to_exe_report(
bts::orderman::ExecutionReport &er,
const python_execution::PyExecutionReport &pyer,
std::function<const bts::api::Security *(
bts::api::Venue, bts::api::SecurityType, const std::string &)>
find_sec) {
*er.order_key = pyer.key();
er.account_name->copy_from(pyer.account());
if (std::holds_alternative<ExchangeOrderID>(er.exchange_order_id)) {
std::get<ExchangeOrderID>(er.exchange_order_id)
->copy_from(pyer.exchange_order_id());
} else {
er.exchange_order_id = ExchangeOrderID{pyer.exchange_order_id()};
}
native_order_type_parse_smart(pyer.order_type(), &er.order_type);
native_order_status_parse_smart(pyer.order_status(), &er.order_status);
time_in_force_parse_smart(pyer.time_in_force(), &er.time_in_force);
exec_type_parse_smart(pyer.exec_type(), &er.exec_type);
Venue venue;
bts_assert(venue_parse_smart(pyer.venue(), &venue));
SecurityType sec_type;
bts_assert(security_type_parse_smart(pyer.security_type(), &sec_type));
er.security = find_sec(venue, sec_type, pyer.exchange_security_id());
er.side = pyer.is_buy() ? bts::api::SIDE_BUY : bts::api::SIDE_SELL;
*er.price = pyer.price();
*er.stop_price = std::numeric_limits<double>::quiet_NaN();
er.quantity = Quantity::create_new(pyer.quantity());
*er.avg_price = pyer.avg_filled_price();
er.filled_quantity = Quantity::create_new(pyer.filled_quantity());
*er.last_price = pyer.last_filled_price();
er.last_shares = Quantity::create_new(pyer.last_filled_quantity());
er.transact_time =
ExchangeTimeStamp64{static_cast<uint64_t>(pyer.transact_time())};
}
void start() {
INFO_TRACE("start");
sm.start();
}
// should not be called directly (use the state machine)
void start_impl() {
INFO_TRACE("start_impl");
bts_assert(!py_thread);
py_keep_going = true;
py_thread.emplace([&]() {
auto orig_signal_handler = std::signal(SIGINT, SIG_DFL);
std::future<shared_ptr<PythonObjectT<ExecutionGateway>>> gw_fut =
plugin_man->start_execution_gateway(
name, config.gateway_type, bridge_ptr);
gw_fut.wait();
std::signal(SIGINT, orig_signal_handler);
bridge.run_py(&py_keep_going, gw_fut.get(), [&](auto callback) {
plugin_man->invoke(callback);
});
});
}
void stop() {
INFO_TRACE("stop");
sm.stop();
}
void stop_impl() {
INFO_TRACE("stop_impl");
py_keep_going = false;
if (py_thread) {
py_thread->join();
py_thread.reset();
}
}
void create_order(const CreateOrderMsg &m) {
LOG_CREATE_ORDER(Info, m);
PyCreateOrderRequest py_req;
to_py(py_req, m);
bridge.on_order_msg(
std::make_shared<PyCreateOrderRequest>(std::move(py_req)));
}
void update_order(const CancelReplaceOrderMsg &m) {
LOG_MODIFY_ORDER(Info, m);
PyCancelReplaceOrderRequest py_req;
to_py(py_req, m);
bridge.on_order_msg(
std::make_shared<PyCancelReplaceOrderRequest>(std::move(py_req)));
}
void cancel_order(const CancelOrderMsg &m) {
LOG_CANCEL_ORDER(Info, m);
PyCancelOrderRequest req;
to_py(req, m);
bridge.on_order_msg(
std::make_shared<PyCancelOrderRequest>(std::move(req)));
}
void on_securities(const std::vector<api::Security const *> &secs) {
std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>> py_secs;
for (const auto *sec : secs) {
bts::md::py_interop::PySecurity py_sec;
to_py(py_sec, *sec);
py_secs.push_back(std::make_shared<bts::md::py_interop::PySecurity>(
std::move(py_sec)));
}
bridge.on_securities(std::move(py_secs));
}
void dispatch_is_ready() {
BTS_LOCK_GUARD(sm.mutex());
if (is_ready_dirty) {
is_ready_dirty = false;
callbacks.gw_status_change(is_ready);
}
}
void from_py(
const python_execution::PyExecutionReport &py_er,
ExecutionReport &er) const {
*er.order_key = py_er.key();
bts_assert(venue_parse_smart(py_er.venue(), &er.exchange));
SecurityType sec_type;
bts_assert(security_type_parse_smart(py_er.security_type(), &sec_type));
er.transact_time =
ExchangeTimeStamp64{static_cast<uint64_t>(py_er.transact_time())};
uint64_t exch_oid;
if (parse_uint(py_er.exchange_order_id(), exch_oid)) {
er.exchange_order_id.emplace<ExchangeOrderID64>().value() = exch_oid;
} else {
er.exchange_order_id.emplace<ExchangeOrderID>().value() =
py_er.exchange_order_id();
}
er.security = sec_oracle.get_security(
er.exchange, sec_type, py_er.exchange_security_id());
er.leg_security = nullptr;
er.side = py_er.is_buy() ? api::SIDE_BUY : api::SIDE_SELL;
time_in_force_parse_smart(py_er.time_in_force(), &er.time_in_force);
native_order_type_parse_smart(py_er.order_type(), &er.order_type);
exec_type_parse_smart(py_er.exec_type(), &er.exec_type);
native_order_status_parse_smart(py_er.order_status(), &er.order_status);
er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO;
er.price.value() = py_er.price();
er.quantity = py_er.quantity();
er.filled_quantity = py_er.filled_quantity();
er.last_shares = py_er.last_filled_quantity();
er.last_price.value() = py_er.last_filled_price();
er.avg_price.value() = py_er.avg_filled_price();
er.stop_price.value() = std::numeric_limits<double>::quiet_NaN();
const string &tx_id = py_er.transact_id();
bts_assert(tx_id.size() < er.exchange_fill_id.value().max_size());
bts_assert(!py_er.transact_id().empty());
er.exchange_fill_id->copy_from(py_er.transact_id());
er.msg = py_er.error_msg();
}
void to_py(
bts::md::py_interop::PySecurity &py_sec, const bts::api::Security &sec) {
py_sec.set_id(sec.id());
py_sec.set_asset(sec.symbol());
py_sec.set_venue(bts::api::to_pretty(sec.exchange()).name());
py_sec.set_currency(sec.currency());
// Exchange ID and name
std::string name = bts::securities::descriptor(sec);
py_sec.set_exchange_id(name);
py_sec.set_name(std::move(name));
// Price ticks
const bts::api::ContractSpecification &spec = sec.contract_spec();
md::py_interop::PyContractSpecification py_spec;
md::py_interop::PyContractPriceInfo &py_price_info =
py_spec.mutable_price_infos().emplace_back();
float tick_unit;
float tick_base;
if (!domain::get_tick_factors(spec, tick_unit, tick_base)) {
WARN_FN(
"on_securities", "Failed to get price tick from {} {}. spec: {}. ",
sec.exchange(), py_sec.name(),
utils::pb::to_string(sec.contract_spec()));
tick_unit = 0.01f;
tick_base = 0.01f;
}
py_price_info.set_tick_unit(tick_unit);
py_price_info.set_tick_base(tick_base);
// Quantity ticks
md::py_interop::PyContractQuantityInfo &py_qty_info =
py_spec.mutable_quantity_infos().emplace_back();
if (spec.quantity_infos().empty()) {
tick_unit = 1.0f;
tick_base = 1.0f;
WARN_FN(
"on_securities", "Failed to get qty tick from {} {}. spec: {}. ",
sec.exchange(), name, utils::pb::to_string(sec.contract_spec()));
} else {
tick_unit = spec.quantity_infos(0).tick_unit();
tick_base = spec.quantity_infos(0).tick_base();
}
py_qty_info.set_tick_unit(tick_unit);
py_qty_info.set_tick_base(tick_base);
py_sec.set_contract_spec(std::move(py_spec));
// Spot/Future/Option
switch (sec.Contract_case()) {
case bts::api::Security::kSpot: {
py_sec.set_spot(bts::md::py_interop::PySpot{});
break;
}
case bts::api::Security::kFuture: {
const bts::api::FutureContract &fut = sec.future();
bts::md::py_interop::PyFuture py_fut;
py_fut.set_expiration(fmt::format(
"{}-{:02}-{:02}", fut.expiration_year(), fut.expiration_month(),
fut.expiration_day()));
py_sec.set_future(std::move(py_fut));
break;
}
case bts::api::Security::kOption: {
const bts::api::OptionContract &opt = sec.option();
bts::md::py_interop::PyOption py_opt;
py_opt.set_expiration(fmt::format(
"{}-{:02}-{:02}", opt.expiration_year(), opt.expiration_month(),
opt.expiration_day()));
py_opt.set_strike(opt.strike());
py_opt.set_is_call(opt.call_put() == bts::api::PUTORCALL_CALL);
py_sec.set_option(std::move(py_opt));
break;
}
default:
bts_assert_msg(false, "Only spots, futures, and options supported");
break;
}
}
void to_py(PyCreateOrderRequest &py_req, const CreateOrderMsg &msg) {
py_req.set_key(msg.key.value());
py_req.set_exchange_security_id(msg.security_name());
py_req.set_venue(api::to_pretty(msg.venue()).name());
py_req.set_security_type(api::to_pretty(msg.security_type()).name());
py_req.set_is_buy(msg.side == bts::api::SIDE_BUY);
py_req.set_price(msg.price.value());
py_req.set_quantity(msg.quantity.full_value_float());
py_req.set_order_type(
bts::api::orderman::native_order_type_name_pretty(msg.order_type));
py_req.set_time_in_force(
bts::api::orderman::time_in_force_name_pretty(msg.time_in_force));
}
void to_py(
PyCancelReplaceOrderRequest &py_req,
const bts::orderman::CancelReplaceOrderMsg &msg) {
auto &cf = msg.create_fields;
py_req.set_key(cf.key.value());
py_req.set_exchange_order_id(
std::to_string(msg.exchange_order_id.value()));
py_req.set_exchange_security_id(cf.security_name());
py_req.set_venue(api::to_pretty(cf.venue()).name());
py_req.set_security_type(api::to_pretty(cf.security_type()).name());
py_req.set_is_buy(cf.side == bts::api::SIDE_BUY);
py_req.set_price(cf.price.value());
py_req.set_quantity(cf.quantity.full_value_float());
}
void to_py(PyCancelOrderRequest &py_req, const CancelOrderMsg &msg) {
py_req.set_key(msg.key.value());
py_req.set_exchange_order_id(
std::to_string(msg.exchange_order_id.value()));
py_req.set_exchange_security_id(msg.security_name());
py_req.set_venue(bts::api::to_pretty(msg.venue()).name());
py_req.set_security_type(api::to_pretty(msg.security_type()).name());
py_req.set_is_buy(msg.side == bts::api::SIDE_BUY);
}
void from_py(
const python_execution::PyPositionInfo &py_pos_info,
ExchangePosition::UnitPosition &upos) {
upos.exchange_sec_desc = py_pos_info.exchange_security_id();
double long_qty = py_pos_info.long_quantity();
if (std::isnan(long_qty)) {
long_qty = 0;
}
double long_avg_cost = py_pos_info.long_avg_cost();
if (std::isnan(long_avg_cost)) {
long_avg_cost = 0;
}
double short_qty = py_pos_info.short_quantity();
if (std::isnan(short_qty)) {
short_qty = 0;
}
double short_avg_cost = py_pos_info.short_avg_cost();
if (std::isnan(short_avg_cost)) {
short_avg_cost = 0;
}
upos.position = long_qty - short_qty;
// We shouldn't combine long/short positions like this. Avg cost is
// lost/meaningless when the net qty is 0.
if (upos.position == 0) {
*upos.avg_filled_price = std::numeric_limits<double>::quiet_NaN();
} else {
*upos.avg_filled_price =
(long_avg_cost * long_qty - short_avg_cost * short_qty) /
(long_qty - short_qty);
}
}
void dispatch_order_updates() {
BTS_LOCK_GUARD(sm.mutex());
std::shared_ptr<const python_execution::PyExecutionReport> py_er_p;
utils::Stopwatch sw;
uint32_t n_dequed = 0;
uint32_t n_invalid = 0;
sw.start();
while (bridge.pop_exe_report(py_er_p)) {
n_dequed++;
const python_execution::PyExecutionReport &py_er = *py_er_p;
shared_ptr<const ExecutionReport> er_p =
orders.update(OrderKey{py_er.key()}, [&](auto &er_p, bool) {
from_py(py_er, *er_p);
});
const ExecutionReport &er = *er_p;
string error;
if (!validate(er, error)) {
LOG_EXECUTION_REPORT(Error, er_p, "INVALID_STATE", error);
n_invalid++;
continue;
}
LOG_EXECUTION_REPORT(Info, er_p);
switch (er.order_status) {
case api::orderman::NATIVE_ORDER_STATUS_FILLED:
case api::orderman::NATIVE_ORDER_STATUS_CANCELED:
case api::orderman::NATIVE_ORDER_STATUS_REJECTED: {
orders.erase(er.order_key);
INFO_FN(
"dispatch_order_updates",
"Order reached terminal state {} (id={})", er.order_status,
er.exchange_order_id);
break;
}
default:
break;
}
INFO_FN(
"dispatch_order_updates",
"Firing exe report callback. {} {} {} @ {} {} (filled {}/{}, "
"id={}). {} active orders",
er.side, er.security_name(), er.quantity, er.price,
er.order_status, er.filled_quantity, er.quantity, er.order_key,
orders.size());
callbacks.exe_report(er_p, {});
}
sw.stop();
if (n_dequed > 0) {
INFO(
R"([py_exe_gw_disp_ord_stats.json]{{ "n_active": {}, "n_dequed": {}, "n_invalid": {}, "dur": {}, "ts": {} }})",
orders.size(), n_dequed, n_invalid, sw.total_elapsed().nanos(),
TimeStamp::now_ns());
}
}
void dispatch_position_updates() {
BTS_LOCK_GUARD(sm.mutex());
if (account.id() == 0 || account.name().empty()) {
const auto now = core::TimeStamp::now();
if ((now - last_unresolved_account_log_time) >
core::TimeSpan::from_seconds(5)) {
WARN_TRACE_PARAMS(
"dispatch_position_updates",
fmt::format(
R"({{ "venue": "{}", "cfg_acc": "{}", "msg": "Waiting for admin data to get account id" }})",
api::to_pretty(pos.exchange), this->config.account));
last_unresolved_account_log_time = now;
}
return;
}
vector<shared_ptr<PyPositionInfo>> py_pos_infos;
utils::Stopwatch sw;
uint32_t n_pos_changes = 0;
sw.start();
while (bridge.pop_position_info(py_pos_infos)) {
for (const shared_ptr<PyPositionInfo> &py_pos_info_ptr :
py_pos_infos) {
PyPositionInfo &py_pos_info = *py_pos_info_ptr;
const string &exch_sec_id = py_pos_info.exchange_security_id();
const api::Security *sec_ptr =
sec_oracle.get_security(pos.exchange, exch_sec_id);
if (sec_ptr == nullptr) {
WARN_TRACE_PARAMS(
"dispatch_position_updates",
fmt::format(
R"({{ "venue": "{}", "acc": "{}", "accid": "{}", "esid": "{}", "msg": "Unresolved security" }})",
api::to_pretty(pos.exchange), account.name(),
account.id(), exch_sec_id));
continue;
}
ExchangePosition::UnitPosition *upos_ptr = nullptr;
for (auto &upos : pos.total_exchange_positions) {
bts_assert(upos.security != nullptr);
if (upos.security->id() == sec_ptr->id()) {
upos_ptr = &upos;
break;
}
}
if (upos_ptr == nullptr) {
upos_ptr = &pos.total_exchange_positions.emplace_back();
upos_ptr->security = sec_ptr;
upos_ptr->exchange_sec_desc = exch_sec_id;
upos_ptr->account_id = account.id();
upos_ptr->account_name = account.name();
}
from_py(py_pos_info, *upos_ptr);
n_pos_changes++;
}
}
if (n_pos_changes > 0) {
callbacks.exchange_position_state(pos);
}
}
};
PyExecutionGatewayAdapter::PyExecutionGatewayAdapter(
SecurityOracle sec_oracle,
PyExecutionGatewayConfig session_cfg,
bts::config::JetFIX::Connection jetfix_connection,
shared_ptr<PluginManager> plugin_man)
: me(std::make_unique<PImpl>(
std::move(sec_oracle),
std::move(session_cfg),
std::move(jetfix_connection),
std::move(plugin_man))) {
bts_assert(
me->config.account.size() <=
ExecutionReport{}.account_name.value().max_size());
}
PyExecutionGatewayAdapter::~PyExecutionGatewayAdapter() = default;
void PyExecutionGatewayAdapter::start() { me->start(); }
void PyExecutionGatewayAdapter::on_securities(
const bts::mercury::Update<NewSecuritiesEvent> & /*update*/) {
INFO_FN(
"on_securities", "t_id: {} t_name: {}", bts::os::get_thread_id(),
bts::os::get_thread_name());
std::vector<api::Security const *> secs =
me->sec_oracle.get_securities([&](const bts::api::Security &sec) {
return sec.exchange() == me->jetfix_connection->venue();
});
me->on_securities(secs);
}
void PyExecutionGatewayAdapter::on_admin_data(
const bts::mercury::Update<NewAdminDataEvent> &update) {
INFO_FN(
"on_admin_data", "t_id: {} t_name: {}", bts::os::get_thread_id(),
bts::os::get_thread_name());
if (update.events == nullptr) {
return;
}
std::lock_guard<std::recursive_mutex> lock{me->sm.mutex()};
const auto *account = bts::admin::AdminServiceUtils::find_account(
*update.events->back().admin_data, me->config.account);
if (account == nullptr) {
WARN_FN(
"on_admin_data",
"Gateway account not found. Cannot execute orders. name: '{}'",
me->config.account);
return;
}
if (me->account.name() == account->name() &&
me->account.id() == account->id()) {
return;
}
me->account.CopyFrom(*account);
INFO_FN("on_admin_data", "Resolved gateway account '{}'", account->name());
}
void PyExecutionGatewayAdapter::send_msgs(
vector<OrderMsg> const &msg_vec, bts::trace::CorID) {
INFO_FN("send_msgs", "count: {}", msg_vec.size());
for (const auto &msg : msg_vec) {
std::visit(
bts::overloaded{
[&](const CreateOrderMsg &m) { me->create_order(m); },
[&](const CancelReplaceOrderMsg &m) { me->update_order(m); },
[&](const CancelOrderMsg &m) { me->cancel_order(m); },
[&](const RFQMsg &) {
bts_assert_msg(
false, "PyExecutionGatewayAdapter RFQ not implemented");
},
},
msg);
}
}
void PyExecutionGatewayAdapter::send_fix(
const bts::jetfix::OutFIXPtr &, bts::trace::CorID) {
bts_assert_msg(false, "ExecutionGateway does not support fix");
}
void PyExecutionGatewayAdapter::dispatch_incoming_fix_messages() {
me->dispatch_is_ready();
me->dispatch_order_updates();
me->dispatch_position_updates();
}
// Callbacks
bts::orderman::OnExeReport &PyExecutionGatewayAdapter::on_exe_report() {
return me->callbacks.exe_report;
}
bts::orderman::OnGwStatusChange &
PyExecutionGatewayAdapter::on_gw_status_change() {
return me->callbacks.gw_status_change;
}
bts::orderman::OnCancelReject &PyExecutionGatewayAdapter::on_cancel_reject() {
return me->callbacks.cancel_reject;
}
bts::orderman::OnExchangePositionState &
PyExecutionGatewayAdapter::on_exchange_position_state() {
return me->callbacks.exchange_position_state;
}
bts::orderman::OnExchangeOpenOrdersState &
PyExecutionGatewayAdapter::on_exchange_open_orders() {
return me->callbacks.exchange_open_orders;
}
} // namespace bts::orderman