CLeaning up

This commit is contained in:
Taha Afzal committed 2023-06-06 18:30:38 +00:00
1 parent 95e9a6e8aa
commit 3078b918d2
10 files changed
+365 -290

No files matched your search

@@ -5,7 +5,6 @@
#include "lib/core/TimeStamp.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/SecurityKey.h"
//#include "lib/domain_model/common.h"
#include "lib/domain_model/common.h"
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
@@ -15,6 +14,7 @@
#include "lib/orderman/OrderMsg.h"
#include "lib/orderman/RFQMsg.h"
#include "lib/orderman/ccapi/CCAPIDeribitOrderMux.h"
#include "lib/orderman/ccapi/ICCAPIOrderMux.h"
#include "lib/orderman/events/NewAdminDataEvent.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
@@ -24,12 +24,9 @@
#include "lib/orderman/orderman_utils/ExecutionReport.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/os/os.h"
//#include "lib/tracing/bts_trace.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/datetime.h"
//#include "lib/utils/helper_macros.h"
#include "lib/orderman/ccapi/ICCAPIOrderMux.h"
#include "lib/api/service_helpers/AdminServiceUtils.h"
#include "lib/config/config.h"
@@ -42,9 +39,8 @@
#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 "lib/orderman/ccapi/CCAPIConnectionConfig.h"
#include <boost/pool/object_pool.hpp>
#include <cmath>
#include <cstdint>
#include <optional>
@@ -84,20 +80,50 @@ api::orderman::NativeOrderStatus native_order_status(const CCAPIOrderState &s) {
return api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNDEFINED;
}
struct SingleOrderMux {
ICCAPIOrderMux *order_mux;
std::string account;
uint32_t account_id;
};
struct InitState {
enum class StateType { Start = 0, Securities = 1, Admin = 2 };
map<StateType, bool> state_map = {
{StateType::Start, false},
{StateType::Securities, false},
{StateType::Admin, false}};
bool set(StateType state) {
bool s0 = inited();
auto it = state_map.find(state);
bts_assert(it != state_map.end());
it->second = true;
bool s1 = inited();
return !s0 && s1;
}
bool inited() const {
for (const auto &kv : state_map)
if (!kv.second)
return false;
return true;
}
};
struct CCAPIExecutionGateway::PImpl {
PImpl(bts::api::Venue venue, md::SecurityOracle o)
: venue(venue), sec_oracle(o) {
LOG(OrderMan, Info, "CCAPIExecutionGateway::PImpl constructor");
}
// TODO: get it from config
std::vector<CCAPIConnectionConfig> configs = {
CCAPIConnectionConfig{CCAPIConnectionConfig::DeribitConnectionConfig{
"ZmIKL6fb", "_FY2PcXYNc8VdlfkZO5c-pkFJQFhn8P9z4_p8YA2fS0", "MAIN"}},
CCAPIConnectionConfig{CCAPIConnectionConfig::DeribitConnectionConfig{
"v1iSzZrY", "fFOX8SBB2ZKj43xoab2f3YV70DDK5064STWK3WOCrK4",
"SMALL"}}};
// callback cashed data
std::vector<CCAPIConnectionConfig> configs = {CCAPIConnectionConfig{
"MAIN", CCAPIConnectionConfig::DeribitConnectionConfig{
"ZmIKL6fb", "_FY2PcXYNc8VdlfkZO5c-pkFJQFhn8P9z4_p8YA2fS0"}}};
// CCAPIConnectionConfig{
// "SMALL",
// CCAPIConnectionConfig::DeribitConnectionConfig{
// "v1iSzZrY", "fFOX8SBB2ZKj43xoab2f3YV70DDK5064STWK3WOCrK4"}}};
const bts::api::Venue venue;
InitState init_state = {};
std::vector<SingleOrderMux> mux_vec = {};
const size_t pos_query_period_ms = 10000;
core::TimeStamp last_pos_query_time = core::TimeStamp::now();
api::admin::AdminData admin_data = {};
@@ -109,15 +135,12 @@ struct CCAPIExecutionGateway::PImpl {
OnExchangePositionState on_exchange_position_state;
OnExchangeOpenOrdersState on_exchange_open_orders;
// orders are sent out and callbacks received via IEMOrderMuxer
ICCAPIOrderMux *order_mux = nullptr;
size_t msg_seq_num = 0;
boost::object_pool<ExecutionReport> exe_report_pool;
std::unordered_map<OrderKey, CreateOrderMsg> order_map = {};
// this is updated as we get exchange positions
ExchangePosition exchange_position;
ExchangeOpenOrders exchange_open_orders;
// periodic query (temporary)
bool inited() const { return order_mux != nullptr; }
};
CCAPIExecutionGateway::CCAPIExecutionGateway(
@@ -134,23 +157,29 @@ CCAPIExecutionGateway::CCAPIExecutionGateway(
CCAPIExecutionGateway::~CCAPIExecutionGateway() {}
void CCAPIExecutionGateway::start() {
_init();
LOG(OrderMan, Info,
"CCAPIExecutionGateway::start t_id: {} "
"t_name: {}",
os::get_thread_id(), os::get_thread_name());
bool just_inited = me->init_state.set(InitState::StateType::Start);
if (just_inited) {
_init();
}
}
// this is a periodic timer
void CCAPIExecutionGateway::dispatch_incoming_fix_messages() {
if (!me->inited())
if (!me->init_state.inited())
return;
me->order_mux->dispatch_incoming_messages();
for (auto &mux : me->mux_vec)
mux.order_mux->dispatch_incoming_messages();
if (core::TimeStamp::now().millis() - me->last_pos_query_time.millis() >=
me->pos_query_period_ms) {
me->last_pos_query_time = core::TimeStamp::now();
me->order_mux->query_positions();
me->order_mux->query_open_orders();
for (auto &mux : me->mux_vec) {
mux.order_mux->query_positions();
mux.order_mux->query_open_orders();
}
}
}
@@ -160,19 +189,24 @@ void CCAPIExecutionGateway::on_securities(
"CCAPIExecutionGateway::on_securities t_id: {} t_name: {} n_secs: {}",
os::get_thread_id(), os::get_thread_name(),
update.events == nullptr ? 0 : update.events->size());
if (!me->inited())
return;
me->order_mux->on_securities(update);
bool just_inited = me->init_state.set(InitState::StateType::Securities);
if (just_inited)
_init();
}
void CCAPIExecutionGateway::on_admin_data(
const mercury::Update<NewAdminDataEvent> &update) {
LOG(OrderMan, Info,
"CCAPIExecutionGateway::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);
"CCAPIExecutionGateway::on_admin_data t_id: {} t_name: {} size: {}",
os::get_thread_id(), os::get_thread_name(),
update.events != nullptr ? update.events->size() : 0);
bool has_data = update.events != nullptr && update.events->size() == 1;
if (!has_data)
return;
me->admin_data.CopyFrom(*(update.events)->at(0).admin_data);
bool just_inited = me->init_state.set(InitState::StateType::Admin);
if (just_inited) {
_init();
}
}
@@ -182,7 +216,7 @@ void CCAPIExecutionGateway::send_fix(const jetfix::OutFIXPtr &, trace::CorID) {
void CCAPIExecutionGateway::send_msgs(
std::vector<OrderMsg> const &msg_vec, trace::CorID) {
if (!me->inited())
if (!me->init_state.inited())
return;
LOG(OrderMan, Info, "CCAPIExecutionGateway::send_msgs count: {}",
msg_vec.size());
@@ -190,31 +224,56 @@ void CCAPIExecutionGateway::send_msgs(
LOG(OrderMan, Info, "CCAPIExecutionGateway::send_msgs {}", m);
if (std::holds_alternative<CreateOrderMsg>(m)) {
const CreateOrderMsg &cr_msg = std::get<CreateOrderMsg>(m);
SingleOrderMux *mux = _find_mux(cr_msg.routing_record);
if (mux == nullptr) {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::send_msgs could not resolve acount for "
"cr: {}",
m);
continue;
}
LOG(OrderMan, Info,
"CCAPIExecutionGateway::send_msgs Create sec: {} order_key",
cr_msg.security->id(), cr_msg.key);
me->order_map.insert({cr_msg.key, cr_msg});
me->order_mux->launch_new_order(cr_msg);
mux->order_mux->launch_new_order(cr_msg);
} else if (std::holds_alternative<CancelReplaceOrderMsg>(m)) {
const CancelReplaceOrderMsg &cxl_repl_msg =
std::get<CancelReplaceOrderMsg>(m);
SingleOrderMux *mux =
_find_mux_from_orderkey(cxl_repl_msg.create_fields.key);
if (mux == nullptr) {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::send_msgs could not resolve acount for "
"cr: {}",
m);
continue;
}
LOG(OrderMan, Info,
"CCAPIExecutionGateway::send_msgs CancelReplace sec: {} "
"order_key: {}",
cxl_repl_msg.create_fields.security->id(),
cxl_repl_msg.create_fields.key);
me->order_mux->update_order(cxl_repl_msg);
mux->order_mux->update_order(cxl_repl_msg);
} else if (std::holds_alternative<CancelOrderMsg>(m)) {
const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(m);
SingleOrderMux *mux = _find_mux_from_orderkey(cxl_msg.key);
if (mux == nullptr) {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::send_msgs could not resolve acount for "
"cr: {}",
m);
continue;
}
LOG(OrderMan, Info,
"CCAPIExecutionGateway::send_msgs Cancel order_key: {}",
cxl_msg.key);
me->order_mux->cancel_order(cxl_msg);
mux->order_mux->cancel_order(cxl_msg);
} else if (std::holds_alternative<RFQMsg>(m)) {
const RFQMsg &rfq_msg = std::get<RFQMsg>(m);
LOG(OrderMan, Info, "CCAPIExecutionGateway::send_msgs RFQ: {}",
utils::pb::to_string(rfq_msg.rfq));
me->order_mux->send_rfq(rfq_msg);
// me->order_mux->send_rfq(rfq_msg);
}
}
}
@@ -240,66 +299,29 @@ OnExchangeOpenOrdersState &CCAPIExecutionGateway::on_exchange_open_orders() {
}
void CCAPIExecutionGateway::_init() {
// TODO: account id
if (me->order_mux != nullptr)
return;
bts_assert(me->init_state.inited());
LOG(OrderMan, Info, "CCAPIExecutionGateway::_init");
me->order_mux = new CCAPIDeribitOrderMux(me->configs.at(0), me->sec_oracle);
me->order_mux->order_update_cb() =
[this](const CCAPIOrderState &order_state) {
_on_order_cb(order_state);
};
me->order_mux
->exchange_position_update_cb() = [this](const std::vector<CCAPIPosition>
&pos_vec) {
LOG(OrderMan, Info, "CCAPIExecutionGateway position cb sz: {}",
pos_vec.size());
ExchangePosition out_pos = {me->venue, {}};
for (const CCAPIPosition &pos : pos_vec) {
auto sec_ptr = _find_sec(pos.venue, pos.exchange_sec_desc);
if (sec_ptr != nullptr) {
out_pos.total_exchange_positions.push_back(
ExchangePosition::UnitPosition{
pos.exchange_sec_desc, sec_ptr, pos.internal_account, 9,
pos.position, Price{pos.average_price}});
} else {
LOG(OrderMan, Error,
"CCAPIExecutionGateway could not resolve exchange: {} desc: {}",
api::Venue_Name(pos.venue), pos.exchange_sec_desc);
}
for (const auto &p : me->configs) {
api::admin::BTSAccount *acct_ptr = admin::AdminServiceUtils::find_account(
me->admin_data, p.internal_account);
if (acct_ptr == nullptr) {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::_init could not resolve account: {}",
p.internal_account);
continue;
}
LOG(OrderMan, Info, "CCAPIExecutionGateway firing positions sz: {}",
out_pos.total_exchange_positions.size());
me->on_exchange_position_state(out_pos);
};
me->order_mux->exchange_open_orders_update_cb() =
[this](const std::vector<CCAPIExchangeOpenOrder> &ord_vec) {
LOG(OrderMan, Info, "CCAPIExecutionGateway working_order cb sz: {}",
ord_vec.size());
ExchangeOpenOrders out_ords = {me->venue, {}};
for (const auto &ord : ord_vec) {
auto sec_ptr = _find_sec(me->venue, ord.exchange_sec_desc);
if (sec_ptr != nullptr) {
out_ords.all_open_orders.push_back(
ExchangeOpenOrders::UnitOrder{
ord.exchange_sec_desc, ord.exchange_account, sec_ptr,
ord.exchange_order_id, ord.account_name, 9, ord.tif,
ord.side, Price{ord.price},
Quantity::create_new(ord.qty), Price{ord.filled_price},
Quantity::create_new(ord.filled_qty),
ord.creation_timestamp, ord.last_update_timestamp,
!ord.web, ord.web});
} else {
LOG(OrderMan, Error,
"CCAPIExecutionGateway order callback could not resolve "
"exchange: {} desc: {}",
me->venue, ord.exchange_sec_desc);
}
}
LOG(OrderMan, Info, "CCAPIExecutionGateway firing orders sz: {}",
out_ords.all_open_orders.size());
me->on_exchange_open_orders(out_ords);
};
CCAPIDeribitOrderMux *mux = new CCAPIDeribitOrderMux(p, me->sec_oracle);
me->mux_vec.push_back({mux, p.internal_account, acct_ptr->id()});
mux->order_update_cb() = [this](const CCAPIOrderState &order_state) {
_on_order_cb(order_state);
};
mux->exchange_position_update_cb() = [this](const auto &pos_vec) {
_on_exchange_positions(pos_vec);
};
mux->exchange_open_orders_update_cb() = [this](const auto &ord_vec) {
_on_exchange_working_orders(ord_vec);
};
}
}
void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
@@ -382,4 +404,88 @@ api::Security const *CCAPIExecutionGateway::_find_sec(
return nullptr;
}
void CCAPIExecutionGateway::_on_exchange_positions(
const std::vector<CCAPIPosition> &pos_vec) {
LOG(OrderMan, Info,
"CCAPIExecutionGateway::_on_exchange_positions cb sz: {}",
pos_vec.size());
ExchangePosition out_pos = {me->venue, {}};
for (const CCAPIPosition &pos : pos_vec) {
auto sec_ptr = _find_sec(pos.venue, pos.exchange_sec_desc);
api::admin::BTSAccount *acct_ptr = admin::AdminServiceUtils::find_account(
me->admin_data, pos.internal_account);
if (sec_ptr != nullptr && acct_ptr != nullptr) {
out_pos.total_exchange_positions.push_back(
ExchangePosition::UnitPosition{
pos.exchange_sec_desc, sec_ptr, pos.internal_account,
acct_ptr->id(), pos.position, Price{pos.average_price}});
} else {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::_on_exchange_positions could not "
"resolve "
"exchange: {} desc: {} account: {} , this position will not be "
"reported",
api::Venue_Name(pos.venue), pos.exchange_sec_desc,
pos.internal_account);
}
}
LOG(OrderMan, Info,
"CCAPIExecutionGateway::_on_exchange_positions firing positions sz: "
"{}",
out_pos.total_exchange_positions.size());
me->on_exchange_position_state(out_pos);
}
void CCAPIExecutionGateway::_on_exchange_working_orders(
const std::vector<CCAPIExchangeOpenOrder> &ord_vec) {
LOG(OrderMan, Info,
"CCAPIExecutionGateway::_on_exchange_working_orders sz: {}",
ord_vec.size());
ExchangeOpenOrders out_ords = {me->venue, {}};
for (const auto &ord : ord_vec) {
auto sec_ptr = _find_sec(me->venue, ord.exchange_sec_desc);
api::admin::BTSAccount *acct_ptr = admin::AdminServiceUtils::find_account(
me->admin_data, ord.account_name);
if (sec_ptr != nullptr && acct_ptr != nullptr) {
out_ords.all_open_orders.push_back(ExchangeOpenOrders::UnitOrder{
ord.exchange_sec_desc, ord.exchange_account, sec_ptr,
ord.exchange_order_id, ord.account_name, acct_ptr->id(), ord.tif,
ord.side, Price{ord.price}, Quantity::create_new(ord.qty),
Price{ord.filled_price}, Quantity::create_new(ord.filled_qty),
ord.creation_timestamp, ord.last_update_timestamp, !ord.web,
ord.web});
} else {
LOG(OrderMan, Error,
"CCAPIExecutionGateway::_on_exchange_working_orders could not "
"resolve exchange: {} desc: {} account: {} this order will not be "
"reported",
me->venue, ord.exchange_sec_desc, ord.account_name);
}
}
LOG(OrderMan, Info,
"CCAPIExecutionGateway::_on_exchange_working_orders firing orders "
"sz: "
"{}",
out_ords.all_open_orders.size());
me->on_exchange_open_orders(out_ords);
}
SingleOrderMux *CCAPIExecutionGateway::_find_mux(
api::orderman::RoutingRecord const *r) {
if (r == nullptr)
return nullptr;
for (auto &m : me->mux_vec) {
if (m.account == r->account())
return &m;
}
return nullptr;
}
SingleOrderMux *CCAPIExecutionGateway::_find_mux_from_orderkey(OrderKey key) {
auto it = me->order_map.find(key);
if (it == me->order_map.end())
return nullptr;
return _find_mux(it->second.routing_record);
}
} // namespace bts::orderman