201 lines
7.4 KiB
C++
201 lines
7.4 KiB
C++
#include "FakeExecutionGateway.h"
|
|
#include "lib/api/services/AdminService.pb.h"
|
|
#include "lib/api/services/OrderService.pb.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
#include "lib/domain_model/common.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/orderman/CancelOrderMsg.h"
|
|
#include "lib/orderman/CancelReplaceOrderMsg.h"
|
|
#include "lib/orderman/CreateOrderMsg.h"
|
|
#include "lib/orderman/OrderMsg.h"
|
|
#include "lib/orderman/events/CancelRejectEvent.h"
|
|
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
|
|
#include "lib/orderman/events/NewAdminDataEvent.h"
|
|
#include "lib/orderman/events/NewExecutionReportEvent.h"
|
|
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
|
#include "lib/orderman/gateways/IExecutionGateway.h"
|
|
#include "lib/orderman/orderman_utils/ExecutionReport.h"
|
|
#include "lib/orderman/orderman_utils/orderman_common.h"
|
|
#include "lib/tracing/bts_trace.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/TimeStamp.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/bts_chrono.h"
|
|
#include "lib/utils/helper_macros.h"
|
|
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
struct FakeExecutionGateway::PImpl {
|
|
PImpl(md::SecurityOracle o)
|
|
: admin_data(), connection_time(), sec_oracle(o) {
|
|
LOG(OrderMan, Info, "FakeExecutionGateway::PImpl constructor");
|
|
}
|
|
|
|
std::unordered_map<SecurityKey::Value, api::Security const *> securities;
|
|
std::unordered_map<int64_t, api::Security const *> securities_by_exchange_id;
|
|
OnExeReport on_exe_report;
|
|
OnGwStatusChange on_gw_status_change;
|
|
OnCancelReject on_cancel_reject;
|
|
orderman::OnExchangePositionState on_exchange_position_state;
|
|
orderman::OnExchangeOpenOrdersState on_exchange_open_orders;
|
|
api::admin::AdminData admin_data;
|
|
TimeStamp connection_time;
|
|
md::SecurityOracle sec_oracle;
|
|
std::vector<OrderMsg> buffered_msgs = {};
|
|
unsigned long exchange_oid = 0;
|
|
std::vector<std::shared_ptr<ExecutionReport>> exe_reports = {};
|
|
IEMOrderMuxer *_order_mux = nullptr;
|
|
};
|
|
|
|
FakeExecutionGateway::FakeExecutionGateway(md::SecurityOracle o)
|
|
: IExecutionGateway(), me(new PImpl(o)) {
|
|
LOG(OrderMan, Info,
|
|
"FakeExecutionGateway::FakeExecutionGateway constructor");
|
|
}
|
|
|
|
FakeExecutionGateway::~FakeExecutionGateway() {}
|
|
|
|
void FakeExecutionGateway::start() {
|
|
LOG(OrderMan, Info, "FakeExecutionGateway::start");
|
|
}
|
|
|
|
// this is a periodic timer
|
|
void FakeExecutionGateway::dispatch_incoming_fix_messages() {
|
|
if (me->buffered_msgs.size() == 0) {
|
|
return;
|
|
}
|
|
std::vector<OrderMsg> aux_msgs = me->buffered_msgs;
|
|
me->buffered_msgs.clear();
|
|
for (OrderMsg m : aux_msgs) {
|
|
if (std::holds_alternative<CreateOrderMsg>(m)) {
|
|
CreateOrderMsg cr_msg = std::get<CreateOrderMsg>(m);
|
|
LOG(OrderMan, Info,
|
|
"FakeExecutionGateway::dispatch_incoming_fix_message key: {} sec: "
|
|
"{} p: {} q: {}",
|
|
cr_msg.key, cr_msg.security->id(), cr_msg.price, cr_msg.quantity);
|
|
me->exe_reports.push_back(std::make_shared<ExecutionReport>());
|
|
std::shared_ptr<ExecutionReport> &er_p = me->exe_reports.back();
|
|
ExecutionReport &er = *er_p;
|
|
std::string account_name = "ACCA1";
|
|
std::array<char, 12> an_array = {};
|
|
size_t i = 0;
|
|
for (const char c : account_name) {
|
|
an_array[i++] = c;
|
|
}
|
|
er.account_name = AccountName(an_array);
|
|
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 = 0;
|
|
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;
|
|
er.exec_type = api::orderman::EXEC_TYPE_NEW;
|
|
er.order_status = api::orderman::NATIVE_ORDER_STATUS_NEW;
|
|
er.manual_order_indicator = api::orderman::MANUAL_ORDER_INDICATOR_NO;
|
|
ExchangeOrderID64 eoid = ExchangeOrderID64(me->exchange_oid++);
|
|
ExchangeOrderIDVar oid = eoid;
|
|
er.exchange_order_id = oid;
|
|
er.price = cr_msg.price;
|
|
er.quantity = cr_msg.quantity;
|
|
er.filled_quantity = Quantity(0);
|
|
er.last_shares = Quantity(0);
|
|
er.msg = "Message Placed";
|
|
er.exchange = api::VENUE_SIM;
|
|
trace::CorID cid = {};
|
|
LOG(OrderMan, Info,
|
|
"FakeExecutionGateway::dispatch_incoming_fix_messages er: {}",
|
|
er.to_string());
|
|
me->on_exe_report(er_p, cid);
|
|
} else if (std::holds_alternative<CancelOrderMsg>(m)) {
|
|
CancelOrderMsg cxl_m = std::get<CancelOrderMsg>(m);
|
|
for (std::shared_ptr<ExecutionReport> &er_p : me->exe_reports) {
|
|
auto &er = *er_p;
|
|
if (er.order_key == cxl_m.key) {
|
|
er.order_status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
|
|
er.msg = "Cancelled";
|
|
me->on_exe_report(er_p, {});
|
|
LOG(OrderMan, Info,
|
|
"FakeExecutionGateway::dispatch_incoming_fix_messages er: "
|
|
"{}",
|
|
er.to_string());
|
|
return;
|
|
}
|
|
}
|
|
} else if (std::holds_alternative<CancelReplaceOrderMsg>(m)) {
|
|
CancelReplaceOrderMsg cxl_r_msg = std::get<CancelReplaceOrderMsg>(m);
|
|
CreateOrderMsg cr_msg = cxl_r_msg.create_fields;
|
|
for (std::shared_ptr<ExecutionReport> &er_p : me->exe_reports) {
|
|
auto &er = *er_p;
|
|
if (er.order_key == cr_msg.key) {
|
|
er.order_status = api::orderman::NATIVE_ORDER_STATUS_REPLACED;
|
|
er.msg = "Replaced";
|
|
er.price = cr_msg.price;
|
|
er.quantity = cr_msg.quantity;
|
|
me->on_exe_report(er_p, {});
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FakeExecutionGateway::on_securities(
|
|
const mercury::Update<NewSecuritiesEvent> &) {
|
|
LOG(OrderMan, Info, "FakeExecutionGateway::on_securities");
|
|
}
|
|
|
|
void FakeExecutionGateway::on_admin_data(
|
|
const mercury::Update<NewAdminDataEvent> &update) {
|
|
LOG(OrderMan, Info, "FakeExecutionGateway::on_admin_data");
|
|
if (update.events) {
|
|
for (auto const &ev : *(update.events))
|
|
me->admin_data.CopyFrom(*ev.admin_data);
|
|
}
|
|
}
|
|
|
|
void FakeExecutionGateway::send_fix(const jetfix::OutFIXPtr &, trace::CorID) {
|
|
// only deals with messages
|
|
bts_assert(false);
|
|
}
|
|
|
|
void FakeExecutionGateway::send_msgs(
|
|
std::vector<OrderMsg> const &msg_vec, trace::CorID) {
|
|
// this is where it will be sent to the exchange
|
|
LOG(OrderMan, Info, "FakeExecutionGateway::send_msgs count: {}",
|
|
msg_vec.size());
|
|
for (auto &m : msg_vec) {
|
|
me->buffered_msgs.emplace_back(m);
|
|
}
|
|
}
|
|
|
|
OnExeReport &FakeExecutionGateway::on_exe_report() { return me->on_exe_report; }
|
|
OnGwStatusChange &FakeExecutionGateway::on_gw_status_change() {
|
|
return me->on_gw_status_change;
|
|
}
|
|
OnCancelReject &FakeExecutionGateway::on_cancel_reject() {
|
|
return me->on_cancel_reject;
|
|
}
|
|
OnExchangePositionState &FakeExecutionGateway::on_exchange_position_state() {
|
|
return me->on_exchange_position_state;
|
|
}
|
|
OnExchangeOpenOrdersState &FakeExecutionGateway::on_exchange_open_orders() {
|
|
return me->on_exchange_open_orders;
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|