diff --git a/src/lib/orderman/ccapi/CCAPIDeribitOrderMux.cpp b/src/lib/orderman/ccapi/CCAPIDeribitOrderMux.cpp index 0422a3a49..e8dee240d 100644 --- a/src/lib/orderman/ccapi/CCAPIDeribitOrderMux.cpp +++ b/src/lib/orderman/ccapi/CCAPIDeribitOrderMux.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -47,38 +48,64 @@ namespace bts::orderman { // in dispatch_incoming_messages class OrderEventHandler : public ccapi::EventHandler { boost::lockfree::spsc_queue &dispatch_queue; + // list of events we are interested in, the rest are logged and discarded std::set event_types; + std::set nolog_corrids; public: OrderEventHandler( boost::lockfree::spsc_queue &dispatch_queue, - const std::vector &event_types) + const std::vector &event_types, + const std::set &nolog_corrids_arg) : dispatch_queue(dispatch_queue), - event_types(event_types.begin(), event_types.end()) {} + event_types(event_types.begin(), event_types.end()), + nolog_corrids(nolog_corrids_arg) {} bool processEvent( const ccapi::Event &event, ccapi::Session *session) override { bts_assert(session != nullptr); bool interested = event_types.find(event.getType()) != event_types.end(); - LOG(OrderMan, Info, - "CCAPIDeribitOrderMux::OrderEventHandler::processEvent tid: {} " - "interested: {} " - "event: {}", - os::get_thread_id(), interested, event.toStringPretty(2, 2)); + if (should_log_event(event)) + LOG(OrderMan, Info, + "CCAPIDeribitOrderMux::OrderEventHandler::processEvent tid: {} " + "interested: {} " + "event: {}", + os::get_thread_id(), interested, event.toStringPretty(2, 2)); if (interested) dispatch_queue.push(event); return true; } + bool should_log_event(const ccapi::Event &event) const { + for (const auto &msg : event.messageList) { + for (const auto &corrid : msg.getCorrelationIdList()) { + if (nolog_corrids.find(corrid) == nolog_corrids.end()) { + // if any corrid is not in the ignore list, we log the whole event + return true; + } + } + } + return false; + } }; // Helper with corrid generation and parsing struct CorrIDHelper { const std::string create_order_prefix = "NewOrder"; + const std::string modify_order_prefix = "ModOrder"; + const std::string cancel_order_prefix = "CxlOrder"; const char delim = ':'; std::string get_create_order_corrid( - OrderKey ok, const std::string &deribit_sec_name) { + OrderKey ok, const std::string &deribit_sec_name) const { return create_order_prefix + delim + std::to_string(ok.value()) + delim + deribit_sec_name; } + std::string get_modify_order_corrid( + OrderKey ok, size_t n_outgoing_mods) const { + return modify_order_prefix + delim + std::to_string(ok.value()) + delim + + std::to_string(n_outgoing_mods); + } + std::string get_cancel_order_corrid(OrderKey ok) const { + return cancel_order_prefix + delim + std::to_string(ok.value()); + } std::vector split_string(const std::string &str, char delim) { // vector of string to save tokens std::vector tokens = {}; @@ -151,6 +178,14 @@ struct PositionOrderRequestHelper { } return rv; } + std::set get_all_corrids() const { + std::set rv = {}; + for (const auto &kv : curr_pos_map) + rv.insert(kv.second.corrid); + for (const auto &kv : curr_ord_map) + rv.insert(kv.second.corrid); + return rv; + } }; // Since we have to subscribe to the orders and trades for each instrument @@ -205,7 +240,8 @@ struct CCAPIDeribitOrderMux::PImpl { incoming_event_queue, {ccapi::Event::Type::RESPONSE, ccapi::Event::Type::SUBSCRIPTION_DATA, - ccapi::Event::Type::SUBSCRIPTION_STATUS}) { + ccapi::Event::Type::SUBSCRIPTION_STATUS}, + pos_ord_req_helper.get_all_corrids()) { auto has_conn = connection_config.get_deribit_config(d_conn_config); bts_assert(has_conn); LOG(OrderMan, Info, @@ -331,11 +367,11 @@ bool CCAPIDeribitOrderMux::update_order( std::string query = "amount=" + std::to_string(new_qty) + "&order_id=" + ord_state.cc_order_key + "&price=" + std::to_string(new_price); - std::string corr_id = "OrderMod:" + ord_state.cc_order_key + ":" + - std::to_string(ord_state.n_outgoing_mods); + std::string corrid = me->corrid_helper.get_modify_order_corrid( + ord_state.order_key, ord_state.n_outgoing_mods); ccapi::Request request( ccapi::Request::Operation::GENERIC_PRIVATE_REQUEST, - CCAPI_EXCHANGE_NAME_DERIBIT, sec_desc, corr_id); + CCAPI_EXCHANGE_NAME_DERIBIT, sec_desc, corrid); request.appendParam( {{CCAPI_HTTP_METHOD, "GET"}, {CCAPI_HTTP_PATH, "/api/v2/private/edit"}, @@ -350,6 +386,7 @@ void send_cancel_generic_private_request( ccapi::Session *session, const std::string &cc_order_key, const std::string &sec_desc) { + bts_assert(false); LOG(OrderMan, Info, "send_cancel_generic_private_request cc_order_key: {} sec_desc: {}", cc_order_key, sec_desc); @@ -380,17 +417,10 @@ bool CCAPIDeribitOrderMux::cancel_order(const CancelOrderMsg &cxl_msg) { return false; } CCAPISingleOrderManager &order_man = it->second; - const auto &sec_desc = - order_man.order_state().security.deribit().security_desc(); - // if (order_man.order_state().security.has_option()) { - // send_cancel_generic_private_request( - // me->session, order_man.order_state().cc_order_key, sec_desc); - // return true; - // } - std::string corr_id = "OrderCxl:" + order_man.order_state().cc_order_key; + std::string corrid = me->corrid_helper.get_cancel_order_corrid(cxl_msg.key); ccapi::Request request( - ccapi::Request::Operation::CANCEL_ORDER, CCAPI_EXCHANGE_NAME_DERIBIT, - sec_desc); + ccapi::Request::Operation::CANCEL_ORDER, CCAPI_EXCHANGE_NAME_DERIBIT, "", + corrid); request.appendParam( {{CCAPI_EM_ORDER_ID, order_man.order_state().cc_order_key}}); LOG(OrderMan, Info, @@ -742,7 +772,7 @@ void CCAPIDeribitOrderMux::_handle_RESPONSE_event( // so this is a no-op } case ccapi::Message::Type::GENERIC_PRIVATE_REQUEST: { - // position request, open order request + // position request, open order request. PositionOrderRequestHelper::PosData *pos_data = me->pos_ord_req_helper.is_position_cb(corrid); if (pos_data != nullptr) { @@ -764,7 +794,9 @@ void CCAPIDeribitOrderMux::_handle_RESPONSE_event( me->pos_ord_req_helper.total_working_orders()); return; } - // orders + // there are other GENERIC_PRIVATE_REQUEST that we dont care about: + // order modofiction request, rfq send request + // and combo create request break; } case ccapi::Message::Type::RESPONSE_ERROR: { diff --git a/src/lib/orderman/gateways/CCAPIExecutionGateway.cpp b/src/lib/orderman/gateways/CCAPIExecutionGateway.cpp index 2cb9486b7..70cea21e6 100644 --- a/src/lib/orderman/gateways/CCAPIExecutionGateway.cpp +++ b/src/lib/orderman/gateways/CCAPIExecutionGateway.cpp @@ -124,7 +124,7 @@ struct CCAPIExecutionGateway::PImpl { const bts::api::Venue venue; InitState init_state = {}; std::vector mux_vec = {}; - const size_t pos_query_period_ms = 10000; + const size_t pos_query_period_ms = 5000; core::TimeStamp last_pos_query_time = core::TimeStamp::now(); api::admin::AdminData admin_data = {}; md::SecurityOracle sec_oracle;