refactor: Abstract out common binance gw code
For all sub-venues to use
This commit is contained in:
1 parent
c60df8bcfa
commit
d010ba7ae7
33 files changed
+275
-738
No files matched your search
+4
-4
@@ -3,9 +3,9 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
namespace binance_execution {
|
||||
|
||||
struct SpotsGatewayConfig {
|
||||
struct GatewayConfig {
|
||||
std::string api_key;
|
||||
std::string secret_key;
|
||||
std::string account;
|
||||
@@ -15,7 +15,7 @@ struct SpotsGatewayConfig {
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
SpotsGatewayConfig,
|
||||
GatewayConfig,
|
||||
api_key,
|
||||
secret_key,
|
||||
account,
|
||||
@@ -23,4 +23,4 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
ws_endpoint,
|
||||
ws_stream_endpoint);
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
} // namespace binance_execution
|
||||
+14
-11
@@ -1,12 +1,11 @@
|
||||
#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 <string>
|
||||
#include <thread>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
namespace binance_execution {
|
||||
|
||||
using bts::core::Delegate;
|
||||
using bts::core::TimeStamp;
|
||||
@@ -16,16 +15,16 @@ using std::string;
|
||||
using std::thread;
|
||||
|
||||
struct ListenKeyRequestor::PImpl {
|
||||
RestApiClient rest_api_client;
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
get_listen_key;
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
refresh_listen_key;
|
||||
string listen_key;
|
||||
TimeStamp listen_key_timestamp;
|
||||
optional<thread> listen_key_thread;
|
||||
Delegate<void(const string &)> callback;
|
||||
bool is_running = false;
|
||||
|
||||
PImpl(RestApiClient rest_api_client)
|
||||
: rest_api_client(std::move(rest_api_client)) {}
|
||||
|
||||
~PImpl() { stop(); }
|
||||
|
||||
void start() {
|
||||
@@ -47,7 +46,7 @@ struct ListenKeyRequestor::PImpl {
|
||||
bts::core::TimeSpan::from_seconds(60 * 30);
|
||||
const auto now = bts::core::TimeStamp::now();
|
||||
if (listen_key.empty()) {
|
||||
const auto &res = rest_api_client.get_listen_key();
|
||||
const auto &res = get_listen_key({});
|
||||
if (!res.ok()) {
|
||||
continue;
|
||||
}
|
||||
@@ -59,7 +58,7 @@ struct ListenKeyRequestor::PImpl {
|
||||
callback(listen_key);
|
||||
}
|
||||
if ((now - listen_key_timestamp) > refresh_period) {
|
||||
const auto &res = rest_api_client.refresh_listen_key();
|
||||
const auto &res = refresh_listen_key({});
|
||||
bts_assert_msgf(
|
||||
res.ok(), "Failed to refresh listen key. {}",
|
||||
json_formatter(res));
|
||||
@@ -70,8 +69,12 @@ struct ListenKeyRequestor::PImpl {
|
||||
}
|
||||
};
|
||||
|
||||
ListenKeyRequestor::ListenKeyRequestor(RestApiClient rest_api_client)
|
||||
: me_(std::make_unique<PImpl>(std::move(rest_api_client))) {}
|
||||
ListenKeyRequestor::ListenKeyRequestor(
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
get_listen_key,
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
refresh_listen_key)
|
||||
: me_(std::make_unique<PImpl>(get_listen_key, refresh_listen_key)) {}
|
||||
|
||||
ListenKeyRequestor::~ListenKeyRequestor() = default;
|
||||
|
||||
@@ -81,4 +84,4 @@ Delegate<void(const string &)> &ListenKeyRequestor::callback() {
|
||||
return me_->callback;
|
||||
}
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
} // namespace binance_execution
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/messages/listen_key_request.h"
|
||||
#include "binance_execution/messages/listen_key_response.h"
|
||||
#include "binance_execution/rest_api_result.h"
|
||||
#include "lib/utils/Delegate.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace binance_execution {
|
||||
|
||||
/**
|
||||
* Requests the listen key for user data stream subscriptions, and periodically
|
||||
* refreshes it to keep subscriptions alive.
|
||||
*/
|
||||
class ListenKeyRequestor {
|
||||
public:
|
||||
explicit ListenKeyRequestor(
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
get_listen_key,
|
||||
std::function<RestApiResult<ListenKeyResponse>(const ListenKeyRequest &)>
|
||||
refresh_listen_key);
|
||||
~ListenKeyRequestor();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
bts::core::Delegate<void(const std::string &)> &callback();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace binance_execution
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution {
|
||||
|
||||
struct ListenKeyRequest {};
|
||||
|
||||
inline void to_query_params(
|
||||
std::string & /*qps*/, const ListenKeyRequest & /*req*/) {}
|
||||
|
||||
} // namespace binance_execution
|
||||
+2
-5
@@ -3,15 +3,12 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
namespace binance_execution {
|
||||
|
||||
/**
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
struct ListenKeyResponse {
|
||||
std::string listenKey;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListenKeyResponse, listenKey);
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
} // namespace binance_execution
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
namespace binance_execution {
|
||||
|
||||
struct OrderKeyMap {
|
||||
enum KeyType {
|
||||
@@ -60,4 +60,4 @@ struct OrderKeyMap {
|
||||
std::mutex instance_mx;
|
||||
};
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
} // namespace binance_execution
|
||||
@@ -1,8 +1,8 @@
|
||||
#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/order_key_map.h"
|
||||
#include "binance_execution/type_converters.h"
|
||||
#include "binance_execution/type_validators.h"
|
||||
#include "lib/core/TimeStamp.h"
|
||||
#include "lib/orderman/CancelOrderMsg.h"
|
||||
#include "lib/utils/checked_arithmetic.h"
|
||||
@@ -14,7 +14,9 @@ bool CancelOrderRequest::init(
|
||||
OrderKeyMap &key_map,
|
||||
std::string &error) {
|
||||
const std::size_t orig_error_sz = error.size();
|
||||
convert_sec_name(m.security, symbol, error);
|
||||
if (validate(m.security, bts::api::VENUE_BINANCE, error)) {
|
||||
symbol = m.security->binance().security_desc();
|
||||
}
|
||||
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
|
||||
|
||||
@@ -8,9 +8,11 @@ namespace bts::orderman {
|
||||
struct CancelOrderMsg;
|
||||
}
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
namespace binance_execution {
|
||||
struct OrderKeyMap;
|
||||
}
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
/**
|
||||
* @a https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#cancel-order-trade
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
#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 "binance_execution/type_validators.h"
|
||||
#include "lib/orderman/CreateOrderMsg.h"
|
||||
|
||||
namespace binance_execution::spots {
|
||||
@@ -12,7 +12,9 @@ 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);
|
||||
if (validate(m.security, bts::api::VENUE_BINANCE, error)) {
|
||||
symbol = m.security->binance().security_desc();
|
||||
}
|
||||
convert_side(m.side, side, error);
|
||||
price = m.price.value();
|
||||
quantity = m.quantity.full_value();
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
/**
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
struct ListenKeyRequest {};
|
||||
|
||||
inline void to_query_params(
|
||||
std::string & /*qps*/, const ListenKeyRequest & /*req*/) {}
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
@@ -2,11 +2,11 @@
|
||||
#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/order_key_map.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 "binance_execution/type_validators.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/orderman/CancelReplaceOrderMsg.h"
|
||||
|
||||
@@ -202,7 +202,9 @@ bool ModifyOrderRequest::init(
|
||||
if (quantity == 0) {
|
||||
ErrorList::append(error, "quantity is 0");
|
||||
}
|
||||
convert_sec_name(m.create_fields.security, symbol, error);
|
||||
if (validate(m.create_fields.security, bts::api::VENUE_BINANCE, error)) {
|
||||
symbol = m.create_fields.security->binance().security_desc();
|
||||
}
|
||||
convert_side(m.create_fields.side, side, error);
|
||||
convert_order_type(m.create_fields.order_type, type, error);
|
||||
cancelReplaceMode = CancelReplaceMode::STOP_ON_FAILURE;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#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 "binance_execution/order_key_map.h"
|
||||
#include "lib/orderman/CancelReplaceOrderMsg.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/messages/listen_key_request.h"
|
||||
#include "binance_execution/messages/listen_key_response.h"
|
||||
#include "binance_execution/rest_api_result.h"
|
||||
#include "binance_execution/spots/messages/all_orders_request.h"
|
||||
#include "binance_execution/spots/messages/cancel_order_request.h"
|
||||
#include "binance_execution/spots/messages/create_order_request.h"
|
||||
#include "binance_execution/spots/messages/listen_key_request.h"
|
||||
#include "binance_execution/spots/messages/listen_key_response.h"
|
||||
#include "binance_execution/spots/messages/modify_order_request.h"
|
||||
#include "binance_execution/spots/messages/modify_order_response.h"
|
||||
#include "binance_execution/spots/messages/order_record.h"
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#include "spots_gateway.h"
|
||||
#include "binance_execution/execution_report_store.h"
|
||||
#include "binance_execution/gateway_config.h"
|
||||
#include "binance_execution/listen_key_requestor.h"
|
||||
#include "binance_execution/order_key_map.h"
|
||||
#include "binance_execution/rest_api_result.h"
|
||||
#include "binance_execution/spots/messages/modify_order_request.h"
|
||||
#include "binance_execution/spots/order_key_map.h"
|
||||
#include "binance_execution/spots/rest_api_client.h"
|
||||
#include "binance_execution/spots/spots_gateway_config.h"
|
||||
#include "binance_execution/spots/type_converters.h"
|
||||
#include "binance_execution/spots/user_data_streams/listen_key_requestor.h"
|
||||
#include "binance_execution/spots/user_data_streams/user_data_subscription.h"
|
||||
#include "binance_execution/type_converters.h"
|
||||
#include "binance_execution/type_validators.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#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"
|
||||
@@ -75,55 +73,6 @@ using std::string;
|
||||
using std::uint32_t;
|
||||
using std::vector;
|
||||
|
||||
static void validate(bts::orderman::OrderKey key, string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "order key is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(bts::orderman::ExchangeOrderID64 key, string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "exch order key is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) {
|
||||
if (std::holds_alternative<bts::orderman::ExchangeOrderID64>(key)) {
|
||||
validate(std::get<bts::orderman::ExchangeOrderID64>(key), error);
|
||||
} else {
|
||||
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) {
|
||||
ErrorList::append(error, "quantity is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(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");
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
ErrorList::append(error, "account is empty");
|
||||
}
|
||||
validate(er.security, error);
|
||||
if (er.cfg == nullptr) {
|
||||
ErrorList::append(error, "jetfix config is null");
|
||||
}
|
||||
return error.size() == orig_sz;
|
||||
}
|
||||
|
||||
struct ExecutionGatewayCallbacks {
|
||||
bts::orderman::OnExeReport exe_report;
|
||||
bts::orderman::OnGwStatusChange gw_status_change;
|
||||
@@ -135,7 +84,7 @@ struct ExecutionGatewayCallbacks {
|
||||
struct SpotsGateway::PImpl {
|
||||
explicit PImpl(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
SpotsGatewayConfig config,
|
||||
GatewayConfig config,
|
||||
bts::config::JetFIX_Connection jetfix_connection)
|
||||
: sec_oracle(std::move(sec_oracle)), config(std::move(config)),
|
||||
jetfix_connection(std::make_shared<bts::config::JetFIX_Connection>(
|
||||
@@ -144,7 +93,13 @@ struct SpotsGateway::PImpl {
|
||||
this->config.rest_endpoint,
|
||||
this->config.api_key,
|
||||
this->config.secret_key),
|
||||
listen_key_requestor(this->rest_api_client),
|
||||
listen_key_requestor(
|
||||
[this](const auto &req) {
|
||||
return rest_api_client.get_listen_key(req);
|
||||
},
|
||||
[this](const auto &req) {
|
||||
return rest_api_client.refresh_listen_key(req);
|
||||
}),
|
||||
key_map(std::make_shared<OrderKeyMap>()),
|
||||
orders(
|
||||
[this](const std::string &exch_key) {
|
||||
@@ -160,7 +115,7 @@ struct SpotsGateway::PImpl {
|
||||
static constexpr const char *CANCEL_REPLACE_CANCEL_SUFFIX = "crc";
|
||||
|
||||
bts::md::SecurityOracle sec_oracle;
|
||||
SpotsGatewayConfig config;
|
||||
GatewayConfig config;
|
||||
std::shared_ptr<bts::config::JetFIX_Connection> jetfix_connection;
|
||||
|
||||
RestApiClient rest_api_client;
|
||||
@@ -441,7 +396,7 @@ struct SpotsGateway::PImpl {
|
||||
|
||||
SpotsGateway::SpotsGateway(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
SpotsGatewayConfig session_cfg,
|
||||
GatewayConfig session_cfg,
|
||||
bts::config::JetFIX::Connection jetfix_connection)
|
||||
: me_(std::make_unique<PImpl>(
|
||||
std::move(sec_oracle),
|
||||
@@ -505,8 +460,7 @@ void SpotsGateway::send_msgs(
|
||||
}
|
||||
}
|
||||
|
||||
void SpotsGateway::send_fix(
|
||||
const bts::jetfix::OutFIXPtr &, bts::trace::CorID) {
|
||||
void SpotsGateway::send_fix(const bts::jetfix::OutFIXPtr &, bts::trace::CorID) {
|
||||
bts_assert_msg(false, "ExecutionGateway does not support fix");
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,17 @@ namespace bts::md {
|
||||
class SecurityOracle;
|
||||
}
|
||||
|
||||
namespace binance_execution::spots {
|
||||
namespace binance_execution {
|
||||
struct GatewayConfig;
|
||||
}
|
||||
|
||||
struct SpotsGatewayConfig;
|
||||
namespace binance_execution::spots {
|
||||
|
||||
class SpotsGateway : public bts::orderman::IExecutionGateway {
|
||||
public:
|
||||
explicit SpotsGateway(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
SpotsGatewayConfig session_cfg,
|
||||
GatewayConfig session_cfg,
|
||||
bts::config::JetFIX_Connection jetfix_connection);
|
||||
~SpotsGateway() override;
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
#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 <limits>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
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()) {
|
||||
ErrorList::append(error, "security not of Binance (spots)");
|
||||
} 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().security_desc();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
bool validate_security(const bts::api::Security *sec, std::string &error);
|
||||
|
||||
bool convert_sec_name(
|
||||
const bts::api::Security *sec, std::string &name, std::string &error);
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/spots/rest_api_client.h"
|
||||
#include "lib/utils/Delegate.h"
|
||||
#include <memory>
|
||||
|
||||
namespace binance_execution::spots {
|
||||
|
||||
/**
|
||||
* Requests the listen key for user data stream subscriptions, and periodically
|
||||
* refreshes it to keep subscriptions alive.
|
||||
*
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
class ListenKeyRequestor {
|
||||
public:
|
||||
explicit ListenKeyRequestor(RestApiClient rest_api_client);
|
||||
~ListenKeyRequestor();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
bts::core::Delegate<void(const std::string &)> &callback();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace binance_execution::spots
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "type_validators.h"
|
||||
|
||||
namespace binance_execution {
|
||||
|
||||
bool validate(
|
||||
const bts::api::Security *sec, bts::api::Venue venue, std::string &error) {
|
||||
if (sec == nullptr) {
|
||||
ErrorList::append(error, "security is null");
|
||||
return false;
|
||||
}
|
||||
switch (venue) {
|
||||
case bts::api::VENUE_BINANCE:
|
||||
if (!sec->has_binance()) {
|
||||
ErrorList::append(error, "security not of Binance");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case bts::api::VENUE_BINANCE_USDS_FUTURES:
|
||||
if (!sec->has_binance_usds_futures()) {
|
||||
ErrorList::append(error, "security not of BinanceUsdsFutures");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case bts::api::VENUE_BINANCE_COIN_FUTURES:
|
||||
if (!sec->has_binance_coin_futures()) {
|
||||
ErrorList::append(error, "security not of BinanceCoinFutures");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case bts::api::VENUE_BINANCE_US:
|
||||
if (!sec->has_binance_us()) {
|
||||
ErrorList::append(error, "security not of BinanceUs");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case bts::api::VENUE_UNSET:
|
||||
switch (sec->ExchangeData_case()) {
|
||||
case bts::api::Security::kBinance:
|
||||
case bts::api::Security::kBinanceCoinFutures:
|
||||
case bts::api::Security::kBinanceUsdsFutures:
|
||||
case bts::api::Security::kBinanceUs:
|
||||
break;
|
||||
default:
|
||||
ErrorList::append(error, "security not of any Binance sub-venue");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool validate(const bts::orderman::ExecutionReport &er, std::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()) {
|
||||
ErrorList::append(error, "account is empty");
|
||||
}
|
||||
validate(er.security, error);
|
||||
if (er.cfg == nullptr) {
|
||||
ErrorList::append(error, "jetfix config is null");
|
||||
}
|
||||
return error.size() == orig_sz;
|
||||
}
|
||||
|
||||
} // namespace binance_execution
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/error_list.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/orderman/orderman_utils/ExecutionReport.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution {
|
||||
|
||||
bool validate(bts::orderman::OrderKey key, std::string &error);
|
||||
bool validate(bts::orderman::ExchangeOrderID64 key, std::string &error);
|
||||
bool validate(bts::orderman::ExchangeOrderIDVar key, std::string &error);
|
||||
bool validate(bts::Quantity qty, std::string &error);
|
||||
bool validate(
|
||||
const bts::api::Security *sec, bts::api::Venue venue, std::string &error);
|
||||
bool validate(const bts::orderman::ExecutionReport &er, std::string &error);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool validate(bts::orderman::OrderKey key, std::string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "order key is 0");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool validate(bts::orderman::ExchangeOrderID64 key, std::string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "exch order key is 0");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool validate(
|
||||
bts::orderman::ExchangeOrderIDVar key, std::string &error) {
|
||||
if (std::holds_alternative<bts::orderman::ExchangeOrderID64>(key)) {
|
||||
return validate(std::get<bts::orderman::ExchangeOrderID64>(key), error);
|
||||
} else {
|
||||
ErrorList::append(error, "exch order id is undefined or not an integer");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool validate(bts::Quantity qty, std::string &error) {
|
||||
if (qty.full_value() == 0) {
|
||||
ErrorList::append(error, "quantity is 0");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool validate(const bts::api::Security *sec, std::string &error) {
|
||||
return validate(sec, bts::api::VENUE_UNSET, error);
|
||||
}
|
||||
|
||||
} // namespace binance_execution
|
||||
+5
-2
@@ -1,7 +1,8 @@
|
||||
#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 "binance_execution/type_validators.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/orderman/CancelOrderMsg.h"
|
||||
#include "lib/utils/checked_arithmetic.h"
|
||||
|
||||
@@ -10,7 +11,9 @@ 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);
|
||||
if (validate(m.security, bts::api::VENUE_BINANCE_USDS_FUTURES, error)) {
|
||||
symbol = m.security->binance_usds_futures().security_desc();
|
||||
}
|
||||
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
|
||||
|
||||
+4
-2
@@ -1,6 +1,6 @@
|
||||
#include "create_order_request.h"
|
||||
#include "binance_execution/type_converters.h"
|
||||
#include "binance_execution/usds_futures/type_converters.h"
|
||||
#include "binance_execution/type_validators.h"
|
||||
#include "lib/orderman/CreateOrderMsg.h"
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
@@ -9,7 +9,9 @@ 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);
|
||||
if (validate(m.security, bts::api::VENUE_BINANCE_USDS_FUTURES, error)) {
|
||||
symbol = m.security->binance_usds_futures().security_desc();
|
||||
}
|
||||
side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL;
|
||||
price = m.price.value();
|
||||
quantity = m.quantity.full_value();
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
|
||||
/**
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
struct ListenKeyRequest {};
|
||||
|
||||
inline void to_query_params(
|
||||
std::string & /*qps*/, const ListenKeyRequest & /*req*/) {}
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
|
||||
/**
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
struct ListenKeyResponse {
|
||||
std::string listenKey;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListenKeyResponse, listenKey);
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/messages/listen_key_request.h"
|
||||
#include "binance_execution/messages/listen_key_response.h"
|
||||
#include "binance_execution/rest_api_result.h"
|
||||
#include "binance_execution/usds_futures/messages/all_orders_request.h"
|
||||
#include "binance_execution/usds_futures/messages/cancel_order_request.h"
|
||||
#include "binance_execution/usds_futures/messages/cancel_order_response.h"
|
||||
#include "binance_execution/usds_futures/messages/create_order_request.h"
|
||||
#include "binance_execution/usds_futures/messages/create_order_response.h"
|
||||
#include "binance_execution/usds_futures/messages/listen_key_request.h"
|
||||
#include "binance_execution/usds_futures/messages/listen_key_response.h"
|
||||
#include "binance_execution/usds_futures/messages/modify_order_request.h"
|
||||
#include "binance_execution/usds_futures/messages/modify_order_response.h"
|
||||
#include "binance_execution/usds_futures/messages/order_record.h"
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
#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,
|
||||
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.data.client_order_id, er.order_key.value())) {
|
||||
return false;
|
||||
}
|
||||
bts_assert(bts::system::math::checked_ltoul(
|
||||
u.data.order_id,
|
||||
er.exchange_order_id.emplace<bts::orderman::ExchangeOrderID64>()
|
||||
.value()));
|
||||
er.security = nullptr;
|
||||
er.leg_security = nullptr;
|
||||
er.side =
|
||||
u.data.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL;
|
||||
er.time_in_force = convert_time_in_force(u.data.time_in_force);
|
||||
er.order_type = convert_order_type(u.data.order_type);
|
||||
er.exec_type = convert_exec_type(u.data.execution_type, u.data.order_status);
|
||||
er.order_status = convert_order_status(u.data.order_status);
|
||||
er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO;
|
||||
er.price.value() = u.data.orig_price;
|
||||
er.quantity = u.data.orig_quantity;
|
||||
er.filled_quantity = u.data.total_filled_qty;
|
||||
er.last_shares = u.data.last_filled_qty;
|
||||
// er.max_show = u.data.;
|
||||
er.last_price.value() = u.data.last_filled_price;
|
||||
er.avg_price.value() = u.data.avg_price;
|
||||
er.stop_price.value() = u.data.stop_price;
|
||||
copy_to_array<40>(
|
||||
std::to_string(u.data.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;
|
||||
}
|
||||
|
||||
bool convert_execution_report(
|
||||
const CreateOrderResponse &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.updateTime).nanos()};
|
||||
if (!parse_uint(res.clientOrderId, er.order_key.value())) {
|
||||
return false;
|
||||
}
|
||||
bts_assert(bts::system::math::checked_ltoul(
|
||||
res.orderId,
|
||||
er.exchange_order_id.emplace<bts::orderman::ExchangeOrderID64>()
|
||||
.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.last_shares = res.executedQty;
|
||||
er.max_show = 0;
|
||||
er.last_price.value() = res.avgPrice;
|
||||
er.avg_price.value() = res.avgPrice;
|
||||
er.stop_price.value() = res.stopPrice;
|
||||
er.msg = "";
|
||||
er.cfg = jetfix_cfg;
|
||||
// Irrelevant fields
|
||||
er.ice_cl_ord_id = 0;
|
||||
er.ice_company_id = 0;
|
||||
er.memo = "";
|
||||
return true;
|
||||
}
|
||||
|
||||
void convert_positions(
|
||||
bts::api::Venue venue,
|
||||
const bts::api::admin::BTSAccount &account,
|
||||
const vector<user_data_streams::AccountUpdate> &acct_upds,
|
||||
std::function<const bts::api::Security *(
|
||||
bts::api::Venue venue, const std::string &name)> get_security,
|
||||
vector<bts::orderman::ExchangePosition::UnitPosition> &dest) {
|
||||
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,
|
||||
[](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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1,63 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/usds_futures/messages/create_order_response.h"
|
||||
#include "binance_execution/usds_futures/user_data_streams/account_update.h"
|
||||
#include "binance_execution/usds_futures/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"
|
||||
|
||||
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,
|
||||
const std::string &account,
|
||||
const bts::config::JetFIX_Connection *jetfix_cfg,
|
||||
bts::orderman::ExecutionReport &er);
|
||||
bool convert_execution_report(
|
||||
const CreateOrderResponse &res,
|
||||
bts::api::Venue venue,
|
||||
const std::string &account,
|
||||
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,
|
||||
const std::vector<user_data_streams::AccountUpdate> &acct_upds,
|
||||
std::function<const bts::api::Security *(
|
||||
bts::api::Venue venue, const std::string &name)> get_security,
|
||||
std::vector<bts::orderman::ExchangePosition::UnitPosition> &dest);
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "usds_futures_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/gateway_config.h"
|
||||
#include "binance_execution/listen_key_requestor.h"
|
||||
#include "binance_execution/rest_api_result.h"
|
||||
#include "binance_execution/type_validators.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"
|
||||
#include "binance_execution/usds_futures/user_data_streams/listen_key_requestor.h"
|
||||
#include "binance_execution/usds_futures/user_data_streams/user_data_subscription.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
@@ -77,55 +77,6 @@ using std::string;
|
||||
using std::uint32_t;
|
||||
using std::vector;
|
||||
|
||||
static void validate(bts::orderman::OrderKey key, string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "order key is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(bts::orderman::ExchangeOrderID64 key, string &error) {
|
||||
if (key.value() == 0) {
|
||||
ErrorList::append(error, "exch order key is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) {
|
||||
if (std::holds_alternative<bts::orderman::ExchangeOrderID64>(key)) {
|
||||
validate(std::get<bts::orderman::ExchangeOrderID64>(key), error);
|
||||
} else {
|
||||
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) {
|
||||
ErrorList::append(error, "quantity is 0");
|
||||
}
|
||||
}
|
||||
|
||||
static void validate(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");
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
ErrorList::append(error, "account is empty");
|
||||
}
|
||||
validate(er.security, error);
|
||||
if (er.cfg == nullptr) {
|
||||
ErrorList::append(error, "jetfix config is null");
|
||||
}
|
||||
return error.size() == orig_sz;
|
||||
}
|
||||
|
||||
struct ExecutionGatewayCallbacks {
|
||||
bts::orderman::OnExeReport exe_report;
|
||||
bts::orderman::OnGwStatusChange gw_status_change;
|
||||
@@ -137,7 +88,7 @@ struct ExecutionGatewayCallbacks {
|
||||
struct UsdsFuturesGateway::PImpl {
|
||||
explicit PImpl(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
UsdsFuturesGatewayConfig config,
|
||||
GatewayConfig config,
|
||||
bts::config::JetFIX::Connection jetfix_connection)
|
||||
: sec_oracle(std::move(sec_oracle)), config(std::move(config)),
|
||||
jetfix_connection(std::make_shared<bts::config::JetFIX_Connection>(
|
||||
@@ -146,7 +97,13 @@ struct UsdsFuturesGateway::PImpl {
|
||||
this->config.rest_endpoint,
|
||||
this->config.api_key,
|
||||
this->config.secret_key),
|
||||
listen_key_requestor(this->rest_api_client),
|
||||
listen_key_requestor(
|
||||
[this](const auto &req) {
|
||||
return rest_api_client.get_listen_key(req);
|
||||
},
|
||||
[this](const auto &req) {
|
||||
return rest_api_client.refresh_listen_key(req);
|
||||
}),
|
||||
orders(
|
||||
[](const std::string &exch_key) {
|
||||
std::uint32_t k;
|
||||
@@ -161,7 +118,7 @@ struct UsdsFuturesGateway::PImpl {
|
||||
}
|
||||
|
||||
bts::md::SecurityOracle sec_oracle;
|
||||
UsdsFuturesGatewayConfig config;
|
||||
GatewayConfig config;
|
||||
std::shared_ptr<bts::config::JetFIX_Connection> jetfix_connection;
|
||||
|
||||
RestApiClient rest_api_client;
|
||||
@@ -403,7 +360,7 @@ struct UsdsFuturesGateway::PImpl {
|
||||
|
||||
UsdsFuturesGateway::UsdsFuturesGateway(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
UsdsFuturesGatewayConfig session_cfg,
|
||||
GatewayConfig session_cfg,
|
||||
bts::config::JetFIX::Connection jetfix_connection)
|
||||
: me_(std::make_unique<PImpl>(
|
||||
std::move(sec_oracle),
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/usds_futures/usds_futures_gateway_config.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/orderman/gateways/IExecutionGateway.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace binance_execution {
|
||||
struct GatewayConfig;
|
||||
}
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
|
||||
class UsdsFuturesGateway : public bts::orderman::IExecutionGateway {
|
||||
public:
|
||||
explicit UsdsFuturesGateway(
|
||||
bts::md::SecurityOracle sec_oracle,
|
||||
UsdsFuturesGatewayConfig session_cfg,
|
||||
GatewayConfig session_cfg,
|
||||
bts::config::JetFIX::Connection jetfix_connection);
|
||||
~UsdsFuturesGateway() override;
|
||||
|
||||
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
|
||||
struct UsdsFuturesGatewayConfig {
|
||||
std::string api_key;
|
||||
std::string secret_key;
|
||||
std::string account;
|
||||
std::string rest_endpoint;
|
||||
std::string ws_endpoint;
|
||||
std::string ws_stream_endpoint;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
UsdsFuturesGatewayConfig,
|
||||
api_key,
|
||||
secret_key,
|
||||
account,
|
||||
rest_endpoint,
|
||||
ws_endpoint,
|
||||
ws_stream_endpoint);
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
-84
@@ -1,84 +0,0 @@
|
||||
#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 <string>
|
||||
#include <thread>
|
||||
|
||||
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;
|
||||
|
||||
struct ListenKeyRequestor::PImpl {
|
||||
RestApiClient rest_api_client;
|
||||
string listen_key;
|
||||
TimeStamp listen_key_timestamp;
|
||||
optional<thread> listen_key_thread;
|
||||
Delegate<void(const string &)> callback;
|
||||
bool is_running = false;
|
||||
|
||||
PImpl(RestApiClient rest_api_client)
|
||||
: rest_api_client(std::move(rest_api_client)) {}
|
||||
|
||||
~PImpl() { stop(); }
|
||||
|
||||
void start() {
|
||||
bts_assert(!is_running);
|
||||
is_running = true;
|
||||
listen_key_thread.emplace([this]() { run(); });
|
||||
}
|
||||
|
||||
void stop() {
|
||||
is_running = false;
|
||||
if (listen_key_thread) {
|
||||
listen_key_thread->join();
|
||||
}
|
||||
}
|
||||
|
||||
void run() {
|
||||
while (is_running) {
|
||||
static const auto refresh_period =
|
||||
bts::core::TimeSpan::from_seconds(60 * 30);
|
||||
const auto now = bts::core::TimeStamp::now();
|
||||
if (listen_key.empty()) {
|
||||
const auto &res = rest_api_client.get_listen_key();
|
||||
if (!res.ok()) {
|
||||
continue;
|
||||
}
|
||||
bts_assert_msgf(
|
||||
res.ok(), "Failed to get listen key. {}", json_formatter(res));
|
||||
listen_key = res.response->listenKey;
|
||||
bts_assert(!listen_key.empty());
|
||||
listen_key_timestamp = now;
|
||||
callback(listen_key);
|
||||
}
|
||||
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. {}",
|
||||
json_formatter(res));
|
||||
listen_key_timestamp = now;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ListenKeyRequestor::ListenKeyRequestor(RestApiClient rest_api_client)
|
||||
: me_(std::make_unique<PImpl>(std::move(rest_api_client))) {}
|
||||
|
||||
ListenKeyRequestor::~ListenKeyRequestor() = default;
|
||||
|
||||
void ListenKeyRequestor::start() { me_->start(); }
|
||||
void ListenKeyRequestor::stop() { me_->stop(); }
|
||||
Delegate<void(const string &)> &ListenKeyRequestor::callback() {
|
||||
return me_->callback;
|
||||
}
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "binance_execution/usds_futures/rest_api_client.h"
|
||||
#include "lib/utils/Delegate.h"
|
||||
#include <memory>
|
||||
|
||||
namespace binance_execution::usds_futures {
|
||||
|
||||
/**
|
||||
* Requests the listen key for user data stream subscriptions, and periodically
|
||||
* refreshes it to keep subscriptions alive.
|
||||
*
|
||||
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
|
||||
*/
|
||||
class ListenKeyRequestor {
|
||||
public:
|
||||
explicit ListenKeyRequestor(RestApiClient rest_api_client);
|
||||
~ListenKeyRequestor();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
bts::core::Delegate<void(const std::string &)> &callback();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace binance_execution::usds_futures
|
||||
@@ -1,8 +1,7 @@
|
||||
#include "execution_gateway_provider.h"
|
||||
#include "binance_execution/gateway_config.h"
|
||||
#include "binance_execution/spots/spots_gateway.h"
|
||||
#include "binance_execution/spots/spots_gateway_config.h"
|
||||
#include "binance_execution/usds_futures/usds_futures_gateway.h"
|
||||
#include "binance_execution/usds_futures/usds_futures_gateway_config.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/orderman/gateways/CCAPIExecutionGateway.h"
|
||||
@@ -89,15 +88,13 @@ struct ExecutionGatewayProvider::PImpl {
|
||||
}
|
||||
case api::VENUE_BINANCE: {
|
||||
const auto cfg =
|
||||
parse_config_json<binance_execution::spots::SpotsGatewayConfig>(
|
||||
conn.name());
|
||||
parse_config_json<binance_execution::GatewayConfig>(conn.name());
|
||||
return std::make_unique<binance_execution::spots::SpotsGateway>(
|
||||
o, std::move(cfg), conn);
|
||||
}
|
||||
case api::VENUE_BINANCE_USDS_FUTURES: {
|
||||
auto cfg = parse_config_json<
|
||||
binance_execution::usds_futures::UsdsFuturesGatewayConfig>(
|
||||
conn.name());
|
||||
auto cfg =
|
||||
parse_config_json<binance_execution::GatewayConfig>(conn.name());
|
||||
return std::make_unique<
|
||||
binance_execution::usds_futures::UsdsFuturesGateway>(
|
||||
o, std::move(cfg), conn);
|
||||
|
||||
Reference in new issue
Block a user