Files
spark/src/lib/orderman/gateways/sim/SimExecutionGateway.cpp
T

791 lines
30 KiB
C++

#include "SimExecutionGateway.h"
#include "lib/api/services/AdminService.pb.h"
#include "lib/api/services/ApiUnitTestService.pb.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/core/TimeStamp.h"
#include "lib/domain_model/Security.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/SecurityKey.h"
#include "lib/domain_model/common.h"
#include "lib/logging/logging.h"
#include "lib/md/GeneralSecurityIndex.h"
#include "lib/md/IntPrice.h"
#include "lib/md/MDClient.h"
#include "lib/md/MDProducer.h"
#include "lib/md/PriceSize.h"
#include "lib/md/Shm/MDStructure.h"
#include "lib/orderman/OrderMsg.h"
#include "lib/orderman/events/NewAdminDataEvent.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/orderman_utils/ExecutionReport.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
#include "lib/os/os.h"
#include "lib/tracing/bts_trace.h"
#include "lib/utils/Puid.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/Uuid.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/helper_macros.h"
#include "lib/utils/thread_manager.h"
#include <bits/stdint-uintn.h>
#include <boost/pool/object_pool.hpp>
#include <cstdint>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_queue.h>
#include <unordered_map>
#include <vector>
namespace bts::orderman {
// book conversions ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Copies bid, asks received from market data to the book (they are diff
// sized arrays). Also removes the 0 qtys
void to_book(
const std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &bids,
const std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &asks,
bts::orderman::Book &book) {
auto copy_side =
[](const std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &from,
BookSide &bk_to) {
size_t to_idx = 0;
for (const auto &l : from) {
if (Quantity::create_new(l.size()).is_zero()) {
continue;
}
bk_to[to_idx++] = l;
if (to_idx >= bk_to.size())
break;
}
};
copy_side(bids, book.bid());
copy_side(asks, book.ask());
}
// Opposite of the above
void from_book(
const bts::orderman::Book &book,
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &bids,
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &asks) {
auto copy_side = [](const BookSide &bk_from,
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &to) {
for (size_t i = 0; i < std::min(to.size(), bk_from.size()); i++) {
to[i] = bk_from[i];
}
};
copy_side(book.bid(), bids);
copy_side(book.ask(), asks);
}
bts::md::IntPrice convert_price(Price p) {
return bts::md::IntPrice_from_double<7>(p.value());
}
void log_ord_msg(std::string /*log_prefix*/, const OrderMsg & /*ord_msg*/) {
// auto sleep_period = std::chrono::milliseconds(500);
// std::this_thread::sleep_for(sleep_period);
// LOG(OrderMan, Info, "{} {}", log_prefix, ord_msg);
//std::this_thread::sleep_for(sleep_period);
}
void fill_execution_report(const SEGOrderState &seg_os, ExecutionReport &er);
// This is the result of applying a new OrderMsg
enum class ActionResult { NewOrder, OrderModification, NoOp };
// Every order send to sim is represented here. These are contained as a vector
// in SEGSecurityData
struct SEGOrderState {
CreateOrderMsg crt_msg;
bts::Quantity filled_qty = 0;
bts::Price avg_filled_price = bts::Price(0);
bts::Quantity last_filled_qty = 0;
bts::Price last_filled_price = bts::Price(os::nan());
bts::api::orderman::NativeOrderStatus status =
bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_NEW;
bts::core::TimeStamp last_touched =
bts::core::TimeStamp::from_nanos(bts::core::TimeStamp::now_ns());
SEGOrderState(const CreateOrderMsg &crt_msg) : crt_msg(crt_msg) {}
// immutable getters
OrderKey ord_key() const { return crt_msg.key; }
bts::SecurityKey sec_key() const {
return bts::SecurityKey(crt_msg.security->id());
}
bts::Price price() const { return crt_msg.price; }
bts::Quantity qty() const { return crt_msg.quantity; }
api::Side side() const { return crt_msg.side; }
bts::Quantity working_qty() const {
auto rv = qty() - filled_qty;
rv.make_zero_if_negative();
return rv;
}
bool active() const {
auto int_status = bts::orderman::get_external_status(status);
bool terminal = bts::orderman::in_terminal_state(int_status);
return !terminal;
}
// mutators ////////////////////////////////////////////////////////////////
void apply_fill(Price lf_pr, Quantity lf_qty) {
if (!active()) {
LOG(OrderMan, Error,
"SEGOrderState::apply_fill called on an inactive order");
}
last_filled_price = lf_pr;
last_filled_qty = lf_qty;
avg_filled_price = Price(
(avg_filled_price.value() * filled_qty.full_value() +
lf_pr.value() * lf_qty.full_value()) /
(filled_qty.full_value() + lf_qty.full_value()));
filled_qty =
Quantity::create_new(filled_qty.full_value() + lf_qty.full_value());
status = api::orderman::NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
if (working_qty().is_zero()) {
status = api::orderman::NATIVE_ORDER_STATUS_FILLED;
}
}
ActionResult apply_change(const CancelReplaceOrderMsg &cxl_repl_msg) {
if (!active())
// already in final state, further changes will be rejected
return ActionResult::NoOp;
// if price changes or qty increases, time gets updated
if (cxl_repl_msg.create_fields.quantity > qty() ||
!securities::is_same(price(), cxl_repl_msg.create_fields.price))
last_touched =
bts::core::TimeStamp::from_nanos(bts::core::TimeStamp::now_ns());
crt_msg = cxl_repl_msg.create_fields;
// if qty is reduced, it can result in a cancel
if (working_qty().is_zero()) {
status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
}
return ActionResult::OrderModification;
}
ActionResult apply_cxl(const CancelOrderMsg &) {
if (!active())
return ActionResult::NoOp;
status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
return ActionResult::OrderModification;
}
void clear_after_transaction() {
last_filled_qty = 0;
last_filled_price = Price(os::nan());
}
};
struct SEGTradeEntry {
api::Side side;
Price price;
Quantity qty;
};
// Represents a security and all its data (orders, book, and transient data)
struct SEGSecurityData {
bts::SecurityKey key = SecurityKey{0};
// book as sent by get_matched_book
bts::orderman::Book real_book = {};
// after the orders have been integated into real_book
bts::orderman::Book matched_book = {};
std::vector<SEGOrderState> orders = {};
std::queue<OrderMsg> pending_actions = {};
// these are all the trades that our orders crossed against the real market.
// they are recorded here and then later subtracted from the real book
std::vector<SEGTradeEntry> trades = {};
SEGOrderState *find_order(OrderKey key) {
for (auto &seg_os : orders) {
if (seg_os.ord_key() == key) {
return &seg_os;
}
}
return nullptr;
}
// actions are enqueued and taken care of at the end of the transaction
void enqueue_action(const OrderMsg &ord_msg) {
pending_actions.push(ord_msg);
}
// applies the action (ie new, cxl, mod) and returns the order alongside
// the result of the action
std::tuple<ActionResult, SEGOrderState const *> apply_order_action(
const OrderMsg &msg) {
// create results in a new order entry, otherwise apply_change or
// apply_cxl are called on an existing entry
if (std::holds_alternative<CreateOrderMsg>(msg)) {
const CreateOrderMsg &cr_msg = std::get<CreateOrderMsg>(msg);
auto &it = orders.emplace_back(cr_msg);
return {ActionResult::NewOrder, &it};
} else if (std::holds_alternative<CancelReplaceOrderMsg>(msg)) {
const CancelReplaceOrderMsg &cxlr_msg =
std::get<CancelReplaceOrderMsg>(msg);
SEGOrderState *seg_os = find_order(cxlr_msg.create_fields.key);
if(seg_os == nullptr){
LOG(OrderMan, Info, "SimExecutionGateway::apply_order_action tried updating unkown order");
return {ActionResult::NoOp, nullptr};
}
auto action_result = seg_os->apply_change(cxlr_msg);
return {action_result, seg_os};
} else if (std::holds_alternative<CancelOrderMsg>(msg)) {
const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(msg);
SEGOrderState *seg_os = find_order(cxl_msg.key);
if(seg_os == nullptr){
LOG(OrderMan, Info, "SimExecutionGateway::apply_order_action tried canceling unkown order");
return {ActionResult::NoOp, nullptr};
}
auto action_result = seg_os->apply_cxl(cxl_msg);
return {action_result, seg_os};
}
return {ActionResult::NoOp, nullptr};
}
void clear_after_transaction() {
bts_assert(pending_actions.empty());
for (auto &seg_os : orders) {
seg_os.clear_after_transaction();
}
}
void remove_inactive_orders() {
return;
// orders.erase(
// std::remove_if(
// orders.begin(), orders.end(),
// [](const auto &ord) { return !ord.active(); }),
// orders.end());
}
void record_trade(const SEGTradeEntry &seg_te) { trades.push_back(seg_te); }
};
struct SimExecutionGateway::PImpl {
PImpl(bts::api::Venue venue) : venue(venue) {
LOG(OrderMan, Info, "SimExecutionGateway::PImpl constructor venue: {}",
bts::api::Venue_Name(venue));
}
// interface functions
const bts::api::Venue venue;
const bool perm_reduce_book_by_trades = true;
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;
md::GeneralSecurityIndex *producer_map = nullptr;
// send_msgs is sent on the order_man thread, its dequeued in the market data
// thread, and execution reports are queued to buffered_exec_reps
tbb::concurrent_queue<OrderMsg> buffered_order_ops;
tbb::concurrent_queue<ExecutionReport *> buffered_exec_reps;
std::unordered_map<bts::SecurityKey, SEGSecurityData> books = {};
tbb::concurrent_hash_map<uint32_t, api::Security const *> sec_map = {};
boost::object_pool<ExecutionReport> exe_report_pool;
size_t msg_seq_num = 0;
std::set<SecurityKey> touched_secs;
api::Security const *find_sec(uint32_t key) const {
tbb::concurrent_hash_map<uint32_t, api::Security const *>::const_accessor
accessor;
if (!sec_map.find(accessor, key)) {
return nullptr;
}
return accessor->second;
}
};
SimExecutionGateway::SimExecutionGateway(bts::api::Venue venue)
: IExecutionGateway(), me(new PImpl(venue)) {
LOG(OrderMan, Info,
"SimExecutionGateway::SimExecutionGateway constructor venue: {} tid: {} "
"tname: "
"{}",
bts::api::Venue_Name(venue), bts::os::get_thread_id(),
bts::os::get_thread_name());
}
SimExecutionGateway::~SimExecutionGateway() {}
void SimExecutionGateway::start() {
LOG(OrderMan, Info, "SimExecutionGateway::start venue: {} tid: {} tname: {}",
bts::api::Venue_Name(me->venue), bts::os::get_thread_id(),
bts::os::get_thread_name());
}
// this is a periodic timer, to process the messages we got from the
// exchange and send the resulting execution reports up by calling
// on_exe_report
void SimExecutionGateway::dispatch_incoming_fix_messages() {
if (me->buffered_exec_reps.empty()) {
return;
}
ExecutionReport *er;
while (me->buffered_exec_reps.try_pop(er)) {
trace::CorID cid = {};
LOG(OrderMan, Info,
"ESimExecutionGateway::dispatch_incoming_fix_messages er: {} ",
er->to_string());
me->on_exe_report(*er, cid);
}
}
void SimExecutionGateway::on_securities(
const mercury::Update<NewSecuritiesEvent> &u) {
LOG(OrderMan, Info,
"SimExecutionGateway::on_securities venue: {} n_secs: {} tid: {} tname: "
"{}",
bts::api::Venue_Name(me->venue),
u.events == nullptr ? 0 : u.events->size(), bts::os::get_thread_id(),
bts::os::get_thread_name());
if (u.events == nullptr)
return;
for (const auto &s : *u.events) {
me->sec_map.insert(std::make_pair(s.security->id(), s.security));
}
}
void SimExecutionGateway::on_admin_data(
const mercury::Update<NewAdminDataEvent> &) {
LOG(OrderMan, Info,
"SimExecutionGateway::on_admin_data venue: {} tid: {} tname: {}",
bts::api::Venue_Name(me->venue), bts::os::get_thread_id(),
bts::os::get_thread_name());
}
void SimExecutionGateway::send_fix(const jetfix::OutFIXPtr &p, trace::CorID) {
LOG(OrderMan, Warn, "SimExecutionGateway::send_fix p sz: {}", p->fix.size());
// TODO
//bts_assert(false);
}
int msg_type(const OrderMsg &msg) {
// if (std::holds_alternative<CreateOrderMsg>(msg)) {
// return 1;
// } else if (std::holds_alternative<CancelReplaceOrderMsg>(msg)) {
// return 2;
// } else if (std::holds_alternative<CancelOrderMsg>(msg)) {
// return 3;
// }
return msg.index();
}
// This is where the messages are received from the client. For sim,
// we simply write the operation to a concurrent queue (buffered_order_ops)
// and dequeue in the market data thread (when the market data source needs the matched_books)
// (the resulting execution rerports are written to another conrrent queue)
// which is then dequeued on dispatch_incoming_fix_messages)
void SimExecutionGateway::send_msgs(
std::vector<OrderMsg> const &msg_vec, trace::CorID) {
// this is where it will be sent to the exchange (to simulate we'll just put
// in the buffer and generate execution reports from it in the
// dispatch_incoming_fix_messages call
LOG(OrderMan, Info,
"SimExecutionGateway::send_msgs tid: {} tname: {} count: {}",
bts::os::get_thread_id(), bts::os::get_thread_name(), msg_vec.size());
for (auto &m : msg_vec) {
auto mtype = msg_type(m);
LOG(OrderMan, Info,
"SimExecutionGateway::send_msgs tid: {} tname: {} m_type: {}",
bts::os::get_thread_id(), bts::os::get_thread_name(), mtype);
me->buffered_order_ops.push(m);
}
}
OnExeReport &SimExecutionGateway::on_exe_report() { return me->on_exe_report; }
OnGwStatusChange &SimExecutionGateway::on_gw_status_change() {
std::string tid_name = "SimExecutionGateway::_send_gw_status_once";
bts::threading::make_thread(tid_name, [&]() {
_send_gw_status_once();
}).detach();
return me->on_gw_status_change;
}
OnCancelReject &SimExecutionGateway::on_cancel_reject() {
return me->on_cancel_reject;
}
OnExchangePositionState &SimExecutionGateway::on_exchange_position_state() {
return me->on_exchange_position_state;
}
OnExchangeOpenOrdersState &SimExecutionGateway::on_exchange_open_orders() {
return me->on_exchange_open_orders;
}
void SimExecutionGateway::set_producers(
md::GeneralSecurityIndex *producer_map) {
LOG(OrderMan, Info, "SimExecutionGateway::set_producers exchange: {}",
bts::api::Venue_Name(me->venue));
me->producer_map = producer_map;
}
void SimExecutionGateway::do_work() {}
// this gets called by GeneralMarketDataListener, so this can alter the based on order activity
bool SimExecutionGateway::get_matched_book(
bts::md::SecurityKey k,
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &out_bids,
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &out_asks) {
bts::SecurityKey key = k;
// security entry is added here
SEGSecurityData &seg_sec = *_find_add_security_entry(key, true, true);
// this is where the book is updated
to_book(out_bids, out_asks, seg_sec.real_book);
if (seg_sec.orders.empty() && seg_sec.trades.empty() &&
me->buffered_order_ops.empty())
return false;
// integrate the orders
core::TimeStamp t1 = core::TimeStamp::now();
LOG(OrderMan, Diag,
"SimExecutionGateway::get_matched_book this will result in a book "
"calculation for key: {}",
key.value());
me->touched_secs.insert(key);
// handle buffered operations
OrderMsg ord_msg;
// all ord_msgs are popped and enqueued in the security's SEGSecurityData
while (me->buffered_order_ops.try_pop(ord_msg)) {
// LOG(OrderMan, Info,
// "SimExecutionGateway::get_matched_book ord_msg dequeued from "
// "get_matched_book, ord_msg: {}",
// ord_msg);
std::optional<SecurityKey> op_sec_key = _extract_sec_key(ord_msg);
if (!op_sec_key.has_value()) {
LOG(OrderMan, Error,
"SimExecutionGateway::get_matched_book could not extract security "
"from op: {}",
ord_msg);
continue;
}
// we could have used get_matched_book as the only source
// of adding SEGSecurityData, but the line below adds it too
// so both user operations and get_matched_book trigger
// SEGSecurityData additions
SEGSecurityData &op_seg_sec =
*_find_add_security_entry(*op_sec_key, true, true);
op_seg_sec.enqueue_action(ord_msg);
me->touched_secs.insert(op_seg_sec.key);
}
// this takes care of everything !
_process_transaction();
from_book(seg_sec.matched_book, out_bids, out_asks);
// cleanup
for (SecurityKey key : me->touched_secs) {
SEGSecurityData &t_seg_sec = *_find_add_security_entry(key, false, true);
t_seg_sec.clear_after_transaction();
t_seg_sec.remove_inactive_orders();
}
me->touched_secs.clear();
LOG(OrderMan, Diag,
"SimExecutionGateway::get_matched_book time: {} ns key: {}",
core::TimeStamp::now().nanos() - t1.nanos(), key);
return true;
}
// subtracts all the trades that have happened against the book
// (not self crosses)
bts::orderman::Book reduce_book_by_trades(SEGSecurityData &seg_sec) {
bts::orderman::Book rv = seg_sec.real_book;
for (const auto &tr : seg_sec.trades) {
rv = SpreaderUtils::remove_order(rv, {tr.price, tr.qty});
}
return rv;
}
// this is where all the processing happens
void SimExecutionGateway::_process_transaction() {
// goes through all the actions and applies it to the orders (actions
// take priority)
for (SecurityKey key : me->touched_secs) {
SEGSecurityData &sec_data = *_find_add_security_entry(key, false, true);
while (!sec_data.pending_actions.empty()) {
OrderMsg ord_msg = sec_data.pending_actions.front();
sec_data.pending_actions.pop();
auto [action_res, seg_os] = sec_data.apply_order_action(ord_msg);
// any cxl and mods (and creates) that affected the order, result
// in an er, which is fired here
if (action_res != ActionResult::NoOp) {
_push_execution_report(*seg_os);
}
}
}
// internally cross orders
for (SecurityKey key : me->touched_secs) {
SEGSecurityData &sec_data = *_find_add_security_entry(key, false, true);
_internally_cross_orders(sec_data);
}
// now apply all the orders to the book (match)
for (SecurityKey key : me->touched_secs) {
SEGSecurityData &sec_data = *_find_add_security_entry(key, false, true);
// this is (optionally) done so that the trades that happened in the past permenantly
// have an affect on the book.
bts::orderman::Book matched_bk = me->perm_reduce_book_by_trades
? reduce_book_by_trades(sec_data)
: sec_data.real_book;
// TODO: apply working orders first (or sort it by time)
// (orders should be sorted)
for (SEGOrderState &seg_os : sec_data.orders) {
// if it was cancelled by the actions above,
if (seg_os.active()) {
// trades and execution reports are written along side
matched_bk = _match_order(matched_bk, seg_os);
}
}
sec_data.matched_book = matched_bk;
_write_book(key);
}
}
// this crosses the BTS generated orders agianst each other. this is done first
// before the orders are crossed with the market book
void SimExecutionGateway::_internally_cross_orders(SEGSecurityData &seg_sec) {
auto try_cross_orders = [&](SEGOrderState &o1, SEGOrderState &o2) {
if (o1.ord_key() == o2.ord_key())
return;
if (!o1.active() || !o2.active())
return;
if (o1.side() == o2.side())
return;
bool is_cross =
securities::is_equal_or_better(o1.side(), o1.price(), o2.price());
if (!is_cross)
return;
// TODO, get the best price
auto o1_qty = o1.working_qty();
auto o2_qty = o2.working_qty();
auto crossed_qty = SpreaderUtils::cross_qtys(o1_qty, o2_qty);
// TODO, we are crossing with our own orders first (FIFO not respected)
o1.apply_fill(o1.price(), crossed_qty);
_push_execution_report(o1);
_write_trade(o1, o1.price(), crossed_qty);
o2.apply_fill(o2.price(), crossed_qty);
_push_execution_report(o2);
_write_trade(o2, o2.price(), crossed_qty);
};
// O(n^2), we can index it better and reduce it to O(n)
for (SEGOrderState &o1 : seg_sec.orders) {
for (SEGOrderState &o2 : seg_sec.orders) {
try_cross_orders(o1, o2);
}
}
}
// We take an order and apply it to the book. As
bts::orderman::Book SimExecutionGateway::_match_order(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
if (!seg_os.active())
return book;
double tick_sz = securities::market_epsilon();
auto crosses_opp_side = SpreaderUtils::crosses_book(
book, seg_os.side(), bts::Price(seg_os.price()), tick_sz);
if (crosses_opp_side) {
return _cross_order(book, seg_os);
} else {
return _work_order(book, seg_os);
}
}
// Given an order that is crossing (we know that from the callee), this
// function does 3 things
// 1. Generates a new book
// 2. Adjust SEGOrderState's filled qty, status etc
// 3. Generate execution report
// 4. Generate trades and writes it to the MDProducer
bts::orderman::Book SimExecutionGateway::_cross_order(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
std::vector<std::tuple<Price, Quantity>> fills = {};
auto rv = SpreaderUtils::apply_crossing_order(
book, seg_os.side(), {seg_os.price(), seg_os.working_qty()}, fills);
SEGSecurityData *seg_sec =
_find_add_security_entry(seg_os.sec_key(), false, true);
// this is where we need to do 2-4
for (const auto &[f_pr, f_qty] : fills) {
seg_os.apply_fill(f_pr, f_qty);
_push_execution_report(seg_os);
_write_trade(seg_os, f_pr, f_qty);
seg_sec->record_trade({seg_os.side(), f_pr, f_qty});
}
return rv;
}
// Given an order that becomes a resting order (we know that from the callee),
// this function does the following:
// 1. Generates a new book
// (Unlike the above, SEGOrderState's status does not need changing, neither
// do we need to generate execution reports)
bts::orderman::Book SimExecutionGateway::_work_order(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
const BookSide &bside = book.side(seg_os.side());
Book rv = book;
rv.side(seg_os.side()) = SpreaderUtils::apply_working_order(
bside, seg_os.side(), {seg_os.price(), seg_os.working_qty()});
return rv;
}
// adds the security entry. this is called from get_book or when we
// get a new order (also processed from within get_book)
SEGSecurityData *SimExecutionGateway::_find_add_security_entry(
SecurityKey security_key, bool add_if_absent, bool assert_for_presence) {
auto find_add = [&]() -> SEGSecurityData * {
auto it = me->books.find(security_key);
if (it == me->books.end()) {
if (!add_if_absent) {
return nullptr;
}
LOG(OrderMan, Info,
"SimExecutionGateway::_find_add_security_entry adding entry for "
"sec: "
"{}",
security_key);
me->books.insert({security_key, {security_key}});
it = me->books.find(security_key);
}
return &it->second;
};
SEGSecurityData *rv = find_add();
if (assert_for_presence)
bts_assert(rv != nullptr);
return rv;
}
// fills execution report form SEGOrderState and writes it to
// buffered_exec_reps, which is picked up from the other thread
// and passed over to the order infrastructure
void SimExecutionGateway::_push_execution_report(const SEGOrderState &seg_os) {
ExecutionReport *er_p = me->exe_report_pool.construct();
ExecutionReport &er = *er_p;
fill_execution_report(seg_os, er);
me->buffered_exec_reps.push(er_p);
}
void SimExecutionGateway::_write_book(SecurityKey key) {
SEGSecurityData &sec_data = *_find_add_security_entry(key, false, true);
const Book &book = sec_data.matched_book;
if (me->producer_map->has_security(key.value())) {
auto idx_data = me->producer_map->get_security(key.value());
bts_assert(idx_data != nullptr);
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> bids, asks;
from_book(book, bids, asks);
idx_data->handle.update_direct(utils::TimeStamp::now(), bids, asks);
} else {
LOG(OrderMan, Error,
"SimExecutionGateway::_write_book could not find handle for key: {}",
key);
}
}
void SimExecutionGateway::_write_trade(
const SEGOrderState &seg_ord, Price price, Quantity qty) {
auto key = seg_ord.sec_key().value();
if (me->producer_map->has_security(key)) {
auto idx_data = me->producer_map->get_security(key);
bts_assert(idx_data != nullptr);
auto now = utils::TimeStamp::now();
auto trade_ts =
datetime::ll_from_unix_epoch_ms(core::TimeStamp::now().millis());
idx_data->handle.add_trade(
now, md::PriceSize(convert_price(price), qty.value()), trade_ts,
// TODO
co2::AggressorSide::AGGRESSOR_SIDE_BUY, 0, false);
} else {
LOG(OrderMan, Error,
"SimExecutionGateway::_write_trade could not find handle for key: {}",
key);
}
}
std::tuple<SEGSecurityData const *, SEGOrderState const *>
SimExecutionGateway::_find_order(OrderKey key) const {
for (const auto &kv : me->books) {
for (const auto &ord : kv.second.orders) {
if (ord.ord_key() == key) {
return {&kv.second, &ord};
}
}
}
return std::tuple<SEGSecurityData const *, SEGOrderState const *>(
nullptr, nullptr);
}
void SimExecutionGateway::_send_gw_status_once() {
auto sleep_period = std::chrono::milliseconds(2500);
std::this_thread::sleep_for(sleep_period);
// fire the status and exit the thread
me->on_gw_status_change(true);
}
// this returns the security key from an OrdMsg. The reason it has more code
// than needed is because CancelOrderMsg might not have the security set.
// therefore we need to find the security from the order key
std::optional<SecurityKey> SimExecutionGateway::_extract_sec_key(
const OrderMsg &msg) {
if (std::holds_alternative<CreateOrderMsg>(msg)) {
const CreateOrderMsg &cr_msg = std::get<CreateOrderMsg>(msg);
return SecurityKey(cr_msg.security->id());
} else if (std::holds_alternative<CancelReplaceOrderMsg>(msg)) {
const CancelReplaceOrderMsg &cxlr_msg =
std::get<CancelReplaceOrderMsg>(msg);
return SecurityKey(cxlr_msg.create_fields.security->id());
} else if (std::holds_alternative<CancelOrderMsg>(msg)) {
const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(msg);
if (cxl_msg.security != nullptr) {
return SecurityKey(cxl_msg.security->id());
}
auto [sec, ord] = _find_order(cxl_msg.key);
if (sec == nullptr || ord == nullptr) {
return std::nullopt;
}
return sec->key;
} else if (std::holds_alternative<RFQMsg>(msg)) {
const RFQMsg &rfq_msg = std::get<RFQMsg>(msg);
return SecurityKey(rfq_msg.rfq.security().security_id());
}
return SecurityKey(0);
}
// generates an execution rerport from SEGOrderState
void fill_execution_report(const SEGOrderState &seg_os, ExecutionReport &er) {
const auto &cr_msg = seg_os.crt_msg;
log_ord_msg("fill_execution_report", cr_msg);
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::ExecType::EXEC_TYPE_NEW;
er.order_status = seg_os.status;
er.manual_order_indicator =
api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO;
std::string exch_oid = std::to_string(cr_msg.key.value());
auto &er_exch_oid = std::get<ExchangeOrderID>(er.exchange_order_id).value();
to_array(exch_oid, &er_exch_oid);
er.price.value() = seg_os.price().value();
er.quantity = seg_os.qty();
// fractional qty
// fills
er.avg_price.value() = seg_os.avg_filled_price.value();
er.filled_quantity = seg_os.filled_qty;
er.last_shares = seg_os.last_filled_qty;
er.last_price = seg_os.last_filled_price;
if (!seg_os.last_filled_qty.is_zero()) {
bts::utils::Uuid guid;
std::string fill_id = guid.to_text_string();
to_array(fill_id, &er.exchange_fill_id.value());
er.exec_type = api::orderman::EXEC_TYPE_PARTIAL_FILL;
if (!seg_os.active()) {
er.exec_type = api::orderman::EXEC_TYPE_FILL;
}
}
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());
}
} // namespace bts::orderman