Adding comments and cleaning SimExecutionGateway
This commit is contained in:
1 parent
db344656e5
commit
ae9366f44a
2 files changed
+107
-114
No files matched your search
@@ -48,6 +48,8 @@
|
||||
|
||||
namespace bts::orderman {
|
||||
|
||||
// book conversions ///////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void to_book(
|
||||
const std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &bids,
|
||||
const std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &asks,
|
||||
@@ -77,30 +79,31 @@ void from_book(
|
||||
copy_side(book.ask(), asks);
|
||||
}
|
||||
|
||||
// Every order send to sim is represented here.
|
||||
struct SEGOrderState {
|
||||
OrderKey ord_key;
|
||||
bts::SecurityKey sec_key;
|
||||
CreateOrderMsg crt_msg;
|
||||
SEGOrderState(const CreateOrderMsg &crt_msg) : crt_msg(crt_msg) {}
|
||||
|
||||
OrderKey ord_key() const { return crt_msg.key; }
|
||||
bts::SecurityKey sec_key() const {
|
||||
return bts::SecurityKey(crt_msg.security->id());
|
||||
}
|
||||
// going out
|
||||
md::IntPrice price;
|
||||
bts::Quantity qty;
|
||||
api::Side side;
|
||||
bts::Price price() const { return crt_msg.price; }
|
||||
bts::Quantity qty() const { return crt_msg.quantity; }
|
||||
api::Side side() const { return crt_msg.side; }
|
||||
// coming in
|
||||
bts::Quantity filled_qty;
|
||||
md::IntPrice avg_filled_price;
|
||||
bts::Quantity last_filled_qty;
|
||||
md::IntPrice last_filled_price;
|
||||
bts::Quantity filled_qty = 0;
|
||||
bts::Price avg_filled_price = bts::Price(os::nan());
|
||||
bts::Quantity last_filled_qty = 0;
|
||||
bts::Price last_filled_price = bts::Price(os::nan());
|
||||
bool active = true;
|
||||
};
|
||||
|
||||
struct SEGBook {
|
||||
struct SEGSecurityData {
|
||||
bts::SecurityKey key;
|
||||
bts::orderman::Book real_book;
|
||||
std::vector<SEGOrderState> orders;
|
||||
// merges the real book with the orders
|
||||
bts::orderman::Book get_matched_book() {
|
||||
bts::orderman::Book rv;
|
||||
return rv;
|
||||
}
|
||||
};
|
||||
|
||||
struct SimExecutionGateway::PImpl {
|
||||
@@ -109,7 +112,7 @@ struct SimExecutionGateway::PImpl {
|
||||
LOG(OrderMan, Info, "SimExecutionGateway::PImpl constructor");
|
||||
}
|
||||
|
||||
std::unordered_map<SecurityKey::Value, api::Security const *> securities;
|
||||
// interface functions
|
||||
OnExeReport on_exe_report;
|
||||
OnGwStatusChange on_gw_status_change;
|
||||
OnCancelReject on_cancel_reject;
|
||||
@@ -120,11 +123,20 @@ struct SimExecutionGateway::PImpl {
|
||||
bts::md::MDClient &md_client;
|
||||
bts::md::MDProducer *md_producer = nullptr;
|
||||
// send_msgs is sent on the order_man thread, its dequeued in the market data
|
||||
// thread, and execution reports are queued to buffered_ers
|
||||
// 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, SEGBook> books = {};
|
||||
std::unordered_map<bts::SecurityKey, SEGSecurityData> books = {};
|
||||
tbb::concurrent_hash_map<uint32_t, api::Security const *> sec_map = {};
|
||||
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(
|
||||
@@ -157,87 +169,6 @@ void SimExecutionGateway::dispatch_incoming_fix_messages() {
|
||||
er.to_string());
|
||||
me->on_exe_report(er, cid);
|
||||
}
|
||||
// if there are nessages, we simply generate an execution report reflecting
|
||||
// the action and send it back via on_exe_report
|
||||
// std::vector<OrderMsg> aux_msgs = me->buffered_msgs;
|
||||
// me->buffered_msgs.clear();
|
||||
// LOG(OrderMan, Info,
|
||||
// "SimExecutionGateway::dispatch_incoming_fix_messages tid: {} tname: "
|
||||
// "{} n_msgs: {}",
|
||||
// bts::os::get_thread_id(), bts::os::get_thread_name(), aux_msgs.size());
|
||||
// for (OrderMsg m : aux_msgs) {
|
||||
// LOG(OrderMan, Diag,
|
||||
// "SimExecutionGateway::dispatch_incoming_fix_messages tid: {} tname: "
|
||||
// "{} m: {}",
|
||||
// bts::os::get_thread_id(), bts::os::get_thread_name(), m);
|
||||
// if (std::holds_alternative<CreateOrderMsg>(m)) {
|
||||
// CreateOrderMsg cr_msg = std::get<CreateOrderMsg>(m);
|
||||
// ExecutionReport er = {};
|
||||
// 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 = {};
|
||||
// me->exe_reports.push_back(er);
|
||||
// const auto &er_pheap = me->exe_reports.back();
|
||||
// LOG(OrderMan, Diag,
|
||||
// "SimExecutionGateway::dispatch_incoming_fix_messages er: {}",
|
||||
// er_pheap.to_string());
|
||||
// me->on_exe_report(er_pheap, cid);
|
||||
// } else if (std::holds_alternative<CancelOrderMsg>(m)) {
|
||||
// CancelOrderMsg cxl_m = std::get<CancelOrderMsg>(m);
|
||||
// for (auto &er : me->exe_reports) {
|
||||
// if (er.order_key == cxl_m.key) {
|
||||
// er.order_status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
|
||||
// er.msg = "Cancelled";
|
||||
// me->on_exe_report(er, {});
|
||||
// LOG(OrderMan, Diag,
|
||||
// "SimExecutionGateway::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 (auto &er : me->exe_reports) {
|
||||
// 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, {});
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void SimExecutionGateway::on_securities(
|
||||
@@ -305,7 +236,7 @@ bool SimExecutionGateway::get_matched_book(
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &bids,
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &asks) {
|
||||
bts::SecurityKey key = k;
|
||||
SEGBook &seg_book = me->books[key];
|
||||
SEGSecurityData &seg_book = me->books[key];
|
||||
to_book(bids, asks, seg_book.real_book);
|
||||
if (seg_book.orders.empty() && me->buffered_order_ops.empty())
|
||||
return false;
|
||||
@@ -332,20 +263,15 @@ bool SimExecutionGateway::get_matched_book(
|
||||
from_book(book, bids, asks);
|
||||
return true;
|
||||
}
|
||||
|
||||
// f(book, existing_order) -> new_book. Also the side affects are
|
||||
//
|
||||
bts::orderman::Book SimExecutionGateway::_process_book_change(
|
||||
const bts::orderman::Book &book, SEGOrderState &seg_os) {
|
||||
tbb::concurrent_hash_map<uint32_t, api::Security const *>::const_accessor
|
||||
accessor;
|
||||
if (!me->sec_map.find(accessor, seg_os.sec_key.value())) {
|
||||
LOG(OrderMan, Error,
|
||||
"SimExecutionGateway::_process_book_change could not find key: {}",
|
||||
seg_os.sec_key.value());
|
||||
}
|
||||
auto sec_ptr = accessor->second;
|
||||
auto sec_ptr = me->find_sec(seg_os.sec_key().value());
|
||||
bts_assert(sec_ptr != nullptr);
|
||||
double tick_sz = sec_ptr->contract_spec().low_tick();
|
||||
auto crosses_opp_side = SpreaderUtils::crosses_book(
|
||||
book, seg_os.side, bts::Price(seg_os.price.price()), tick_sz);
|
||||
book, seg_os.side(), bts::Price(seg_os.price()), tick_sz);
|
||||
if (crosses_opp_side) {
|
||||
return _cross_order(book, seg_os);
|
||||
} else {
|
||||
@@ -354,24 +280,89 @@ bts::orderman::Book SimExecutionGateway::_process_book_change(
|
||||
}
|
||||
|
||||
bts::orderman::Book SimExecutionGateway::_process_new_order(
|
||||
const bts::orderman::Book &book, const CreateOrderMsg &) {
|
||||
return book;
|
||||
const bts::orderman::Book &book, const CreateOrderMsg &crt_ord_msg) {
|
||||
// we can treat this case of an existing order with a book change
|
||||
SEGSecurityData &seg_sec_state =
|
||||
me->books[bts::SecurityKey(crt_ord_msg.security->id())];
|
||||
// this is where a new order is added to a security's state
|
||||
SEGOrderState &seg_ord_state =
|
||||
seg_sec_state.orders.emplace_back(crt_ord_msg);
|
||||
// TODO: generate new order execution report
|
||||
auto rv = _process_book_change(book, seg_ord_state);
|
||||
return rv;
|
||||
}
|
||||
bts::orderman::Book SimExecutionGateway::_process_cxl_order(
|
||||
const bts::orderman::Book &book, const CancelOrderMsg &) {
|
||||
const bts::orderman::Book &book, const CancelOrderMsg &cxl_ord) {
|
||||
// first thing we need to check is if the order is working (it could have
|
||||
// been already filled)
|
||||
if (!_is_working_order(
|
||||
cxl_ord.key, bts::SecurityKey(cxl_ord.security->id()))) {
|
||||
LOG(OrderMan, Warn,
|
||||
"Order ok: {} can not be cancelled because it is no longer a working "
|
||||
"order",
|
||||
cxl_ord.key);
|
||||
return book;
|
||||
}
|
||||
// TODO:
|
||||
// 1. substract the order from the book
|
||||
// 2. generate a cxl execution report
|
||||
return book;
|
||||
}
|
||||
bts::orderman::Book SimExecutionGateway::_process_modify_order(
|
||||
const bts::orderman::Book &book, const CancelReplaceOrderMsg &) {
|
||||
const bts::orderman::Book &book,
|
||||
const CancelReplaceOrderMsg &cxl_repl_ord) {
|
||||
const auto &crt_msg = cxl_repl_ord.create_fields;
|
||||
if (!_is_working_order(
|
||||
crt_msg.key, bts::SecurityKey(crt_msg.security->id()))) {
|
||||
LOG(OrderMan, Warn,
|
||||
"Order ok: {} can not be modified because it is no longer a working "
|
||||
"order",
|
||||
crt_msg.key);
|
||||
return book;
|
||||
}
|
||||
// TODO:
|
||||
// this is the hard one
|
||||
// if qty changed, since this is a resting order just update the book
|
||||
// if price change, check if it crosses the market
|
||||
// if not, make it a resting order
|
||||
// ... etc
|
||||
// can we deal with it as cxl, new order ?
|
||||
return book;
|
||||
}
|
||||
|
||||
// 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 &) {
|
||||
return book;
|
||||
}
|
||||
|
||||
// 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 &) {
|
||||
// iterate the book, find the level and add
|
||||
return book;
|
||||
}
|
||||
|
||||
bool SimExecutionGateway::_is_working_order(
|
||||
OrderKey order_key, SecurityKey security_key) const {
|
||||
auto it = me->books.find(security_key);
|
||||
if (it == me->books.end())
|
||||
return false;
|
||||
for (const auto &o : it->second.orders) {
|
||||
if (o.active && o.ord_key() == order_key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace bts::orderman
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "lib/orderman/CreateOrderMsg.h"
|
||||
#include "lib/orderman/OrderMsg.h"
|
||||
#include "lib/orderman/gateways/IExecutionGateway.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
|
||||
namespace bts::md {
|
||||
class MDClient;
|
||||
@@ -69,6 +70,7 @@ class SimExecutionGateway : public IExecutionGateway {
|
||||
const bts::orderman::Book &book, SEGOrderState &seg_ord);
|
||||
bts::orderman::Book _work_order(
|
||||
const bts::orderman::Book &book, SEGOrderState &seg_ord);
|
||||
bool _is_working_order(OrderKey order_key, SecurityKey security_key) const;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
Reference in new issue
Block a user