SimExecutionGateway almost working

This commit is contained in:
Taha Afzal committed 2023-02-23 13:43:09 -05:00
1 parent bed6feae3f
commit 7e5c71f337
19 files changed
+422 -368

No files matched your search

+4
View File
@@ -88,6 +88,10 @@ AlgoWrapperOrder::me->services->subscribe_to_child_updates
AlgoWrapperOrder::on_child_order_update
TaskingEyeAlgo::on_child_change
ORDER ROUTING (JetFix connections)
-------------
EMNativeOrder::SessionServices = OrderServices::get_session_services("deribit_1");
MARKET DATA REPLAY:
------------------
main.cpp ->
+1 -1
View File
@@ -1 +1 @@
set(sources api/analytics/ExpirationDriver.proto;api/analytics/TimeWeightedInterpolatedDriver.proto;api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/core/RegexFormatting.proto;api/domain/CallPut.proto;api/domain/Currency.proto;api/domain/SecurityCriteria.proto;api/domain/SecurityCriteriaClause.proto;api/domain/SecurityExpiration.proto;api/domain/SecurityMetadata.proto;api/domain/SecurityMetadataCollection.proto;api/domain/SecurityMetadataItem.proto;api/domain/SecurityType.proto;api/domain/TickDisplaySettings.proto;api/domain/TickFactors.proto;api/domain/Venue.proto;api/io/URL.proto;api/md/BinanceMarketDataConfig.proto;api/md/BinanceUsMarketDataConfig.proto;api/md/BinanceUsdsFuturesMarketDataConfig.proto;api/md/DeribitMarketDataConfig.proto;api/md/FTXMarketDataConfig.proto;api/md/MarketDataConfig.proto;api/md/MarketDataConfigUtils.cpp;api/md/MarketDataRecordingConfig.proto;api/md/MarketDataReplayConfig.proto;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/service_helpers/PositionUtilities.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/AnalyticsService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/BasePositionService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/MarketDataService.proto;api/services/MoneyFileService.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;communincation/websockets/tests/WebsocketSyncClientTests.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/extensions/extensions.proto;config/invalid/invalid.proto;config/profiles/default/MarketData.conf;config/profiles/default/Ports.conf;config/profiles/default/SecurityMetadataCollection.conf;config/profiles/default/Symbols.conf;config/profiles/default/cme_reference_data.conf;config/profiles/default/edge.conf;config/profiles/default/jetfix.conf;config/profiles/default/keycode.conf;config/profiles/default/logging.conf;config/profiles/default/orderman_cfg.conf;config/profiles/default/orders_fills_cleanup.conf;config/profiles/default/position_cfg.conf;config/profiles/default/span.conf;config/profiles/default/spreader.conf;config/profiles/default/testkeys.conf;config/profiles/default/threads.conf;config/profiles/default/toggles.conf;config/protos/algo_types.proto;config/protos/cme_reference_data.proto;config/protos/exchange_sim_cfg.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/market_data.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/ports.proto;config/protos/position_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spartan.proto;config/protos/spreader.proto;config/protos/subprocess.proto;config/protos/symbols.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/protos/ui.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;core/TimeStamp.cpp;domain/DomainSecurityUtils.cpp;domain/NewOrder.cpp;domain/OrderDefinition.cpp;domain/OrderStatus.cpp;domain/OrderUpdate.cpp;domain/Security.cpp;domain/SecurityCriteria.cpp;domain/SecurityCriteriaClause.cpp;domain/Side.cpp;domain/TimeInForce.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/sod.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;io/JsonReader.cpp;io/QueryParams.cpp;io/URL.cpp;io/http/CurlHandle.cpp;io/http/HTTPClient.cpp;io/http/HTTPHeaders.cpp;io/http/HTTPRequest.cpp;io/http/HTTPResponse.cpp;io/websocket/WebsocketClient.cpp;io/websocket/WebsocketClientConfiguration.cpp;io/websocket/WebsocketError.cpp;io/websocket/WebsocketMessage.cpp;io/websocket/WebsocketSSLClientInternal.cpp;iLine truncated
set(sources api/analytics/ExpirationDriver.proto;api/analytics/TimeWeightedInterpolatedDriver.proto;api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/core/RegexFormatting.proto;api/domain/CallPut.proto;api/domain/Currency.proto;api/domain/SecurityCriteria.proto;api/domain/SecurityCriteriaClause.proto;api/domain/SecurityExpiration.proto;api/domain/SecurityMetadata.proto;api/domain/SecurityMetadataCollection.proto;api/domain/SecurityMetadataItem.proto;api/domain/SecurityType.proto;api/domain/TickDisplaySettings.proto;api/domain/TickFactors.proto;api/domain/Venue.proto;api/io/URL.proto;api/md/BinanceMarketDataConfig.proto;api/md/BinanceUsMarketDataConfig.proto;api/md/BinanceUsdsFuturesMarketDataConfig.proto;api/md/DeribitMarketDataConfig.proto;api/md/FTXMarketDataConfig.proto;api/md/MarketDataConfig.proto;api/md/MarketDataConfigUtils.cpp;api/md/MarketDataRecordingConfig.proto;api/md/MarketDataReplayConfig.proto;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/service_helpers/PositionUtilities.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/AnalyticsService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/BasePositionService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/MarketDataService.proto;api/services/MoneyFileService.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;communincation/websockets/tests/WebsocketSyncClientTests.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/extensions/extensions.proto;config/invalid/invalid.proto;config/profiles/default/MarketData.conf;config/profiles/default/Ports.conf;config/profiles/default/SecurityMetadataCollection.conf;config/profiles/default/Symbols.conf;config/profiles/default/cme_reference_data.conf;config/profiles/default/edge.conf;config/profiles/default/jetfix.conf;config/profiles/default/keycode.conf;config/profiles/default/logging.conf;config/profiles/default/orderman_cfg.conf;config/profiles/default/orders_fills_cleanup.conf;config/profiles/default/position_cfg.conf;config/profiles/default/span.conf;config/profiles/default/spreader.conf;config/profiles/default/testkeys.conf;config/profiles/default/threads.conf;config/profiles/default/toggles.conf;config/protos/algo_types.proto;config/protos/cme_reference_data.proto;config/protos/exchange_sim_cfg.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/market_data.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/ports.proto;config/protos/position_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spartan.proto;config/protos/spreader.proto;config/protos/subprocess.proto;config/protos/symbols.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/protos/ui.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;core/TimeStamp.cpp;domain/DomainSecurityUtils.cpp;domain/NewOrder.cpp;domain/OrderDefinition.cpp;domain/OrderStatus.cpp;domain/OrderUpdate.cpp;domain/Security.cpp;domain/SecurityCriteria.cpp;domain/SecurityCriteriaClause.cpp;domain/Side.cpp;domain/TimeInForce.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/sod.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;io/JsonReader.cpp;io/QueryParams.cpp;io/URL.cpp;io/http/CurlHandle.cpp;io/http/HTTPClient.cpp;io/http/HTTPHeaders.cpp;io/http/HTTPRequest.cpp;io/http/HTTPResponse.cpp;io/websocket/WebsocketClient.cpp;io/websocket/WebsocketClientConfiguration.cpp;io/websocket/WebsocketError.cpp;io/websocket/WebsocketMessage.cpp;io/websocket/WebsocketSSLClientInternal.cpp;iLine truncated
@@ -168,9 +168,17 @@ AdminServiceUtils::resolve_connection(
case api::Venue::VENUE_FTX:
case api::Venue::VENUE_BINANCE:
case api::Venue::VENUE_BINANCE_US:
case api::Venue::VENUE_BINANCE_USDS_FUTURES:
rv = resolve_sim_connection(data, account, sec);
break;
case api::Venue::VENUE_BINANCE_USDS_FUTURES:{
if (account.has_connections())
for (const std::string &conn_name :
account.connections().exchange_connection_names()) {
const bts::api::admin::ExchangeConnection *conn =
find_connection(data, conn_name);
if (conn != nullptr && conn->has_general_connection())
return conn;
}
return nullptr;
}
case api::Venue::VENUE_CBOE:
case api::Venue::VENUE_CBOECFE:
case api::Venue::Venue_INT_MIN_SENTINEL_DO_NOT_USE_:
@@ -223,6 +231,8 @@ void AdminServiceUtils::fill_connections_from_config(
add_connection(admin_data, connection);
break;
default:
connection.mutable_general_connection()->set_exchange(c.venue());
add_connection(admin_data, connection);
break;
}
}
@@ -539,7 +549,7 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(
}
switch (sec.exchange()) {
case api::Venue::VENUE_DERIBIT: {
rv.set_session("sim");
rv.set_session("deribit_1");
rv.set_account(account_ptr->name());
rv.set_operator_id(user_ptr->name());
AdminServiceUtils::ExchangeSubAccountResolver resolver;
+5
View File
@@ -238,6 +238,7 @@ message ExchangeConnection{
CMEConnection cme_connection = 30;
ICEConnection ice_connection = 40;
SIMConnection sim_connection = 50;
GeneralExchangeConnection general_connection = 60;
}
}
@@ -263,6 +264,10 @@ message CMECGWConnection{
message SIMConnection {
}
message GeneralExchangeConnection{
api.Venue exchange = 10;
}
message CMEMSGWConnection{
uint32 port = 10;
string session_and_firm_id = 20;
+1
View File
@@ -55,6 +55,7 @@ struct TimeStamp {
return ns;
#endif
}
static TimeStamp now() { return from_nanos(now_ns()); }
private:
// posix time
+7 -4
View File
@@ -156,13 +156,16 @@ void copy_books(
// interface functions ////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
GeneralMarketDataListener::GeneralMarketDataListener(
GeneralSecurityIndex &index, MDProducer &md_producer, api::Venue /*venue*/)
GeneralSecurityIndex &index, MDProducer &md_producer, api::Venue venue)
: me(std::make_unique<PImpl>(
index,
md_producer,
/*bts::orderman::SimExecutionGatewayFactory::get()->get_getayeway(
venue)*/
nullptr)) {}
bts::orderman::SimExecutionGatewayFactory::get()->get_getayeway(
venue))) {
LOG(md, Info,
"GeneralMarketDataListener::GeneralMarketDataListener venue: {}",
bts::api::Venue_Name(venue));
}
GeneralMarketDataListener::~GeneralMarketDataListener() {}
-76
View File
@@ -1,76 +0,0 @@
#ifdef SPARK_ENABLE_CCAPI
#include "CCMain.h"
#include "lib/api/md/MarketDataConfig.pb.h"
#include "lib/config/config.h"
#include "lib/config/protos/market_data.pb.h"
#include "lib/logging/logging.h"
#include "lib/md/GeneralMarketDataListener.h"
#include "lib/md/GeneralSecurityIndex.h"
#include "lib/md/MDProducer.h"
#include "lib/md/MarketDataGateway.h"
#include "lib/md/MarketDataRegistration.h"
#include "lib/md/MarketDataSubscription.h"
#include "lib/md/SecurityDB.h"
#include "lib/md/SecurityIDRecorder.h"
#include "lib/md/ccApi/CCDemoListener.h"
#include "lib/md/coconut2.h"
#include "lib/md/common.h"
#include "lib/md/protos/co2.pb.h"
#include "lib/os/os.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/proto_helper.h"
#include "lib/utils/thread_manager.h"
#include "server/sparkd/cmds/crypto/DeribitDemoConfiguration.h"
#include <emumba-deribit-connector/deribit_listener.h>
#include "lib/md/GeneralSecurityIndex.h"
#include <boost/filesystem.hpp>
#include <memory>
#include <optional>
#include <set>
using namespace std;
namespace bts::md {
struct CCMain::PImpl {
GeneralSecurityIndex sec_index;
SecurityIDRecorder recorder;
MDProducer producer;
bts::md::MarketDataGateway gw;
PImpl()
: sec_index(), recorder(
{utils::Dispatcher::current(),
[=](ExchangeKey k) {
return SecurityKey(sec_index.get_key(k.value()));
}}),
producer(bts::co2::PLATFORM_SIM, recorder) {}
};
CCMain::CCMain()
: // me(std::make_unique<PImpl>()) {
me(new PImpl()) {
LOG(md, Info, "CCMain::CCMain constructed on t_id: {} t_name: {}",
bts::os::get_thread_id(), bts::os::get_thread_name());
}
void CCMain::send_heartbeats() {}
CCMain::~CCMain() {}
void CCMain::run() {
LOG(md, Info, "Run::run_ccapi t_id:{} t_name:{}", os::get_thread_id(),
os::get_thread_name());
me->gw.run();
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "Exiting run_ccapi" << std::endl;
}
} // namespace bts::md
#endif
-23
View File
@@ -1,23 +0,0 @@
#pragma once
#ifdef SPARK_ENABLE_CCAPI
#include "server/sparkd/md/platforms/IMDPlatform.h"
#include <memory>
namespace bts::md {
class CCMain : public IMDPlatform {
public:
CCMain();
virtual ~CCMain();
// IMDPlatform implementation
void send_heartbeats() override;
void run();
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
} // namespace bts::md
#endif
+1 -1
View File
@@ -123,7 +123,7 @@ void MDReplayer::_replay_file_once(const std::string &file_name) {
void MDReplayer::_replay_message(const bts::co2::RecordedMDMessage &m) {
switch (m.MDRecord_case()) {
case (co2::RecordedMDMessage::MDRecordCase::kSecurity): {
LOG(md, Info, "MDReplayer::_replay_message m: {}",
LOG(md, Diag, "MDReplayer::_replay_message m: {}",
utils::pb::to_string(m));
bts::domain::Security new_sec = m.security();
me->sec_keys.insert(m.security().id());
@@ -1,4 +1,5 @@
#pragma once
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/actors/ContentSubscription.h"
#include "lib/mercury/events/Stream.h"
@@ -35,6 +36,7 @@
#include "lib/orderman/limiter/Limiter.h"
#include "lib/orderman/order_factory/OrderFactory.h"
#include "lib/orderman/securities/MapSecurityDB.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/Puid.h"
#include "lib/utils/datetime.h"
@@ -60,6 +62,8 @@ struct SessionServicesData {
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
gw(ExecutionGatewayFactory::create_gateway(cfg, log, o)) {
buffered_order_msgs.reserve(1000);
LOG(OrderMan, Info, "SessionServicesData::SessionServicesData cfg: {}",
utils::pb::to_string(cfg));
}
std::string session;
@@ -3,10 +3,10 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/jetfix/common/OutFIX.h"
#include "lib/logging/logging.h"
#include "lib/orderman/Governor.h"
#include "lib/orderman/actors/order_manager/OrderManager.h"
#include "lib/orderman/actors/order_manager/OrderServicesData.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/orderman/Governor.h"
#include "lib/orderman/orderman_utils/EventStream.h"
#include "lib/orderman/orders/Order.h"
@@ -78,6 +78,8 @@ class SessionServices {
template <class OP>
void queue_order_msg(const OP &order_op, trace::CorID const &corid) {
LOG(OrderMan, Info, "SessionServices::queue_order_msg order_op: {}",
order_op);
auto &buffer = _session_data->buffered_order_msgs;
buffer.emplace_back(OrderMsg{order_op});
if (buffer.size() == 1)
@@ -107,7 +109,8 @@ class SessionServices {
_cancel_rejects.unsub(sid);
}
template <class CB> void subscribe_to_gateway_status(CB &&cb) {
template <class CB>
void subscribe_to_gateway_status(CB &&cb) {
_gateway_status_sub = _gateway_status.sub(std::forward<CB>(cb));
}
void unsubscribe_from_gateway_status() {
@@ -2,6 +2,8 @@
#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"
@@ -10,6 +12,8 @@
#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/CancelOrderMsg.h"
#include "lib/orderman/CancelReplaceOrderMsg.h"
#include "lib/orderman/CreateOrderMsg.h"
@@ -37,10 +41,12 @@
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
#include <algorithm>
#include <array>
#include <atomic>
#include <bits/stdint-uintn.h>
#include <boost/pool/object_pool.hpp>
#include <cstdint>
#include <optional>
#include <queue>
#include <set>
#include <string>
@@ -83,9 +89,22 @@ void from_book(
copy_side(book.bid(), bids);
copy_side(book.ask(), asks);
}
void fill_execution_report(const SEGOrderState &seg_os, ExecutionReport &er);
SecurityKey get_sec_key(const OrderMsg &msg);
bts::md::IntPrice convert_price(Price p) {
return bts::md::IntPrice_from_double<7>(p.value());
}
bts::md::SecurityIndexData *find_handle(
bts::md::GeneralSecurityIndex &idx_map, SecurityKey sec_key) {
return idx_map.get_security(sec_key.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);
enum class ActionResult { NewOrder, OrderModification, NoOp };
// Every order send to sim is represented here.
struct SEGOrderState {
CreateOrderMsg crt_msg;
@@ -106,13 +125,16 @@ struct SEGOrderState {
bts::Price last_filled_price = bts::Price(os::nan());
//bool active = true;
bts::Quantity working_qty() const { return qty() - filled_qty; }
std::vector<std::tuple<Price, Quantity>> trades;
bts::api::orderman::NativeOrderStatus status =
bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_NEW;
void add_fill(Price lf_pr, Quantity lf_qty) {
trades.push_back({lf_pr, lf_qty});
}
bts::core::TimeStamp last_touched =
bts::core::TimeStamp::from_nanos(bts::core::TimeStamp::now_ns());
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(
@@ -124,42 +146,44 @@ struct SEGOrderState {
status = api::orderman::NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
if (filled_qty >= qty()) {
status = api::orderman::NATIVE_ORDER_STATUS_FILLED;
//active = false;
}
}
void apply_change(const CancelReplaceOrderMsg &cxl_repl_msg) {
if (status == api::orderman::NATIVE_ORDER_STATUS_CANCELED)
ActionResult apply_change(const CancelReplaceOrderMsg &cxl_repl_msg) {
last_touched =
bts::core::TimeStamp::from_nanos(bts::core::TimeStamp::now_ns());
if (!active())
// already cancelled, further changes will be rejected
return;
return ActionResult::NoOp;
crt_msg = cxl_repl_msg.create_fields;
// if qty is reduced, it can result in a cancel
if (working_qty() <= Quantity::create_new(0.0)) {
status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
}
return ActionResult::OrderModification;
}
void apply_cxl(const CancelOrderMsg &) {
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());
trades.clear();
}
bool active() const {
bool final_state =
status == api::orderman::NATIVE_ORDER_STATUS_CANCELED ||
status ==
api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_FILLED;
return !final_state;
auto int_status = bts::orderman::get_external_status(status);
bool terminal = bts::orderman::in_terminal_state(int_status);
return !terminal;
}
}; // namespace bts::orderman
struct SEGSecurityData {
bts::SecurityKey key;
bts::orderman::Book real_book;
std::map<OrderKey, SEGOrderState> previous_snapshot;
std::vector<SEGOrderState> orders;
std::queue<OrderMsg> pending_actions;
bts::SecurityKey key = SecurityKey{0};
bts::orderman::Book real_book = {};
bts::orderman::Book matched_book = {};
std::vector<SEGOrderState> orders = {};
std::queue<OrderMsg> pending_actions = {};
SEGOrderState *find_order(OrderKey key) {
for (auto &seg_os : orders) {
if (seg_os.ord_key() == key) {
@@ -170,48 +194,59 @@ struct SEGSecurityData {
}
void enqueue_action(const OrderMsg &ord_msg) {
pending_actions.push(ord_msg);
log_ord_msg("SEGSecurityData::enqueue_action", ord_msg);
}
void take_snapshot() {
previous_snapshot.clear();
for (const auto &seg_os : orders) {
previous_snapshot.insert({seg_os.ord_key(), seg_os});
}
}
void apply_order_action(const OrderMsg &msg) {
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);
orders.emplace_back(cr_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);
bts_assert(seg_os != nullptr);
seg_os->apply_change(cxlr_msg);
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);
bts_assert(seg_os != nullptr);
seg_os->apply_cxl(cxl_msg);
auto action_result = seg_os->apply_cxl(cxl_msg);
return {action_result, seg_os};
}
return {ActionResult::NoOp, nullptr};
}
void clear_after_transaction() {
previous_snapshot.clear();
bts_assert(pending_actions.empty());
for (auto &seg_os : orders) {
seg_os.clear_after_transaction();
}
}
void remove_inactive_orders() {
orders.erase(
std::remove_if(
orders.begin(), orders.end(),
[](const auto &ord) { return !ord.active(); }),
orders.end());
}
};
struct SimExecutionGateway::PImpl {
PImpl(md::SecurityOracle o, bts::md::MDClient &md_client)
: admin_data(), sec_oracle(o), md_client(md_client) {
LOG(OrderMan, Info, "SimExecutionGateway::PImpl constructor");
PImpl(
bts::api::Venue venue,
md::SecurityOracle o,
bts::md::MDClient &md_client)
: venue(venue), admin_data(), sec_oracle(o), md_client(md_client) {
LOG(OrderMan, Info, "SimExecutionGateway::PImpl constructor venue: {}",
bts::api::Venue_Name(venue));
}
// interface functions
bts::api::Venue venue;
OnExeReport on_exe_report;
OnGwStatusChange on_gw_status_change;
OnCancelReject on_cancel_reject;
@@ -225,12 +260,12 @@ struct SimExecutionGateway::PImpl {
// 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;
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> cached_action_secs;
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;
@@ -242,19 +277,22 @@ struct SimExecutionGateway::PImpl {
};
SimExecutionGateway::SimExecutionGateway(
md::SecurityOracle o, bts::md::MDClient &md_client)
: IExecutionGateway(), me(new PImpl(o, md_client)) {
bts::api::Venue venue, md::SecurityOracle o, bts::md::MDClient &md_client)
: IExecutionGateway(), me(new PImpl(venue, o, md_client)) {
LOG(OrderMan, Info,
"SimExecutionGateway::SimExecutionGateway constructor tid: {} tname: "
"SimExecutionGateway::SimExecutionGateway constructor venue: {} tid: {} "
"tname: "
"{}",
bts::os::get_thread_id(), bts::os::get_thread_name());
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 tid: {} tname: {}",
bts::os::get_thread_id(), bts::os::get_thread_name());
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
@@ -264,20 +302,26 @@ void SimExecutionGateway::dispatch_incoming_fix_messages() {
if (me->buffered_exec_reps.empty()) {
return;
}
ExecutionReport er;
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);
er->to_string());
me->on_exe_report(*er, cid);
}
}
void SimExecutionGateway::on_securities(
const mercury::Update<NewSecuritiesEvent> &u) {
LOG(OrderMan, Info, "SimExecutionGateway::on_securities tid: {} tname: {}",
bts::os::get_thread_id(), bts::os::get_thread_name());
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));
}
@@ -285,8 +329,10 @@ void SimExecutionGateway::on_securities(
void SimExecutionGateway::on_admin_data(
const mercury::Update<NewAdminDataEvent> &update) {
LOG(OrderMan, Info, "SimExecutionGateway::on_admin_data tid: {} tname: {}",
bts::os::get_thread_id(), bts::os::get_thread_name());
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());
if (update.events) {
for (auto const &ev : *(update.events))
me->admin_data.CopyFrom(*ev.admin_data);
@@ -307,7 +353,7 @@ void SimExecutionGateway::send_msgs(
"SimExecutionGateway::send_msgs tid: {} tname: {} count: {}",
bts::os::get_thread_id(), bts::os::get_thread_name(), msg_vec.size());
for (auto &m : msg_vec) {
LOG(OrderMan, Diag,
LOG(OrderMan, Info,
"SimExecutionGateway::send_msgs tid: {} tname: {} m: {}",
bts::os::get_thread_id(), bts::os::get_thread_name(), m);
me->buffered_order_ops.push(m);
@@ -330,6 +376,8 @@ OnExchangeOpenOrdersState &SimExecutionGateway::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;
}
@@ -340,91 +388,91 @@ 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;
SEGSecurityData &seg_book = me->books[key];
to_book(bids, asks, seg_book.real_book);
if (seg_book.orders.empty() && me->buffered_order_ops.empty())
SEGSecurityData &seg_sec = *_find_add_security_entry(key, true, true);
// this is where the book is updated
to_book(bids, asks, seg_sec.real_book);
if (seg_sec.orders.empty() && me->buffered_order_ops.empty())
return false;
// integrate the orders
me->cached_action_secs.insert(key);
bts::orderman::Book book = seg_book.real_book;
for (SEGOrderState &seg_os : seg_book.orders) {
book = _process_book_change(book, seg_os);
}
core::TimeStamp t1 = core::TimeStamp::now();
LOG(OrderMan, Info,
"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)) {
auto sec_data = _find_security(get_sec_key(ord_msg));
if (sec_data != nullptr) {
sec_data->enqueue_action(ord_msg);
me->cached_action_secs.insert(sec_data->key);
log_ord_msg("Right after popping from buffered_order_ops", 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;
}
// if (std::holds_alternative<CreateOrderMsg>(ord_msg)) {
// const CreateOrderMsg &cr_msg = std::get<CreateOrderMsg>(ord_msg);
// _process_new_order(book, cr_msg);
// } else if (std::holds_alternative<CancelOrderMsg>(ord_msg)) {
// const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(ord_msg);
// _process_cxl_order(book, cxl_msg);
// } else if (std::holds_alternative<CancelReplaceOrderMsg>(ord_msg)) {
// const CancelReplaceOrderMsg &cxl_repl_msg =
// std::get<CancelReplaceOrderMsg>(ord_msg);
// _process_modify_order(book, cxl_repl_msg);
// }
// 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);
}
_process_transaction();
from_book(book, bids, asks);
from_book(seg_sec.matched_book, bids, 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();
core::TimeStamp t2 = core::TimeStamp::now();
auto time_diff = t2.nanos() - t1.nanos();
LOG(OrderMan, Info, "SimExecutionGateway::get_matched_book time: {} key: {}",
time_diff, key);
return true;
}
void SimExecutionGateway::_process_transaction() {
// goes through all the actions and applies it to the orders
for (SecurityKey key : me->cached_action_secs) {
SEGSecurityData *sec_data_ptr = _find_security(key);
bts_assert(sec_data_ptr != nullptr);
SEGSecurityData &sec_data = *sec_data_ptr;
sec_data.take_snapshot();
auto &pending_actions = sec_data.pending_actions;
while (!pending_actions.empty()) {
// 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();
pending_actions.pop();
sec_data.apply_order_action(ord_msg);
sec_data.pending_actions.pop();
log_ord_msg(
"Right after dequeueing from SEGSecurityData's pending_actions",
ord_msg);
auto [action_res, seg_os] = sec_data.apply_order_action(ord_msg);
log_ord_msg(
"Right after sec_data.apply_order_action", seg_os->crt_msg);
// any cxl and mods that affected the order, result in an er,
// which is fired here
if (action_res != ActionResult::NoOp) {
_push_execution_report(*seg_os);
}
}
}
// now apply all the orders to the book
for (SecurityKey key : me->cached_action_secs) {
SEGSecurityData &sec_data = *_find_security(key);
for (SecurityKey key : me->touched_secs) {
SEGSecurityData &sec_data = *_find_add_security_entry(key, false, true);
Book sec_bk = sec_data.real_book;
// TODO: apply working orders first (or sort it by time)
for (SEGOrderState &seg_os : sec_data.orders) {
_match_order(sec_bk, seg_os);
// if it was cancelled by the actions above,
if (seg_os.active()) {
// trades and execution reports are written along side
sec_bk = _match_order(sec_bk, seg_os);
}
}
}
// now fire execution reports (and write trades)
for (SecurityKey key : me->cached_action_secs) {
SEGSecurityData &sec_data = *_find_security(key);
for (const SEGOrderState &seg_os : sec_data.orders) {
const auto it = sec_data.previous_snapshot.find(seg_os.ord_key());
SEGOrderState const *prev =
it == sec_data.previous_snapshot.end() ? nullptr : &it->second;
_compute_and_fire_execution_report(prev, &seg_os);
}
}
for (SecurityKey key : me->cached_action_secs) {
SEGSecurityData *sec_data_ptr = _find_security(key);
sec_data_ptr->clear_after_transaction();
}
}
// f(book, existing_order) -> new_book. Also the side affects
// could be execution reports and trades
bts::orderman::Book SimExecutionGateway::_process_book_change(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
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()), tick_sz);
if (crosses_opp_side) {
return _cross_order(book, seg_os);
} else {
return _work_order(book, seg_os);
sec_data.matched_book = sec_bk;
_write_book(key);
}
}
@@ -433,9 +481,7 @@ bts::orderman::Book SimExecutionGateway::_match_order(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
if (!seg_os.active())
return book;
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();
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) {
@@ -445,65 +491,6 @@ bts::orderman::Book SimExecutionGateway::_match_order(
}
}
// bts::orderman::Book SimExecutionGateway::_process_new_order(
// 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);
// _push_execution_report(seg_ord_state);
// 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 &cxl_ord) {
// SEGOrderState *ord_ptr =
// _find_order(cxl_ord.key, bts::SecurityKey(cxl_ord.security->id()));
// if (ord_ptr == nullptr || !ord_ptr->active) {
// LOG(OrderMan, Warn,
// "Order ok: {} can not be cancelled because it is no longer a "
// "working "
// "order",
// cxl_ord.key);
// return book;
// }
// ord_ptr->last_filled_qty = 0;
// ord_ptr->status =
// bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_CANCELED;
// ord_ptr->active = false;
// _push_execution_report(*ord_ptr);
// BookSide bside = SpreaderUtils::cxl_working_order(
// book.side(ord_ptr->side()), {ord_ptr->price(), ord_ptr->working_qty()});
// // 2. generate a cxl execution report
// Book rv = book;
// rv.side(ord_ptr->side()) = bside;
// return rv;
// }
// bts::orderman::Book SimExecutionGateway::_process_modify_order(
// 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
@@ -513,14 +500,17 @@ bts::orderman::Book SimExecutionGateway::_match_order(
bts::orderman::Book SimExecutionGateway::_cross_order(
const bts::orderman::Book &book, SEGOrderState &seg_os) {
std::vector<std::tuple<Price, Quantity>> fills = {};
SpreaderUtils::apply_crossing_order(
auto rv = SpreaderUtils::apply_crossing_order(
book, seg_os.side(), {seg_os.price(), seg_os.working_qty()}, fills);
// TODO: if one (or more) of our orders was a passive order in this fill
// it needs to be accounted for
// this is where we need to do 2-4
for (const auto &[f_pr, f_qty] : fills) {
seg_os.add_fill(f_pr, f_qty);
// 3, 4 are done later
seg_os.apply_fill(f_pr, f_qty);
_push_execution_report(seg_os);
_write_trade(seg_os, f_pr, f_qty);
}
return book;
return rv;
}
// Given an order that becomes a resting order (we know that from the callee),
@@ -537,19 +527,6 @@ bts::orderman::Book SimExecutionGateway::_work_order(
return rv;
}
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;
}
SEGOrderState *SimExecutionGateway::_find_order(
OrderKey order_key, SecurityKey security_key) {
auto it = me->books.find(security_key);
@@ -563,29 +540,115 @@ SEGOrderState *SimExecutionGateway::_find_order(
return nullptr;
}
SEGSecurityData *SimExecutionGateway::_find_security(SecurityKey sec_key) {
auto it = me->books.find(sec_key);
return it == me->books.end() ? nullptr : &it->second;
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;
}
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);
me->buffered_exec_reps.push(er_p);
}
// it looks at the now vs prev and computes all the execution reports
void SimExecutionGateway::_compute_and_fire_execution_report(
SEGOrderState const *prev, SEGOrderState const *now) {
bts_assert(now != nullptr);
if (prev == nullptr) {
_push_execution_report(*now);
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);
}
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);
}
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;
@@ -623,22 +686,4 @@ void fill_execution_report(const SEGOrderState &seg_os, ExecutionReport &er) {
"EMExecutionGateway::_process_cb_and_fire_er firing er: {}",
er.to_string());
}
SecurityKey get_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);
return SecurityKey(cxl_msg.security->id());
} 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);
}
} // namespace bts::orderman
@@ -2,6 +2,7 @@
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/SecurityKey.h"
#include "lib/domain_model/common.h"
#include "lib/md/GeneralSecurityIndex.h"
#include "lib/md/MDClient.h"
#include "lib/md/MDProducer.h"
@@ -13,6 +14,7 @@
#include "lib/orderman/OrderMsg.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include <tuple>
namespace bts::md {
class MDClient;
@@ -33,9 +35,10 @@ struct SEGSecurityData;
class SimExecutionGateway : public IExecutionGateway {
public:
SimExecutionGateway(md::SecurityOracle, bts::md::MDClient &md_client);
SimExecutionGateway(
bts::api::Venue venue, md::SecurityOracle, bts::md::MDClient &md_client);
virtual ~SimExecutionGateway();
// IExecutionGateway
void start() override;
void dispatch_incoming_fix_messages() override;
void on_securities(const mercury::Update<NewSecuritiesEvent> &) override;
@@ -49,6 +52,7 @@ class SimExecutionGateway : public IExecutionGateway {
OnExchangePositionState &on_exchange_position_state() override;
OnExchangeOpenOrdersState &on_exchange_open_orders() override;
// SimExecutionGateway
void set_producers(md::GeneralSecurityIndex *producer_map);
void do_work();
bool get_matched_book(
@@ -57,29 +61,22 @@ class SimExecutionGateway : public IExecutionGateway {
std::array<md::PriceSize, md::COCONUT_MAX_DEPTH> &asks);
private:
bts::orderman::Book _process_book_change(
const bts::orderman::Book &book, SEGOrderState &seg_ord);
bts::orderman::Book _match_order(
const bts::orderman::Book &book, SEGOrderState &seg_ord);
// bts::orderman::Book _process_new_order(
// const bts::orderman::Book &book, const CreateOrderMsg &cr_ord);
// bts::orderman::Book _process_cxl_order(
// const bts::orderman::Book &book, const CancelOrderMsg &cxl_ord);
// bts::orderman::Book _process_modify_order(
// const bts::orderman::Book &book,
// const CancelReplaceOrderMsg &cxl_repl_ord);
bts::orderman::Book _cross_order(
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;
SEGOrderState *_find_order(OrderKey order_key, SecurityKey security_key);
SEGSecurityData *_find_security(SecurityKey security_key);
SEGSecurityData *_find_add_security_entry(
SecurityKey security_key, bool add_if_absent, bool assert_for_presence);
void _push_execution_report(const SEGOrderState &seg_os);
void _process_transaction();
void _compute_and_fire_execution_report(
SEGOrderState const *prev, SEGOrderState const *now);
void _write_book(SecurityKey);
void _write_trade(const SEGOrderState &seg_ord, Price price, Quantity qty);
std::tuple<SEGSecurityData const *, SEGOrderState const *> _find_order(
OrderKey key) const;
std::optional<SecurityKey> _extract_sec_key(const OrderMsg &msg);
private:
struct PImpl;
@@ -6,6 +6,7 @@
#include "lib/md/common.h"
#include "lib/utils/LockBox.h"
#include "lib/utils/bts_assert.h"
#include <chrono>
#include <memory>
#include <mutex>
@@ -24,6 +25,8 @@ SimExecutionGatewayFactory::SimExecutionGatewayFactory(
std::shared_ptr<SimExecutionGateway> SimExecutionGatewayFactory::get_getayeway(
api::Venue venue) {
LOG(OrderMan, Info, "SimExecutionGatewayFactory::get_getayeway venue: {}",
bts::api::Venue_Name(venue));
// This needs to look at config, to check if an exchange is set to sim or
// not. If it is not to set to sim, it should return a nullptr
std::shared_ptr<SimExecutionGateway> rv = nullptr;
@@ -34,7 +37,7 @@ std::shared_ptr<SimExecutionGateway> SimExecutionGatewayFactory::get_getayeway(
if (it == data.end()) {
data.insert(
{venue, std::make_shared<SimExecutionGateway>(
md_client.oracle(), md_client)});
venue, md_client.oracle(), md_client)});
it = data.find(venue);
}
rv = it->second;
@@ -57,6 +60,10 @@ void SimExecutionGatewayFactory::init_once(bts::md::MDClient &md_client) {
});
}
SimExecutionGatewayFactory *SimExecutionGatewayFactory::get() {
auto sleep_period = std::chrono::milliseconds(100);
while (instance == nullptr) {
std::this_thread::sleep_for(sleep_period);
}
bts_assert(instance != nullptr);
return instance;
}
+1 -1
View File
@@ -505,7 +505,7 @@ OrderOpResult EMNativeOrder::_on_activate(
me->session_services = &me->services->get_session_services(
get_native_state(me->state).routing_record().session());
// TODO: just to get going
me->session_services = &me->services->get_session_services("sim");
me->session_services = &me->services->get_session_services("deribit_1");
// Checks for the following:
// 1. Gateway connection
@@ -201,10 +201,16 @@ void SpreaderUtils::transform_spread_price_mults(api::Security &sec) {
SpreaderUtils::PriceSearchResult SpreaderUtils::find_working_order_level(
const BookSide &in_book, api::Side ord_side, bts::Price ord_price) {
for (size_t i = 0; i < in_book.size(); i++) {
Price l_price = Price{in_book.at(i).price()};
if (securities::is_same(ord_price, l_price)) {
const md::PriceSize &ps_lev = in_book.at(i);
Price l_price = Price{ps_lev.price()};
Quantity l_qty = Quantity::create_new(ps_lev.size());
if (l_qty.is_zero()) { // empty level
return {PriceSearchResult::PriceSearchLocation::NEW_LEVEL, i};
}
if (securities::is_same(ord_price, l_price)) { // add to this level
return {PriceSearchResult::PriceSearchLocation::EXACT_MATCH, i};
} else if (securities::is_better(ord_side, ord_price, l_price)) {
}
if (securities::is_better(ord_side, ord_price, l_price)) { // insert
return {PriceSearchResult::PriceSearchLocation::NEW_LEVEL, i};
}
}
@@ -299,13 +299,13 @@ class SpreaderUtils {
// false.
static bool crosses_book(
const Book &book, api::Side side, Price price, double tick_size) {
const auto &book_side = book.side(side);
return (side == api::SIDE_BUY && book_side[0].size() > 0 &&
const auto &opp_side = book.side(securities::other_side(side));
return (side == api::SIDE_BUY && opp_side[0].size() > 0 &&
securities::gr_than_eq(
book_side[0].price(), price.value(), tick_size)) ||
(side == api::SIDE_SELL && book_side[0].size() > 0 &&
price.value(), opp_side[0].price(), tick_size)) ||
(side == api::SIDE_SELL && opp_side[0].size() > 0 &&
securities::gr_than_eq(
price.value(), book_side[0].price(), tick_size));
opp_side[0].price(), price.value(), tick_size));
}
// find the top of the synthetic book
static Price top_of_book_price(const ISpreaderManager &, api::Side) {
@@ -193,6 +193,49 @@ TEST_CASE("quoter price quantity state", "[spreader]") {
// }
}
}
TEST_CASE("general_utilities", "[spreader]") {
SECTION("crosses_book") {
Book bk;
bk.bid() = {
SpreaderUtils::create_price_level(2500, 10),
SpreaderUtils::create_price_level(2499, 3),
SpreaderUtils::create_price_level(2498, 12.5),
SpreaderUtils::create_price_level(2495, 13.8),
SpreaderUtils::create_price_level(2492, 15.2)};
bk.ask() = {
SpreaderUtils::create_price_level(2500.5, 10),
SpreaderUtils::create_price_level(2501.5, 3),
SpreaderUtils::create_price_level(2502, 12.5),
SpreaderUtils::create_price_level(2503.75, 13.8),
SpreaderUtils::create_price_level(2504, 15.2)};
// buy orders
auto res = SpreaderUtils::crosses_book(
bk, api::SIDE_BUY, Price(2501), securities::market_epsilon());
REQUIRE(res == true);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_BUY, Price(2500.5), securities::market_epsilon());
REQUIRE(res == true);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_BUY, Price(2500.25), securities::market_epsilon());
REQUIRE(res == false);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_BUY, Price(2499), securities::market_epsilon());
REQUIRE(res == false);
// sell orders
res = SpreaderUtils::crosses_book(
bk, api::SIDE_SELL, Price(2499), securities::market_epsilon());
REQUIRE(res == true);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_SELL, Price(2500), securities::market_epsilon());
REQUIRE(res == true);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_SELL, Price(2500.25), securities::market_epsilon());
REQUIRE(res == false);
res = SpreaderUtils::crosses_book(
bk, api::SIDE_SELL, Price(2500.5), securities::market_epsilon());
REQUIRE(res == false);
}
}
// tests spread_qty -> leg_qty and leg_2_qty -> leg_1_qty
TEST_CASE("test_leg_quantity_normalization", "[spreader]") {
int n_runs = 20;
@@ -5500,6 +5543,32 @@ TEST_CASE("working_orders", "[spreader_matching]") {
match_bks(bside_out, bside_ex);
}
}
SECTION("apply_multiple_working_orders") {
BookSide bside = {
SpreaderUtils::create_price_level(2500, 10),
SpreaderUtils::create_price_level(os::nan(), 0),
SpreaderUtils::create_price_level(os::nan(), 0),
SpreaderUtils::create_price_level(os::nan(), 0),
SpreaderUtils::create_price_level(os::nan(), 0)};
// outside the range
BookSide bside_out = bside;
// add 10 orders
for (size_t i = 0; i < 10; i++) {
bside_out = SpreaderUtils::apply_working_order(
bside_out, api::SIDE_BUY, {Price(2495), 10});
}
for (size_t i = 0; i < 100; i++) {
bside_out = SpreaderUtils::apply_working_order(
bside_out, api::SIDE_BUY, {Price(2497), 2});
}
BookSide bside_ex = {
SpreaderUtils::create_price_level(2500, 10),
SpreaderUtils::create_price_level(2497, 200),
SpreaderUtils::create_price_level(2495, 100),
SpreaderUtils::create_price_level(os::nan(), 0),
SpreaderUtils::create_price_level(os::nan(), 0)};
match_bks(bside_out, bside_ex);
}
}
void match_trades(
+5 -6
View File
@@ -4,7 +4,6 @@
#include "lib/logging/logging.h"
#include "lib/md/MarketDataGateway.h"
#include "lib/md/UserSymbol.h"
#include "lib/md/ccApi/CCMain.h"
#include "lib/md/relay/RelayRecorder.h"
#include "lib/os/os.h"
#include "lib/utils/bts_assert.h"
@@ -161,11 +160,11 @@ void Run::run(
if (is_run_emumba) {
threading::make_thread("Emumba", []() { run_emumba(); }).detach();
}
if (bts::config::get<bts::md::HistoricMarketDataConfig>().use_sim()) {
threading::make_thread("ExchangeSimGLBX", [&mbo_writer]() {
sim::start_exchange_sim(api::Venue::VENUE_SIM, mbo_writer);
}).detach();
}
// if (bts::config::get<bts::md::HistoricMarketDataConfig>().use_sim()) {
// threading::make_thread("ExchangeSimGLBX", [&mbo_writer]() {
// sim::start_exchange_sim(api::Venue::VENUE_SIM, mbo_writer);
// }).detach();
// }
bool is_run_md_gwy = true;
if (is_run_md_gwy) {
threading::make_thread("MarketDataGateway", []() {