From dfacf73289f23359be776aedbcb54a59f55edbde Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 2 Mar 2025 05:04:38 +0000 Subject: [PATCH] Improve logging/error reporting in binance execution gw In the process, consolidate binance exe report stores into single class. --- src/lib/api/services/OrderService.h | 9 + src/lib/collections/concurrent_factory.h | 98 ++++ .../utils/formatters/error_code_formatter.h | 17 + .../binance_execution/error_list.h | 45 ++ .../execution_report_store.cpp | 76 +++ .../execution_report_store.h | 59 ++ .../binance_execution/rest_api_error.h | 16 + .../binance_execution/rest_api_result.h | 36 +- .../spots/execution_gateway.cpp | 553 ++++++++---------- .../spots/messages/cancel_order_request.cpp | 35 ++ .../spots/messages/cancel_order_request.h | 11 + .../spots/messages/create_order_request.cpp | 20 + .../spots/messages/create_order_request.h | 6 + .../spots/messages/modify_order_request.cpp | 43 ++ .../spots/messages/modify_order_request.h | 9 + .../binance_execution/spots/order_key_map.h | 34 +- .../spots/rest_api_client.cpp | 10 +- .../spots/type_converters.cpp | 181 +----- .../binance_execution/spots/type_converters.h | 33 +- .../listen_key_requestor.cpp | 7 +- .../spots/user_data_streams/order_update.cpp | 56 ++ .../spots/user_data_streams/order_update.h | 10 + .../binance_execution/type_converters.h | 74 ++- .../usds_futures/execution_gateway.cpp | 470 +++++++-------- .../messages/cancel_order_request.cpp | 26 + .../messages/cancel_order_request.h | 6 + .../messages/create_order_request.cpp | 25 + .../messages/create_order_request.h | 6 + .../messages/modify_order_request.cpp | 37 ++ .../messages/modify_order_request.h | 6 + .../usds_futures/rest_api_client.cpp | 8 +- .../usds_futures/type_converters.cpp | 110 +++- .../usds_futures/type_converters.h | 26 + .../listen_key_requestor.cpp | 7 +- .../user_data_streams/order_update.cpp | 43 ++ .../user_data_streams/order_update.h | 10 + 36 files changed, 1382 insertions(+), 836 deletions(-) create mode 100644 src/lib/collections/concurrent_factory.h create mode 100644 src/lib/utils/formatters/error_code_formatter.h create mode 100644 src/lib_binance_execution/binance_execution/error_list.h create mode 100644 src/lib_binance_execution/binance_execution/execution_report_store.cpp create mode 100644 src/lib_binance_execution/binance_execution/execution_report_store.h create mode 100644 src/lib_binance_execution/binance_execution/rest_api_error.h create mode 100644 src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp create mode 100644 src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp create mode 100644 src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp create mode 100644 src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.cpp diff --git a/src/lib/api/services/OrderService.h b/src/lib/api/services/OrderService.h index c54978535..68084e4ad 100644 --- a/src/lib/api/services/OrderService.h +++ b/src/lib/api/services/OrderService.h @@ -35,6 +35,9 @@ PB_ENUM_TO_JSON(CustomerOrFirm) void to_json(nlohmann::json &j, const RoutingRecord &r); void from_json(const nlohmann::json &j, RoutingRecord &r); +PB_ENUM_FROM_JSON(NativeOrderStatus) +PB_ENUM_TO_JSON(NativeOrderStatus) + PB_ENUM_FROM_JSON(NativeOrderState_SubError) PB_ENUM_TO_JSON(NativeOrderState_SubError) @@ -67,6 +70,12 @@ FMT_IMPL( "{}", bts::api::orderman::SelfMatchPreventionInstruction_Name(s)); +FMT_IMPL( + bts::api::orderman::NativeOrderStatus, + s, + "{}", + bts::api::orderman::NativeOrderStatus_Name(s)); + FMT_IMPL( bts::api::orderman::NativeOrderState_SubError, s, diff --git a/src/lib/collections/concurrent_factory.h b/src/lib/collections/concurrent_factory.h new file mode 100644 index 000000000..d701ae4f1 --- /dev/null +++ b/src/lib/collections/concurrent_factory.h @@ -0,0 +1,98 @@ +#pragma once + +#include "lib/utils/LockBox.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/type_name.h" + +#include +#include + +namespace bts::collections { + +template +class ConcurrentFactory { + utils::LockBox>> store; + + public: + std::shared_ptr add( + const KeyT &key, + std::function &)> callback) { + return store.lock([&](auto &map) { + auto [it, is_new] = map.try_emplace(key, std::make_shared()); + bts_assert_msgf( + is_new, "{}::add. Duplicate key '{}'", + utils::cached_type_name>(), key); + callback(it->second); + return it->second; + }); + } + + std::size_t erase(const KeyT &key) { + return store.lock([&](auto &map) { + map.erase(key); + return map.size(); + }); + } + + std::size_t size() const { + return store.lock([&](auto &map) { return map.size(); }); + } + + template < + typename CallbackT = std::function &)>> + std::shared_ptr get(const KeyT &key, CallbackT callback) { + return store.lock([&](auto &map) { + auto [it, is_new] = map.try_emplace(key, std::make_shared()); + bts_assert_msgf( + !is_new, "{}::get. Key not found '{}'", + utils::cached_type_name>(), key); + callback(it->second); + return it->second; + }); + } + + template < + typename CallbackT = + std::function &, bool)>> + std::shared_ptr get_or_add( + const KeyT &key, CallbackT callback) { + return store.lock([&](auto &map) { + auto [it, is_new] = map.try_emplace(key); + if (is_new) { + it->second = std::make_shared(); + } + callback(it->second, is_new); + return it->second; + }); + } + + template < + typename CallbackT = + std::function &, bool)>> + std::shared_ptr replace(const KeyT &key, CallbackT callback) { + return store.lock([&](auto &map) { + auto [it, is_new] = map.try_emplace(key); + bts_assert_msgf( + !is_new, "{}::replace. Key not found '{}'", + utils::cached_type_name>(), key); + it->second = std::make_shared(); + callback(it->second, is_new); + return it->second; + }); + } + + template < + typename CallbackT = + std::function &, bool)>> + std::shared_ptr replace_or_add( + const KeyT &key, CallbackT callback) { + return store.lock([&](auto &map) { + auto [it, is_new] = map.try_emplace(key); + it->second = std::make_shared(); + callback(it->second, is_new); + return it->second; + }); + } +}; + +} // namespace bts::collections diff --git a/src/lib/utils/formatters/error_code_formatter.h b/src/lib/utils/formatters/error_code_formatter.h new file mode 100644 index 000000000..f072a2e20 --- /dev/null +++ b/src/lib/utils/formatters/error_code_formatter.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +template <> +struct fmt::formatter { + constexpr auto parse(fmt::format_parse_context &ctx) -> decltype(ctx.end()) { + return ctx.end(); + } + + template + auto format(const std::error_code &e, FormatContext &ctx) const + -> decltype(ctx.out()) { + return fmt::format_to(ctx.out(), "{}", e.message()); + } +}; diff --git a/src/lib_binance_execution/binance_execution/error_list.h b/src/lib_binance_execution/binance_execution/error_list.h new file mode 100644 index 000000000..aab4df8c9 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/error_list.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +namespace binance_execution { + +struct ErrorList { + ErrorList() = default; + ErrorList(std::string *errors_p) : errors_p(errors_p) {} + + static void append(std::string &errors, const char *err) { + ErrorList el{&errors}; + el.append(err); + } + + static void append(std::string &errors, const std::string &err) { + ErrorList el{&errors}; + el.append(err.c_str()); + } + + void set_delimiter(std::string delimiter) { + this->delimiter = std::move(delimiter); + } + + void append(const char *err) { + if (err == nullptr || err[0] == '\0') { + return; + } + if (errors_p == nullptr) { + errors_p = &errors_.emplace(); + } + if (!errors_p->empty()) { + errors_p->append(delimiter); + } + errors_p->append(err); + } + + private: + std::string *errors_p = nullptr; + std::string delimiter = ". "; + std::optional errors_; +}; + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/execution_report_store.cpp b/src/lib_binance_execution/binance_execution/execution_report_store.cpp new file mode 100644 index 000000000..7cf3cee0e --- /dev/null +++ b/src/lib_binance_execution/binance_execution/execution_report_store.cpp @@ -0,0 +1,76 @@ +#include "execution_report_store.h" +#include "lib/core/TimeStamp.h" +#include "lib/orderman/CreateOrderMsg.h" +#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/utils/string_utils.h" + +namespace binance_execution { + +using bts::orderman::ExecutionReport; + +std::shared_ptr ExecutionReportStore::update( + const std::string &exch_id, + std::function &, bool)> init_update) { + auto init = [&](std::shared_ptr &er_p, bool is_new) { + auto &er = *er_p; + bts::system::string_utils::copy_to_array<12>( + account, er.account_name.value()); + er.exchange = venue; + er.cfg = jetfix_connection.get(); + // Irrelevant fields + er.ice_cl_ord_id = 0; + er.ice_company_id = 0; + init_update(er_p, is_new); + }; + const auto bts_key = get_internal_key(exch_id); + return store.replace_or_add(bts_key, init); +} + +std::shared_ptr ExecutionReportStore::reject( + const bts::orderman::CreateOrderMsg &m, const std::string &error) { + auto init = [&](std::shared_ptr &er_p) { + auto &er = *er_p; + er.exchange = venue; + er.exchange_order_id = bts::orderman::ExchangeOrderID64{0}; + bts::system::string_utils::copy_to_array<12>( + account, er.account_name.value()); + er.msg = error; + er.order_key = m.key; + er.order_type = m.order_type; + er.time_in_force = m.time_in_force; + er.quantity = m.quantity; + er.max_show = m.display_quantity; + er.stop_price.value() = m.stop_px.value(); + er.order_status = bts::api::orderman::NATIVE_ORDER_STATUS_REJECTED; + er.transact_time = bts::orderman::ExchangeTimeStamp64{ + bts::core::TimeStamp::now().nanos()}; + er.price.value() = std::numeric_limits::quiet_NaN(); + er.avg_price.value() = std::numeric_limits::quiet_NaN(); + er.filled_quantity = bts::Quantity{0}; + er.last_shares = bts::Quantity{0}; + er.last_price.value() = std::numeric_limits::quiet_NaN(); + er.avg_price.value() = std::numeric_limits::quiet_NaN(); + er.stop_price.value() = std::numeric_limits::quiet_NaN(); + er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; + er.security = nullptr; + er.leg_security = nullptr; + er.msg_seq_num = 0; + }; + return store.add(m.key, init); +} + +std::shared_ptr ExecutionReportStore::set_error( + bts::orderman::OrderKey key, const std::string &error) { + auto init = [&error](std::shared_ptr &er_p, bool is_new) { + // Set underlying data to a new instance rather than mutate it. The shared + // pointer we return may be copied to a buffer/queue and processed on + // another thread. + if (!is_new) { + er_p = std::make_shared(*er_p); + } + er_p->msg = error; + }; + return store.get_or_add(key, init); +} + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/execution_report_store.h b/src/lib_binance_execution/binance_execution/execution_report_store.h new file mode 100644 index 000000000..b0531c349 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/execution_report_store.h @@ -0,0 +1,59 @@ +#pragma once + +#include "lib/collections/concurrent_factory.h" +#include "lib/domain_model/Security.pb.h" +#include "lib/orderman/orderman_utils/orderman_common.h" + +#include + +namespace bts::api { +class Security; +} + +namespace bts::config { +class JetFIX_Connection; +} + +namespace bts::orderman { +struct CreateOrderMsg; +class ExecutionReport; +} // namespace bts::orderman + +namespace binance_execution { + +class ExecutionReportStore { + std::function get_internal_key; + std::string account; + std::shared_ptr jetfix_connection; + bts::api::Venue venue; + bts::collections::ConcurrentFactory< + bts::orderman::OrderKey, + bts::orderman::ExecutionReport> + store; + + public: + ExecutionReportStore( + std::function + get_internal_key, + std::string account, + std::shared_ptr jetfix_connection) + : get_internal_key(get_internal_key), account(std::move(account)), + jetfix_connection(std::move(jetfix_connection)) {} + + std::size_t erase(bts::orderman::OrderKey key) { return store.erase(key); } + std::size_t size() const { return store.size(); } + + std::shared_ptr update( + const std::string &exch_id, + std::function< + void(std::shared_ptr &, bool)> + init_update); + + std::shared_ptr reject( + const bts::orderman::CreateOrderMsg &m, const std::string &error); + + std::shared_ptr set_error( + bts::orderman::OrderKey key, const std::string &error); +}; + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/rest_api_error.h b/src/lib_binance_execution/binance_execution/rest_api_error.h new file mode 100644 index 000000000..4232f682c --- /dev/null +++ b/src/lib_binance_execution/binance_execution/rest_api_error.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +namespace binance_execution { + +struct RestApiError { + std::int64_t code; + std::string msg; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(RestApiError, code, msg); + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/rest_api_result.h b/src/lib_binance_execution/binance_execution/rest_api_result.h index e6bebfd2a..248cb870f 100644 --- a/src/lib_binance_execution/binance_execution/rest_api_result.h +++ b/src/lib_binance_execution/binance_execution/rest_api_result.h @@ -1,5 +1,7 @@ #pragma once +#include "binance_execution/rest_api_error.h" + #include #include #include @@ -9,30 +11,35 @@ namespace binance_execution { template struct RestApiResult { - std::string error; + std::string http_error; + std::int64_t http_code; + std::optional error; + std::optional response; RestApiResult() = default; - static RestApiResult from_error(const std::string &error) { - return RestApiResult{error}; + static RestApiResult from_error( + const std::string &error, + const std::string &error_body, + std::int64_t code) { + return RestApiResult{error, error_body, code}; } static RestApiResult from_success(ResponseT response) { return RestApiResult{std::move(response)}; } - constexpr bool ok() const { return error.empty(); } + constexpr bool ok() const { return !error; } void reset() { - error.clear(); + error.reset(); response.reset(); } private: explicit RestApiResult(ResponseT response) : response(std::move(response)) {} - - explicit RestApiResult(std::string error) : error(std::move(error)) {} + explicit RestApiResult(RestApiError error) : error(std::move(error)) {} }; template < @@ -42,7 +49,11 @@ template < int> = 0> inline void to_json(nlohmann::json &j, const RestApiResult &res) { if (!res.ok()) { - j["error"] = res.error; + if (res.error) { + j["error"] = *res.error; + } else { + j["error"] = nlohmann::json{}; + } } else if (res.response) { j["response"] = *res.response; } else { @@ -58,7 +69,14 @@ template < inline void from_json(const nlohmann::json &j, RestApiResult &res) { auto it = j.find("error"); if (it != j.end()) { - it->get_to(res.error); + if (it->is_null()) { + res.error.reset(); + } else { + if (!res.error) { + res.error.emplace(); + } + it->get_to(*res.error); + } } it = j.find("response"); if (it != j.end()) { diff --git a/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp b/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp index 0cb48fcf1..d411639dd 100644 --- a/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp @@ -1,9 +1,8 @@ #include "execution_gateway.h" -#include "binance_execution/enums/time_in_force.h" +#include "binance_execution/execution_report_store.h" #include "binance_execution/rest_api_result.h" #include "binance_execution/spots/execution_gateway_config.h" #include "binance_execution/spots/messages/modify_order_request.h" -#include "binance_execution/spots/messages/order_record.h" #include "binance_execution/spots/order_key_map.h" #include "binance_execution/spots/rest_api_client.h" #include "binance_execution/spots/type_converters.h" @@ -31,7 +30,6 @@ #include "lib/orderman/orderman_utils/ExchangePosition.h" #include "lib/orderman/orderman_utils/ExecutionReport.h" #include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/utils/checked_arithmetic.h" #include "lib/utils/overloaded.h" #include @@ -40,7 +38,6 @@ #include #include #include -#include #include #include @@ -65,33 +62,28 @@ using bts::io::json_formatter; using bts::orderman::CancelOrderMsg; using bts::orderman::CancelReplaceOrderMsg; using bts::orderman::CreateOrderMsg; +using bts::orderman::ExecutionReport; using bts::orderman::NewAdminDataEvent; using bts::orderman::NewSecuritiesEvent; using bts::orderman::OrderMsg; using bts::orderman::RFQMsg; using std::int64_t; using std::optional; +using std::shared_ptr; using std::size_t; using std::string; using std::uint32_t; using std::vector; -static void append_error(string &dest, const char *err) { - if (!dest.empty()) { - dest.append(" "); - } - dest.append(err); -} - static void validate(bts::orderman::OrderKey key, string &error) { if (key.value() == 0) { - append_error(error, "order key is 0."); + ErrorList::append(error, "order key is 0"); } } static void validate(bts::orderman::ExchangeOrderID64 key, string &error) { if (key.value() == 0) { - append_error(error, "exch order key is 0."); + ErrorList::append(error, "exch order key is 0"); } } @@ -99,94 +91,35 @@ static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) { if (std::holds_alternative(key)) { validate(std::get(key), error); } else { - append_error(error, "exch order id is undefined or not an integer."); + ErrorList::append(error, "exch order id is undefined or not an integer"); } } static void validate(bts::Quantity qty, string &error) { if (qty.full_value() == 0) { - append_error(error, "quantity is 0."); + ErrorList::append(error, "quantity is 0"); } } static void validate(const bts::api::Security *sec, string &error) { if (sec == nullptr) { - append_error(error, "security is null."); + ErrorList::append(error, "security is null"); } else if (!sec->has_binance()) { - append_error(error, "security not of Binance."); + ErrorList::append(error, "security not of Binance"); } } -static void validate(bts::api::Side side, string &error) { - switch (side) { - case bts::api::SIDE_BUY: - case bts::api::SIDE_SELL: - break; - default: - append_error(error, "invalid side."); - break; - } -} - -static void validate(bts::api::orderman::TimeInForce bts_tif, string &error) { - const auto tif = convert_time_in_force(bts_tif); - switch (tif.value) { - case TimeInForce::GTC: - case TimeInForce::IOC: - case TimeInForce::FOK: - break; - default: - append_error( - error, fmt::format("invalid TimeInForce '{}'.", bts_tif).c_str()); - break; - } -} - -// TODO: Store exch order id in EMNativeOrder (until then, it'll always be 0). -// We can ignore this for now and use "orig client id" in cancel/replace requests. -static constexpr const bool VALIDATE_EXCH_ORDER_ID = false; - -static bool validate(const CreateOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.security, error); - validate(m.key, error); - validate(m.quantity, error); - validate(m.side, error); - validate(m.time_in_force, error); - return error.size() == orig_sz; -} - -static bool validate(const CancelOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.security, error); - validate(m.key, error); - if (VALIDATE_EXCH_ORDER_ID) { - validate(m.exchange_order_id, error); - } - validate(m.side, error); - return error.size() == orig_sz; -} - -static bool validate(const CancelReplaceOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.create_fields, error); - if (VALIDATE_EXCH_ORDER_ID) { - validate(m.exchange_order_id, error); - } - return error.size() == orig_sz; -} - static bool validate(const bts::orderman::ExecutionReport &er, string &error) { const size_t orig_sz = error.size(); validate(er.order_key, error); validate(er.quantity, error); validate(er.exchange_order_id, error); if (er.account_name.value().empty()) { - append_error(error, "account is empty."); + ErrorList::append(error, "account is empty"); } validate(er.security, error); if (er.cfg == nullptr) { - append_error(error, "jetfix config is null."); + ErrorList::append(error, "jetfix config is null"); } return error.size() == orig_sz; } @@ -203,24 +136,32 @@ struct ExecutionGateway::PImpl { explicit PImpl( bts::md::SecurityOracle sec_oracle, ExecutionGatewayConfig config, - bts::config::JetFIX::Connection jetfix_connection) + bts::config::JetFIX_Connection jetfix_connection) : sec_oracle(std::move(sec_oracle)), config(std::move(config)), - jetfix_connection(std::move(jetfix_connection)), + jetfix_connection(std::make_shared( + std::move(jetfix_connection))), rest_api_client( this->config.rest_endpoint, this->config.api_key, this->config.secret_key), - listen_key_requestor(this->rest_api_client) { - bts_assert(this->jetfix_connection.venue() == bts::api::VENUE_BINANCE); - bts_assert(!this->jetfix_connection.name().empty()); - pos.exchange = bts::api::VENUE_BINANCE; + listen_key_requestor(this->rest_api_client), + key_map(std::make_shared()), + orders( + [this](const std::string &exch_key) { + return *key_map->get_bts_key(exch_key); + }, + this->config.account, + this->jetfix_connection) { + bts_assert(this->jetfix_connection->venue() == VENUE); + bts_assert(!this->jetfix_connection->name().empty()); + pos.exchange = VENUE; } static constexpr const char *CANCEL_REPLACE_CANCEL_SUFFIX = "crc"; bts::md::SecurityOracle sec_oracle; ExecutionGatewayConfig config; - bts::config::JetFIX_Connection jetfix_connection; + std::shared_ptr jetfix_connection; RestApiClient rest_api_client; optional user_data_sub; @@ -228,21 +169,16 @@ struct ExecutionGateway::PImpl { Delegate order_update_callback; ListenKeyRequestor listen_key_requestor; - spsc_queue incoming_msgs{1024}; spsc_queue incoming_account_updates{1024}; spsc_queue incoming_order_updates{1024}; - spsc_queue> incoming_new_orders{1024}; - + spsc_queue> internal_order_rejects{256}; vector account_updates_vec; ExecutionGatewayCallbacks callbacks; bts::orderman::ExchangePosition pos; bts::api::admin::BTSAccount account; - // Since modifications are implemented as cancel/replace at Binance, modified - // orders don't keep their original id. The version number correlates the - // original id with the current one. - std::unordered_map order_versions; - OrderKeyMap key_map; + std::shared_ptr key_map; + ExecutionReportStore orders; void start() { INFO_FN("start"); @@ -263,7 +199,7 @@ struct ExecutionGateway::PImpl { void on_order_update(user_data_streams::OrderUpdate u) { INFO_FN("on_order_update", "\n{:2}", json_formatter(u)); if (u.orig_client_order_id.ends_with(CANCEL_REPLACE_CANCEL_SUFFIX)) { - // This is the "canceled order part of a cancel/replace op. This op + // This is the "canceled" order part of a cancel/replace op. This op // generates another update without such a suffix. That's the new // order; that's the one we want. DIAG_FN( @@ -292,105 +228,214 @@ struct ExecutionGateway::PImpl { } void create_order(const CreateOrderMsg &m) { - string sec_desc = m.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.security); - RestApiResult res; - validate(m, res.error); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "create_order", - "{} {} {} @ {} (id={}, type={}, tif={}, err={})", m.side, m.quantity, - sec_desc, m.price, m.key, m.order_type, m.time_in_force, res.error); CreateOrderRequest req; - req.newClientOrderId = std::to_string(m.key.value()); - req.symbol = bts::securities::descriptor(*m.security); - req.side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; - req.price = m.price.value(); - req.quantity = m.quantity.full_value(); - req.type = convert_order_type(m.order_type); - req.timeInForce = convert_time_in_force(m.time_in_force); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - if (res.ok()) { - res = rest_api_client.create_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "create_order", "id={}; {}", - m.key, json_formatter(res)); + std::string err; + if (!req.init(m, err)) { + ERROR_FN( + "create_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Rejected " + "internally (request not sent). {}", + m.side, m.quantity, req.symbol, m.price, m.key, m.order_type, + m.time_in_force, err); + internal_order_rejects.push(orders.reject(m, err)); + return; + } + INFO_FN( + "create_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", m.side, + m.quantity, req.symbol, m.price, m.key, m.order_type, + m.time_in_force); + const auto res = rest_api_client.create_order(req); + if (!res.ok()) { + ERROR_FN("create_order", "id={}. {}", m.key, json_formatter(res)); + internal_order_rejects.push( + orders.reject(m, json_formatter(*res.error).to_string())); } - incoming_new_orders.push(std::move(res)); } void update_order(const CancelReplaceOrderMsg &m) { - string sec_desc = - m.create_fields.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.create_fields.security); + const auto &fields = m.create_fields; RestApiResult res; - validate(m, res.error); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "update_order", - "{} {} {} @ {} (id={}, type={}, tif={}, err={})", - m.create_fields.side, m.create_fields.quantity, sec_desc, - m.create_fields.price, m.create_fields.key, - m.create_fields.order_type, m.create_fields.time_in_force, res.error); - if (res.ok()) { - ModifyOrderRequest req; - const auto orig_key = m.create_fields.key; - req.cancelOrderId = 0; - req.cancelOrigClientOrderId = key_map.get_exch_key(orig_key); - // Can't reuse the same order id. - key_map.increment_order_version(orig_key); - req.cancelNewClientOrderId = key_map.get_exch_key(orig_key).append( - CANCEL_REPLACE_CANCEL_SUFFIX); - key_map.increment_order_version(orig_key); - req.newClientOrderId = key_map.get_exch_key(orig_key); - // The exchange accepts either cancelOrderId, or cancelOrigClientOrderId (only one - // need be populated). - bts_assert( - req.cancelOrderId != 0 || !req.cancelOrigClientOrderId.empty()); - req.price = m.create_fields.price.value(); - req.quantity = m.create_fields.quantity.full_value(); - req.symbol = m.create_fields.security->binance().security_desc(); - req.side = convert_side(m.create_fields.side); - req.type = convert_order_type(m.create_fields.order_type); - req.cancelReplaceMode = CancelReplaceMode::STOP_ON_FAILURE; - req.timeInForce = convert_time_in_force(m.create_fields.time_in_force); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - res = rest_api_client.modify_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "update_order", "id={}; {}", - req.cancelOrderId, json_formatter(res)); + ModifyOrderRequest req; + std::string err; + if (!req.init(m, *key_map, err)) { + ERROR_FN( + "update_order", + "{} {} {} @ {} (id={}, exch_id={}, type={}, tif={}). Rejected " + "internally (request not sent). {}", + fields.side, fields.quantity, req.symbol, fields.price, fields.key, + m.exchange_order_id, fields.order_type, fields.time_in_force, err); + internal_order_rejects.push(orders.set_error( + fields.key, + "Update rejected internally (request not sent). " + err)); + return; + } + INFO_FN( + "update_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", + fields.side, fields.quantity, req.symbol, fields.price, fields.key, + m.exchange_order_id, fields.order_type, fields.time_in_force); + res = rest_api_client.modify_order(req); + if (!res.ok()) { + ERROR_FN( + "update_order", "id={}, exch_id={}. {}", fields.key, + m.exchange_order_id, json_formatter(res)); + internal_order_rejects.push(orders.set_error( + fields.key, + fmt::format( + "Update request failed. {}", json_formatter(*res.error)))); } } void cancel_order(const CancelOrderMsg &m) { - string sec_desc = m.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.security); - RestApiResult res; - validate(m, res.error); CancelOrderRequest req; - LOG_FN( - res.ok() ? bts::Info : bts::Error, "cancel_order", - "{} {} (id={}, exch_id={}, err={})", m.side, sec_desc, m.key, - m.exchange_order_id, res.error); - if (res.ok()) { - req.orderId = - bts::system::math::checked_ultol(m.exchange_order_id.value()); - req.origClientOrderId = key_map.get_exch_key(m.key); - // Can't reuse the same order id. - key_map.increment_order_version(m.key); - req.newClientOrderId = key_map.get_exch_key(m.key); - // The exchange accepts either orderId, or origClientOrderId (only one - // need be populated). - bts_assert(req.orderId != 0 || !req.origClientOrderId.empty()); - req.symbol = m.security->binance().security_desc(); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - res = rest_api_client.cancel_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "cancel_order", - "id={}, exch_id={}; {}", m.key, m.exchange_order_id, - json_formatter(res)); + std::string err; + if (!req.init(m, *key_map, err)) { + ERROR_FN( + "cancel_order", "{} {} (id={}, exch_id={}). {}", m.side, + req.symbol, m.key, m.exchange_order_id, err); + internal_order_rejects.push(orders.set_error( + m.key, "Rejected internally (request not sent). " + err)); + return; } + INFO_FN( + "cancel_order", "{} {} (id={}, exch_id={})", m.side, req.symbol, + m.key, m.exchange_order_id); + const auto res = rest_api_client.cancel_order(req); + if (!res.ok()) { + ERROR_FN( + "cancel_order", "{} {} (id={}, exch_id={}). {}", m.side, + req.symbol, m.key, m.exchange_order_id, json_formatter(res)); + internal_order_rejects.push(orders.set_error( + m.key, + fmt::format( + "Cancel request failed. {}", json_formatter(*res.error)))); + } + } + + void dispatch_internal_rejects() { + shared_ptr curr; + while (internal_order_rejects.pop(curr)) { + callbacks.exe_report(curr, {}); + } + } + + void dispatch_order_updates() { + if (incoming_order_updates.empty()) { + return; + } + user_data_streams::OrderUpdate curr; + auto &km = *key_map; + while (incoming_order_updates.pop(curr)) { + auto bts_key = km.get_bts_key(curr.client_order_id); + if (!bts_key) { + ERROR_FN( + "dispatch_order_updates", + "Failed to generate exe report. Did not resolve bts key from " + "exch clientOrderId '{}'", + curr.client_order_id); + continue; + } + auto exch_key = km.get_exch_key(*bts_key); + if (exch_key != curr.client_order_id) { + INFO_FN( + "dispatch_order_updates", + "Ignoring update w/ old clientOrderId '{}' (expected '{}', " + "exch_id={}). This is expected for CancelReplace ops.", + curr.client_order_id, curr.order_id, exch_key); + continue; + } + // orders + shared_ptr er_p = + orders.update(curr.client_order_id, [&](auto &er_p, bool) { + auto &er = *er_p; + std::string error; + if (!curr.to_execution_report(er, error)) { + WARN_FN( + "update", "Failed to initialize exe report. {}. {}", + error, json_formatter(curr)); + return; + } + er.security = sec_oracle.get_security( + VENUE, bts::api::SECURITYTYPE_SPOT, curr.symbol); + }); + const ExecutionReport &er = *er_p; + string error; + if (!validate(er, error)) { + ERROR_FN( + "dispatch_order_updates", + "Generated invalid ExecutionReport (id={}, exch_id={}). {}", + curr.client_order_id, curr.order_id, error); + continue; + } + switch (curr.order_status.value) { + case OrderStatus::FILLED: + case OrderStatus::CANCELED: + case OrderStatus::REJECTED: { + const size_t n_active = orders.erase(er.order_key); + INFO_FN( + "dispatch_order_updates", + "Order reached terminal state {} (id={}, exch_id={}). {} " + "active orders.", + curr.order_status, curr.client_order_id, curr.order_id, + n_active); + break; + } + default: + break; + } + INFO_FN( + "dispatch_order_updates", + "Firing exe report callback. {} {} {} @ {} {} (filled {}/{}, " + "id={}). {} active orders", + er.side, er.security->binance().security_desc(), er.quantity, + er.price, er.order_status, er.filled_quantity, er.quantity, + er.order_key, orders.size()); + callbacks.exe_report(std::move(er_p), {}); + } + } + + void dispatch_account_updates() { + user_data_streams::AccountUpdate u; + auto &acct_upds = account_updates_vec; + acct_upds.clear(); + while (incoming_account_updates.pop(acct_upds.emplace_back())) { + } + acct_upds.pop_back(); + if (acct_upds.empty()) { + return; + } + const auto pos_ticker_comp = + [](const bts::orderman::ExchangePosition::UnitPosition &unit_pos, + const string &ticker) { + return unit_pos.exchange_sec_desc < ticker; + }; + auto &dest = pos.total_exchange_positions; + for (const auto &acct_upd : acct_upds) { + for (const auto &pos : acct_upd.balances) { + bts_assert(pos.free >= 0); + bts_assert(pos.locked >= 0); + auto it = std::lower_bound( + dest.begin(), dest.end(), pos.asset, pos_ticker_comp); + if (it == dest.end() || it->exchange_sec_desc != pos.asset) { + const auto *sec = sec_oracle.get_security( + VENUE, bts::api::SECURITYTYPE_SPOT, pos.asset); + if (sec == nullptr) { + continue; + } + bts::orderman::ExchangePosition::UnitPosition new_pos; + new_pos.exchange_sec_desc = pos.asset; + new_pos.security = sec; + new_pos.account_id = account.id(); + new_pos.account_name = account.name(); + it = dest.insert(it, std::move(new_pos)); + } + it->position = pos.locked + pos.free; + it->avg_filled_price.value() = + std::numeric_limits::quiet_NaN(); + } + } + callbacks.exchange_position_state(pos); } }; @@ -466,149 +511,9 @@ void ExecutionGateway::send_fix( } void ExecutionGateway::dispatch_incoming_fix_messages() { - // Note: We send orders through the REST api, and receive order updates over - // websocket (user data stream). The REST responses are added to the - // "incoming_new_orders" queue for logging purposes, but we only generate - // ExecutionReports in response to the websocket updates (which are added to - // the "incoming_order_updates" queue). - static constexpr const bool IGNORE_CREATE_ORDER_RESPONSE = true; - // Order creates - { - RestApiResult curr; - while (me_->incoming_new_orders.pop(curr)) { - if (!curr.response) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report due to invalid REST response. " - "{}", - curr.error); - continue; - } - const auto bts_key = - me_->key_map.get_bts_key(curr.response->clientOrderId); - if (!bts_key) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report. Did not resolve bts key from " - "exch clientOrderId '{}'", - curr.response->clientOrderId); - continue; - } - const auto exch_key = me_->key_map.get_exch_key(*bts_key); - if (exch_key != curr.response->clientOrderId) { - INFO_FN( - "dispatch_incoming_fix_messages", - "Ignoring update w/ old clientOrderId '{}'. The current " - "clientOrderId is '{}' (this is expected for CancelReplace " - "ops)", - curr.response->clientOrderId, exch_key); - continue; - } - bts::orderman::ExecutionReport er; - if (!convert_execution_report( - *curr.response, VENUE, me_->account.name(), - &me_->jetfix_connection, er)) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report from REST response. {}", - json_formatter(*curr.response)); - continue; - } - er.security = me_->sec_oracle.get_security( - VENUE, bts::api::SECURITYTYPE_SPOT, curr.response->symbol); - er.msg = curr.error; - if (!validate(er, er.msg)) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Generated invalid ExecutionReport. {}", er.msg); - continue; - } - // Always propagate ExecutionReport if there are errors - const bool has_error = !er.msg.empty(); - if (!IGNORE_CREATE_ORDER_RESPONSE || has_error) { - INFO_FN( - "dispatch_incoming_fix_messages", - "Dispatching ExecutionReport. bts_id: {}, exch_id: {}", - er.order_key, er.exchange_order_id); - me_->callbacks.exe_report( - std::make_shared(std::move(er)), - {}); - } - } - } - - // Order updates - { - user_data_streams::OrderUpdate curr; - while (me_->incoming_order_updates.pop(curr)) { - auto bts_key = me_->key_map.get_bts_key(curr.client_order_id); - if (!bts_key) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report. Did not resolve bts key from " - "exch clientOrderId '{}'", - curr.client_order_id); - continue; - } - auto exch_key = me_->key_map.get_exch_key(*bts_key); - if (exch_key != curr.client_order_id) { - INFO_FN( - "dispatch_incoming_fix_messages", - "Ignoring update w/ old clientOrderId '{}'. The current " - "clientOrderId is '{}' (this is expected for CancelReplace " - "ops)", - curr.client_order_id); - continue; - } - bts::orderman::ExecutionReport er; - if (!convert_execution_report( - curr, VENUE, me_->account.name(), &me_->jetfix_connection, - er)) { - WARN_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report from OrderUpdate: {}", - json_formatter(curr)); - continue; - } - er.security = me_->sec_oracle.get_security(VENUE, curr.symbol); - string error; - if (!validate(er, error)) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Generated invalid ExecutionReport. {}", error); - continue; - } - INFO_FN( - "dispatch_incoming_fix_messages", - "Dispatching ExecutionReport. bts_id: {}, exch_id: {}", - er.order_key, er.exchange_order_id); - me_->callbacks.exe_report( - std::make_shared(std::move(er)), - {}); - } - } - - // Account updates - { - user_data_streams::AccountUpdate u; - auto &acct_upds = me_->account_updates_vec; - acct_upds.clear(); - while (me_->incoming_account_updates.pop(acct_upds.emplace_back())) { - } - acct_upds.pop_back(); - if (!acct_upds.empty()) { - convert_positions( - VENUE, me_->account, acct_upds, - [sec_oracle = &me_->sec_oracle]( - bts::api::Venue venue, const std::string &name) { - return sec_oracle->get_security(venue, name); - }, - me_->pos.total_exchange_positions); - if (me_->callbacks.exchange_position_state) { - me_->callbacks.exchange_position_state(me_->pos); - } - } - } + me_->dispatch_internal_rejects(); + me_->dispatch_order_updates(); + me_->dispatch_account_updates(); } // Callbacks diff --git a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp new file mode 100644 index 000000000..56828f5cf --- /dev/null +++ b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp @@ -0,0 +1,35 @@ +#include "cancel_order_request.h" +#include "binance_execution/error_list.h" +#include "binance_execution/spots/order_key_map.h" +#include "binance_execution/spots/type_converters.h" +#include "binance_execution/type_converters.h" +#include "lib/core/TimeStamp.h" +#include "lib/orderman/CancelOrderMsg.h" +#include "lib/utils/checked_arithmetic.h" + +namespace binance_execution::spots { + +bool CancelOrderRequest::init( + const bts::orderman::CancelOrderMsg &m, + OrderKeyMap &key_map, + std::string &error) { + const std::size_t orig_error_sz = error.size(); + convert_sec_name(m.security, symbol, error); + orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); + origClientOrderId = key_map.get_exch_key(m.key); + // The exchange accepts either orderId, or origClientOrderId (only one + // need be populated). + if (orderId == 0 && origClientOrderId.empty()) { + ErrorList::append( + error, "Either orderId or origClientOrderId must be defined"); + } else { + // Can't reuse the same order id (only increment order version + // when init is successful, as this means the order will be sent). + key_map.increment_order_version(m.key); + newClientOrderId = key_map.get_exch_key(m.key); + } + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return error.size() == orig_error_sz; +} + +} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h index 4e357026a..3edd57fe3 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h +++ b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h @@ -4,8 +4,14 @@ #include #include +namespace bts::orderman { +struct CancelOrderMsg; +} + namespace binance_execution::spots { +struct OrderKeyMap; + /** * @a https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#cancel-order-trade */ @@ -17,6 +23,11 @@ struct CancelOrderRequest { OrderCancelRestriction cancelRestrictions = OrderCancelRestriction::NONE; std::int64_t recvWindow = 0; std::int64_t timestamp = 0; + + bool init( + const bts::orderman::CancelOrderMsg &m, + OrderKeyMap &key_map, + std::string &err); }; void to_json(nlohmann::json &j, const CancelOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp index ef07519d0..b5a9292e0 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp @@ -2,9 +2,29 @@ #include "binance_execution/enums/new_order_response_type.h" #include "binance_execution/price.h" #include "binance_execution/quantity.h" +#include "binance_execution/spots/type_converters.h" +#include "binance_execution/type_converters.h" +#include "lib/orderman/CreateOrderMsg.h" namespace binance_execution::spots { +bool CreateOrderRequest::init( + const bts::orderman::CreateOrderMsg &m, std::string &error) { + const std::size_t orig_error_sz = error.size(); + newClientOrderId = std::to_string(m.key.value()); + convert_sec_name(m.security, symbol, error); + convert_side(m.side, side, error); + price = m.price.value(); + quantity = m.quantity.full_value(); + convert_order_type(m.order_type, type, error); + convert_time_in_force(m.time_in_force, timeInForce, error); + if (error.size() != orig_error_sz) { + return false; + } + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return true; +} + void to_json(nlohmann::json &j, const CreateOrderRequest &req) { j["symbol"] = req.symbol; j["side"] = req.side; diff --git a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.h b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.h index 6fcdacb9d..014eda633 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.h +++ b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.h @@ -8,6 +8,10 @@ #include #include +namespace bts::orderman { +struct CreateOrderMsg; +} + namespace binance_execution::spots { /** @@ -24,6 +28,8 @@ struct CreateOrderRequest { TimeInForce timeInForce = TimeInForce::NONE; std::int64_t recvWindow = 0; NewOrderResponseType newOrderRespType = NewOrderResponseType::NONE; + + bool init(const bts::orderman::CreateOrderMsg &m, std::string &error); }; void to_json(nlohmann::json &j, const CreateOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp index 9c822720b..63c4580bb 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp @@ -1,9 +1,14 @@ #include "modify_order_request.h" #include "binance_execution/enums/new_order_response_type.h" #include "binance_execution/enums/time_in_force.h" +#include "binance_execution/error_list.h" #include "binance_execution/price.h" #include "binance_execution/quantity.h" +#include "binance_execution/spots/order_key_map.h" +#include "binance_execution/spots/type_converters.h" +#include "binance_execution/type_converters.h" #include "lib/io/JsonReader.h" +#include "lib/orderman/CancelReplaceOrderMsg.h" namespace binance_execution::spots { @@ -177,4 +182,42 @@ void to_query_params(std::string &qps, const ModifyOrderRequest &req) { qps.append(std::to_string(req.timestamp)); } +bool ModifyOrderRequest::init( + const bts::orderman::CancelReplaceOrderMsg &m, + OrderKeyMap &key_map, + std::string &error) { + const auto orig_error_sz = error.size(); + const auto orig_key = m.create_fields.key; + cancelOrderId = 0; + cancelOrigClientOrderId = key_map.get_exch_key(orig_key); + // The exchange accepts either cancelOrderId, or cancelOrigClientOrderId (only one + // need be populated). + if (cancelOrderId == 0 && cancelOrigClientOrderId.empty()) { + ErrorList::append( + error, + "Either cancelOrderId or cancelOrigClientOrderId must be defined"); + } + price = m.create_fields.price.value(); + quantity = m.create_fields.quantity.full_value(); + if (quantity == 0) { + ErrorList::append(error, "quantity is 0"); + } + convert_sec_name(m.create_fields.security, symbol, error); + convert_side(m.create_fields.side, side, error); + convert_order_type(m.create_fields.order_type, type, error); + cancelReplaceMode = CancelReplaceMode::STOP_ON_FAILURE; + convert_time_in_force(m.create_fields.time_in_force, timeInForce, error); + const bool is_valid = error.size() == orig_error_sz; + if (is_valid) { + // Can't reuse the same order id. + key_map.increment_order_version(orig_key); + cancelNewClientOrderId = + key_map.get_exch_key(orig_key).append(CANCEL_REPLACE_CANCEL_SUFFIX); + key_map.increment_order_version(orig_key); + newClientOrderId = key_map.get_exch_key(orig_key); + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + } + return is_valid; +} + } // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h index a1f39c1ca..b1be30af4 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h +++ b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h @@ -8,6 +8,8 @@ #include "binance_execution/enums/self_trade_prevention_mode.h" #include "binance_execution/enums/side.h" #include "binance_execution/enums/time_in_force.h" +#include "binance_execution/spots/order_key_map.h" +#include "lib/orderman/CancelReplaceOrderMsg.h" #include #include @@ -35,6 +37,8 @@ struct ModifyOrderRequest { orderRateLimitExceededMode(OrderRateLimitExceededMode::NONE), recvWindow(0), timestamp(0) {} + static constexpr const char *CANCEL_REPLACE_CANCEL_SUFFIX = "crc"; + std::string symbol; // mandatory Side side; // mandatory OrderType type; // mandatory @@ -59,6 +63,11 @@ struct ModifyOrderRequest { std::int64_t recvWindow; // optional std::int64_t timestamp; // mandatory // * Either cancelOrderId or cancelOrigClientOrderId must be specified + + bool init( + const bts::orderman::CancelReplaceOrderMsg &m, + OrderKeyMap &key_map, + std::string &error); }; void to_json(nlohmann::json &j, const ModifyOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/spots/order_key_map.h b/src/lib_binance_execution/binance_execution/spots/order_key_map.h index 20cef8c76..ce2459c37 100644 --- a/src/lib_binance_execution/binance_execution/spots/order_key_map.h +++ b/src/lib_binance_execution/binance_execution/spots/order_key_map.h @@ -26,6 +26,7 @@ struct OrderKeyMap { std::optional get_bts_key( const std::string &exch_order_key) { + std::lock_guard lock{instance_mx}; std::size_t n = exch_order_key.find_last_of('_'); bts::orderman::OrderKey k; if (n == std::string::npos) { @@ -40,6 +41,7 @@ struct OrderKeyMap { std::string get_exch_key(bts::orderman::OrderKey key) { const auto key_l = bts::system::math::checked_ultol(key.value()); + std::lock_guard lock{instance_mx}; std::string rv = std::to_string(key_l); uint32_t version = order_versions.try_emplace(key, 0).first->second; if (version > 0) { @@ -49,37 +51,13 @@ struct OrderKeyMap { return rv; } - // std::string get_exch_key( - // bts::orderman::OrderKey key, KeyType type = KeyType::CURRENT) { - // const auto key_l = bts::system::math::checked_ultol(key.value()); - // std::string rv = std::to_string(key_l); - // if (type == ORIGINAL) { - // return rv; - // } - // uint32_t version = order_versions.try_emplace(key, 0).first->second; - // if (version > 0) { - // rv.append("_"); - // rv.append(std::to_string(version)); - // } - // switch (type) { - // case CANCEL_REPLACE_CANCEL: - // rv.append("crc"); - // break; - // case CANCEL_REPLACE_REPLACE: - // rv.append("crr"); - // break; - // case CANCEL: - // rv.append("c"); - // break; - // default: - // break; - // } - // return rv; - // } - void increment_order_version(bts::orderman::OrderKey key) { + std::lock_guard lock{instance_mx}; order_versions.try_emplace(key, 0).first->second++; } + + private: + std::mutex instance_mx; }; } // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp b/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp index e7fb1dfa4..3a2962158 100644 --- a/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp +++ b/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp @@ -1,6 +1,8 @@ #include "rest_api_client.h" +#include "binance_execution/rest_api_error.h" #include "lib/io/http/http_client.h" #include "lib/io/http/http_response.h" +#include "lib/io/json_formatter.h" #include "lib/utils/crypto/hmac.h" namespace binance_execution::spots { @@ -78,9 +80,15 @@ void RestApiClient::request_( shrink_string<4096, 128>(http_res.body); http_res.reset(); HttpClient::send(http_req, &http_res); + res.http_code = http_res.code; + res.http_error = http_res.errors; if (!http_res.ok()) { ERROR_FN("{} {}", "res: {}", method, path, http_res); - res.error = std::move(http_res.errors); + try { + nlohmann::json::parse(http_res.body).get_to(res.error.emplace()); + } catch (const std::exception &ex) { + ERROR_FN("request", "Failed to parse error json. {}", http_res); + } return; } // Parse response diff --git a/src/lib_binance_execution/binance_execution/spots/type_converters.cpp b/src/lib_binance_execution/binance_execution/spots/type_converters.cpp index 360483982..8d313ee2b 100644 --- a/src/lib_binance_execution/binance_execution/spots/type_converters.cpp +++ b/src/lib_binance_execution/binance_execution/spots/type_converters.cpp @@ -1,8 +1,17 @@ #include "type_converters.h" +#include "binance_execution/error_list.h" +#include "binance_execution/spots/messages/cancel_order_request.h" +#include "binance_execution/spots/messages/create_order_request.h" +#include "binance_execution/spots/messages/modify_order_request.h" #include "binance_execution/spots/messages/order_record.h" +#include "binance_execution/spots/order_key_map.h" #include "binance_execution/type_converters.h" #include "lib/api/services/AdminService.pb.h" +#include "lib/domain_model/Security.pb.h" #include "lib/domain_model/common.h" +#include "lib/orderman/CancelOrderMsg.h" +#include "lib/orderman/CancelReplaceOrderMsg.h" +#include "lib/orderman/CreateOrderMsg.h" #include "lib/utils/checked_arithmetic.h" #include "lib/utils/string_utils.h" #include @@ -16,172 +25,24 @@ using bts::system::string_utils::parse_uint; using std::string; using std::vector; -bool convert_execution_report( - const user_data_streams::OrderUpdate &u, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er) { - copy_to_array<12>(account, er.account_name.value()); - er.exchange = venue; - er.transact_time = bts::orderman::ExchangeTimeStamp64{ - convert_time_stamp(u.transaction_time).nanos()}; - if (!parse_uint(u.client_order_id, er.order_key.value())) { - // For CancelReplace ops, we append a version number to the original key (separated by an - // underscore). Extract the original key. - const auto version_sep = u.client_order_id.find_last_of('_'); - if (version_sep != std::string::npos) { - parse_uint( - u.client_order_id.begin(), u.client_order_id.begin() + version_sep, - er.order_key.value()); - } else { - return false; - } +bool validate_security(const bts::api::Security *sec, string &error) { + if (sec == nullptr) { + ErrorList::append(error, "security is null"); + } else if (!sec->has_binance()) { + ErrorList::append(error, "security not of Binance (spots)"); + } else { + return true; } - bts_assert(bts::system::math::checked_ltoul( - u.order_id, - er.exchange_order_id.emplace() - .value())); - er.security = nullptr; - er.leg_security = nullptr; - er.side = u.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; - er.time_in_force = convert_time_in_force(u.time_in_force); - er.order_type = convert_order_type(u.order_type); - er.exec_type = convert_exec_type(u.execution_type, u.order_status); - er.order_status = convert_order_status(u.order_status); - er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; - er.price.value() = u.order_price; - er.quantity = u.order_quantity; - er.filled_quantity = u.filled_quantity; - er.last_shares = u.last_executed_quantity; - // er.max_show = u.data.; - er.last_price.value() = u.last_executed_price; - er.avg_price.value() = - u.filled_quantity > 0 - ? u.quote_asset_transacted_quantity / u.filled_quantity - : 0.0; - er.stop_price.value() = u.stop_price; - copy_to_array<40>(std::to_string(u.trade_id), er.exchange_fill_id.value()); - // er.msg = u.reject_reason; - er.cfg = jetfix_cfg; - // Irrelevant fields - er.ice_cl_ord_id = 0; - er.ice_company_id = 0; - er.memo = ""; - return true; + return false; } -bool convert_execution_report( - const OrderRecord &res, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er) { - copy_to_array<12>(account, er.account_name.value()); - er.exchange = venue; - er.transact_time = bts::orderman::ExchangeTimeStamp64{ - convert_time_stamp(res.workingTime).nanos()}; - if (!parse_uint(res.clientOrderId, er.order_key.value())) { +bool convert_sec_name( + const bts::api::Security *sec, std::string &name, std::string &error) { + if (!validate_security(sec, error)) { return false; } - bts_assert(bts::system::math::checked_ltoul( - res.orderId, - er.exchange_order_id.emplace() - .value())); - er.security = nullptr; - er.leg_security = nullptr; - er.side = res.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; - er.time_in_force = convert_time_in_force(res.timeInForce); - er.order_type = convert_order_type(res.type); - er.exec_type = bts::api::orderman::EXEC_TYPE_NEW; - er.order_status = convert_order_status(res.status); - er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; - er.price.value() = res.price; - er.quantity = res.origQty; - er.filled_quantity = res.executedQty; - er.max_show = 0; - std::tie(er.last_price.value(), er.last_shares) = - res.fills.empty() ? std::make_pair( - std::numeric_limits::quiet_NaN(), - bts::Quantity::create_new( - std::numeric_limits::quiet_NaN())) - : std::make_pair( - res.fills.back().price, - bts::Quantity::create_new(res.fills.back().qty)); - - const auto [total_fill_cost, total_fill_qty] = std::accumulate( - res.fills.begin(), res.fills.end(), std::make_pair(0.0, 0.0), - [](std::pair sum, const OrderRecord::Fill &f) { - return std::make_pair(sum.first + f.price, sum.second + f.qty); - }); - if (total_fill_qty != res.executedQty) { - LOG(OrderMan, bts::Error, - "Binance ExecutionReport total_fill_qty != res.executedQty. {} != {}", - total_fill_qty, res.executedQty); - bts_assert_msgf( - false, - "Binance ExecutionReport total_fill_qty != res.executedQty. {} != {}", - total_fill_qty, res.executedQty); - return false; - } - er.avg_price.value() = total_fill_qty > 0 - ? total_fill_cost / total_fill_qty - : std::numeric_limits::quiet_NaN(); - er.stop_price.value() = std::numeric_limits::quiet_NaN(); - er.msg = ""; - er.cfg = jetfix_cfg; - // Irrelevant fields - er.ice_cl_ord_id = 0; - er.ice_company_id = 0; - er.memo = ""; + name = sec->binance().security_desc(); return true; } -struct ExchPosCmp { - bool operator()( - const string &ticker, - const bts::orderman::ExchangePosition::UnitPosition &u) { - return ticker < u.exchange_sec_desc; - } - - bool operator()( - const bts::orderman::ExchangePosition::UnitPosition &u, - const string &ticker) { - return u.exchange_sec_desc < ticker; - } -}; - -void convert_positions( - bts::api::Venue venue, - const bts::api::admin::BTSAccount &account, - const vector &acct_upds, - std::function get_security, - vector &dest) { - ExchPosCmp exch_pos_cmp; - for (const auto &acct_upd : acct_upds) { - for (const auto &pos : acct_upd.balances) { - bts_assert(pos.free >= 0); - bts_assert(pos.locked >= 0); - auto it = std::lower_bound( - dest.begin(), dest.end(), pos.asset, exch_pos_cmp); - if (it == dest.end() || it->exchange_sec_desc != pos.asset) { - const auto *sec = get_security(venue, pos.asset); - if (sec == nullptr) { - continue; - } - bts::orderman::ExchangePosition::UnitPosition new_pos; - new_pos.exchange_sec_desc = pos.asset; - new_pos.security = sec; - new_pos.account_id = account.id(); - new_pos.account_name = account.name(); - it = dest.insert(it, new_pos); - } - it->position = pos.locked + pos.free; - it->avg_filled_price.value() = - std::numeric_limits::quiet_NaN(); - } - } -} - } // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/type_converters.h b/src/lib_binance_execution/binance_execution/spots/type_converters.h index 847200c07..6ff2a8378 100644 --- a/src/lib_binance_execution/binance_execution/spots/type_converters.h +++ b/src/lib_binance_execution/binance_execution/spots/type_converters.h @@ -1,37 +1,14 @@ #pragma once -#include "binance_execution/spots/messages/order_record.h" -#include "binance_execution/spots/user_data_streams/account_update.h" -#include "binance_execution/spots/user_data_streams/order_update.h" -#include "lib/config/protos/jetfix.pb.h" -#include "lib/orderman/orderman_utils/ExchangePosition.h" -#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/domain_model/Security.pb.h" -namespace bts::api::admin { -class BTSAccount; -} +#include namespace binance_execution::spots { -bool convert_execution_report( - const user_data_streams::OrderUpdate &u, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er); -bool convert_execution_report( - const OrderRecord &res, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er); +bool validate_security(const bts::api::Security *sec, std::string &error); -void convert_positions( - bts::api::Venue venue, - const bts::api::admin::BTSAccount &account, - const std::vector &acct_upds, - std::function get_security, - std::vector &dest); +bool convert_sec_name( + const bts::api::Security *sec, std::string &name, std::string &error); } // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp b/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp index bf4a8b354..e457373e8 100644 --- a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp +++ b/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp @@ -1,6 +1,7 @@ #include "listen_key_requestor.h" #include "binance_execution/spots/rest_api_client.h" #include "lib/core/TimeStamp.h" +#include "lib/io/json_formatter.h" #include #include @@ -9,6 +10,7 @@ namespace binance_execution::spots { using bts::core::Delegate; using bts::core::TimeStamp; +using bts::io::json_formatter; using std::optional; using std::string; using std::thread; @@ -50,7 +52,7 @@ struct ListenKeyRequestor::PImpl { continue; } bts_assert_msgf( - res.ok(), "Failed to get listen key. {}", res.error); + res.ok(), "Failed to get listen key. {}", json_formatter(res)); listen_key = res.response->listenKey; bts_assert(!listen_key.empty()); listen_key_timestamp = now; @@ -59,7 +61,8 @@ struct ListenKeyRequestor::PImpl { if ((now - listen_key_timestamp) > refresh_period) { const auto &res = rest_api_client.refresh_listen_key(); bts_assert_msgf( - res.ok(), "Failed to refresh listen key. {}", res.error); + res.ok(), "Failed to refresh listen key. {}", + json_formatter(res)); listen_key_timestamp = now; } std::this_thread::sleep_for(std::chrono::seconds(2)); diff --git a/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.cpp b/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.cpp index b15d1824f..3e586a84b 100644 --- a/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.cpp +++ b/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.cpp @@ -1,8 +1,20 @@ #include "order_update.h" +#include "binance_execution/type_converters.h" +#include "lib/config/protos/jetfix.pb.h" +#include "lib/domain_model/Security.pb.h" #include "lib/io/JsonReader.h" +#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/utils/checked_arithmetic.h" +#include "lib/utils/string_utils.h" + +#include namespace binance_execution::spots::user_data_streams { +using bts::system::math::checked_ltoul; +using bts::system::string_utils::copy_to_array; +using bts::system::string_utils::parse_uint; + void to_json(nlohmann::json &j, const OrderUpdate &u) { j["e"] = u.event_type; j["E"] = u.event_time; @@ -96,4 +108,48 @@ void from_json(const nlohmann::json &j, OrderUpdate &u) { j.at("V").get_to(u.self_trade_prevention); } +bool OrderUpdate::to_execution_report( + bts::orderman::ExecutionReport &er, std::string &error) const { + er.exchange = bts::api::VENUE_BINANCE; + er.transact_time = bts::orderman::ExchangeTimeStamp64{ + convert_time_stamp(transaction_time).nanos()}; + if (!parse_uint(client_order_id, er.order_key.value())) { + // For CancelReplace ops, we append a version number to the original key (separated by an + // underscore). Extract the original key. + const auto version_sep = client_order_id.find_last_of('_'); + if (version_sep != std::string::npos) { + parse_uint( + client_order_id.begin(), client_order_id.begin() + version_sep, + er.order_key.value()); + } else { + ErrorList::append( + error, + fmt::format( + "failed to parse client order id '{}'", client_order_id)); + return false; + } + } + bts_assert(checked_ltoul( + order_id, + er.exchange_order_id.emplace() + .value())); + er.side = side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; + er.time_in_force = convert_time_in_force(time_in_force); + er.order_type = convert_order_type(order_type); + er.exec_type = convert_exec_type(execution_type, order_status); + er.order_status = convert_order_status(order_status); + er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; + er.price.value() = order_price; + er.quantity = order_quantity; + er.filled_quantity = filled_quantity; + er.last_shares = last_executed_quantity; + er.last_price.value() = last_executed_price; + er.avg_price.value() = + filled_quantity > 0 ? quote_asset_transacted_quantity / filled_quantity + : 0.0; + er.stop_price.value() = stop_price; + copy_to_array<40>(std::to_string(trade_id), er.exchange_fill_id.value()); + return true; +} + } // namespace binance_execution::spots::user_data_streams diff --git a/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.h b/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.h index 3c90ce643..d56093466 100644 --- a/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.h +++ b/src/lib_binance_execution/binance_execution/spots/user_data_streams/order_update.h @@ -13,6 +13,13 @@ #include #include +namespace bts::config { +class JetFIX_Connection; +} +namespace bts::orderman { +class ExecutionReport; +} + namespace binance_execution::spots::user_data_streams { /** @@ -53,6 +60,9 @@ struct OrderUpdate { double quote_order_quantity; // json: Q std::optional working_time; // json: W SelfTradePreventionMode self_trade_prevention; // json: V + + bool to_execution_report( + bts::orderman::ExecutionReport &er, std::string &error) const; }; void to_json(nlohmann::json &j, const OrderUpdate &d); diff --git a/src/lib_binance_execution/binance_execution/type_converters.h b/src/lib_binance_execution/binance_execution/type_converters.h index e2da85ff2..07e88baef 100644 --- a/src/lib_binance_execution/binance_execution/type_converters.h +++ b/src/lib_binance_execution/binance_execution/type_converters.h @@ -5,15 +5,12 @@ #include "binance_execution/enums/order_type.h" #include "binance_execution/enums/side.h" #include "binance_execution/enums/time_in_force.h" +#include "binance_execution/error_list.h" #include "lib/api/services/OrderService.pb.h" #include "lib/core/TimeStamp.h" #include "lib/domain/Side.h" #include "lib/utils/bts_assert.h" -namespace bts::api::admin { -class BTSAccount; -} - namespace binance_execution { bts::api::orderman::NativeOrderType convert_order_type(OrderType tif); @@ -33,6 +30,7 @@ bts::core::TimeSpan convert_time_span(std::int64_t ts); bts::domain::Side convert_side(Side side); Side convert_side(bts::domain::Side side); Side convert_side(bts::api::Side side); +bool convert_side(bts::api::Side side, Side &res, std::string &error); bts::api::orderman::ExecType convert_exec_type( ExecutionType exec_type, OrderStatus order_status); @@ -138,6 +136,21 @@ inline Side convert_side(bts::api::Side side) { } } +inline bool convert_side(bts::api::Side side, Side &res, std::string &error) { + switch (side) { + case bts::api::SIDE_BUY: + res = Side::BUY; + break; + case bts::api::SIDE_SELL: + res = Side::SELL; + break; + default: + ErrorList::append(error, "invalid side"); + return false; + } + return true; +} + inline bts::api::orderman::TimeInForce convert_time_in_force(TimeInForce tif) { switch (tif.value) { case TimeInForce::GTC: @@ -201,6 +214,30 @@ inline OrderType convert_order_type(bts::api::orderman::NativeOrderType tif) { } } +inline bool convert_order_type( + bts::api::orderman::NativeOrderType native_type, + OrderType &result, + std::string &error) { + switch (native_type) { + case bts::api::orderman::NATIVE_ORDER_TYPE_LIMIT: + result = OrderType::LIMIT; + break; + case bts::api::orderman::NATIVE_ORDER_TYPE_MARKET: + result = OrderType::MARKET; + break; + case bts::api::orderman::NATIVE_ORDER_TYPE_UNDEFINED: + result = OrderType::NONE; + break; + default: + ErrorList::append( + error, fmt::format( + "NativeOrderType '{}' not implemented at Binance", + bts::api::orderman::NativeOrderType_Name(native_type))); + return false; + } + return true; +} + inline TimeInForce convert_time_in_force(bts::api::orderman::TimeInForce tif) { switch (tif) { case bts::api::orderman::TIME_IN_FORCE_GTC: @@ -217,6 +254,35 @@ inline TimeInForce convert_time_in_force(bts::api::orderman::TimeInForce tif) { } } +inline bool convert_time_in_force( + bts::api::orderman::TimeInForce tif, + TimeInForce &result, + std::string &error, + bool allow_none = false) { + switch (tif) { + case bts::api::orderman::TIME_IN_FORCE_GTC: + result = TimeInForce::GTC; + break; + case bts::api::orderman::TIME_IN_FORCE_DAY: + result = TimeInForce::GTD; + break; + case bts::api::orderman::TIME_IN_FORCE_IOC: + result = TimeInForce::IOC; + break; + case bts::api::orderman::TIME_IN_FORCE_UNDEFINED: + if (allow_none) { + result = TimeInForce::NONE; + break; + } + ErrorList::append(error, "TimeInForce is undefined"); + return false; + default: + ErrorList::append(error, "Unknown BinanceTimeInForce"); + return false; + } + return true; +} + inline ExecutionType convert_exec_type(bts::api::orderman::ExecType exec_type) { switch (exec_type) { case bts::api::orderman::EXEC_TYPE_NEW: diff --git a/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp b/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp index d13025e9a..f9d24ec36 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp @@ -1,7 +1,9 @@ #include "execution_gateway.h" +#include "binance_execution/enums/order_status.h" +#include "binance_execution/error_list.h" +#include "binance_execution/execution_report_store.h" #include "binance_execution/rest_api_result.h" -#include "binance_execution/type_converters.h" -#include "binance_execution/usds_futures/messages/create_order_response.h" +#include "binance_execution/usds_futures/messages/create_order_request.h" #include "binance_execution/usds_futures/messages/modify_order_request.h" #include "binance_execution/usds_futures/rest_api_client.h" #include "binance_execution/usds_futures/type_converters.h" @@ -11,7 +13,6 @@ #include "lib/api/services/AdminService.pb.h" #include "lib/api/services/OrderService.h" #include "lib/config/protos/jetfix.pb.h" -#include "lib/core/TimeStamp.h" #include "lib/domain_model/Security.h" #include "lib/domain_model/Security.pb.h" #include "lib/domain_model/Side.h" @@ -30,6 +31,7 @@ #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/utils/checked_arithmetic.h" #include "lib/utils/overloaded.h" +#include "lib/utils/string_utils.h" #include #include @@ -37,7 +39,6 @@ #include #include #include -#include #include #include @@ -61,34 +62,30 @@ using bts::io::json_formatter; using bts::orderman::CancelOrderMsg; using bts::orderman::CancelReplaceOrderMsg; using bts::orderman::CreateOrderMsg; +using bts::orderman::ExecutionReport; using bts::orderman::NewAdminDataEvent; using bts::orderman::NewSecuritiesEvent; +using bts::orderman::OrderKey; using bts::orderman::OrderMsg; using bts::orderman::RFQMsg; +using bts::system::math::checked_ltou; using std::int64_t; using std::optional; +using std::shared_ptr; using std::size_t; using std::string; using std::uint32_t; -using std::unordered_map; using std::vector; -static void append_error(string &dest, const char *err) { - if (!dest.empty()) { - dest.append(" "); - } - dest.append(err); -} - static void validate(bts::orderman::OrderKey key, string &error) { if (key.value() == 0) { - append_error(error, "order key is 0."); + ErrorList::append(error, "order key is 0"); } } static void validate(bts::orderman::ExchangeOrderID64 key, string &error) { if (key.value() == 0) { - append_error(error, "exch order key is 0."); + ErrorList::append(error, "exch order key is 0"); } } @@ -96,94 +93,35 @@ static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) { if (std::holds_alternative(key)) { validate(std::get(key), error); } else { - append_error(error, "exch order id is undefined or not an integer."); + ErrorList::append(error, "exch order id is undefined or not an integer"); } } static void validate(bts::Quantity qty, string &error) { if (qty.full_value() == 0) { - append_error(error, "quantity is 0."); + ErrorList::append(error, "quantity is 0"); } } static void validate(const bts::api::Security *sec, string &error) { if (sec == nullptr) { - append_error(error, "security is null."); + ErrorList::append(error, "security is null"); } else if (!sec->has_binance_usds_futures()) { - append_error(error, "security not of Binance Usds Futures."); + ErrorList::append(error, "security not of Binance Usds Futures"); } } -static void validate(bts::api::Side side, string &error) { - switch (side) { - case bts::api::SIDE_BUY: - case bts::api::SIDE_SELL: - break; - default: - append_error(error, "invalid side."); - break; - } -} - -static void validate(bts::api::orderman::TimeInForce bts_tif, string &error) { - const auto tif = convert_time_in_force(bts_tif); - switch (tif.value) { - case TimeInForce::GTC: - case TimeInForce::IOC: - case TimeInForce::FOK: - break; - default: - append_error( - error, fmt::format("invalid TimeInForce '{}'.", bts_tif).c_str()); - break; - } -} - -// TODO: Store exch order id in EMNativeOrder (until then, it'll always be 0). -// We can ignore this for now and use "orig client id" in cancel/replace requests. -static constexpr const bool VALIDATE_EXCH_ORDER_ID = false; - -static bool validate(const CreateOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.security, error); - validate(m.key, error); - validate(m.quantity, error); - validate(m.side, error); - validate(m.time_in_force, error); - return error.size() == orig_sz; -} - -static bool validate(const CancelOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.security, error); - validate(m.key, error); - if (VALIDATE_EXCH_ORDER_ID) { - validate(m.exchange_order_id, error); - } - validate(m.side, error); - return error.size() == orig_sz; -} - -static bool validate(const CancelReplaceOrderMsg &m, string &error) { - const size_t orig_sz = error.size(); - validate(m.create_fields, error); - if (VALIDATE_EXCH_ORDER_ID) { - validate(m.exchange_order_id, error); - } - return error.size() == orig_sz; -} - static bool validate(const bts::orderman::ExecutionReport &er, string &error) { const size_t orig_sz = error.size(); validate(er.order_key, error); validate(er.quantity, error); validate(er.exchange_order_id, error); if (er.account_name.value().empty()) { - append_error(error, "account is empty."); + ErrorList::append(error, "account is empty"); } validate(er.security, error); if (er.cfg == nullptr) { - append_error(error, "jetfix config is null."); + ErrorList::append(error, "jetfix config is null"); } return error.size() == orig_sz; } @@ -202,37 +140,42 @@ struct ExecutionGateway::PImpl { ExecutionGatewayConfig config, bts::config::JetFIX::Connection jetfix_connection) : sec_oracle(std::move(sec_oracle)), config(std::move(config)), - jetfix_connection(std::move(jetfix_connection)), + jetfix_connection(std::make_shared( + std::move(jetfix_connection))), rest_api_client( this->config.rest_endpoint, this->config.api_key, this->config.secret_key), - listen_key_requestor(this->rest_api_client) { - bts_assert( - this->jetfix_connection.venue() == - bts::api::VENUE_BINANCE_USDS_FUTURES); - bts_assert(!this->jetfix_connection.name().empty()); - pos.exchange = bts::api::VENUE_BINANCE_USDS_FUTURES; + listen_key_requestor(this->rest_api_client), + orders( + [](const std::string &exch_key) { + std::uint32_t k; + bts::system::string_utils::parse_uint(exch_key, k); + return OrderKey{k}; + }, + this->config.account, + this->jetfix_connection) { + bts_assert(this->jetfix_connection->venue() == VENUE); + bts_assert(!this->jetfix_connection->name().empty()); + pos.exchange = VENUE; } bts::md::SecurityOracle sec_oracle; ExecutionGatewayConfig config; - bts::config::JetFIX_Connection jetfix_connection; + std::shared_ptr jetfix_connection; - unordered_map> response_handlers; RestApiClient rest_api_client; optional user_data_sub; Delegate account_update_callback; Delegate order_update_callback; ListenKeyRequestor listen_key_requestor; - spsc_queue incoming_msgs{1024}; spsc_queue incoming_account_updates{1024}; spsc_queue incoming_order_updates{1024}; - spsc_queue> incoming_new_orders{1024}; - + spsc_queue> internal_order_rejects{256}; vector account_updates_vec; + ExecutionReportStore orders; ExecutionGatewayCallbacks callbacks; bts::orderman::ExchangePosition pos; bts::api::admin::BTSAccount account; @@ -275,94 +218,186 @@ struct ExecutionGateway::PImpl { } void create_order(const CreateOrderMsg &m) { - string sec_desc = m.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.security); - RestApiResult res; - validate(m, res.error); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "create_order", - "{} {} {} @ {} (id={}, type={}, tif={}, err={})", m.side, m.quantity, - sec_desc, m.price, m.key, m.order_type, m.time_in_force, res.error); CreateOrderRequest req; - req.newClientOrderId = std::to_string(m.key.value()); - req.symbol = bts::securities::descriptor(*m.security); - req.side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; - req.price = m.price.value(); - req.quantity = m.quantity.full_value(); - req.type = convert_order_type(m.order_type); - req.timeInForce = convert_time_in_force(m.time_in_force); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - if (res.ok()) { - res = rest_api_client.create_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "create_order", "id={}; {}", - m.key, json_formatter(res)); + std::string err; + if (!req.init(m, err)) { + ERROR_FN( + "create_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Rejected " + "internally (request not sent). {}", + m.side, m.quantity, req.symbol, m.price, m.key, m.order_type, + m.time_in_force, err); + internal_order_rejects.push(orders.reject(m, err)); + return; + } + INFO_FN( + "create_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", m.side, + m.quantity, req.symbol, m.price, m.key, m.order_type, + m.time_in_force); + const auto res = rest_api_client.create_order(req); + if (!res.ok()) { + ERROR_FN("create_order", "id={}. {}", m.key, json_formatter(res)); + internal_order_rejects.push( + orders.reject(m, json_formatter(*res.error).to_string())); } - incoming_new_orders.push(std::move(res)); } void update_order(const CancelReplaceOrderMsg &m) { - string sec_desc = - m.create_fields.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.create_fields.security); - RestApiResult res; - validate(m, res.error); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "update_order", - "{} {} {} @ {} (id={}, type={}, tif={}, err={})", - m.create_fields.side, m.create_fields.quantity, sec_desc, - m.create_fields.price, m.create_fields.key, - m.create_fields.order_type, m.create_fields.time_in_force, res.error); - if (res.ok()) { - ModifyOrderRequest req; - req.orderId = - bts::system::math::checked_ultol(m.exchange_order_id.value()); - req.origClientOrderId = std::to_string(m.create_fields.key.value()); - // The exchange accepts either orderId, or origClientOrderId (only one - // need be populated). - bts_assert(req.orderId != 0 || !req.origClientOrderId.empty()); - req.price = m.create_fields.price.value(); - req.priceMatch = PriceMatchMode::NONE; - req.quantity = m.create_fields.quantity.full_value(); - req.symbol = - m.create_fields.security->binance_usds_futures().security_desc(); - req.side = convert_side(m.create_fields.side); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - res = rest_api_client.modify_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "update_order", "id={}; {}", - req.orderId, json_formatter(res)); + const auto &fields = m.create_fields; + std::string err; + ModifyOrderRequest req; + if (!req.init(m, err)) { + ERROR_FN( + "update_order", + "{} {} {} @ {} (id={}, exch_id={}, type={}, tif={}). Rejected " + "internally (request not sent). {}", + fields.side, fields.quantity, req.symbol, fields.price, fields.key, + m.exchange_order_id, fields.order_type, fields.time_in_force, err); + internal_order_rejects.push(orders.set_error( + fields.key, + "Update rejected internally (request not sent). " + err)); + return; + } + INFO_FN( + "update_order", + "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", + fields.side, fields.quantity, req.symbol, fields.price, fields.key, + m.exchange_order_id, fields.order_type, fields.time_in_force); + const auto res = rest_api_client.modify_order(req); + if (!res.ok()) { + ERROR_FN( + "update_order", "id={}, exch_id={}. {}", fields.key, + m.exchange_order_id, json_formatter(res)); } } void cancel_order(const CancelOrderMsg &m) { - string sec_desc = m.security == nullptr - ? "(null)" - : bts::securities::descriptor(*m.security); - RestApiResult res; - validate(m, res.error); CancelOrderRequest req; - LOG_FN( - res.ok() ? bts::Info : bts::Error, "cancel_order", - "{} {} (id={}, exch_id={}, err={})", m.side, sec_desc, m.key, - m.exchange_order_id, res.error); - if (res.ok()) { - req.orderId = - bts::system::math::checked_ultol(m.exchange_order_id.value()); - req.origClientOrderId = std::to_string(m.key.value()); - // The exchange accepts either orderId, or origClientOrderId (only one - // need be populated). - bts_assert(req.orderId != 0 || !req.origClientOrderId.empty()); - req.symbol = m.security->binance_usds_futures().security_desc(); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - res = rest_api_client.cancel_order(req); - LOG_FN( - res.ok() ? bts::Info : bts::Error, "cancel_order", - "id={}, exch_id={}; {}", m.key, m.exchange_order_id, - json_formatter(res)); + string err; + if (!req.init(m, err)) { + ERROR_FN( + "cancel_order", "{} {} (id={}, exch_id={}). {}", m.side, + req.symbol, m.key, m.exchange_order_id, err); + internal_order_rejects.push(orders.set_error( + m.key, "Rejected internally (request not sent). " + err)); + return; } + INFO_FN( + "cancel_order", "{} {} (id={}, exch_id={})", m.side, req.symbol, + m.key, m.exchange_order_id); + const auto res = rest_api_client.cancel_order(req); + if (!res.ok()) { + ERROR_FN( + "cancel_order", "{} {} (id={}, exch_id={}). {}", m.side, + req.symbol, m.key, m.exchange_order_id, json_formatter(res)); + internal_order_rejects.push(orders.set_error( + m.key, + fmt::format( + "Cancel request failed. {}", json_formatter(*res.error)))); + } + } + + void dispatch_internal_rejects() { + shared_ptr curr; + while (internal_order_rejects.pop(curr)) { + INFO_FN("dispatch_internal_rejects", "{}", curr->to_string()); + callbacks.exe_report(curr, {}); + } + } + + void dispatch_order_updates() { + if (incoming_order_updates.empty()) { + return; + } + user_data_streams::OrderUpdate u; + OrderKey key; + while (incoming_order_updates.pop(u)) { + bts_assert(checked_ltou(u.data.order_id, key.value())); + shared_ptr er_p = + orders.update(u.data.client_order_id, [&](auto &er_p, bool) { + auto &er = *er_p; + if (!u.to_execution_report(er)) { + WARN_FN( + "update", "Failed to initialize exe report. {}", + json_formatter(u)); + return; + } + er.security = sec_oracle.get_security( + VENUE, bts::api::SECURITYTYPE_FUTURE, u.data.symbol); + }); + const ExecutionReport &er = *er_p; + string error; + if (!validate(er, error)) { + ERROR_FN( + "dispatch_order_updates", + "Generated invalid ExecutionReport. {}", error); + continue; + } + switch (u.data.order_status.value) { + case OrderStatus::FILLED: + case OrderStatus::CANCELED: + case OrderStatus::REJECTED: { + std::size_t n_active = orders.erase(er.order_key); + INFO_FN( + "dispatch_order_updates", + "Order reached terminal state {} (id={}). {} active orders.", + u.data.order_status, u.data.client_order_id, n_active); + break; + } + default: + break; + } + INFO_FN( + "dispatch_order_updates", + "Firing exe report callback. {} {} {} @ {} {} (filled {}/{}, " + "id={}). {} active orders", + er.side, er.security->binance_usds_futures().security_desc(), + er.quantity, er.price, er.order_status, er.filled_quantity, + er.quantity, er.order_key, orders.size()); + callbacks.exe_report(er_p, {}); + } + } + + void dispatch_account_updates() { + user_data_streams::AccountUpdate u; + account_updates_vec.clear(); + while (incoming_account_updates.pop(account_updates_vec.emplace_back())) { + } + account_updates_vec.pop_back(); + if (account_updates_vec.empty()) { + return; + } + auto &dest = pos.total_exchange_positions; + for (const auto &acct_upd : account_updates_vec) { + for (const auto &pos : acct_upd.data.positions) { + bts_assert(pos.position_amount >= 0); + auto it = std::lower_bound( + dest.begin(), dest.end(), pos.symbol, + [](const bts::orderman::ExchangePosition::UnitPosition &u, + const string &ticker) { + return u.exchange_sec_desc < ticker; + }); + if (it == dest.end() || it->exchange_sec_desc != pos.symbol) { + const auto *sec = sec_oracle.get_security( + VENUE, bts::api::SECURITYTYPE_FUTURE, pos.symbol); + if (sec == nullptr) { + continue; + } + bts::orderman::ExchangePosition::UnitPosition new_pos; + new_pos.exchange_sec_desc = pos.symbol; + new_pos.security = sec; + new_pos.account_id = account.id(); + new_pos.account_name = account.name(); + it = dest.insert(it, new_pos); + } + it->position = pos.position_side == PositionSide::SHORT + ? -pos.position_amount + : pos.position_amount; + it->avg_filled_price.value() = pos.break_even_price; + } + } + callbacks.exchange_position_state(pos); } }; @@ -376,7 +411,7 @@ ExecutionGateway::ExecutionGateway( std::move(jetfix_connection))) { bts_assert( me_->config.account.size() <= - bts::orderman::ExecutionReport{}.account_name.value().size()); + ExecutionReport{}.account_name.value().size()); } ExecutionGateway::~ExecutionGateway() = default; @@ -425,7 +460,8 @@ void ExecutionGateway::send_msgs( [&](const CancelReplaceOrderMsg &m) { me_->update_order(m); }, [&](const CancelOrderMsg &m) { me_->cancel_order(m); }, [&](const RFQMsg &) { - bts_assert_msg(false, "Binance RFQ not implemented"); + bts_assert_msg( + false, "BinanceUsdsFutures RFQ not implemented"); }, }, msg); @@ -438,97 +474,9 @@ void ExecutionGateway::send_fix( } void ExecutionGateway::dispatch_incoming_fix_messages() { - // Note: We send orders through the REST api, and receive order updates over - // websocket (user data stream). The REST responses are added to the - // "incoming_new_orders" queue for logging purposes, but we only generate - // ExecutionReports in response to the websocket updates (which are added to - // the "incoming_order_updates" queue). - static constexpr const bool IGNORE_CREATE_ORDER_RESPONSE = true; - // Order creates - { - RestApiResult r; - while (me_->callbacks.exe_report && me_->incoming_new_orders.pop(r)) { - if (!r.response) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Cannot generate exe report from BinanceCreateOrderResult. {}", - r.error); - continue; - } - bts::orderman::ExecutionReport er; - if (!convert_execution_report( - *r.response, VENUE, me_->account.name(), - &me_->jetfix_connection, er)) { - WARN_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report from CreateOrderResponse: {}", - json_formatter(*r.response)); - continue; - } - er.security = me_->sec_oracle.get_security(VENUE, r.response->symbol); - er.msg = r.error; - if (!validate(er, er.msg)) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Generated invalid ExecutionReport. {}", er.msg); - continue; - } - // Always propagate ExecutionReport if there are errors - const bool has_error = !er.msg.empty(); - if (!IGNORE_CREATE_ORDER_RESPONSE || has_error) { - me_->callbacks.exe_report( - std::make_shared(std::move(er)), - {}); - } - } - } - // Order updates - { - user_data_streams::OrderUpdate u; - while (me_->callbacks.exe_report && me_->incoming_order_updates.pop(u)) { - bts::orderman::ExecutionReport er; - if (!convert_execution_report( - u, VENUE, me_->account.name(), &me_->jetfix_connection, er)) { - WARN_FN( - "dispatch_incoming_fix_messages", - "Failed to generate exe report from OrderUpdate: {}", - json_formatter(u)); - continue; - } - er.security = me_->sec_oracle.get_security(VENUE, u.data.symbol); - string error; - if (!validate(er, error)) { - ERROR_FN( - "dispatch_incoming_fix_messages", - "Generated invalid ExecutionReport. {}", error); - continue; - } - me_->callbacks.exe_report( - std::make_shared(std::move(er)), - {}); - } - } - // Account updates - { - user_data_streams::AccountUpdate u; - auto &acct_upds = me_->account_updates_vec; - acct_upds.clear(); - while (me_->incoming_account_updates.pop(acct_upds.emplace_back())) { - } - acct_upds.pop_back(); - if (!acct_upds.empty()) { - convert_positions( - VENUE, me_->account, acct_upds, - [sec_oracle = &me_->sec_oracle]( - bts::api::Venue venue, const std::string &name) { - return sec_oracle->get_security(venue, name); - }, - me_->pos.total_exchange_positions); - if (me_->callbacks.exchange_position_state) { - me_->callbacks.exchange_position_state(me_->pos); - } - } - } + me_->dispatch_internal_rejects(); + me_->dispatch_order_updates(); + me_->dispatch_account_updates(); } // Callbacks diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp new file mode 100644 index 000000000..835b9071d --- /dev/null +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp @@ -0,0 +1,26 @@ +#include "cancel_order_request.h" +#include "binance_execution/error_list.h" +#include "binance_execution/type_converters.h" +#include "binance_execution/usds_futures/type_converters.h" +#include "lib/orderman/CancelOrderMsg.h" +#include "lib/utils/checked_arithmetic.h" + +namespace binance_execution::usds_futures { + +bool CancelOrderRequest::init( + const bts::orderman::CancelOrderMsg &m, std::string &error) { + const auto orig_error_sz = error.size(); + convert_sec_name(m.security, symbol, error); + orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); + origClientOrderId = std::to_string(m.key.value()); + // The exchange accepts either orderId, or origClientOrderId (only one + // need be populated). + if (orderId == 0 && origClientOrderId.empty()) { + ErrorList::append( + error, "Either orderId or origClientOrderId must be defined"); + } + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return error.size() == orig_error_sz; +} + +} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.h b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.h index 54619362e..cc27c2f31 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.h @@ -3,6 +3,10 @@ #include #include +namespace bts::orderman { +struct CancelOrderMsg; +} + namespace binance_execution::usds_futures { /** @@ -13,6 +17,8 @@ struct CancelOrderRequest { std::string origClientOrderId; std::string symbol; std::int64_t timestamp = 0; + + bool init(const bts::orderman::CancelOrderMsg &m, std::string &error); }; void to_json(nlohmann::json &j, const CancelOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp new file mode 100644 index 000000000..9c71da7f2 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp @@ -0,0 +1,25 @@ +#include "create_order_request.h" +#include "binance_execution/type_converters.h" +#include "binance_execution/usds_futures/type_converters.h" +#include "lib/orderman/CreateOrderMsg.h" + +namespace binance_execution::usds_futures { + +bool CreateOrderRequest::init( + const bts::orderman::CreateOrderMsg &m, std::string &error) { + const auto orig_error_sz = error.size(); + newClientOrderId = std::to_string(m.key.value()); + convert_sec_name(m.security, symbol, error); + side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; + price = m.price.value(); + quantity = m.quantity.full_value(); + type = convert_order_type(m.order_type); + timeInForce = convert_time_in_force(m.time_in_force); + if (error.size() != orig_error_sz) { + return false; + } + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return true; +} + +} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.h b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.h index 4d34dde1a..ccd2cb6c4 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.h @@ -9,6 +9,10 @@ #include #include +namespace bts::orderman { +struct CreateOrderMsg; +} + namespace binance_execution::usds_futures { /** @@ -24,6 +28,8 @@ struct CreateOrderRequest { double quantity = 0; TimeInForce timeInForce = TimeInForce::NONE; std::int64_t recvWindow = 0; + + bool init(const bts::orderman::CreateOrderMsg &m, std::string &error); }; void to_json(nlohmann::json &j, const CreateOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.cpp b/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.cpp new file mode 100644 index 000000000..1bb789891 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.cpp @@ -0,0 +1,37 @@ +#include "modify_order_request.h" +#include "binance_execution/error_list.h" +#include "binance_execution/type_converters.h" +#include "lib/orderman/CancelReplaceOrderMsg.h" +#include "lib/utils/checked_arithmetic.h" + +namespace binance_execution::usds_futures { + +bool ModifyOrderRequest::init( + const bts::orderman::CancelReplaceOrderMsg &m, std::string &error) { + const auto orig_error_sz = error.size(); + const auto &cf = m.create_fields; + if (cf.security == nullptr) { + ErrorList::append(error, "security is null"); + } else if (!cf.security->has_binance_usds_futures()) { + ErrorList::append(error, "security not of Binance Usds Futures"); + } else { + symbol = cf.security->binance_usds_futures().security_desc(); + } + orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); + origClientOrderId = std::to_string(cf.key.value()); + // The exchange accepts either orderId, or origClientOrderId (only one + // need be populated). + if (orderId == 0 && origClientOrderId.empty()) { + ErrorList::append( + error, "Either orderId or origClientOrderId must be defined"); + } + bts_assert(orderId != 0 || !origClientOrderId.empty()); + price = cf.price.value(); + priceMatch = PriceMatchMode::NONE; + quantity = cf.quantity.full_value(); + side = convert_side(cf.side); + timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return error.size() == orig_error_sz; +} + +} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.h b/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.h index 8b92aa257..b4ca3aefc 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/modify_order_request.h @@ -8,6 +8,10 @@ #include #include +namespace bts::orderman { +struct CancelReplaceOrderMsg; +} + namespace binance_execution::usds_futures { /** @@ -24,6 +28,8 @@ struct ModifyOrderRequest { std::int64_t recvWindow = 0; // optional std::int64_t timestamp = 0; // mandatory // * Either orderId or origClientOrderId must be specified + + bool init(const bts::orderman::CancelReplaceOrderMsg &m, std::string &error); }; void to_json(nlohmann::json &j, const ModifyOrderRequest &req); diff --git a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp index a41b31870..01c4eddc5 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp @@ -78,9 +78,15 @@ void RestApiClient::request_( shrink_string<4096, 128>(http_res.body); http_res.reset(); HttpClient::send(http_req, &http_res); + res.http_code = http_res.code; + res.http_error = http_res.errors; if (!http_res.ok()) { ERROR_FN("{} {}", "res: {}", method, path, http_res); - res.error = std::move(http_res.errors); + try { + nlohmann::json::parse(http_res.body).get_to(res.error.emplace()); + } catch (const std::exception &ex) { + ERROR_FN("request", "Failed to parse error json. {}", http_res); + } return; } // Parse response diff --git a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp index 24138d4f0..8ae5c61fd 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp @@ -1,16 +1,44 @@ #include "type_converters.h" +#include "binance_execution/error_list.h" #include "binance_execution/type_converters.h" +#include "binance_execution/usds_futures/messages/cancel_order_request.h" +#include "binance_execution/usds_futures/messages/create_order_request.h" +#include "binance_execution/usds_futures/messages/modify_order_request.h" #include "lib/api/services/AdminService.pb.h" +#include "lib/orderman/CancelOrderMsg.h" +#include "lib/orderman/CancelReplaceOrderMsg.h" +#include "lib/orderman/CreateOrderMsg.h" #include "lib/utils/checked_arithmetic.h" #include "lib/utils/string_utils.h" namespace binance_execution::usds_futures { +using bts::system::math::checked_ultol; using bts::system::string_utils::copy_to_array; using bts::system::string_utils::parse_uint; using std::string; using std::vector; +bool validate_security(const bts::api::Security *sec, string &error) { + if (sec == nullptr) { + ErrorList::append(error, "security is null"); + } else if (!sec->has_binance_usds_futures()) { + ErrorList::append(error, "security not of Binance Usds Futures"); + } else { + return true; + } + return false; +} + +bool convert_sec_name( + const bts::api::Security *sec, std::string &name, std::string &error) { + if (!validate_security(sec, error)) { + return false; + } + name = sec->binance_usds_futures().security_desc(); + return true; +} + bool convert_execution_report( const user_data_streams::OrderUpdate &u, bts::api::Venue venue, @@ -98,20 +126,6 @@ bool convert_execution_report( return true; } -struct ExchPosCmp { - bool operator()( - const string &ticker, - const bts::orderman::ExchangePosition::UnitPosition &u) { - return ticker < u.exchange_sec_desc; - } - - bool operator()( - const bts::orderman::ExchangePosition::UnitPosition &u, - const string &ticker) { - return u.exchange_sec_desc < ticker; - } -}; - void convert_positions( bts::api::Venue venue, const bts::api::admin::BTSAccount &account, @@ -119,12 +133,13 @@ void convert_positions( std::function get_security, vector &dest) { - ExchPosCmp exch_pos_cmp; for (const auto &acct_upd : acct_upds) { for (const auto &pos : acct_upd.data.positions) { bts_assert(pos.position_amount >= 0); auto it = std::lower_bound( - dest.begin(), dest.end(), pos.symbol, exch_pos_cmp); + dest.begin(), dest.end(), pos.symbol, + [](const bts::orderman::ExchangePosition::UnitPosition &u, + const string &ticker) { return u.exchange_sec_desc < ticker; }); if (it == dest.end() || it->exchange_sec_desc != pos.symbol) { const auto *sec = get_security(venue, pos.symbol); if (sec == nullptr) { @@ -145,4 +160,67 @@ void convert_positions( } } +bool convert_create_request( + const bts::orderman::CreateOrderMsg &m, + CreateOrderRequest &req, + std::string &error) { + const auto orig_error_sz = error.size(); + req.newClientOrderId = std::to_string(m.key.value()); + convert_sec_name(m.security, req.symbol, error); + req.side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; + req.price = m.price.value(); + req.quantity = m.quantity.full_value(); + req.type = convert_order_type(m.order_type); + req.timeInForce = convert_time_in_force(m.time_in_force); + req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return error.size() == orig_error_sz; +} + +bool convert_modify_request( + const bts::orderman::CancelReplaceOrderMsg &m, + ModifyOrderRequest &req, + std::string &error) { + const auto orig_error_sz = error.size(); + const auto &cf = m.create_fields; + if (cf.security == nullptr) { + ErrorList::append(error, "security is null"); + } else if (!cf.security->has_binance_usds_futures()) { + ErrorList::append(error, "security not of Binance Usds Futures"); + } else { + req.symbol = cf.security->binance_usds_futures().security_desc(); + } + req.orderId = checked_ultol(m.exchange_order_id.value()); + req.origClientOrderId = std::to_string(cf.key.value()); + // The exchange accepts either orderId, or origClientOrderId (only one + // need be populated). + if (req.orderId == 0 && req.origClientOrderId.empty()) { + ErrorList::append( + error, "Either orderId or origClientOrderId must be defined"); + } + bts_assert(req.orderId != 0 || !req.origClientOrderId.empty()); + req.price = cf.price.value(); + req.priceMatch = PriceMatchMode::NONE; + req.quantity = cf.quantity.full_value(); + req.side = convert_side(cf.side); + return error.size() == orig_error_sz; +} + +bool convert_cancel_request( + const bts::orderman::CancelOrderMsg &m, + CancelOrderRequest &req, + std::string &error) { + const auto orig_error_sz = error.size(); + convert_sec_name(m.security, req.symbol, error); + req.orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); + req.origClientOrderId = std::to_string(m.key.value()); + // The exchange accepts either orderId, or origClientOrderId (only one + // need be populated). + if (req.orderId == 0 && req.origClientOrderId.empty()) { + ErrorList::append( + error, "Either orderId or origClientOrderId must be defined"); + } + req.symbol = m.security->binance_usds_futures().security_desc(); + return error.size() == orig_error_sz; +} + } // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h index 154a45b24..d7131343c 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h @@ -11,8 +11,21 @@ namespace bts::api::admin { class BTSAccount; } +namespace bts::orderman { +struct CreateOrderMsg; +struct CancelReplaceOrderMsg; +struct CancelOrderMsg; +} // namespace bts::orderman + namespace binance_execution::usds_futures { +struct ModifyOrderRequest; +struct CreateOrderRequest; +struct CancelOrderRequest; + +bool convert_sec_name( + const bts::api::Security *sec, std::string &name, std::string &error); + bool convert_execution_report( const user_data_streams::OrderUpdate &u, bts::api::Venue venue, @@ -26,6 +39,19 @@ bool convert_execution_report( const bts::config::JetFIX_Connection *jetfix_cfg, bts::orderman::ExecutionReport &er); +bool convert_create_request( + const bts::orderman::CreateOrderMsg &m, + CreateOrderRequest &req, + std::string &error); +bool convert_modify_request( + const bts::orderman::CancelReplaceOrderMsg &msg, + ModifyOrderRequest &req, + std::string &error); +bool convert_cancel_request( + const bts::orderman::CancelOrderMsg &msg, + CancelOrderRequest &req, + std::string &error); + void convert_positions( bts::api::Venue venue, const bts::api::admin::BTSAccount &account, diff --git a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp index 93845c722..2724943fb 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp @@ -1,6 +1,7 @@ #include "listen_key_requestor.h" #include "binance_execution/usds_futures/rest_api_client.h" #include "lib/core/TimeStamp.h" +#include "lib/io/json_formatter.h" #include #include @@ -9,6 +10,7 @@ namespace binance_execution::usds_futures { using bts::core::Delegate; using bts::core::TimeStamp; +using bts::io::json_formatter; using std::optional; using std::string; using std::thread; @@ -50,7 +52,7 @@ struct ListenKeyRequestor::PImpl { continue; } bts_assert_msgf( - res.ok(), "Failed to get listen key. {}", res.error); + res.ok(), "Failed to get listen key. {}", json_formatter(res)); listen_key = res.response->listenKey; bts_assert(!listen_key.empty()); listen_key_timestamp = now; @@ -59,7 +61,8 @@ struct ListenKeyRequestor::PImpl { if ((now - listen_key_timestamp) > refresh_period) { const auto &res = rest_api_client.refresh_listen_key(); bts_assert_msgf( - res.ok(), "Failed to refresh listen key. {}", res.error); + res.ok(), "Failed to refresh listen key. {}", + json_formatter(res)); listen_key_timestamp = now; } std::this_thread::sleep_for(std::chrono::seconds(2)); diff --git a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.cpp b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.cpp index 61f718b72..8afbf5367 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.cpp @@ -1,8 +1,51 @@ #include "order_update.h" +#include "binance_execution/type_converters.h" +#include "lib/config/protos/jetfix.pb.h" +#include "lib/domain_model/Security.pb.h" #include "lib/io/JsonReader.h" +#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/utils/checked_arithmetic.h" +#include "lib/utils/string_utils.h" namespace binance_execution::usds_futures::user_data_streams { +using bts::system::math::checked_ltoul; +using bts::system::string_utils::copy_to_array; +using bts::system::string_utils::parse_uint; + +bool OrderUpdate::to_execution_report( + bts::orderman::ExecutionReport &er) const { + er.exchange = bts::api::VENUE_BINANCE_USDS_FUTURES; + er.transact_time = bts::orderman::ExchangeTimeStamp64{ + convert_time_stamp(transaction_time).nanos()}; + if (!parse_uint(data.client_order_id, er.order_key.value())) { + return false; + } + bts_assert(checked_ltoul( + data.order_id, + er.exchange_order_id.emplace() + .value())); + er.security = nullptr; + er.leg_security = nullptr; + er.side = data.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; + er.time_in_force = convert_time_in_force(data.time_in_force); + er.order_type = convert_order_type(data.order_type); + er.exec_type = convert_exec_type(data.execution_type, data.order_status); + er.order_status = convert_order_status(data.order_status); + er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; + er.price.value() = data.orig_price; + er.quantity = data.orig_quantity; + er.filled_quantity = data.total_filled_qty; + er.last_shares = data.last_filled_qty; + // er.max_show = data.; + er.last_price.value() = data.last_filled_price; + er.avg_price.value() = data.avg_price; + er.stop_price.value() = data.stop_price; + copy_to_array<40>( + std::to_string(data.trade_id), er.exchange_fill_id.value()); + return true; +} + void to_json(nlohmann::json &j, const OrderUpdate::Data &d) { j["s"] = d.symbol; j["c"] = d.client_order_id; diff --git a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.h b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.h index 71f91b9b5..882d3f668 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/order_update.h @@ -12,6 +12,14 @@ #include #include +namespace bts::config { +class JetFIX_Connection; +} + +namespace bts::orderman { +class ExecutionReport; +} + namespace binance_execution::usds_futures::user_data_streams { /** @@ -60,6 +68,8 @@ struct OrderUpdate { }; Data data; // json: o + + bool to_execution_report(bts::orderman::ExecutionReport &er) const; }; void to_json(nlohmann::json &j, const OrderUpdate::Data &d);