Files
spark/src/lib/orderman/actors/ServiceBridge.cpp
T

460 lines
17 KiB
C++

#include "ServiceBridge.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/service_helpers/PositionUtilities.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/api/services/PositionService.pb.h"
#include "lib/logging/logging.h"
#include "lib/mercury/broker/BrokerProxy.h"
#include "lib/mercury/events/Stream.h"
#include "lib/orderman/OrderRouter.h"
#include "lib/orderman/calculations/ITheoCalculator.h"
#include "lib/orderman/calculations/TheoCalculator.h"
#include "lib/orderman/events/AccountPnLUpdateEvent.h"
#include "lib/orderman/events/ActivateOrderEvent.h"
#include "lib/orderman/events/CancelOrderEvent.h"
#include "lib/orderman/events/DetachOrderEvent.h"
#include "lib/orderman/events/ExcessLiquidityUpdateEvent.h"
#include "lib/orderman/events/ExchangeOpenOrdersEvent.h"
#include "lib/orderman/events/ExchangePositionEvent.h"
#include "lib/orderman/events/FilledPositionEvent.h"
#include "lib/orderman/events/ManuallyBustFillsEvent.h"
#include "lib/orderman/events/NewAdminDataEvent.h"
#include "lib/orderman/events/OrderManagerReadyEvent.h"
#include "lib/orderman/events/OrderOpResultEvent.h"
#include "lib/orderman/events/RFQEvent.h"
#include "lib/orderman/events/StoredFillEvent.h"
#include "lib/orderman/events/StoredOrderEvent.h"
#include "lib/orderman/events/TheoEvent.h"
#include "lib/orderman/events/UpdateOrderEvent.h"
#include "lib/orderman/order_factory/OrderFactory.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/orderman/orders/Order.h"
#include "lib/orderman/securities/MapSecurityDB.h"
#include "lib/tracing/PerfLog.h"
#include "lib/utils/PBUtils.h"
#include <future>
namespace bts {
namespace orderman {
#define _LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "ServiceBridge::" fn ". " __VA_ARGS__)
#define INFO_FN(fn, ...) _LOG_FN(Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) _LOG_FN(Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) _LOG_FN(Error, fn, __VA_ARGS__)
struct ServiceBridge::PImpl {
PImpl(
OnReady const &on_ready,
OnOrders const &on_orders,
OnFills const &on_fills,
OnOrderOpResult const &on_order_op_result,
OnExchangePosition const &on_exchange_position,
OnExchangeOpenOrders const &on_exchange_open_orders,
DynamicOrderRegistry const &dyn_order_registry,
mercury::BrokerProxy &service_call_marshaller,
md::SecurityOracle o)
: on_ready(on_ready), on_orders(on_orders), on_fills(on_fills),
on_order_op_result(on_order_op_result),
on_exchange_position(on_exchange_position),
on_exchange_open_orders(on_exchange_open_orders), have_orders(false),
order_manager_ready(false), sec_db(o), order_router(),
order_factory(sec_db, order_router, dyn_order_registry),
known_orders(), service_call_marshaller(service_call_marshaller) {}
OnReady on_ready;
OnOrders on_orders;
OnFills on_fills;
OnOrderOpResult on_order_op_result;
OnExchangePosition on_exchange_position;
OnExchangeOpenOrders on_exchange_open_orders;
bool have_orders;
bool order_manager_ready;
securities::MapSecurityDB sec_db;
OrderRouter order_router;
OrderFactory order_factory;
mercury::Stream<ActivateOrderEvent> activate_order_pub;
mercury::Stream<UpdateOrderEvent> update_order_pub;
mercury::Stream<CancelOrderEvent> cancel_order_pub;
mercury::Stream<DetachOrderEvent> detach_order_pub;
mercury::Stream<ManuallyBustFillsEvent> manually_bust_fills_pub;
mercury::Stream<RFQEvent> rfq_pub;
mercury::Stream<NewAdminDataEvent> new_admin_data_pub;
mercury::Stream<AccountPnLUpdateEvent> account_pnl_pub;
mercury::Stream<FilledPositionEvent> filled_position_pub;
mercury::Stream<ExcessLiquidityUpdateEvent> excess_liquidity_pub;
mercury::Stream<TheoEvent> theo_pub;
std::unordered_set<OrderKey> known_orders;
std::vector<std::shared_ptr<const api::admin::AdminData>> admin_data_updates;
mercury::BrokerProxy &service_call_marshaller;
std::promise<void> ready;
bts::orderman::analytics::TheoCalculator theo_calculator = {};
bool theos_set = false;
};
ServiceBridge::ServiceBridge(
OnReady const &on_ready,
OnOrders const &on_orders,
OnFills const &on_fills,
OnOrderOpResult const &on_order_op_result,
OnExchangePosition const &on_exchange_position,
OnExchangeOpenOrders const &on_exchange_open_orders,
DynamicOrderRegistry const &dyn_order_registry,
mercury::BrokerProxy &service_call_marshaller,
md::SecurityOracle o)
: me(new PImpl(
on_ready,
on_orders,
on_fills,
on_order_op_result,
on_exchange_position,
on_exchange_open_orders,
dyn_order_registry,
service_call_marshaller,
o)) {
LOG(md, Info, "ServiceBridge cons t_id: {} t_name: {}", os::get_thread_id(),
os::get_thread_name());
}
ServiceBridge::~ServiceBridge() {}
void ServiceBridge::block_until_ready() { me->ready.get_future().wait(); }
void ServiceBridge::init() {
subscribe<NewAdminDataEvent>([this](auto const &u) { this->on(u); });
subscribe<StoredFillEvent>([this](auto const &u) { this->on(u); });
subscribe<StoredOrderEvent>([this](auto const &u) { this->on(u); });
subscribe<OrderManagerReadyEvent>([this](auto const &u) { this->on(u); });
subscribe<OrderOpResultEvent>([this](auto const &u) { this->on(u); });
subscribe<ExchangePositionEvent>(
[this](mercury::Update<ExchangePositionEvent> const &u) {
if (u.events == nullptr) {
LOG(OrderMan, Warn,
"ServiceBridge ExchangePositionEvent callback null events");
return;
}
LOG(OrderMan, Info,
"ServiceBridge ExchangePositionEvent callback sz: {}",
u.events->size());
for (const auto &ev : *(u.events)) {
LOG(OrderMan, Info,
"ServiceBridge received ExchangePositionEvent n_pos: {}",
ev.position.total_exchange_positions.size());
me->on_exchange_position(ev.position);
return;
}
});
subscribe<ExchangeOpenOrdersEvent>([this](mercury::Update<
ExchangeOpenOrdersEvent> const
&u) {
if (u.events == nullptr) {
LOG(OrderMan, Warn,
"ServiceBridge ExchangeOpenOrdersEvent callback null events");
return;
}
LOG(OrderMan, Info,
"ServiceBridge ExchangeOpenOrdersEvent callback sz: {}",
u.events->size());
for (const auto &ev : *(u.events)) {
LOG(OrderMan, Info,
"ServiceBridge received ExchangeOpenOrdersEvent n_open_orders: {}",
ev.open_orders.all_open_orders.size());
me->on_exchange_open_orders(ev.open_orders);
return;
}
});
me->activate_order_pub = make_stream<ActivateOrderEvent>();
me->update_order_pub = make_stream<UpdateOrderEvent>();
me->cancel_order_pub = make_stream<CancelOrderEvent>();
me->detach_order_pub = make_stream<DetachOrderEvent>();
me->manually_bust_fills_pub = make_stream<ManuallyBustFillsEvent>();
me->rfq_pub = make_stream<RFQEvent>();
me->theo_pub = make_stream<TheoEvent>();
me->new_admin_data_pub = make_stream<NewAdminDataEvent>();
me->account_pnl_pub = make_stream<AccountPnLUpdateEvent>();
me->filled_position_pub = make_stream<FilledPositionEvent>();
me->excess_liquidity_pub = make_stream<ExcessLiquidityUpdateEvent>();
me->ready.set_value();
} // namespace orderman
void ServiceBridge::on(mercury::Update<StoredFillEvent> const &update) {
if (!update.events) {
WARN_FN("on StoredFillEvent", "no event data");
return;
}
INFO_FN("on StoredFillEvent", "n_events: {}", update.events->size());
std::vector<api::orderman::Fill> fills;
fills.reserve(update.events->size());
for (auto ev : *(update.events))
fills.push_back(ev.fill);
me->on_fills(fills);
}
void ServiceBridge::on(mercury::Update<StoredOrderEvent> const &update) {
if (!update.events) {
WARN_FN("on StoredOrderEvent", "no event data");
return;
}
INFO_FN("on StoredOrderEvent", "n_events: {}", update.events->size());
bool was_ready = is_ready();
me->have_orders = true;
std::vector<api::orderman::OrderRecord> orders;
orders.reserve(update.events->size());
for (auto ev : *(update.events)) {
me->known_orders.insert(OrderKey(ev.order_record.key()));
orders.push_back(ev.order_record);
}
me->on_orders(orders);
maybe_signal_ready(was_ready);
}
void ServiceBridge::on(mercury::Update<OrderManagerReadyEvent> const &update) {
if (!update.events) {
WARN_FN("on OrderManagerReadyEvent", "no event data");
return;
}
INFO_FN("on OrderManagerReadyEvent", "n_events: {}", update.events->size());
bool was_ready = is_ready();
me->order_manager_ready = true;
maybe_signal_ready(was_ready);
}
void ServiceBridge::on(mercury::Update<NewAdminDataEvent> const &update) {
if (!update.events) {
WARN_FN("on NewAdminDataEvent", "no event data");
return;
}
INFO_FN("on NewAdminDataEvent", "n_events: {}", update.events->size());
for (auto ev : *(update.events)) {
me->order_router.update(*ev.admin_data);
}
}
void ServiceBridge::on(mercury::Update<OrderOpResultEvent> const &update) {
if (!update.events) {
WARN_FN("on OrderOpResultEvent", "no event data");
return;
}
INFO_FN("on OrderOpResultEvent", "n_events: {}", update.events->size());
for (auto ev : *(update.events)) {
me->on_order_op_result(ev);
}
}
void ServiceBridge::create_order(uint32_t rid, api::orderman::Create const &c) {
INFO_FN("create_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
me->service_call_marshaller.schedule_action([this, c, rid]() {
if (is_ready()) {
auto &order =
me->order_factory.create(null_order_key(), c.new_order());
me->known_orders.insert(order.key());
publish(
me->activate_order_pub, rid, order, Price(c.price()),
bts::orderman::get_create_quantity(c));
} else
me->on_order_op_result(OrderOpResultEvent(
null_order_key(), rid,
OrderOpResult(api::orderman::ORDER_ERROR_SERVER_NOT_READY)));
});
}
void ServiceBridge::update_order(uint32_t rid, api::orderman::Update const &c) {
INFO_FN("update_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
me->service_call_marshaller.schedule_action([this, c, rid]() {
auto ok = OrderKey(c.order_key());
if (is_ready()) {
if (me->known_orders.count(ok))
publish(me->update_order_pub, rid, c);
else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_UNKNOWN_ORDER)));
} else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_SERVER_NOT_READY)));
});
}
void ServiceBridge::cancel_order(uint32_t rid, api::orderman::Cancel const &c) {
INFO_FN("cancel_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
me->service_call_marshaller.schedule_action([this, c, rid]() {
auto ok = OrderKey(c.order_key());
if (is_ready()) {
if (me->known_orders.count(ok))
publish(me->cancel_order_pub, rid, ok);
else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_UNKNOWN_ORDER)));
} else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_SERVER_NOT_READY)));
});
}
void ServiceBridge::detach_order(uint32_t rid, api::orderman::Detach const &c) {
INFO_FN("detach_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
me->service_call_marshaller.schedule_action([this, c, rid]() {
auto ok = OrderKey(c.order_key());
if (is_ready()) {
if (me->known_orders.count(ok))
publish(me->detach_order_pub, rid, ok);
else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_UNKNOWN_ORDER)));
} else
me->on_order_op_result(OrderOpResultEvent(
ok, rid,
OrderOpResult(api::orderman::ORDER_ERROR_SERVER_NOT_READY)));
});
}
void ServiceBridge::manually_bust_all_fills(
const api::orderman::ManualBustParameters &c) {
INFO_FN("manually_bust_all_fills", "req: {}", utils::pb::to_string(c));
me->service_call_marshaller.schedule_action(
[this, c]() { publish(me->manually_bust_fills_pub, c.account()); });
}
void ServiceBridge::send_rfq(api::orderman::RFQRequest const &rfq) {
INFO_FN("send_rfq", "req: {}", utils::pb::to_string(rfq));
me->service_call_marshaller.schedule_action(
[this, rfq]() { publish(me->rfq_pub, rfq); });
}
void ServiceBridge::update_admin_data(
std::shared_ptr<const api::admin::AdminData> const &admin) {
me->service_call_marshaller.schedule_action([this, admin]() {
me->admin_data_updates.push_back(admin);
publish(me->new_admin_data_pub, *admin);
});
}
void ServiceBridge::update_account_pnl(
api::position::Pnl_account_dataUpdate const &pnl) {
me->service_call_marshaller.schedule_action([this, pnl]() {
for (auto const &account_pnl : pnl.added()) {
publish(
me->account_pnl_pub, AccountID(account_pnl->account_id()),
account_pnl->day_pnl());
}
for (auto const &account_pnl : pnl.changed()) {
publish(
me->account_pnl_pub,
AccountID(account_pnl.current()->account_id()),
account_pnl.current()->day_pnl());
}
});
}
void ServiceBridge::update_excess_liquidity_report(
api::position::Excess_liquidity_reportUpdate const &elr) {
me->service_call_marshaller.schedule_action([this, elr]() {
for (auto const &elr : elr.added()) {
publish(
me->excess_liquidity_pub, AccountID(elr->account_id()),
elr->excess_liquidity());
}
for (auto const &elr : elr.changed()) {
publish(
me->excess_liquidity_pub, AccountID(elr.current()->account_id()),
elr.current()->excess_liquidity());
}
});
}
// updates the calculator and publishes the event (with a pointer to it)
void ServiceBridge::set_precomputed_theos(
const bts::api::orderman::PrecomputedTheos &precomputed_theos) {
LOG(Gen, Info, "ServiceBridge::update_theos size: {} t_id: {} t_name: {}",
precomputed_theos.theos_size(), os::get_thread_id(),
os::get_thread_name());
me->theo_calculator.set_data(precomputed_theos);
me->service_call_marshaller.schedule_action([this]() {
publish(
me->theo_pub, analytics::ITheoCalculator::Impetus::DATA,
&(me->theo_calculator));
me->theos_set = true;
});
}
void ServiceBridge::do_work() {
LOG(Gen, Diag, "ServiceBridge::do_work t_id: {} t_name: {}",
os::get_thread_id(), os::get_thread_name());
if (!me->theos_set)
return;
me->service_call_marshaller.schedule_action([this]() {
publish(
me->theo_pub, analytics::ITheoCalculator::Impetus::PERIODIC_LATEJOIN,
&(me->theo_calculator));
});
}
std::optional<FilledPositionEvent> make_pos_event(
api::position::PositionUnit const &pu, securities::MapSecurityDB const &o) {
auto sec = o.resolve(SecurityKey(pu.security_id()));
if (sec) {
uint32_t total_long_pos, total_short_pos;
bts::position::PositionUtilities::total_position(
pu, total_long_pos, total_short_pos);
FilledPositionEvent ev{
*sec, AccountID(pu.account_id()), Quantity(total_long_pos),
Quantity(total_short_pos)};
return {ev};
} else
return {};
}
void ServiceBridge::update_position(
api::position::Position_dataUpdate const &pos) {
INFO_FN(
"update_position",
"n_added: {}; n_changed: {}; n_removed: {}; n_total: {}",
pos.added().size(), pos.changed().size(), pos.removed().size(),
pos.all().size());
std::vector<FilledPositionEvent> evs;
for (auto const &pu : pos.added()) {
auto ev = make_pos_event(*pu, me->sec_db);
if (ev.has_value())
evs.push_back(ev.value());
else
LOG(OrderMan, Error,
"Could not update position limiter for unknown security {}. This "
"should not happen",
pu->security_id());
}
for (auto const &pu_change : pos.changed()) {
auto ev = make_pos_event(*pu_change.current(), me->sec_db);
if (ev.has_value())
evs.push_back(ev.value());
else
LOG(OrderMan, Error,
"Could not update position limiter for unknown security {}. This "
"should not happen",
pu_change.current()->security_id());
}
me->service_call_marshaller.schedule_action(
[this, evs]() { publish_batch(me->filled_position_pub, evs); });
}
bool ServiceBridge::is_ready() const {
return me->have_orders && me->order_manager_ready;
}
void ServiceBridge::maybe_signal_ready(bool old_ready) {
INFO_FN(
"maybe_signal_ready", "old_ready: {}; is_ready: {}", old_ready,
is_ready());
if (!old_ready && is_ready())
me->on_ready();
}
} // namespace orderman
} // namespace bts