feat: Add Binance USDS Futures execution

This commit is contained in:
John Colvin committed 2024-12-22 04:00:23 +00:00
1 parent 25439b094a
commit 1b232aa610
71 files changed
+3913 -66

No files matched your search

+1
View File
@@ -91,6 +91,7 @@ else()
add_compile_definitions(CCAPI_BINANCE_USDS_FUTURES_URL_REST_BASE="https://dapi.binance.com")
add_compile_definitions(BINANCE_USDS_FUTURES_INSTRUMENTS_PATH="/dapi/v1/exchangeInfo")
add_compile_definitions(BINANCE_USDS_FUTURES_PRICE_STATS_PATH="/dapi/v1/ticker/24hr")
add_compile_definitions(BINANCE_USDS_FUTURES_USER_DATA_PATH="/api/v3/userDataStream")
# ** Binance Coin Futures **
add_compile_definitions(CCAPI_BINANCE_COIN_FUTURES_URL_REST_BASE="https://dapi.binance.com")
@@ -767,12 +767,18 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(
out_result.set_is_success(true);
return rv;
}
case api::VENUE_BINANCE_USDS_FUTURES: {
rv.set_session("binance_usds_futures_1");
rv.set_account(account_ptr->name());
rv.set_account(user_ptr->name());
out_result.set_is_success(true);
return rv;
}
case api::VENUE_FTX: // TODO
case api::VENUE_BINANCE: // TODO
case api::VENUE_BINANCE_COIN_FUTURES:
case api::VENUE_BINANCE_US:
case api::Venue::VENUE_COINBASE:
case api::VENUE_BINANCE_USDS_FUTURES:
case api::Venue::VENUE_UNSET:
case api::Venue::VENUE_BTS:
case api::Venue::VENUE_CBOECFE:
@@ -41,6 +41,18 @@ class AdminServiceUtils {
}
return nullptr;
}
bts::config::toggles::ExchangeSubAccountUnit const *find_entry(
const std::string &account, bool is_internal_account) {
for (const auto &esa : exch_sub_accounts.accounts()) {
const auto &a = is_internal_account ? esa.bts_account_name()
: esa.exchange_subaccount();
if (a == account) {
return &esa;
}
}
return nullptr;
}
};
////////////////////////////////////////////
// connection accessors ////////////////////
+9
View File
@@ -14,12 +14,21 @@ class HttpRequest {
std::string body;
io::http::HttpMethod method;
std::unordered_map<std::string, std::string> headers;
void reset();
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void HttpRequest::reset() {
url.clear();
body.clear();
method = io::http::HttpMethod::NONE;
headers.clear();
}
inline void to_json(nlohmann::json &j, const HttpRequest &req) {
j["url"] = req.url;
j["body"] = req.body;
+26 -6
View File
@@ -13,6 +13,7 @@
#include "lib/logging/logging.h"
#include "lib/md/MDClient.h"
#include "lib/md/MDConsumer.h"
#include "lib/md/UserSymbol.h"
#include "lib/mercury/actors/Timer.h"
#include "lib/mercury/executor/Executor.h"
#include "lib/metrics/metrics.h"
@@ -24,16 +25,23 @@
#include "lib/orderman/actors/SecuritySrc.h"
#include "lib/orderman/actors/ServiceBridge.h"
#include "lib/orderman/actors/order_manager/OrderManager.h"
#include "lib/orderman/actors/order_manager/OrderServicesData.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/events/ExchangePositionEvent.h"
#include "lib/orderman/events/OrderOpResultEvent.h"
#include "lib/orderman/gateways/execution_gateway_provider.h"
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include "lib/orderman/order_factory/DynamicOrderRegistry.h"
#include "lib/orderman/orderman_utils/ExchangeOpenOrders.h"
#include "lib/orderman/orders/Order.h"
#include "lib/os/spark_data_directory.h"
#include "lib/plugin/SparkServices.h"
#include "lib/plugin/plugin.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/thread_manager.h"
#include "lib/utils/type_name.h"
#include "lib_binance_execution/binance_execution/execution_gateway_provider.h"
#include <bits/stdint-uintn.h>
#include <boost/algorithm/string/predicate.hpp>
@@ -43,6 +51,8 @@
namespace bts {
namespace orderman {
using std::unique_ptr;
using std::vector;
struct ActorThread {
ActorThread(
@@ -126,10 +136,13 @@ struct OrderServiceServer::PImpl {
const Exchange_open_ordersStatefulPublisher &open_orders_publisher,
md::MDClient &mdc,
DynamicOrderRegistry const &d,
std::vector<std::unique_ptr<IExecutionGatewayProvider>>
gateway_providers,
metrics::MetricsContainer &stats,
trace::PerfLog &log)
: context(ctx), service_bridge(), service_ready(false), known_orders(),
known_fills(), publish_orders(), publish_fills(),
: context(ctx), service_bridge(),
gateway_providers(std::move(gateway_providers)), service_ready(false),
known_orders(), known_fills(), publish_orders(), publish_fills(),
exchange_pos_publisher(exchange_pos_publisher),
open_orders_publisher(open_orders_publisher),
admin_service(ctx.locate<api::admin::AdminService>()),
@@ -137,11 +150,15 @@ struct OrderServiceServer::PImpl {
margin_service(ctx.locate<api::position::MarginService>()),
md_consumer(mdc.current_consumer()), stats(stats),
dyn_order_registry(d), log(log), in_flight_order_ops(),
current_req_op_id(0) {}
current_req_op_id(0) {
LOG(OrderMan, Info, "OrderServiceServer ctr. n_gateway_providers: {}",
this->gateway_providers.size());
}
bts::api::ServiceContext &context;
ServiceBridge *service_bridge;
std::vector<std::unique_ptr<IExecutionGatewayProvider>> gateway_providers;
std::vector<std::unique_ptr<ActorThread>> actor_threads;
std::atomic<bool> service_ready;
@@ -169,6 +186,7 @@ OrderServiceServer::OrderServiceServer(
bts::api::ServiceContext &ctx,
md::MDClient &mdc,
DynamicOrderRegistry const &d,
std::vector<std::unique_ptr<IExecutionGatewayProvider>> gateway_providers,
metrics::MetricsContainer &stats,
trace::PerfLog &log,
md::TradeSummarySignalDispatcher &tssd)
@@ -179,6 +197,7 @@ OrderServiceServer::OrderServiceServer(
make_exchange_open_orders_stateful_publisher(),
mdc,
d,
std::move(gateway_providers),
stats,
log)) {
LOG(md, Info, "OrderServiceServer cons t_id: {} t_name: {}",
@@ -209,7 +228,10 @@ OrderServiceServer::OrderServiceServer(
bts_assert_msg(
thread, "Could not find a configured thread for OrderManager. See "
"the OrdermanCfg key");
thread->executor.spawn<OrderManager>(d, me->log, mdc.oracle());
{
thread->executor.spawn<OrderManager>(
d, me->log, mdc.oracle(), std::move(me->gateway_providers));
}
// Timer
thread = find_thread_for_actor("Timer", me->actor_threads);
@@ -404,7 +426,6 @@ void OrderServiceServer::create_order(
void OrderServiceServer::cancel_order(
const api::orderman::Cancel &cancel,
const api::Response<api::orderman::Response> &r) {
if (!me->service_ready) {
api::orderman::Response response;
response.set_order_key(cancel.order_key());
@@ -435,7 +456,6 @@ void OrderServiceServer::update_order(
void OrderServiceServer::detach_order(
const api::orderman::Detach &detach,
const api::Response<api::orderman::Response> &r) {
if (!me->service_ready) {
api::orderman::Response response;
response.set_order_key(detach.order_key());
+3
View File
@@ -5,6 +5,7 @@
#include "lib/md/MDClient.h"
#include "lib/mercury/actors/IPollableActor.h"
#include "lib/orderman/events/ExchangeOpenOrdersEvent.h"
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include <memory>
namespace bts {
@@ -28,6 +29,8 @@ class OrderServiceServer : public api::orderman::OrderServiceBase {
bts::api::ServiceContext &context,
md::MDClient &,
DynamicOrderRegistry const &,
std::vector<std::unique_ptr<IExecutionGatewayProvider>>
gateway_providers,
metrics::MetricsContainer &,
trace::PerfLog &,
md::TradeSummarySignalDispatcher &);
+1 -1
View File
@@ -190,7 +190,7 @@ SessionServices &OrderServices::get_session_services(
for (auto &s : _sessions)
if (s.session_name() == for_session)
return s;
bts_assert(false);
bts_assert_msgf(false, "No session named '{}'", for_session);
}
void OrderServices::unsubscribe_from_order_creation_reply(
@@ -1,10 +1,15 @@
#include "OrderManager.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/core/FMT.h"
#include "lib/jetfix/common/OutFIX.h"
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/events/TimerEvent.h"
#include "lib/orderman/OrderServices.h"
#include "lib/orderman/actors/order_manager/OrderServicesData.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/actors/order_manager/session_services_data.h"
#include "lib/orderman/events/AccountPnLUpdateEvent.h"
#include "lib/orderman/events/ExcessLiquidityUpdateEvent.h"
#include "lib/orderman/events/ExchangeOpenOrdersEvent.h"
@@ -17,10 +22,13 @@
#include "lib/orderman/events/StoredOrderEvent.h"
#include "lib/orderman/events/TheoEvent.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include "lib/orderman/order_factory/OrderFactory.h"
#include "lib/orderman/orderman_utils/ExchangePosition.h"
#include "lib/orderman/orders/Order.h"
#include "lib/orderman/securities/ISecurityDB.h"
#include "lib/os/spark_data_directory.h"
#include "lib/tracing/PerfLog.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/inplace_function.h"
#include "lib/utils/thread_manager.h"
@@ -42,13 +50,27 @@ enum class OrderManager::State {
READY
};
static std::vector<OrderGatewayDefinition> create_gateways(
std::vector<std::unique_ptr<IExecutionGatewayProvider>> providers) {
std::vector<OrderGatewayDefinition> gws;
for (auto &p : providers) {
auto defs = p->create();
for (auto &def : defs) {
gws.push_back(std::move(def));
}
}
return gws;
}
struct OrderManager::PImpl {
PImpl(
OrderManager &owner,
DynamicOrderRegistry const &b,
trace::PerfLog &l,
md::SecurityOracle o)
: state(State::WAIT_ADMIN), services_data(owner, b, l, o) {}
md::SecurityOracle o,
std::vector<OrderGatewayDefinition> gateways)
: state(State::WAIT_ADMIN),
services_data(owner, b, l, o, std::move(gateways)) {}
State state;
std::unordered_map<OrderKey, Order *> orders;
@@ -61,8 +83,12 @@ struct OrderManager::PImpl {
};
OrderManager::OrderManager(
DynamicOrderRegistry const &b, trace::PerfLog &l, md::SecurityOracle o)
: me(new PImpl(*this, b, l, o)) {
DynamicOrderRegistry const &b,
trace::PerfLog &l,
md::SecurityOracle o,
std::vector<std::unique_ptr<IExecutionGatewayProvider>> gw_providers)
: me(std::make_unique<PImpl>(
*this, b, l, o, create_gateways(std::move(gw_providers)))) {
LOG(OrderMan, Info, "OrderManager cons t_id: {} t_name: {}",
os::get_thread_id(), os::get_thread_name());
auto id_gen = &me->services_data.id_gen;
@@ -437,6 +463,14 @@ std::vector<jetfix::OutFIXPtr> &OrderManager::free_buffers(
void OrderManager::init_order_services() {
auto &log = me->services_data.log;
const std::string session_names = fmt::to_string(fmt::join(
me->services_data.sessions |
ranges::views::transform(
[](const std::unique_ptr<SessionServicesData> &s) {
return s->session;
}),
", "));
LOG(OrderMan, Info, "init_order_services. sessions: {}", session_names);
for (auto &s : me->services_data.sessions) {
auto gw_name = GatewayName(s->session);
@@ -5,11 +5,13 @@
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/actors/Actor.h"
#include "lib/mercury/actors/IPollableActor.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/events/ActivateOrderEvent.h"
#include "lib/orderman/events/CancelOrderEvent.h"
#include "lib/orderman/events/DetachOrderEvent.h"
#include "lib/orderman/events/RFQEvent.h"
#include "lib/orderman/events/UpdateOrderEvent.h"
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include "lib/utils/inplace_function.h"
#include <memory>
@@ -68,7 +70,8 @@ class OrderManager : public mercury::Actor, public mercury::IPollableActor {
OrderManager(
DynamicOrderRegistry const &dyn_order_builder,
trace::PerfLog &,
md::SecurityOracle);
md::SecurityOracle,
std::vector<std::unique_ptr<IExecutionGatewayProvider>> gw_providers);
~OrderManager();
void init() override;
@@ -1,6 +1,11 @@
#include "OrderServicesData.h"
#include "lib/config/config.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
#include "lib/os/spark_data_directory.h"
#include "lib/tracing/PerfLog.h"
namespace bts {
namespace orderman {
@@ -9,7 +14,8 @@ OrderServicesData::OrderServicesData(
OrderManager &owner,
DynamicOrderRegistry const &b,
trace::PerfLog &l,
md::SecurityOracle o)
md::SecurityOracle o,
std::vector<OrderGatewayDefinition> gateways)
: owner(owner), log(l), sec_db(o), order_router(),
order_factory(sec_db, order_router, b),
last_timer_ev(
@@ -17,9 +23,12 @@ OrderServicesData::OrderServicesData(
to<mercury::TimeStampStr>(datetime::FIX_timestamp(datetime::now()))),
id_gen("orderman_ids", 5000) {
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
sessions.push_back(std::make_unique<SessionServicesData>(c, l, o));
for (auto &g : gateways) {
sessions.push_back(std::make_unique<SessionServicesData>(std::move(g)));
}
// spark::SparkDataDirectory data_dir;
// auto binance_j = data_dir.load_config_json("binance_execution");
}
} // namespace orderman
} // namespace bts
@@ -1,4 +1,5 @@
#pragma once
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/actors/ContentSubscription.h"
@@ -7,6 +8,8 @@
#include "lib/orderman/Governor.h"
#include "lib/orderman/OrderRouter.h"
#include "lib/orderman/actors/order_manager/PositionIndex.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/actors/order_manager/session_services_data.h"
#include "lib/orderman/events/ActivateOrderEvent.h"
#include "lib/orderman/events/BookUpdateEvent.h"
#include "lib/orderman/events/CancelOrderEvent.h"
@@ -53,54 +56,14 @@ struct ParentKey {
}
};
struct SessionServicesData {
SessionServicesData(
config::JetFIX::Connection const &cfg,
trace::PerfLog &log,
md::SecurityOracle o)
: session(cfg.name()), gw_connected(false), cfg(cfg),
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
gw(ExecutionGatewayFactory::create_gateway(cfg, log, o)) {
buffered_order_msgs.reserve(1000);
LOG(OrderMan, Info, "SessionServicesData::SessionServicesData cfg: {}",
utils::pb::to_string(cfg));
}
std::string session;
std::vector<jetfix::OutFIXPtr> free_buffers;
bool gw_connected;
config::JetFIX::Connection cfg;
std::unique_ptr<mercury::ContentSubscription<NewExecutionReportEvent>>
exe_report_subs;
std::unique_ptr<mercury::ContentSubscription<CancelRejectEvent>>
cancel_reject_subs;
std::unique_ptr<
mercury::ContentSubscription<GatewayStatusChangeEvent, mercury::NoKey>>
gateway_status_subs;
Governor governor;
// only one of these will actually be in use. This is messy but expedient
// in that it minimizes the modifications to existing FIX generation code
jetfix::OutFIXPtr buffered_fix_msgs;
std::vector<OrderMsg> buffered_order_msgs;
trace::CorID current_trace;
std::unique_ptr<IExecutionGateway> gw;
mercury::Stream<NewExecutionReportEvent> exe_report_pub;
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_pub;
mercury::Stream<CancelRejectEvent> cancel_reject_pub;
mercury::Stream<ExchangePositionEvent> exchange_position_pub;
mercury::Stream<ExchangeOpenOrdersEvent> exchange_open_ordes_pub;
};
class OrderServicesData {
public:
OrderServicesData(
OrderManager &owner,
DynamicOrderRegistry const &b,
trace::PerfLog &l,
md::SecurityOracle);
md::SecurityOracle,
std::vector<OrderGatewayDefinition> gateways);
OrderManager &owner;
trace::PerfLog &log;
@@ -0,0 +1,15 @@
#pragma once
#include "lib/config/protos/jetfix.pb.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include <memory>
namespace bts::orderman {
struct OrderGatewayDefinition {
std::shared_ptr<IExecutionGateway> gateway;
config::JetFIX_Connection connection;
};
} // namespace bts::orderman
@@ -0,0 +1,61 @@
#pragma once
#include "lib/logging/logging.h"
#include "lib/mercury/actors/ContentSubscription.h"
#include "lib/mercury/events/Stream.h"
#include "lib/orderman/Governor.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/events/CancelRejectEvent.h"
#include "lib/orderman/events/ExchangeOpenOrdersEvent.h"
#include "lib/orderman/events/ExchangePositionEvent.h"
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include "lib/utils/PBUtils.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <memory>
namespace bts::orderman {
struct SessionServicesData {
SessionServicesData(OrderGatewayDefinition gw_def)
: session(gw_def.connection.name()), gw_connected(false),
cfg(gw_def.connection),
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
gw(gw_def.gateway) {
buffered_order_msgs.reserve(1000);
LOG(OrderMan, Info, "SessionServicesData::SessionServicesData cfg: {}",
utils::pb::to_string(cfg));
}
std::string session;
std::vector<jetfix::OutFIXPtr> free_buffers;
bool gw_connected;
config::JetFIX::Connection cfg;
std::unique_ptr<mercury::ContentSubscription<NewExecutionReportEvent>>
exe_report_subs;
std::unique_ptr<mercury::ContentSubscription<CancelRejectEvent>>
cancel_reject_subs;
std::unique_ptr<
mercury::ContentSubscription<GatewayStatusChangeEvent, mercury::NoKey>>
gateway_status_subs;
Governor governor;
// only one of these will actually be in use. This is messy but expedient
// in that it minimizes the modifications to existing FIX generation code
jetfix::OutFIXPtr buffered_fix_msgs;
std::vector<OrderMsg> buffered_order_msgs;
trace::CorID current_trace;
std::shared_ptr<IExecutionGateway> gw;
mercury::Stream<NewExecutionReportEvent> exe_report_pub;
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_pub;
mercury::Stream<CancelRejectEvent> cancel_reject_pub;
mercury::Stream<ExchangePositionEvent> exchange_position_pub;
mercury::Stream<ExchangeOpenOrdersEvent> exchange_open_ordes_pub;
};
} // namespace bts::orderman
@@ -0,0 +1,7 @@
#include "IExecutionGateway.h"
namespace bts::orderman {
IExecutionGateway::~IExecutionGateway() = default;
} // namespace bts::orderman
@@ -39,8 +39,15 @@ using OnExchangeOpenOrdersState =
class IExecutionGateway {
public:
IExecutionGateway() = default;
virtual ~IExecutionGateway() {}
virtual ~IExecutionGateway();
virtual bool has_name() const {
return false;
}
virtual const std::string &get_name() const {
static std::string default_name;
return default_name;
}
virtual void start() = 0;
virtual void dispatch_incoming_fix_messages() = 0;
virtual void on_securities(const mercury::Update<NewSecuritiesEvent> &) = 0;
@@ -0,0 +1,31 @@
#include "execution_gateway_provider.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
namespace bts::orderman {
struct ExecutionGatewayProvider::PImpl {
PImpl(trace::PerfLog *l, md::SecurityOracle o) : l(l), o(std::move(o)) {}
trace::PerfLog *l;
md::SecurityOracle o;
};
ExecutionGatewayProvider::ExecutionGatewayProvider(
trace::PerfLog *l, md::SecurityOracle o)
: me_(std::make_unique<PImpl>(l, std::move(o))) {}
ExecutionGatewayProvider::~ExecutionGatewayProvider() = default;
std::vector<OrderGatewayDefinition> ExecutionGatewayProvider::create() {
auto conns = config::get<config::JetFIX>().connection();
std::vector<OrderGatewayDefinition> gateways;
for (auto c : conns) {
auto &gw = gateways.emplace_back();
gw.connection = std::move(c);
gw.gateway = ExecutionGatewayFactory::create_gateway(c, *me_->l, me_->o);
}
return gateways;
}
} // namespace bts::orderman
@@ -0,0 +1,22 @@
#pragma once
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include <memory>
#include <vector>
namespace bts::orderman {
class ExecutionGatewayProvider : public IExecutionGatewayProvider {
public:
ExecutionGatewayProvider(trace::PerfLog *l, md::SecurityOracle o);
~ExecutionGatewayProvider() override;
std::vector<OrderGatewayDefinition> create() override;
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::orderman
@@ -0,0 +1,7 @@
#include "i_execution_gateway_provider.h"
namespace bts::orderman {
IExecutionGatewayProvider::~IExecutionGatewayProvider() = default;
} // namespace bts::orderman
@@ -0,0 +1,17 @@
#pragma once
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include <vector>
namespace bts::orderman {
class IExecutionGatewayProvider {
public:
IExecutionGatewayProvider() = default;
virtual ~IExecutionGatewayProvider();
virtual std::vector<OrderGatewayDefinition> create() = 0;
};
} // namespace bts::orderman
+4 -1
View File
@@ -520,6 +520,9 @@ OrderOpResult EMNativeOrder::_on_activate(
case api::VENUE_OKX:
session_name="okx_1";
break;
case api::VENUE_BINANCE_USDS_FUTURES:
session_name = "binance_usds_futures_1";
break;
default:
session_name ="deribit_1";
@@ -801,4 +804,4 @@ void EMNativeOrder::_absorb_fill(const ExecutionReport &er) {
}
} // namespace orderman
} // namespace bts
} // namespace bts
+31 -2
View File
@@ -6,8 +6,11 @@
namespace bts::system::math {
static std::uint32_t checked_ltou(std::int64_t l);
static std::uint64_t checked_ltoul(std::int64_t l);
std::uint32_t checked_ltou(std::int64_t l);
std::uint64_t checked_ltoul(std::int64_t l);
bool checked_ltou(std::int64_t l, std::uint32_t &res);
bool checked_ltoul(std::int64_t l, std::uint64_t &res);
bool checked_ultol(std::uint64_t l, std::int64_t &res);
//////////////////////////////////////////////////
// Definition
@@ -34,4 +37,30 @@ inline std::uint64_t checked_ltoul(std::int64_t l) {
return static_cast<std::uint64_t>(l);
}
inline bool checked_ltou(std::int64_t l, std::uint32_t &res) {
if (l < 0 || l > static_cast<std::int64_t>(
std::numeric_limits<std::uint32_t>::max())) {
return false;
}
res = static_cast<std::uint32_t>(l);
return true;
}
inline bool checked_ltoul(std::int64_t l, std::uint64_t &res) {
if (l < 0) {
return false;
}
res = static_cast<std::uint64_t>(l);
return true;
}
inline std::int64_t checked_ultol(std::uint64_t ul) {
if (ul >
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())) {
throw std::overflow_error{
"Failed to convert uint64 to int64 (value > max)"};
}
return static_cast<std::int64_t>(ul);
}
} // namespace bts::system::math
+44
View File
@@ -0,0 +1,44 @@
#include "hmac.h"
#include <iomanip>
#include <openssl/evp.h>
#include <openssl/hmac.h>
namespace bts::system::crypto {
static std::string to_hex(const unsigned char *data, size_t length) {
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for (size_t i = 0; i < length; ++i) {
oss << std::setw(2) << static_cast<int>(data[i]);
}
return oss.str();
}
bool hmac_digest(
const std::string &secret,
const std::string &message,
unsigned char **bytes,
unsigned int *result_len) {
*bytes = HMAC(
EVP_sha256(), // Hash function
secret.c_str(), secret.size(), // Key
reinterpret_cast<const unsigned char *>(message.c_str()),
message.size(), // Data
nullptr, result_len // Output buffer and result length
);
return true;
}
bool hmac_digest_hex(
const std::string &secret,
const std::string &message,
std::string &result_hex) {
unsigned char *bytes;
unsigned int result_len;
hmac_digest(secret, message, &bytes, &result_len);
result_hex = to_hex(bytes, result_len);
return true;
}
} // namespace bts::system::crypto
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <string>
namespace bts::system::crypto {
bool hmac_digest(
const std::string &secret,
const std::string &message,
unsigned char **bytes,
unsigned int *result_len);
bool hmac_digest_hex(
const std::string &secret,
const std::string &message,
std::string &result_hex);
} // namespace bts::system::crypto
+17
View File
@@ -26,5 +26,22 @@ std::string normalize_data_path(std::string const &input_path) {
return input_path;
}
std::string read_file(const std::string &path) {
std::string out;
read_file(path, out);
return out;
}
void read_file(const std::string &path, std::string &out) {
std::ifstream ifstr{path};
// Preallocate memory
ifstr.seekg(0, std::ios::end);
out.reserve(ifstr.tellg());
ifstr.seekg(0, std::ios::beg);
out.assign(
(std::istreambuf_iterator<char>(ifstr)),
std::istreambuf_iterator<char>());
}
} // namespace utils
} // namespace bts
+4
View File
@@ -6,5 +6,9 @@ namespace bts {
namespace utils {
std::string normalize_data_path(std::string const &input_path);
bool create_if_not_there(std::string const &path);
std::string read_file(const std::string &path);
void read_file(const std::string &path, std::string &out);
} // namespace utils
} // namespace bts
+86
View File
@@ -0,0 +1,86 @@
#pragma once
#include <cstddef>
#include <string>
namespace bts::system::string_utils {
template <int N>
bool copy_to_array(
const std::string &src,
std::array<char, N> &dest,
bool null_terminate = true);
template <class UIntType>
bool parse_uint(const std::string &s, UIntType &i);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
template <int N>
bool copy_to_array(
const std::string &src, std::array<char, N> &dest, bool null_terminate) {
std::size_t i = 0;
auto sz = dest.size();
if (null_terminate) {
sz--;
}
if (src.size() < sz) {
sz = src.size();
}
for (; i < sz; i++) {
dest[i] = src.at(i);
}
if (null_terminate) {
dest[sz] = '\0';
}
return src.size() < dest.size() ||
(!null_terminate && src.size() == dest.size());
}
template <class UIntType>
inline bool parse_uint(const std::string &s, UIntType &i) {
i = 0;
const auto sz = s.size();
int pow10 = 1;
for (std::size_t n = 0; n < sz; n++) {
switch (s[sz - n - 1]) {
case '0':
break;
case '1':
i += pow10;
break;
case '2':
i += 2 * pow10;
break;
case '3':
i += 3 * pow10;
break;
case '4':
i += 4 * pow10;
break;
case '5':
i += 5 * pow10;
break;
case '6':
i += 6 * pow10;
break;
case '7':
i += 7 * pow10;
break;
case '8':
i += 8 * pow10;
break;
case '9':
i += 9 * pow10;
break;
default:
return false;
}
pow10 *= 10;
}
return true;
}
} // namespace bts::system::string_utils
+17
View File
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.23)
project(binance_execution)
file(
GLOB_RECURSE ${PROJECT_NAME}_SOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
*.cpp)
add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_SOURCES})
target_include_directories(
${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME} PUBLIC libspark)
@@ -0,0 +1,44 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(EventReasonType, NONE) \
per_value(EventReasonType, DEPOSIT) \
per_value(EventReasonType, WITHDRAW) \
per_value(EventReasonType, ORDER) \
per_value(EventReasonType, FUNDING_FEE) \
per_value(EventReasonType, WITHDRAW_REJECT) \
per_value(EventReasonType, ADJUSTMENT) \
per_value(EventReasonType, INSURANCE_CLEAR) \
per_value(EventReasonType, ADMIN_DEPOSIT) \
per_value(EventReasonType, ADMIN_WITHDRAW) \
per_value(EventReasonType, MARGIN_TRANSFER) \
per_value(EventReasonType, MARGIN_TYPE_CHANGE) \
per_value(EventReasonType, ASSET_TRANSFER) \
per_value(EventReasonType, OPTIONS_PREMIUM_FEE) \
per_value(EventReasonType, OPTIONS_SETTLE_PROFIT) \
per_value(EventReasonType, AUTO_EXCHANGE) \
per_value(EventReasonType, COIN_SWAP_DEPOSIT) \
per_value(EventReasonType, COIN_SWAP_WITHDRAW)
// clang-format on
/**
* @brief Represents the different Event Reason Types at Binance
*
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Balance-and-Position-Update
*/
ENUM_CLASS_DEF(EventReasonType, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::EventReasonType, t, "{}", t.name());
@@ -0,0 +1,32 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(ExecutionType, NONE) \
per_value(ExecutionType, NEW) \
per_value(ExecutionType, CANCELED) \
per_value(ExecutionType, REPLACED) \
per_value(ExecutionType, REJECTED) \
per_value(ExecutionType, TRADE) \
per_value(ExecutionType, EXPIRED) \
per_value(ExecutionType, TRADE_PREVENTION)
// clang-format on
/**
* @brief Represents the different execution types at Binance
*/
ENUM_CLASS_DEF(ExecutionType, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::ExecutionType, t, "{}", t.name());
@@ -0,0 +1,34 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(OrderStatus, NONE) \
per_value(OrderStatus, NEW) \
per_value(OrderStatus, PENDING_NEW) \
per_value(OrderStatus, PARTIALLY_FILLED) \
per_value(OrderStatus, FILLED) \
per_value(OrderStatus, CANCELED) \
per_value(OrderStatus, PENDING_CANCEL) \
per_value(OrderStatus, REJECTED) \
per_value(OrderStatus, EXPIRED) \
per_value(OrderStatus, EXPIRED_IN_MATCH)
// clang-format on
/**
* @brief Represents the different order statuses at Binance
*/
ENUM_CLASS_DEF(OrderStatus, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::OrderStatus, t, "{}", t.name());
@@ -0,0 +1,27 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(OrderType, NONE) \
per_value(OrderType, LIMIT) \
per_value(OrderType, MARKET)
// clang-format on
/**
* @brief Represents the different order types at Binance
*/
ENUM_CLASS_DEF(OrderType, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::OrderType, t, "{}", t.name());
@@ -0,0 +1,25 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution::usds_futures {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(PositionSide, NONE) \
per_value(PositionSide, LONG) \
per_value(PositionSide, SHORT) \
per_value(PositionSide, BOTH)
// clang-format on
ENUM_CLASS_DEF(PositionSide, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution::usds_futures
FMT_IMPL(binance_execution::usds_futures::PositionSide, s, "{}", s.name());
@@ -0,0 +1,30 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(PriceMatchMode, NONE) \
per_value(PriceMatchMode, OPPONENT) \
per_value(PriceMatchMode, OPPONENT_5) \
per_value(PriceMatchMode, OPPONENT_10) \
per_value(PriceMatchMode, OPPONENT_20) \
per_value(PriceMatchMode, QUEUE) \
per_value(PriceMatchMode, QUEUE_5) \
per_value(PriceMatchMode, QUEUE_10) \
per_value(PriceMatchMode, QUEUE_20)
// clang-format on
ENUM_CLASS_DEF(PriceMatchMode, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::PriceMatchMode, m, "{}", m.name());
@@ -0,0 +1,22 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(SelfTradePreventionMode, NONE)
// clang-format on
ENUM_CLASS_DEF(SelfTradePreventionMode, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::SelfTradePreventionMode, m, "{}", m.name());
@@ -0,0 +1,24 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(Side, NONE) \
per_value(Side, BUY) \
per_value(Side, SELL)
// clang-format on
ENUM_CLASS_DEF(Side, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::Side, s, "{}", s.name());
@@ -0,0 +1,30 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(TimeInForce, NONE) \
per_value(TimeInForce, GTC) \
per_value(TimeInForce, IOC) \
per_value(TimeInForce, FOK) \
per_value(TimeInForce, GTX) \
per_value(TimeInForce, GTD)
// clang-format on
/**
* @brief Represents the different TimeInForce types at Binance
*/
ENUM_CLASS_DEF(TimeInForce, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::TimeInForce, t, "{}", t.name());
@@ -0,0 +1,23 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace binance_execution {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(WorkingType, NONE) \
per_value(WorkingType, CONTRACT_PRICE)
// clang-format on
ENUM_CLASS_DEF(WorkingType, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace binance_execution
FMT_IMPL(binance_execution::WorkingType, t, "{}", t.name());
@@ -0,0 +1,40 @@
#include "execution_gateway_provider.h"
#include "binance_execution/usds_futures/execution_gateway.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
#include "lib/os/spark_data_directory.h"
namespace binance_execution {
struct ExecutionGatewayProvider::PImpl {
PImpl(bts::md::SecurityOracle sec_oracle)
: sec_oracle(std::move(sec_oracle)) {}
bts::md::SecurityOracle sec_oracle;
};
ExecutionGatewayProvider::ExecutionGatewayProvider(
bts::md::SecurityOracle sec_oracle)
: me_(std::make_unique<PImpl>(std::move(sec_oracle))) {}
ExecutionGatewayProvider::~ExecutionGatewayProvider() = default;
std::vector<bts::orderman::OrderGatewayDefinition>
ExecutionGatewayProvider::create() {
std::vector<bts::orderman::OrderGatewayDefinition> gws;
bts::spark::SparkDataDirectory data_dir;
usds_futures::ExecutionGatewayConfig usds_futures_cfg;
if (data_dir.load_config_json(
"binance_usds_futures_execution", usds_futures_cfg)) {
auto &binance_gw = gws.emplace_back();
*binance_gw.connection.mutable_name() = "binance_usds_futures_1";
binance_gw.connection.set_venue(bts::api::VENUE_BINANCE_USDS_FUTURES);
binance_gw.gateway = std::make_shared<usds_futures::ExecutionGateway>(
me_->sec_oracle, usds_futures_cfg, binance_gw.connection.name());
}
return gws;
}
} // namespace binance_execution
@@ -0,0 +1,24 @@
#pragma once
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
#include <memory>
#include <vector>
namespace binance_execution {
class ExecutionGatewayProvider
: public bts::orderman::IExecutionGatewayProvider {
public:
explicit ExecutionGatewayProvider(bts::md::SecurityOracle sec_oracle);
~ExecutionGatewayProvider() override;
std::vector<bts::orderman::OrderGatewayDefinition> create() override;
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace binance_execution
@@ -0,0 +1,40 @@
#pragma once
#include <charconv>
#include <string>
namespace binance_execution {
void price_to_string(
double value,
std::string &dest,
int n_decimals = 4,
std::size_t capacity = 24);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void price_to_string(
double value, std::string &dest, int n_decimals, std::size_t capacity) {
const std::size_t orig_sz = dest.size();
dest.resize(orig_sz + capacity);
char *buffer = &dest[orig_sz];
auto [ptr, ec] = std::to_chars(
buffer, buffer + capacity, value, std::chars_format::fixed, n_decimals);
if (ec != std::errc{}) {
// Formatting failed
dest.resize(orig_sz);
return;
}
// Resize to the actual formatted length
dest.resize(orig_sz + (ptr - buffer));
// Remove trailing zeros
dest.erase(dest.find_last_not_of('0') + 1);
// Remove trailing decimal point if necessary
if (!dest.empty() && dest.back() == '.') {
dest.pop_back();
}
}
} // namespace binance_execution
@@ -0,0 +1,40 @@
#pragma once
#include <charconv>
#include <string>
namespace binance_execution {
void quantity_to_string(
double value,
std::string &dest,
int n_decimals = 4,
std::size_t capacity = 24);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void quantity_to_string(
double value, std::string &dest, int n_decimals, std::size_t capacity) {
const std::size_t orig_sz = dest.size();
dest.resize(orig_sz + capacity);
char *buffer = &dest[orig_sz];
auto [ptr, ec] = std::to_chars(
buffer, buffer + capacity, value, std::chars_format::fixed, n_decimals);
if (ec != std::errc{}) {
// Formatting failed
dest.resize(orig_sz);
return;
}
// Resize to the actual formatted length
dest.resize(orig_sz + (ptr - buffer));
// Remove trailing zeros
dest.erase(dest.find_last_not_of('0') + 1);
// Remove trailing decimal point if necessary
if (!dest.empty() && dest.back() == '.') {
dest.pop_back();
}
}
} // namespace binance_execution
@@ -0,0 +1,405 @@
#include "execution_gateway.h"
#include "binance_execution/usds_futures/messages/create_order_response.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/rest_api_result.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"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/config/protos/toggles.pb.h"
#include "lib/core/TimeStamp.h"
#include "lib/domain_model/Security.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/events/Update.h"
#include "lib/orderman/CancelOrderMsg.h"
#include "lib/orderman/CancelReplaceOrderMsg.h"
#include "lib/orderman/CreateOrderMsg.h"
#include "lib/orderman/RFQMsg.h"
#include "lib/orderman/events/NewAdminDataEvent.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
#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 <boost/lockfree/spsc_queue.hpp>
#include <boost/pool/object_pool.hpp>
#include <nlohmann/json.hpp>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
using nlohmann::json;
namespace binance_execution::usds_futures {
#define _LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "ExecutionGateway::" fn ". " __VA_ARGS__)
#define INFO_FN(fn, ...) _LOG_FN(bts::Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) _LOG_FN(bts::Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__)
#define VENUE bts::api::VENUE_BINANCE_USDS_FUTURES
using boost::object_pool;
using boost::lockfree::spsc_queue;
using bts::core::Delegate;
using bts::orderman::CancelOrderMsg;
using bts::orderman::CancelReplaceOrderMsg;
using bts::orderman::CreateOrderMsg;
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::size_t;
using std::string;
using std::uint32_t;
using std::unordered_map;
using std::vector;
struct ExecutionGatewayCallbacks {
bts::orderman::OnExeReport exe_report;
bts::orderman::OnGwStatusChange gw_status_change;
bts::orderman::OnCancelReject cancel_reject;
bts::orderman::OnExchangePositionState exchange_position_state;
bts::orderman::OnExchangeOpenOrdersState exchange_open_orders;
};
struct ExecutionGateway::PImpl {
explicit PImpl(
bts::md::SecurityOracle sec_oracle,
ExecutionGatewayConfig config,
string session_name)
: sec_oracle(std::move(sec_oracle)), config(std::move(config)),
rest_api_client(
this->config.rest_endpoint,
this->config.api_key,
this->config.secret_key),
listen_key_requestor(this->rest_api_client) {
jetfix_connection.set_venue(bts::api::VENUE_BINANCE_USDS_FUTURES);
*jetfix_connection.mutable_name() = std::move(session_name);
pos.exchange = bts::api::VENUE_BINANCE_USDS_FUTURES;
}
bts::md::SecurityOracle sec_oracle;
ExecutionGatewayConfig config;
bts::config::JetFIX_Connection jetfix_connection;
unordered_map<int64_t, Delegate<void(json &)>> response_handlers;
RestApiClient rest_api_client;
optional<user_data_streams::UserDataSubscription> user_data_sub;
Delegate<void(user_data_streams::AccountUpdate)> account_update_callback;
Delegate<void(user_data_streams::OrderUpdate)> order_update_callback;
ListenKeyRequestor listen_key_requestor;
spsc_queue<nlohmann::json> incoming_msgs{1024};
spsc_queue<user_data_streams::AccountUpdate> incoming_account_updates{1024};
spsc_queue<user_data_streams::OrderUpdate> incoming_order_updates{1024};
spsc_queue<RestApiResult<CreateOrderResponse>> incoming_new_orders{1024};
object_pool<bts::orderman::ExecutionReport> exe_report_pool;
vector<user_data_streams::AccountUpdate> account_updates_vec;
ExecutionGatewayCallbacks callbacks;
bts::orderman::ExchangePosition pos;
bts::admin::AdminServiceUtils::ExchangeSubAccountResolver
subaccount_resolver{};
bts::api::admin::AdminData admin_data = {};
bts::api::admin::BTSAccount account;
void start() {
listen_key_requestor.callback().bind<&PImpl::on_listen_key_>(*this);
listen_key_requestor.start();
}
void stop() { user_data_sub.reset(); }
void on_account_update(user_data_streams::AccountUpdate u) {
incoming_account_updates.push(u);
}
void on_order_update(user_data_streams::OrderUpdate u) {
incoming_order_updates.push(u);
}
void on_listen_key_(const string &listen_key) {
user_data_sub.reset();
if (listen_key.empty()) {
LOG(OrderMan, bts::Error,
"Unable to subscribe to user data because 'listen_key' "
"is empty.");
return;
}
auto &uds = user_data_sub.emplace(
config.ws_stream_endpoint + "/ws/" + listen_key);
uds.account_update_callback().bind<&PImpl::on_account_update>(*this);
uds.order_update_callback().bind<&PImpl::on_order_update>(*this);
uds.start();
}
void create_order(const CreateOrderMsg &m) {
if (m.security == nullptr) {
ERROR_FN("send_order", "CreateOrderMsg security is nullptr");
incoming_new_orders.push(
RestApiResult<CreateOrderResponse>::from_error(
"security is nullptr"));
return;
}
bts_assert(m.security->exchange() == VENUE);
if (!m.security->has_binance_usds_futures()) {
ERROR_FN(
"send_order",
"CreateOrderMsg security not of Binance Usds Futures");
incoming_new_orders.push(
RestApiResult<CreateOrderResponse>::from_error(
"security not of Binance Usds Futures"));
return;
}
if (m.quantity == 0) {
ERROR_FN("send_order", "CreateOrderMsg quantity is 0");
incoming_new_orders.push(
RestApiResult<CreateOrderResponse>::from_error("quantity is 0"));
return;
}
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());
auto res = rest_api_client.create_order(req);
if (!res.ok()) {
ERROR_FN("send_order", "Send order failed. err: {}", res.error);
}
incoming_new_orders.push(res);
}
void update_order(const CancelReplaceOrderMsg &m) {
ModifyOrderRequest req;
req.orderId =
bts::system::math::checked_ultol(m.exchange_order_id.value());
req.origClientOrderId = std::to_string(m.create_fields.key.value());
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());
const auto res = rest_api_client.modify_order(req);
if (!res.ok()) {
ERROR_FN(
"update_order", "ord_id: {}; err: {}", req.orderId, res.error);
}
}
void cancel_order(const CancelOrderMsg &m) {
CancelOrderRequest req;
req.orderId =
bts::system::math::checked_ultol(m.exchange_order_id.value());
req.origClientOrderId = std::to_string(m.key.value());
req.symbol = m.security->binance_usds_futures().security_desc();
req.timestamp = convert_time_stamp(bts::core::TimeStamp::now());
const auto res = rest_api_client.cancel_order(req);
if (!res.ok()) {
ERROR_FN(
"cancel_order", "ord_id: {}; err: {}", req.orderId, res.error);
}
}
};
ExecutionGateway::ExecutionGateway(
bts::md::SecurityOracle sec_oracle,
ExecutionGatewayConfig session_cfg,
string session_name)
: me_(std::make_unique<PImpl>(
std::move(sec_oracle),
std::move(session_cfg),
std::move(session_name))) {
bts_assert(
me_->config.account.size() <=
bts::orderman::ExecutionReport{}.account_name.value().size());
}
ExecutionGateway::~ExecutionGateway() = default;
void ExecutionGateway::start() { me_->start(); }
void ExecutionGateway::on_securities(
const bts::mercury::Update<NewSecuritiesEvent> & /*update*/) {
INFO_FN(
"on_securities", "t_id: {} t_name: {}", bts::os::get_thread_id(),
bts::os::get_thread_name());
}
void ExecutionGateway::on_admin_data(
const bts::mercury::Update<NewAdminDataEvent> &update) {
INFO_FN(
"on_admin_data", "t_id: {} t_name: {}", bts::os::get_thread_id(),
bts::os::get_thread_name());
if (update.events == nullptr) {
return;
}
for (const auto &ev : *(update.events)) {
me_->admin_data.CopyFrom(*ev.admin_data);
}
auto *exch_sub_acct =
me_->subaccount_resolver.find_entry(me_->config.account, false);
if (exch_sub_acct == nullptr) {
WARN_FN(
"on_admin_data",
"Failed to map exchange account '{}' to internal account",
me_->config.account);
return;
}
auto *account = bts::admin::AdminServiceUtils::find_account(
me_->admin_data, exch_sub_acct->bts_account_name());
if (account == nullptr) {
WARN_FN(
"on_admin_data",
"Failed to find internal account '{}' (mapped from exchange account "
"'{}')",
exch_sub_acct->bts_account_name(), me_->config.account);
return;
}
if (me_->account.name() == account->name() &&
me_->account.id() == account->id()) {
return;
}
me_->account = *account;
INFO_FN(
"on_admin_data", "bts_account: {}; exch_subaccount: {}", account->name(),
exch_sub_acct->exchange_subaccount());
}
void ExecutionGateway::send_msgs(
vector<OrderMsg> const &msg_vec, bts::trace::CorID) {
INFO_FN("send_msgs", "count: {}", msg_vec.size());
for (const auto &msg : msg_vec) {
std::visit(
bts::overloaded{
[&](const CreateOrderMsg &m) { me_->create_order(m); },
[&](const CancelReplaceOrderMsg &m) { me_->update_order(m); },
[&](const CancelOrderMsg &m) { me_->cancel_order(m); },
[&](const RFQMsg &) {
bts_assert_msg(false, "Binance RFQ not implemented");
},
},
msg);
}
}
void ExecutionGateway::send_fix(
const bts::jetfix::OutFIXPtr &, bts::trace::CorID) {
bts_assert_msg(false, "ExecutionGateway does not support fix");
}
void ExecutionGateway::dispatch_incoming_fix_messages() {
static constexpr const bool IGNORE_CREATE_ORDER_RESPONSE = true;
// Order creates
{
RestApiResult<CreateOrderResponse> r;
while (me_->callbacks.exe_report && me_->incoming_new_orders.pop(r)) {
if (!r.ok() || !r.response) {
WARN_FN(
"dispatch_incoming_fix_messages",
"Cannot generate exe report from BinanceCreateOrderResult. {}",
r.error);
continue;
}
bts::orderman::ExecutionReport &er = *me_->exe_report_pool.construct();
if (!convert_execution_report(
*r.response, VENUE, me_->config.account,
&me_->jetfix_connection, er)) {
me_->exe_report_pool.destroy(&er);
WARN_FN(
"dispatch_incoming_fix_messages",
"Failed to generate exe report from CreateOrderResponse: {}",
nlohmann::json{*r.response}.dump());
}
er.security = me_->sec_oracle.get_security(VENUE, r.response->symbol);
if (er.security == nullptr) {
WARN_FN(
"dispatch_incoming_fix_messages",
"Failed to resolve bts security from name '{}'",
r.response->symbol);
}
if (!IGNORE_CREATE_ORDER_RESPONSE) {
me_->callbacks.exe_report(er, {});
}
}
}
// Order updates
{
user_data_streams::OrderUpdate u;
while (me_->callbacks.exe_report && me_->incoming_order_updates.pop(u)) {
bts::orderman::ExecutionReport &er = *me_->exe_report_pool.construct();
if (!convert_execution_report(
u, VENUE, me_->config.account, &me_->jetfix_connection, er)) {
me_->exe_report_pool.destroy(&er);
WARN_FN(
"dispatch_incoming_fix_messages",
"Failed to generate exe report from OrderUpdate: {}",
nlohmann::json{u}.dump());
} else {
er.security = me_->sec_oracle.get_security(VENUE, u.data.symbol);
me_->callbacks.exe_report(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);
}
}
}
}
// Callbacks
bts::orderman::OnExeReport &ExecutionGateway::on_exe_report() {
return me_->callbacks.exe_report;
}
bts::orderman::OnGwStatusChange &ExecutionGateway::on_gw_status_change() {
return me_->callbacks.gw_status_change;
}
bts::orderman::OnCancelReject &ExecutionGateway::on_cancel_reject() {
return me_->callbacks.cancel_reject;
}
bts::orderman::OnExchangePositionState &
ExecutionGateway::on_exchange_position_state() {
return me_->callbacks.exchange_position_state;
}
bts::orderman::OnExchangeOpenOrdersState &
ExecutionGateway::on_exchange_open_orders() {
return me_->callbacks.exchange_open_orders;
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,43 @@
#pragma once
#include "binance_execution/usds_futures/execution_gateway_config.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/gateways/IExecutionGateway.h"
#include <memory>
namespace binance_execution::usds_futures {
class ExecutionGateway : public bts::orderman::IExecutionGateway {
public:
explicit ExecutionGateway(
bts::md::SecurityOracle sec_oracle,
ExecutionGatewayConfig session_cfg,
std::string session_name);
~ExecutionGateway() override;
void start() override;
void dispatch_incoming_fix_messages() override;
void on_securities(
const bts::mercury::Update<bts::orderman::NewSecuritiesEvent> &)
override;
void on_admin_data(
const bts::mercury::Update<bts::orderman::NewAdminDataEvent> &) override;
void send_fix(const bts::jetfix::OutFIXPtr &, bts::trace::CorID) override;
void send_msgs(
std::vector<bts::orderman::OrderMsg> const &,
bts::trace::CorID) override;
bts::orderman::OnExeReport &on_exe_report() override;
bts::orderman::OnGwStatusChange &on_gw_status_change() override;
bts::orderman::OnCancelReject &on_cancel_reject() override;
bts::orderman::OnExchangePositionState &
on_exchange_position_state() override;
bts::orderman::OnExchangeOpenOrdersState &on_exchange_open_orders() override;
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace binance_execution::usds_futures
@@ -0,0 +1,37 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace binance_execution::usds_futures {
struct ExecutionGatewayConfig {
std::string api_key;
std::string secret_key;
std::string account;
std::string rest_endpoint;
std::string ws_endpoint;
std::string ws_stream_endpoint;
struct Subscription {
std::string symbol;
std::string pair;
std::string contract_type;
std::string base_asset;
std::string quote_asset;
};
std::vector<Subscription> subscriptions;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
ExecutionGatewayConfig,
api_key,
secret_key,
account,
rest_endpoint,
ws_endpoint,
ws_stream_endpoint);
} // namespace binance_execution::usds_futures
@@ -0,0 +1,109 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/All-Orders
*/
struct AllOrdersRequest {
std::string symbol;
std::int64_t orderId;
std::int64_t startTime;
std::int64_t endTime;
int limit;
std::int64_t recvWindow;
std::int64_t timestamp;
};
void to_json(nlohmann::json &j, const AllOrdersRequest &req);
void from_json(const nlohmann::json &j, AllOrdersRequest &req);
void to_query_params(std::string &qps, const AllOrdersRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const AllOrdersRequest &req) {
j["timestamp"] = req.timestamp;
if (!req.symbol.empty()) {
j["symbol"] = req.symbol;
}
if (req.orderId) {
j["orderId"] = req.orderId;
}
if (req.startTime) {
j["startTime"] = req.startTime;
}
if (req.endTime) {
j["endTime"] = req.endTime;
}
if (req.limit) {
j["limit"] = req.limit;
}
if (req.recvWindow) {
j["recvWindow"] = req.recvWindow;
}
}
inline void from_json(const nlohmann::json &j, AllOrdersRequest &req) {
j.at("timestamp").get_to(req.timestamp);
auto it = j.find("symbol");
if (it != j.end()) {
it->get_to(req.symbol);
}
it = j.find("orderId");
if (it != j.end()) {
it->get_to(req.orderId);
}
it = j.find("startTime");
if (it != j.end()) {
it->get_to(req.startTime);
}
it = j.find("endTime");
if (it != j.end()) {
it->get_to(req.endTime);
}
it = j.find("limit");
if (it != j.end()) {
it->get_to(req.limit);
}
it = j.find("recvWindow");
if (it != j.end()) {
it->get_to(req.recvWindow);
}
}
inline void to_query_params(std::string &qps, const AllOrdersRequest &req) {
qps.append("timestamp=");
qps.append(std::to_string(req.timestamp));
if (!req.symbol.empty()) {
qps.append("&symbol=");
qps.append(req.symbol);
}
if (req.orderId) {
qps.append("&orderId=");
qps.append(std::to_string(req.orderId));
}
if (req.startTime) {
qps.append("&startTime=");
qps.append(std::to_string(req.startTime));
}
if (req.endTime) {
qps.append("&endTime=");
qps.append(std::to_string(req.endTime));
}
if (req.limit) {
qps.append("&limit=");
qps.append(std::to_string(req.limit));
}
if (req.recvWindow) {
qps.append("&recvWindow=");
qps.append(std::to_string(req.recvWindow));
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,66 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Order
*/
struct CancelOrderRequest {
std::int64_t orderId = 0;
std::string origClientOrderId;
std::string symbol;
std::int64_t timestamp = 0;
};
void to_json(nlohmann::json &j, const CancelOrderRequest &req);
void from_json(const nlohmann::json &j, CancelOrderRequest &req);
void to_query_params(std::string &qps, const CancelOrderRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const CancelOrderRequest &req) {
j["symbol"] = req.symbol;
if (req.orderId != 0) {
j["orderId"] = req.orderId;
}
if (!req.origClientOrderId.empty()) {
j["origClientOrderId"] = req.origClientOrderId;
}
j["timestamp"] = req.timestamp;
}
inline void from_json(const nlohmann::json &j, CancelOrderRequest &req) {
j.at("symbol").get_to(req.symbol);
auto it = j.find("orderId");
if (it != j.end()) {
it->get_to(req.orderId);
}
it = j.find("origClientOrderId");
if (it != j.end()) {
it->get_to(req.origClientOrderId);
}
j.at("timestamp").get_to(req.timestamp);
}
inline void to_query_params(std::string &qps, const CancelOrderRequest &req) {
qps.append("symbol=");
qps.append(req.symbol);
if (req.orderId != 0) {
qps.append("&orderId=");
qps.append(std::to_string(req.orderId));
}
if (!req.origClientOrderId.empty()) {
qps.append("&origClientOrderId=");
qps.append(req.origClientOrderId);
}
qps.append("&timestamp=");
qps.append(std::to_string(req.timestamp));
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,112 @@
#pragma once
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/position_side.h"
#include "binance_execution/enums/price_match_mode.h"
#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/enums/working_type.h"
#include "lib/io/JsonReader.h"
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Order
*/
struct CancelOrderResponse {
std::string clientOrderId;
double cumQuote;
double executedQty;
std::int64_t orderId;
double origQty;
OrderType origType;
double price;
bool reduceOnly;
Side side;
PositionSide positionSide;
OrderStatus status;
double stopPrice;
bool closePosition;
std::string symbol;
TimeInForce timeInForce;
OrderType type;
double activatePrice;
double priceRate;
std::int64_t updateTime;
WorkingType workingType;
bool priceProtect;
PriceMatchMode priceMatch;
SelfTradePreventionMode selfTradePreventionMode;
std::int64_t goodTillDate;
};
void to_json(nlohmann::json &j, const CancelOrderResponse &res);
void from_json(const nlohmann::json &j, CancelOrderResponse &res);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const CancelOrderResponse &res) {
bts::io::JsonReader jr{&j};
j["clientOrderId"] = res.clientOrderId;
j["cumQuote"] = res.cumQuote;
j["executedQty"] = res.executedQty;
j["orderId"] = res.orderId;
j["origQty"] = res.origQty;
j["origType"] = res.origType;
j["price"] = res.price;
j["reduceOnly"] = res.reduceOnly;
j["side"] = res.side;
j["positionSide"] = res.positionSide;
j["status"] = res.status;
j["stopPrice"] = res.stopPrice;
j["closePosition"] = res.closePosition;
j["symbol"] = res.symbol;
j["timeInForce"] = res.timeInForce;
j["type"] = res.type;
j["activatePrice"] = res.activatePrice;
j["priceRate"] = res.priceRate;
j["updateTime"] = res.updateTime;
j["workingType"] = res.workingType;
j["priceProtect"] = res.priceProtect;
j["priceMatch"] = res.priceMatch;
j["selfTradePreventionMode"] = res.selfTradePreventionMode;
j["goodTillDate"] = res.goodTillDate;
}
inline void from_json(const nlohmann::json &j, CancelOrderResponse &res) {
bts::io::JsonReader jr{&j};
j.at("clientOrderId").get_to(res.clientOrderId);
jr.read("cumQuote", res.cumQuote);
jr.read("executedQty", res.executedQty);
j.at("orderId").get_to(res.orderId);
jr.read("origQty", res.origQty);
j.at("origType").get_to(res.origType);
jr.read("price", res.price);
j.at("reduceOnly").get_to(res.reduceOnly);
j.at("side").get_to(res.side);
j.at("positionSide").get_to(res.positionSide);
j.at("status").get_to(res.status);
jr.read("stopPrice", res.stopPrice);
j.at("closePosition").get_to(res.closePosition);
j.at("symbol").get_to(res.symbol);
j.at("timeInForce").get_to(res.timeInForce);
j.at("type").get_to(res.type);
jr.read("activatePrice", res.activatePrice);
jr.read("priceRate", res.priceRate);
j.at("updateTime").get_to(res.updateTime);
j.at("workingType").get_to(res.workingType);
j.at("priceProtect").get_to(res.priceProtect);
j.at("priceMatch").get_to(res.priceMatch);
j.at("selfTradePreventionMode").get_to(res.selfTradePreventionMode);
j.at("goodTillDate").get_to(res.goodTillDate);
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,120 @@
#pragma once
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/side.h"
#include "binance_execution/enums/time_in_force.h"
#include "binance_execution/price.h"
#include "binance_execution/quantity.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api
*/
struct CreateOrderRequest {
std::string symbol;
Side side = Side::NONE;
OrderType type = OrderType::NONE;
std::int64_t timestamp = 0;
std::string newClientOrderId;
double price = std::numeric_limits<double>::quiet_NaN();
double quantity = 0;
TimeInForce timeInForce = TimeInForce::NONE;
std::int64_t recvWindow = 0;
};
void to_json(nlohmann::json &j, const CreateOrderRequest &req);
void from_json(const nlohmann::json &j, CreateOrderRequest &req);
void to_query_params(std::string &query_params, const CreateOrderRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const CreateOrderRequest &req) {
j["symbol"] = req.symbol;
j["side"] = req.side;
j["type"] = req.type;
j["timestamp"] = req.timestamp;
if (!req.newClientOrderId.empty()) {
j["newClientOrderId"] = req.newClientOrderId;
}
if (!std::isnan(req.price)) {
std::string price_str;
price_to_string(req.price, price_str);
j["price"] = price_str;
}
if (req.quantity > 0) {
std::string qty_str;
quantity_to_string(req.quantity, qty_str);
j["quantity"] = qty_str;
}
if (req.timeInForce != TimeInForce::NONE) {
j["timeInForce"] = req.timeInForce;
}
if (req.recvWindow > 0) {
j["recvWindow"] = std::to_string(req.recvWindow);
}
}
inline void from_json(const nlohmann::json &j, CreateOrderRequest &req) {
j.at("symbol").get_to(req.symbol);
j.at("side").get_to(req.side);
j.at("type").get_to(req.type);
j.at("timestamp").get_to(req.timestamp);
j.at("newClientOrderId").get_to(req.newClientOrderId);
auto it = j.find("price");
if (it != j.end()) {
it->get_to(req.price);
}
it = j.find("quantity");
if (it != j.end()) {
it->get_to(req.quantity);
}
it = j.find("timeInForce");
if (it != j.end()) {
it->get_to(req.timeInForce);
}
it = j.find("recvWindow");
if (it != j.end()) {
it->get_to(req.recvWindow);
}
}
inline void to_query_params(std::string &qps, const CreateOrderRequest &req) {
qps.append("symbol=");
qps.append(req.symbol);
qps.append("&side=");
qps.append(req.side.name());
qps.append("&type=");
qps.append(req.type.name());
qps.append("&timestamp=");
qps.append(std::to_string(req.timestamp));
if (!req.newClientOrderId.empty()) {
qps.append("&newClientOrderId=");
qps.append(req.newClientOrderId);
}
// Format price & qty with 4 decimal places (should be sufficient the contracts we support)
if (!std::isnan(req.price)) {
qps.append("&price=");
price_to_string(req.price, qps);
}
if (req.quantity > 0) {
qps.append("&quantity=");
quantity_to_string(req.quantity, qps);
}
if (req.timeInForce != TimeInForce::NONE) {
qps.append("&timeInForce=");
qps.append(req.timeInForce.name());
}
if (req.recvWindow > 0) {
qps.append("&recvWindow=");
qps.append(std::to_string(req.recvWindow));
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,113 @@
#pragma once
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/position_side.h"
#include "binance_execution/enums/price_match_mode.h"
#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/enums/working_type.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api
*/
struct CreateOrderResponse {
std::string clientOrderId;
double cumQty;
double cumQuote;
double executedQty;
std::int64_t orderId;
double avgPrice;
double origQty;
double price;
bool reduceOnly;
Side side;
PositionSide positionSide;
OrderStatus status;
double stopPrice;
bool closePosition;
std::string symbol;
TimeInForce timeInForce;
OrderType type;
OrderType origType;
double activatePrice;
double priceRate;
std::int64_t updateTime;
WorkingType workingType;
bool priceProtect;
PriceMatchMode priceMatch;
SelfTradePreventionMode selfTradePreventionMode;
std::int64_t goodTillDate;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const CreateOrderResponse &res) {
j["clientOrderId"] = res.clientOrderId;
j["cumQty"] = res.cumQty;
j["cumQuote"] = res.cumQuote;
j["executedQty"] = res.executedQty;
j["orderId"] = res.orderId;
j["avgPrice"] = res.avgPrice;
j["origQty"] = res.origQty;
j["price"] = res.price;
j["reduceOnly"] = res.reduceOnly;
j["side"] = res.side;
j["positionSide"] = res.positionSide;
j["status"] = res.status;
j["stopPrice"] = res.stopPrice;
j["closePosition"] = res.closePosition;
j["symbol"] = res.symbol;
j["timeInForce"] = res.timeInForce;
j["type"] = res.type;
j["origType"] = res.origType;
j["activatePrice"] = res.activatePrice;
j["priceRate"] = res.priceRate;
j["updateTime"] = res.updateTime;
j["workingType"] = res.workingType;
j["priceProtect"] = res.priceProtect;
j["priceMatch"] = res.priceMatch;
j["selfTradePreventionMode"] = res.selfTradePreventionMode;
j["goodTillDate"] = res.goodTillDate;
}
inline void from_json(const nlohmann::json &j, CreateOrderResponse &res) {
bts::io::JsonReader jr{&j};
j.at("clientOrderId").get_to(res.clientOrderId);
jr.read("cumQty", res.cumQty);
jr.read("cumQuote", res.cumQuote);
jr.read("executedQty", res.executedQty);
j.at("orderId").get_to(res.orderId);
jr.read("avgPrice", res.avgPrice);
jr.read("origQty", res.origQty);
jr.read("price", res.price);
j.at("reduceOnly").get_to(res.reduceOnly);
j.at("side").get_to(res.side);
j.at("positionSide").get_to(res.positionSide);
j.at("status").get_to(res.status);
jr.read("stopPrice", res.stopPrice);
j.at("closePosition").get_to(res.closePosition);
j.at("symbol").get_to(res.symbol);
j.at("timeInForce").get_to(res.timeInForce);
j.at("type").get_to(res.type);
j.at("origType").get_to(res.origType);
jr.read("activatePrice", res.activatePrice);
jr.read("priceRate", res.priceRate);
j.at("updateTime").get_to(res.updateTime);
j.at("workingType").get_to(res.workingType);
j.at("priceProtect").get_to(res.priceProtect);
j.at("priceMatch").get_to(res.priceMatch);
j.at("selfTradePreventionMode").get_to(res.selfTradePreventionMode);
j.at("goodTillDate").get_to(res.goodTillDate);
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,15 @@
#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
@@ -0,0 +1,17 @@
#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
@@ -0,0 +1,107 @@
#pragma once
#include "binance_execution/enums/price_match_mode.h"
#include "binance_execution/enums/side.h"
#include "binance_execution/price.h"
#include "binance_execution/quantity.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Modify-Order
*/
struct ModifyOrderRequest {
std::int64_t orderId = 0; // *optional
std::string origClientOrderId; // *optional
std::string symbol; // mandatory
Side side = Side::NONE; // mandatory
double quantity = 0; // mandatory
double price = std::numeric_limits<double>::quiet_NaN(); // mandatory
PriceMatchMode priceMatch = PriceMatchMode::NONE; // optional
std::int64_t recvWindow = 0; // optional
std::int64_t timestamp = 0; // mandatory
// * Either orderId or origClientOrderId must be specified
};
void to_json(nlohmann::json &j, const ModifyOrderRequest &req);
void from_json(const nlohmann::json &j, ModifyOrderRequest &req);
void to_query_params(std::string &query_params, const ModifyOrderRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const ModifyOrderRequest &req) {
if (req.orderId != 0) {
j["orderId"] = req.orderId;
}
if (!req.origClientOrderId.empty()) {
j["origClientOrderId"] = req.origClientOrderId;
}
j["symbol"] = req.symbol;
j["side"] = req.side;
j["quantity"] = req.quantity;
j["price"] = req.price;
if (req.priceMatch != PriceMatchMode::NONE) {
j["priceMatch"] = req.price;
}
if (req.recvWindow > 0) {
j["recvWindow"] = req.recvWindow;
}
j["timestamp"] = req.timestamp;
}
inline void from_json(const nlohmann::json &j, ModifyOrderRequest &req) {
auto it = j.find("orderId");
if (it != j.end()) {
it->get_to(req.orderId);
}
it = j.find("origClientOrderId");
if (it != j.end()) {
it->get_to(req.origClientOrderId);
}
j.at("symbol").get_to(req.symbol);
j.at("side").get_to(req.side);
j.at("quantity").get_to(req.quantity);
j.at("price").get_to(req.price);
it = j.find("priceMatch");
if (it != j.end()) {
it->get_to(req.priceMatch);
}
it = j.find("recvWindow");
if (it != j.end()) {
it->get_to(req.recvWindow);
}
j.at("timestamp").get_to(req.timestamp);
}
inline void to_query_params(std::string &qps, const ModifyOrderRequest &req) {
qps.append("symbol=");
qps.append(req.symbol);
qps.append("&side=");
qps.append(req.side.name());
qps.append("&quantity=");
quantity_to_string(req.quantity, qps);
qps.append("&price=");
price_to_string(req.price, qps);
qps.append("&timestamp=");
qps.append(std::to_string(req.timestamp));
if (!req.origClientOrderId.empty()) {
qps.append("&origClientOrderId=");
qps.append(req.origClientOrderId);
}
if (req.orderId != 0) {
qps.append("&orderId=");
qps.append(std::to_string(req.orderId));
}
if (req.recvWindow > 0) {
qps.append("&orderId=");
qps.append(std::to_string(req.orderId));
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,114 @@
#pragma once
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/position_side.h"
#include "binance_execution/enums/price_match_mode.h"
#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/enums/working_type.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Modify-Order
*/
struct ModifyOrderResponse {
std::int64_t orderId;
std::string symbol;
std::string pair;
OrderStatus status;
std::string clientOrderId;
double price;
double avgPrice;
double origQty;
double executedQty;
double cumQty;
double cumBase;
TimeInForce timeInForce;
OrderType type;
bool reduceOnly;
bool closePosition;
Side side;
PositionSide positionSide;
double stopPrice;
WorkingType workingType;
bool priceProtect;
OrderType origType;
PriceMatchMode priceMatch;
SelfTradePreventionMode selfTradePreventionMode;
std::int64_t goodTillDate;
std::int64_t updateTime;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const ModifyOrderResponse &res) {
j["orderId"] = res.orderId;
j["symbol"] = res.symbol;
// omit pair if empty
if (!res.pair.empty()) {
j["pair"] = res.pair;
}
j["status"] = res.status;
j["clientOrderId"] = res.clientOrderId;
j["price"] = res.price;
j["avgPrice"] = res.avgPrice;
j["origQty"] = res.origQty;
j["executedQty"] = res.executedQty;
j["cumQty"] = res.cumQty;
j["cumBase"] = res.cumBase;
j["timeInForce"] = res.timeInForce;
j["type"] = res.type;
j["reduceOnly"] = res.reduceOnly;
j["closePosition"] = res.closePosition;
j["side"] = res.side;
j["positionSide"] = res.positionSide;
j["stopPrice"] = res.stopPrice;
j["workingType"] = res.workingType;
j["priceProtect"] = res.priceProtect;
j["origType"] = res.origType;
j["priceMatch"] = res.priceMatch;
j["selfTradePreventionMode"] = res.selfTradePreventionMode;
j["goodTillDate"] = res.goodTillDate;
j["updateTime"] = res.updateTime;
}
inline void from_json(const nlohmann::json &j, ModifyOrderResponse &res) {
bts::io::JsonReader jr{&j};
j.at("orderId").get_to(res.orderId);
j.at("symbol").get_to(res.symbol);
// pair might be omitted
jr.read("pair", res.pair);
j.at("status").get_to(res.status);
j.at("clientOrderId").get_to(res.clientOrderId);
jr.read("price", res.price);
jr.read("avgPrice", res.avgPrice);
jr.read("origQty", res.origQty);
jr.read("executedQty", res.executedQty);
jr.read("cumQty", res.cumQty);
jr.read("cumBase", res.cumBase);
j.at("timeInForce").get_to(res.timeInForce);
j.at("type").get_to(res.type);
j.at("reduceOnly").get_to(res.reduceOnly);
j.at("closePosition").get_to(res.closePosition);
j.at("side").get_to(res.side);
j.at("positionSide").get_to(res.positionSide);
jr.read("stopPrice", res.stopPrice);
j.at("workingType").get_to(res.workingType);
j.at("priceProtect").get_to(res.priceProtect);
j.at("origType").get_to(res.origType);
j.at("priceMatch").get_to(res.priceMatch);
j.at("selfTradePreventionMode").get_to(res.selfTradePreventionMode);
j.at("goodTillDate").get_to(res.goodTillDate);
j.at("updateTime").get_to(res.updateTime);
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,121 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Current-All-Open-Orders
*/
struct OpenOrdersRequest {
std::string symbol;
std::string pair;
std::int64_t orderId;
std::int64_t startTime;
std::int64_t endTime;
int limit;
std::int64_t recvWindow;
std::int64_t timestamp;
};
void to_json(nlohmann::json &j, const OpenOrdersRequest &req);
void from_json(const nlohmann::json &j, OpenOrdersRequest &req);
void to_query_params(std::string &qps, const OpenOrdersRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const OpenOrdersRequest &req) {
j["timestamp"] = req.timestamp;
if (!req.symbol.empty()) {
j["symbol"] = req.symbol;
}
if (!req.pair.empty()) {
j["pair"] = req.pair;
}
if (req.orderId) {
j["orderId"] = req.orderId;
}
if (req.startTime) {
j["startTime"] = req.startTime;
}
if (req.endTime) {
j["endTime"] = req.endTime;
}
if (req.limit) {
j["limit"] = req.limit;
}
if (req.recvWindow) {
j["recvWindow"] = req.recvWindow;
}
}
inline void from_json(const nlohmann::json &j, OpenOrdersRequest &req) {
j.at("timestamp").get_to(req.timestamp);
auto it = j.find("symbol");
if (it != j.end()) {
it->get_to(req.symbol);
}
it = j.find("pair");
if (it != j.end()) {
it->get_to(req.pair);
}
it = j.find("orderId");
if (it != j.end()) {
it->get_to(req.orderId);
}
it = j.find("startTime");
if (it != j.end()) {
it->get_to(req.startTime);
}
it = j.find("endTime");
if (it != j.end()) {
it->get_to(req.endTime);
}
it = j.find("limit");
if (it != j.end()) {
it->get_to(req.limit);
}
it = j.find("recvWindow");
if (it != j.end()) {
it->get_to(req.recvWindow);
}
}
inline void to_query_params(std::string &qps, const OpenOrdersRequest &req) {
qps.append("timestamp=");
qps.append(std::to_string(req.timestamp));
if (!req.symbol.empty()) {
qps.append("&symbol=");
qps.append(req.symbol);
}
if (!req.pair.empty()) {
qps.append("&pair=");
qps.append(req.pair);
}
if (req.orderId) {
qps.append("&orderId=");
qps.append(std::to_string(req.orderId));
}
if (req.startTime) {
qps.append("&startTime=");
qps.append(std::to_string(req.startTime));
}
if (req.endTime) {
qps.append("&endTime=");
qps.append(std::to_string(req.endTime));
}
if (req.limit) {
qps.append("&limit=");
qps.append(std::to_string(req.limit));
}
if (req.recvWindow) {
qps.append("&recvWindow=");
qps.append(std::to_string(req.recvWindow));
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,104 @@
#pragma once
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/position_side.h"
#include "binance_execution/enums/price_match_mode.h"
#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/enums/working_type.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
struct OrderRecord {
double avgPrice;
std::string clientOrderId;
double cumQuote;
double executedQty;
std::uint32_t orderId;
double origQty;
OrderType origType;
double price;
bool reduceOnly;
Side side;
PositionSide positionSide;
OrderStatus status;
double stopPrice;
bool closePosition;
std::string symbol;
std::int64_t time;
TimeInForce timeInForce;
OrderType type;
double activatePrice;
double priceRate;
std::int64_t updateTime;
WorkingType workingType;
bool priceProtect;
PriceMatchMode priceMatch;
SelfTradePreventionMode selfTradePreventionMode;
std::int64_t goodTillDate;
};
inline void to_json(nlohmann::json &j, const OrderRecord &r) {
j["avgPrice"] = r.avgPrice;
j["clientOrderId"] = r.clientOrderId;
j["cumQuote"] = r.cumQuote;
j["executedQty"] = r.executedQty;
j["orderId"] = r.orderId;
j["origQty"] = r.origQty;
j["origType"] = r.origType;
j["price"] = r.price;
j["reduceOnly"] = r.reduceOnly;
j["side"] = r.side;
j["positionSide"] = r.positionSide;
j["status"] = r.status;
j["stopPrice"] = r.stopPrice;
j["closePosition"] = r.closePosition;
j["symbol"] = r.symbol;
j["time"] = r.time;
j["timeInForce"] = r.timeInForce;
j["type"] = r.type;
j["activatePrice"] = r.activatePrice;
j["priceRate"] = r.priceRate;
j["updateTime"] = r.updateTime;
j["workingType"] = r.workingType;
j["priceProtect"] = r.priceProtect;
j["priceMatch"] = r.priceMatch;
j["selfTradePreventionMode"] = r.selfTradePreventionMode;
j["goodTillDate"] = r.goodTillDate;
}
inline void from_json(const nlohmann::json &j, OrderRecord &r) {
j.at("avgPrice").get_to(r.avgPrice);
j.at("clientOrderId").get_to(r.clientOrderId);
j.at("cumQuote").get_to(r.cumQuote);
j.at("executedQty").get_to(r.executedQty);
j.at("orderId").get_to(r.orderId);
j.at("origQty").get_to(r.origQty);
j.at("origType").get_to(r.origType);
j.at("price").get_to(r.price);
j.at("reduceOnly").get_to(r.reduceOnly);
j.at("side").get_to(r.side);
j.at("positionSide").get_to(r.positionSide);
j.at("status").get_to(r.status);
j.at("stopPrice").get_to(r.stopPrice);
j.at("closePosition").get_to(r.closePosition);
j.at("symbol").get_to(r.symbol);
j.at("time").get_to(r.time);
j.at("timeInForce").get_to(r.timeInForce);
j.at("type").get_to(r.type);
j.at("activatePrice").get_to(r.activatePrice);
j.at("priceRate").get_to(r.priceRate);
j.at("updateTime").get_to(r.updateTime);
j.at("workingType").get_to(r.workingType);
j.at("priceProtect").get_to(r.priceProtect);
j.at("priceMatch").get_to(r.priceMatch);
j.at("selfTradePreventionMode").get_to(r.selfTradePreventionMode);
j.at("goodTillDate").get_to(r.goodTillDate);
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,92 @@
#pragma once
#include "binance_execution/enums/position_side.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Balance-and-Position-Update
*/
struct PositionInfo {
std::string symbol;
PositionSide positionSide;
double positionAmt;
double entryPrice;
double breakEvenPrice;
double markPrice;
double unRealizedProfit;
double liquidationPrice;
double isolatedMargin;
double notional;
std::string marginAsset;
double isolatedWallet;
double initialMargin;
double maintMargin;
double positionInitialMargin;
double openOrderInitialMargin;
std::uint32_t adl;
double bidNotional;
double askNotional;
std::int64_t updateTime;
};
void to_json(nlohmann::json &j, const PositionInfo &p);
void from_json(const nlohmann::json &j, PositionInfo &p);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const PositionInfo &p) {
j["symbol"] = p.symbol;
j["positionSide"] = p.positionSide;
j["positionAmt"] = p.positionAmt;
j["entryPrice"] = p.entryPrice;
j["breakEvenPrice"] = p.breakEvenPrice;
j["markPrice"] = p.markPrice;
j["unRealizedProfit"] = p.unRealizedProfit;
j["liquidationPrice"] = p.liquidationPrice;
j["isolatedMargin"] = p.isolatedMargin;
j["notional"] = p.notional;
j["marginAsset"] = p.marginAsset;
j["isolatedWallet"] = p.isolatedWallet;
j["initialMargin"] = p.initialMargin;
j["maintMargin"] = p.maintMargin;
j["positionInitialMargin"] = p.positionInitialMargin;
j["openOrderInitialMargin"] = p.openOrderInitialMargin;
j["adl"] = p.adl;
j["bidNotional"] = p.bidNotional;
j["askNotional"] = p.askNotional;
j["updateTime"] = p.updateTime;
}
inline void from_json(const nlohmann::json &j, PositionInfo &p) {
j.at("symbol").get_to(p.symbol);
j.at("positionSide").get_to(p.positionSide);
bts::io::JsonReader jr{&j};
jr.read("positionAmt", p.positionAmt);
jr.read("entryPrice", p.entryPrice);
jr.read("breakEvenPrice", p.breakEvenPrice);
jr.read("markPrice", p.markPrice);
jr.read("unRealizedProfit", p.unRealizedProfit);
jr.read("liquidationPrice", p.liquidationPrice);
jr.read("isolatedMargin", p.isolatedMargin);
jr.read("notional", p.notional);
j.at("marginAsset").get_to(p.marginAsset);
jr.read("isolatedWallet", p.isolatedWallet);
jr.read("initialMargin", p.initialMargin);
jr.read("maintMargin", p.maintMargin);
jr.read("positionInitialMargin", p.positionInitialMargin);
jr.read("openOrderInitialMargin", p.openOrderInitialMargin);
j.at("adl").get_to(p.adl);
jr.read("bidNotional", p.bidNotional);
jr.read("askNotional", p.askNotional);
j.at("updateTime").get_to(p.updateTime);
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,62 @@
#pragma once
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Balance-and-Position-Update
*/
struct PositionInfoRequest {
std::string symbol;
std::int64_t recvWindow;
std::int64_t timestamp;
};
void to_json(nlohmann::json &j, const PositionInfoRequest &req);
void from_json(const nlohmann::json &j, PositionInfoRequest &req);
void to_query_params(std::string &qps, const PositionInfoRequest &req);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const PositionInfoRequest &req) {
j["timestamp"] = req.timestamp;
if (!req.symbol.empty()) {
j["symbol"] = req.symbol;
}
if (req.recvWindow) {
j["recvWindow"] = req.recvWindow;
}
}
inline void from_json(const nlohmann::json &j, PositionInfoRequest &req) {
j.at("timestamp").get_to(req.timestamp);
auto it = j.find("symbol");
if (it != j.end()) {
it->get_to(req.symbol);
}
it = j.find("recvWindow");
if (it != j.end()) {
it->get_to(req.recvWindow);
}
}
inline void to_query_params(std::string &qps, const PositionInfoRequest &req) {
qps.append("timestamp=");
qps.append(std::to_string(req.timestamp));
if (!req.symbol.empty()) {
qps.append("&symbol=");
qps.append(req.symbol);
}
if (req.recvWindow) {
qps.append("&recvWindow=");
qps.append(std::to_string(req.recvWindow));
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,109 @@
#include "rest_api_client.h"
#include "lib/io/http/http_client.h"
#include "lib/io/http/http_response.h"
#include "lib/utils/crypto/hmac.h"
namespace binance_execution::usds_futures {
using bts::http::HttpClient;
using bts::http::HttpResponse;
using bts::system::crypto::hmac_digest_hex;
using std::string;
#define _LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "RestApiClient::" fn ". " __VA_ARGS__)
#define DIAG_FN(fn, ...) _LOG_FN(bts::Diag, fn, __VA_ARGS__)
#define INFO_FN(fn, ...) _LOG_FN(bts::Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) _LOG_FN(bts::Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__)
template <std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
static void shrink_string(std::string &str) {
if (str.size() > MAX_SIZE) {
str.resize(DEFAULT_SIZE);
str.shrink_to_fit();
}
}
template <class T, std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
static void shrink_vec(std::vector<T> &vec) {
if (vec.size() > MAX_SIZE) {
vec.resize(DEFAULT_SIZE);
vec.shrink_to_fit();
}
}
bool RestApiClient::sign_query_params_(std::string &qp) {
std::string signature;
if (!hmac_digest_hex(secret_key_, qp, signature)) {
LOG(OrderMan, bts::Error, "Failed to sign request. {}", qp);
return false;
}
static constexpr const std::string SIGNATURE_LABEL = "&signature=";
qp.reserve(qp.size() + SIGNATURE_LABEL.size() + signature.size());
qp.append(SIGNATURE_LABEL);
qp.append(signature);
return true;
}
template <class ResponseT, class RequestT>
void RestApiClient::request_(
const RequestT &req,
const std::string &path,
bts::io::http::HttpMethod method,
const std::string &body,
RestApiResult<ResponseT> &res) {
// Use thread_local vars to reduce memory allocations. Shrink strings
// to reduce memory footprint from previous requests.
thread_local bts::http::HttpRequest http_req;
shrink_string<4096, 128>(http_req.body);
http_req.reset();
// Prepare request
http_req.url = url_;
http_req.url.append(path);
http_req.body = body;
http_req.method = method;
http_req.headers["X-MBX-APIKEY"] = api_key_;
thread_local std::string qps;
shrink_string<256, 16>(qps);
qps.clear();
to_query_params(qps, req);
if (!qps.empty()) {
sign_query_params_(qps);
http_req.url.append("?");
http_req.url.append(qps);
}
// Send it
thread_local HttpResponse http_res;
shrink_string<4096, 128>(http_res.body);
http_res.reset();
HttpClient::send(http_req, &http_res);
if (!http_res.ok()) {
ERROR_FN("{} {}", "res: {}", method, path, http_res);
res.error = std::move(http_res.errors);
return;
}
// Parse response
INFO_FN("{} {}", "res: {}", method, path, http_res);
try {
nlohmann::json::parse(http_res.body).get_to(res.response.emplace());
} catch (const std::exception &ex) {
ERROR_FN("{} {}", "ex: {}", method, path, string{ex.what()});
bts_assert_msgf(false, "Failed to parse {} response", path);
}
}
#define REQUEST_DEF(ResponseT, RequestT) \
template void RestApiClient::request_<ResponseT, RequestT>( \
const RequestT &req, const std::string &path, \
bts::io::http::HttpMethod method, const std::string &body, \
RestApiResult<ResponseT> &res)
REQUEST_DEF(CreateOrderResponse, CreateOrderRequest);
REQUEST_DEF(ModifyOrderResponse, ModifyOrderRequest);
REQUEST_DEF(CancelOrderResponse, CancelOrderRequest);
REQUEST_DEF(std::vector<OrderRecord>, AllOrdersRequest);
REQUEST_DEF(std::vector<PositionInfo>, PositionInfoRequest);
REQUEST_DEF(ListenKeyResponse, ListenKeyRequest);
} // namespace binance_execution::usds_futures
@@ -0,0 +1,136 @@
#pragma once
#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"
#include "binance_execution/usds_futures/messages/position_info.h"
#include "binance_execution/usds_futures/messages/position_info_request.h"
#include "binance_execution/usds_futures/rest_api_result.h"
#include "lib/io/http/HttpMethod.h"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace binance_execution::usds_futures {
class RestApiClient {
public:
explicit RestApiClient(
std::string url, std::string api_key, std::string secret_key);
const RestApiResult<CreateOrderResponse> &create_order(
const CreateOrderRequest &req);
const RestApiResult<ModifyOrderResponse> &modify_order(
const ModifyOrderRequest &req);
const RestApiResult<CancelOrderResponse> &cancel_order(
const CancelOrderRequest &req);
const RestApiResult<std::vector<OrderRecord>> &get_all_orders(
const AllOrdersRequest &req);
const RestApiResult<std::vector<PositionInfo>> &get_position_info(
const PositionInfoRequest &req);
const RestApiResult<ListenKeyResponse> &get_listen_key(
const ListenKeyRequest &req = {});
const RestApiResult<ListenKeyResponse> &refresh_listen_key(
const ListenKeyRequest &req = {});
private:
std::string url_;
std::string api_key_;
std::string secret_key_;
template <class ResponseT, class RequestT>
void request_(
const RequestT &req,
const std::string &path,
bts::io::http::HttpMethod method,
const std::string &body,
RestApiResult<ResponseT> &op_res);
bool sign_query_params_(std::string &qp);
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline RestApiClient::RestApiClient(
std::string url, std::string api_key, std::string secret_key)
: url_(std::move(url)), api_key_(std::move(api_key)),
secret_key_(std::move(secret_key)) {}
inline const RestApiResult<CreateOrderResponse> &RestApiClient::create_order(
const CreateOrderRequest &req) {
static constexpr const std::string path{"/fapi/v1/order"};
thread_local RestApiResult<CreateOrderResponse> res;
res.reset();
request_<CreateOrderResponse>(
req, path, bts::io::http::HttpMethod::POST, "", res);
return res;
}
inline const RestApiResult<ModifyOrderResponse> &RestApiClient::modify_order(
const ModifyOrderRequest &req) {
static const std::string path{"/fapi/v1/order"};
thread_local RestApiResult<ModifyOrderResponse> res;
res.reset();
request_<ModifyOrderResponse>(
req, path, bts::io::http::HttpMethod::PUT, "", res);
return res;
}
inline const RestApiResult<CancelOrderResponse> &RestApiClient::cancel_order(
const CancelOrderRequest &req) {
static const std::string path{"/fapi/v1/order"};
thread_local RestApiResult<CancelOrderResponse> res;
res.reset();
request_<CancelOrderResponse>(
req, path, bts::io::http::HttpMethod::DELETE, "", res);
return res;
}
inline const RestApiResult<std::vector<OrderRecord>> &
RestApiClient::get_all_orders(const AllOrdersRequest &req) {
static const std::string path{"/fapi/v1/allOrders"};
thread_local RestApiResult<std::vector<OrderRecord>> res;
res.reset();
request_<std::vector<OrderRecord>>(
req, path, bts::io::http::HttpMethod::GET, "", res);
return res;
}
inline const RestApiResult<std::vector<PositionInfo>> &
RestApiClient::get_position_info(const PositionInfoRequest &req) {
static const std::string path{"/fapi/v3/positionRisk"};
thread_local RestApiResult<std::vector<PositionInfo>> res;
res.reset();
request_(req, path, bts::io::http::HttpMethod::GET, "", res);
return res;
}
inline const RestApiResult<ListenKeyResponse> &RestApiClient::get_listen_key(
const ListenKeyRequest &req) {
static const std::string path{"/fapi/v1/listenKey"};
thread_local RestApiResult<ListenKeyResponse> res;
res.reset();
request_(req, path, bts::io::http::HttpMethod::POST, "", res);
return res;
}
inline const RestApiResult<ListenKeyResponse> &
RestApiClient::refresh_listen_key(const ListenKeyRequest &req) {
static const std::string path{"/fapi/v1/listenKey"};
thread_local RestApiResult<ListenKeyResponse> res;
res.reset();
request_(req, path, bts::io::http::HttpMethod::PUT, "", res);
return res;
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,36 @@
#pragma once
#include <optional>
#include <string>
namespace binance_execution::usds_futures {
template <class ResponseT>
struct RestApiResult {
std::string error;
std::optional<ResponseT> response;
RestApiResult() = default;
static RestApiResult from_error(const std::string &error) {
return RestApiResult{error};
}
static RestApiResult from_success(ResponseT response) {
return RestApiResult{std::move(response)};
}
constexpr bool ok() const { return error.empty(); }
void reset() {
error.clear();
response.reset();
}
private:
explicit RestApiResult(ResponseT response) : response(std::move(response)) {}
explicit RestApiResult(std::string error) : error(std::move(error)) {}
};
} // namespace binance_execution::usds_futures
@@ -0,0 +1,147 @@
#include "type_converters.h"
#include "lib/api/services/AdminService.pb.h"
#include "lib/utils/checked_arithmetic.h"
#include "lib/utils/string_utils.h"
namespace binance_execution::usds_futures {
using bts::system::string_utils::copy_to_array;
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.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;
}
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<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) {
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);
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;
}
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,263 @@
#pragma once
#include "binance_execution/enums/execution_type.h"
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/side.h"
#include "binance_execution/enums/time_in_force.h"
#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/api/services/OrderService.pb.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/domain/Side.h"
#include "lib/orderman/orderman_utils/ExchangePosition.h"
#include "lib/orderman/orderman_utils/ExecutionReport.h"
#include "lib/utils/bts_assert.h"
namespace bts::api::admin {
class BTSAccount;
}
namespace binance_execution::usds_futures {
bts::api::orderman::NativeOrderType convert_order_type(OrderType tif);
OrderType convert_order_type(bts::api::orderman::NativeOrderType tif);
bts::api::orderman::NativeOrderStatus convert_order_status(OrderStatus status);
bts::api::orderman::TimeInForce convert_time_in_force(TimeInForce tif);
TimeInForce convert_time_in_force(bts::api::orderman::TimeInForce tif);
bts::core::TimeStamp convert_time_stamp(std::int64_t ts);
std::int64_t convert_time_stamp(bts::core::TimeStamp ts);
std::int64_t convert_time_span(bts::core::TimeSpan ts);
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);
bts::api::orderman::ExecType convert_exec_type(
ExecutionType exec_type, OrderStatus order_status);
bts::api::orderman::ExecType convert_exec_type(
ExecutionType exec_type, bool is_filled);
ExecutionType convert_exec_type(bts::api::orderman::ExecType exec_type);
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);
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);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline bts::api::orderman::NativeOrderType convert_order_type(OrderType tif) {
switch (tif.value) {
case OrderType::LIMIT:
return bts::api::orderman::NATIVE_ORDER_TYPE_LIMIT;
case OrderType::MARKET:
return bts::api::orderman::NATIVE_ORDER_TYPE_MARKET;
case OrderType::NONE:
return bts::api::orderman::NATIVE_ORDER_TYPE_UNDEFINED;
default:
bts_assert_msg(false, "Unknown BinanceOrderType");
return bts::api::orderman::NATIVE_ORDER_TYPE_UNDEFINED;
}
}
inline bts::api::orderman::NativeOrderStatus convert_order_status(
OrderStatus status) {
switch (status.value) {
case OrderStatus::NEW:
return bts::api::orderman::NATIVE_ORDER_STATUS_NEW;
case OrderStatus::PENDING_NEW:
return bts::api::orderman::NATIVE_ORDER_STATUS_PENDING_NEW;
case OrderStatus::PARTIALLY_FILLED:
return bts::api::orderman::NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
case OrderStatus::FILLED:
return bts::api::orderman::NATIVE_ORDER_STATUS_FILLED;
case OrderStatus::CANCELED:
return bts::api::orderman::NATIVE_ORDER_STATUS_CANCELED;
case OrderStatus::REJECTED:
return bts::api::orderman::NATIVE_ORDER_STATUS_REJECTED;
case OrderStatus::EXPIRED:
case OrderStatus::EXPIRED_IN_MATCH:
return bts::api::orderman::NATIVE_ORDER_STATUS_EXPIRED;
case OrderStatus::NONE:
return bts::api::orderman::NATIVE_ORDER_STATUS_UNDEFINED;
default:
bts_assert_msg(false, "Unknown BinanceOrderStatus");
return bts::api::orderman::NATIVE_ORDER_STATUS_UNDEFINED;
}
}
inline bts::core::TimeStamp convert_time_stamp(std::int64_t ts) {
return bts::core::TimeStamp::from_millis(ts);
}
inline std::int64_t convert_time_stamp(bts::core::TimeStamp ts) {
return static_cast<std::int64_t>(ts.millis());
}
inline bts::core::TimeSpan convert_time_span(std::int64_t ts) {
return bts::core::TimeSpan::from_millis(ts);
}
inline std::int64_t convert_time_span(bts::core::TimeSpan ts) {
return static_cast<std::int64_t>(ts.millis());
}
inline bts::domain::Side convert_side(Side side) {
switch (side.value) {
case Side::BUY:
return bts::domain::Side::Buy;
case Side::SELL:
return bts::domain::Side::Sell;
default:
bts_assert_msg(
false, "Failed to convert BinanceSide to bts::domain::Side");
return bts::domain::Side::Buy;
}
}
inline Side convert_side(bts::domain::Side side) {
switch (side.value) {
case bts::domain::Side::Buy:
return Side::BUY;
case bts::domain::Side::Sell:
return Side::SELL;
default:
bts_assert_msg(
false, "Failed to convert bts::domain::Side to BinanceSide");
return Side::NONE;
}
}
inline Side convert_side(bts::api::Side side) {
switch (side) {
case bts::api::SIDE_BUY:
return Side::BUY;
case bts::api::SIDE_SELL:
return Side::SELL;
default:
bts_assert_msg(false, "Failed to convert bts::api::Side to BinanceSide");
return Side::NONE;
}
}
inline bts::api::orderman::TimeInForce convert_time_in_force(TimeInForce tif) {
switch (tif.value) {
case TimeInForce::GTC:
return bts::api::orderman::TIME_IN_FORCE_GTC;
case TimeInForce::IOC:
case TimeInForce::FOK:
return bts::api::orderman::TIME_IN_FORCE_IOC;
case TimeInForce::NONE:
return bts::api::orderman::TIME_IN_FORCE_UNDEFINED;
default:
bts_assert_msg(false, "Unknown BinanceTimeInForce");
return bts::api::orderman::TIME_IN_FORCE_UNDEFINED;
}
}
inline bts::api::orderman::ExecType convert_exec_type(
ExecutionType exec_type, OrderStatus order_status) {
return convert_exec_type(exec_type, order_status == OrderStatus::FILLED);
}
inline bts::api::orderman::ExecType convert_exec_type(
ExecutionType exec_type, bool is_filled) {
switch (exec_type.value) {
case ExecutionType::NEW:
return bts::api::orderman::EXEC_TYPE_NEW;
case ExecutionType::CANCELED:
return bts::api::orderman::EXEC_TYPE_CANCELED;
case ExecutionType::REPLACED:
return bts::api::orderman::EXEC_TYPE_REPLACED;
case ExecutionType::REJECTED:
case ExecutionType::TRADE_PREVENTION:
return bts::api::orderman::EXEC_TYPE_REJECTED;
case ExecutionType::TRADE:
return is_filled ? bts::api::orderman::EXEC_TYPE_FILL
: bts::api::orderman::EXEC_TYPE_PARTIAL_FILL;
case ExecutionType::EXPIRED:
return bts::api::orderman::EXEC_TYPE_EXPIRED;
case ExecutionType::NONE:
return bts::api::orderman::EXEC_TYPE_UNDEFINED;
default:
bts_assert_msg(false, "Unknown BinanceExecutionType");
return bts::api::orderman::EXEC_TYPE_UNDEFINED;
}
}
inline OrderType convert_order_type(bts::api::orderman::NativeOrderType tif) {
switch (tif) {
case bts::api::orderman::NATIVE_ORDER_TYPE_LIMIT:
return OrderType::LIMIT;
case bts::api::orderman::NATIVE_ORDER_TYPE_MARKET:
return OrderType::MARKET;
case bts::api::orderman::NATIVE_ORDER_TYPE_UNDEFINED:
return OrderType::NONE;
default:
bts_assert_msgf(
false, "NativeOrderType '{}' not implemented at Binance",
bts::api::orderman::NativeOrderType_Name(tif));
return OrderType::NONE;
}
}
inline TimeInForce convert_time_in_force(bts::api::orderman::TimeInForce tif) {
switch (tif) {
case bts::api::orderman::TIME_IN_FORCE_GTC:
case bts::api::orderman::TIME_IN_FORCE_DAY:
return TimeInForce::GTC;
case bts::api::orderman::TIME_IN_FORCE_IOC:
return TimeInForce::IOC;
case bts::api::orderman::TIME_IN_FORCE_UNDEFINED:
return TimeInForce::NONE;
default:
bts_assert_msg(false, "Unknown BinanceTimeInForce");
return TimeInForce::NONE;
}
}
inline ExecutionType convert_exec_type(bts::api::orderman::ExecType exec_type) {
switch (exec_type) {
case bts::api::orderman::EXEC_TYPE_NEW:
case bts::api::orderman::EXEC_TYPE_PENDING_NEW:
return ExecutionType::NEW;
case bts::api::orderman::EXEC_TYPE_FILL:
case bts::api::orderman::EXEC_TYPE_PARTIAL_FILL:
return ExecutionType::TRADE;
case bts::api::orderman::EXEC_TYPE_CANCELED:
return ExecutionType::CANCELED;
case bts::api::orderman::EXEC_TYPE_REJECTED:
return ExecutionType::REJECTED;
case bts::api::orderman::EXEC_TYPE_EXPIRED:
return ExecutionType::EXPIRED;
default:
bts_assert_msg(false, "Unknown BinanceExecutionType");
return ExecutionType::NONE;
}
}
} // namespace binance_execution::usds_futures
@@ -0,0 +1,133 @@
#pragma once
#include "binance_execution/enums/event_reason_type.h"
#include "binance_execution/enums/position_side.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures::user_data_streams {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Balance-and-Position-Update
*/
struct AccountUpdate {
struct Balance {
std::string asset; // json: a
double wallet_balance; // json: wb
double cross_wallet_balance; // json: cw
double balance_change; // json: bc
};
struct Position {
std::string symbol; // json: s
double position_amount; // json: pa
double entry_price; // json: ep
double break_even_price; // json: bep
double realized_pnl; // json: cr
double unrealized_pnl; // json: up
std::string margin_type; // json: mt
double isolated_wallet; // json: iw
PositionSide position_side; // json: ps
};
struct Data {
EventReasonType event_reason_type; // json: m
std::vector<Balance> balances; // json: B
std::vector<Position> positions; // json: P
};
std::string event_type; // json: e
std::int64_t event_time; // json: E
std::int64_t transaction_time; // json: T
Data data; // json: a
};
void to_json(nlohmann::json &j, const AccountUpdate::Balance &b);
void from_json(const nlohmann::json &j, AccountUpdate::Balance &b);
void to_json(nlohmann::json &j, const AccountUpdate::Position &b);
void from_json(const nlohmann::json &j, AccountUpdate::Position &p);
void to_json(nlohmann::json &j, const AccountUpdate::Data &d);
void from_json(const nlohmann::json &j, AccountUpdate::Data &d);
void to_json(nlohmann::json &j, const AccountUpdate &u);
void from_json(const nlohmann::json &j, AccountUpdate &u);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const AccountUpdate::Balance &b) {
j["a"] = b.asset;
j["wb"] = b.wallet_balance;
j["cw"] = b.cross_wallet_balance;
j["bc"] = b.balance_change;
}
inline void from_json(const nlohmann::json &j, AccountUpdate::Balance &b) {
bts::io::JsonReader jr{&j};
j.at("a").get_to(b.asset);
jr.read("wb", b.wallet_balance);
jr.read("cw", b.cross_wallet_balance);
jr.read("bc", b.balance_change);
}
inline void to_json(nlohmann::json &j, const AccountUpdate::Position &b) {
j["s"] = b.symbol;
j["pa"] = b.position_amount;
j["ep"] = b.entry_price;
j["bep"] = b.break_even_price;
j["cr"] = b.realized_pnl;
j["up"] = b.unrealized_pnl;
j["mt"] = b.margin_type;
j["iw"] = b.isolated_wallet;
j["ps"] = b.position_side;
}
inline void from_json(const nlohmann::json &j, AccountUpdate::Position &p) {
bts::io::JsonReader jr{&j};
j.at("s").get_to(p.symbol);
jr.read("pa", p.position_amount);
jr.read("ep", p.entry_price);
jr.read("bep", p.break_even_price);
jr.read("cr", p.realized_pnl);
jr.read("up", p.unrealized_pnl);
j.at("mt").get_to(p.margin_type);
jr.read("iw", p.isolated_wallet);
j.at("ps").get_to(p.position_side);
}
inline void to_json(nlohmann::json &j, const AccountUpdate::Data &d) {
j["m"] = d.event_reason_type;
j["B"] = d.balances;
j["P"] = d.positions;
}
inline void from_json(const nlohmann::json &j, AccountUpdate::Data &d) {
j.at("m").get_to(d.event_reason_type);
j.at("B").get_to(d.balances);
j.at("P").get_to(d.positions);
}
inline void to_json(nlohmann::json &j, const AccountUpdate &u) {
j["e"] = u.event_type;
j["E"] = u.event_time;
j["T"] = u.transaction_time;
j["a"] = u.data;
}
inline void from_json(const nlohmann::json &j, AccountUpdate &u) {
j.at("e").get_to(u.event_type);
j.at("E").get_to(u.event_time);
j.at("T").get_to(u.transaction_time);
j.at("a").get_to(u.data);
};
} // namespace binance_execution::usds_futures::user_data_streams
@@ -0,0 +1,81 @@
#include "listen_key_requestor.h"
#include "binance_execution/usds_futures/rest_api_client.h"
#include "lib/core/TimeStamp.h"
#include <string>
#include <thread>
namespace binance_execution::usds_futures {
using bts::core::Delegate;
using bts::core::TimeStamp;
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. {}", res.error);
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. {}", res.error);
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
@@ -0,0 +1,30 @@
#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
@@ -0,0 +1,81 @@
#include "order_update.h"
#include "lib/io/JsonReader.h"
namespace binance_execution::usds_futures::user_data_streams {
void to_json(nlohmann::json &j, const OrderUpdate::Data &d) {
j["s"] = d.symbol;
j["c"] = d.client_order_id;
j["S"] = d.side;
j["o"] = d.order_type;
j["f"] = d.time_in_force;
j["q"] = d.orig_quantity;
j["p"] = d.orig_price;
j["ap"] = d.avg_price;
j["sp"] = d.stop_price;
j["x"] = d.execution_type;
j["X"] = d.order_status;
j["i"] = d.order_id;
j["l"] = d.last_filled_qty;
j["z"] = d.total_filled_qty;
j["L"] = d.last_filled_price;
j["N"] = d.commission_asset;
j["n"] = d.commission;
j["T"] = d.trade_time;
j["t"] = d.trade_id;
j["b"] = d.bids_notional;
j["a"] = d.ask_notional;
j["m"] = d.is_maker_trade;
j["R"] = d.is_reduce_only;
j["wt"] = d.stop_price_working_type;
j["ot"] = d.orig_order_type;
j["ps"] = d.position_side;
j["cp"] = d.close_all;
j["AP"] = d.activation_price;
j["cr"] = d.callback_rate;
j["pP"] = d.is_price_protection_on;
j["rp"] = d.realized_trade_pnl;
j["V"] = d.stp_mode;
j["pm"] = d.price_match_mode;
j["gtd"] = d.good_till_time;
}
void from_json(const nlohmann::json &j, OrderUpdate::Data &d) {
j.at("s").get_to(d.symbol);
j.at("c").get_to(d.client_order_id);
j.at("S").get_to(d.side);
j.at("o").get_to(d.order_type);
j.at("f").get_to(d.time_in_force);
bts::io::JsonReader jr{&j};
jr.read("q", d.orig_quantity);
jr.read("p", d.orig_price);
jr.read("ap", d.avg_price);
jr.read("sp", d.stop_price);
j.at("x").get_to(d.execution_type);
j.at("X").get_to(d.order_status);
jr.read("i", d.order_id);
jr.read("l", d.last_filled_qty);
jr.read("z", d.total_filled_qty);
jr.read("L", d.last_filled_price);
j.at("N").get_to(d.commission_asset);
jr.read("n", d.commission);
j.at("T").get_to(d.trade_time);
jr.read("t", d.trade_id);
jr.read("b", d.bids_notional);
jr.read("a", d.ask_notional);
j.at("m").get_to(d.is_maker_trade);
j.at("R").get_to(d.is_reduce_only);
j.at("wt").get_to(d.stop_price_working_type);
j.at("ot").get_to(d.orig_order_type);
j.at("ps").get_to(d.position_side);
j.at("cp").get_to(d.close_all);
jr.read("AP", d.activation_price);
jr.read("cr", d.callback_rate);
j.at("pP").get_to(d.is_price_protection_on);
jr.read("rp", d.realized_trade_pnl);
j.at("V").get_to(d.stp_mode);
j.at("pm").get_to(d.price_match_mode);
j.at("gtd").get_to(d.good_till_time);
}
} // namespace binance_execution::usds_futures::user_data_streams
@@ -0,0 +1,89 @@
#pragma once
#include "binance_execution/enums/execution_type.h"
#include "binance_execution/enums/order_status.h"
#include "binance_execution/enums/order_type.h"
#include "binance_execution/enums/position_side.h"
#include "binance_execution/enums/price_match_mode.h"
#include "binance_execution/enums/side.h"
#include "binance_execution/enums/time_in_force.h"
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
namespace binance_execution::usds_futures::user_data_streams {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Event-Order-Update
*/
struct OrderUpdate {
std::string event_type; // json: e
std::int64_t event_time; // json: E
std::int64_t transaction_time; // json: T
struct Data {
std::string symbol; // json: s
std::string client_order_id; // json: c
Side side; // json: S
OrderType order_type; // json: o
TimeInForce time_in_force; // json: f
double orig_quantity; // json: q
double orig_price; // json: p
double avg_price; // json: ap
double stop_price; // json: sp
ExecutionType execution_type; // json: x
OrderStatus order_status; // json: X
std::int64_t order_id; // json: i
double last_filled_qty; // json: l
double total_filled_qty; // json: z
double last_filled_price; // json: L
std::string commission_asset; // json: N
double commission; // json: n
std::int64_t trade_time; // json: T
std::uint32_t trade_id; // json: t
double bids_notional; // json: b
double ask_notional; // json: a
bool is_maker_trade; // json: m
bool is_reduce_only; // json: R
std::string stop_price_working_type; // json: wt
OrderType orig_order_type; // json: ot
PositionSide position_side; // json: ps
bool close_all; // json: cp
double activation_price; // json: AP
double callback_rate; // json: cr
bool is_price_protection_on; // json: pP
double realized_trade_pnl; // json: rp
std::string stp_mode; // json: V
PriceMatchMode price_match_mode; // json: pm
std::int64_t good_till_time; // json: gtd
};
Data data; // json: o
};
void to_json(nlohmann::json &j, const OrderUpdate::Data &d);
void from_json(const nlohmann::json &j, OrderUpdate::Data &d);
void to_json(nlohmann::json &j, const OrderUpdate &u);
void from_json(const nlohmann::json &j, OrderUpdate &u);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const OrderUpdate &u) {
j["e"] = u.event_type;
j["E"] = u.event_time;
j["T"] = u.transaction_time;
j["o"] = u.data;
}
inline void from_json(const nlohmann::json &j, OrderUpdate &u) {
j.at("e").get_to(u.event_type);
j.at("E").get_to(u.event_time);
j.at("T").get_to(u.transaction_time);
j.at("o").get_to(u.data);
}
} // namespace binance_execution::usds_futures::user_data_streams
@@ -0,0 +1,163 @@
#include "user_data_subscription.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/core/TimeStamp.h"
#include "lib/io/websocket/websocket_client.h"
#include "lib/logging/logging.h"
#include <mutex>
#include <optional>
#include <string>
#include <system_error>
namespace binance_execution::usds_futures::user_data_streams {
#define _LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "UserDataSubscription::" fn ". " __VA_ARGS__)
#define DIAG_FN(fn, ...) _LOG_FN(bts::Diag, fn, __VA_ARGS__)
#define INFO_FN(fn, ...) _LOG_FN(bts::Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) _LOG_FN(bts::Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__)
using bts::core::Delegate;
using bts::core::TimeSpan;
using bts::core::TimeStamp;
using bts::websocket::WebsocketClient;
using bts::websocket::WebsocketClientConfig;
using nlohmann::json;
using std::exception;
using std::lock_guard;
using std::mutex;
using std::optional;
using std::string;
using std::thread;
struct UserDataSubscription::PImpl {
explicit PImpl(string url) : url(std::move(url)) {}
~PImpl() { stop(); }
string url;
mutex instance_mx;
optional<WebsocketClient> ws_client;
optional<thread> ws_run_thread;
optional<thread> ws_ping_thread;
Delegate<void(user_data_streams::AccountUpdate)> account_update_callback;
Delegate<void(user_data_streams::OrderUpdate)> order_update_callback;
void start() {
lock_guard<mutex> lock{instance_mx};
INFO_FN("start", "Subscribing to user data");
bts_assert(!ws_client);
bts_assert(!ws_run_thread);
bts_assert(!ws_ping_thread);
LOG(OrderMan, bts::Info, "Creating websocket client. url: {}", url);
WebsocketClientConfig ws_stream_client_cfg;
ws_stream_client_cfg.url = url;
auto &ws_cli = ws_client.emplace(ws_stream_client_cfg);
ws_cli.message_handler().bind<&PImpl::on_message>(*this);
ws_cli.close_handler().bind<&PImpl::on_close>(*this);
INFO_FN("start", "Creating websocket client. url: {}", url);
ws_cli.connect({}, false);
ws_run_thread.emplace([&]() {
DIAG_FN("ws_run_thread", "thread starting. url: {}", url);
ws_cli.run();
DIAG_FN("ws_run_thread", "thread exiting. url: {}", url);
});
ws_ping_thread.emplace([&]() {
DIAG_FN("ws_ping_thread", "thread starting. url: {}", url);
auto last_ping_time = TimeStamp::now();
static constexpr const auto ping_interval = TimeSpan::from_seconds(25);
std::this_thread::sleep_for(
std::chrono::milliseconds(ping_interval.millis()));
while (ws_cli.is_connected()) {
const auto now = bts::core::TimeStamp::now();
if ((now - last_ping_time) > ping_interval) {
DIAG_FN("ws_ping_thread", "Pinging websocket. url: {}", url);
ws_cli.ping();
last_ping_time = now;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
DIAG_FN("ws_ping_thread", "thread exiting. url: {}", url);
});
}
void stop() {
lock_guard<mutex> lock{instance_mx};
if (ws_client) {
bts_assert(ws_run_thread);
bts_assert(ws_ping_thread);
ws_client->disconnect();
ws_ping_thread->join();
ws_run_thread->join();
ws_client.reset();
}
}
void on_message(const string &msg_str) {
DIAG_FN("on_message", "{}", msg_str);
json msg;
try {
msg = json::parse(msg_str);
} catch (const exception &ex) {
LOG(OrderMan, bts::Error, "Failed to parse json. {}\n{}",
string{ex.what()}, msg_str);
return;
}
const auto event_type_it = msg.find("e");
if (event_type_it != msg.end()) {
const auto event_type = event_type_it->get<string>();
if (event_type == "ACCOUNT_UPDATE") {
try {
account_update_callback(
msg.get<user_data_streams::AccountUpdate>());
} catch (const std::exception &ex) {
ERROR_FN(
"on_message", "account_update callback threw: {}",
string{ex.what()});
}
} else if (event_type == "ORDER_TRADE_UPDATE") {
try {
order_update_callback(msg.get<user_data_streams::OrderUpdate>());
} catch (const std::exception &ex) {
ERROR_FN(
"on_message", "order_update callback threw: {}",
string{ex.what()});
}
} else {
ERROR_FN(
"on_message", "Response has unexpected event type '{}'",
event_type);
}
return;
}
ERROR_FN("on_message", "Failed to process response json: {}", msg_str);
}
void on_close(const std::error_code &ec) {
bts_assert_msgf(
false, "Websocket stream client disconnected. {}", ec.message());
}
};
UserDataSubscription::UserDataSubscription(string url)
: me_(std::make_unique<PImpl>(std::move(url))) {}
UserDataSubscription::~UserDataSubscription() = default;
void UserDataSubscription::start() { me_->start(); }
void UserDataSubscription::stop() { me_->stop(); }
Delegate<void(user_data_streams::AccountUpdate)> &
UserDataSubscription::account_update_callback() {
return me_->account_update_callback;
}
Delegate<void(user_data_streams::OrderUpdate)> &
UserDataSubscription::order_update_callback() {
return me_->order_update_callback;
}
} // namespace binance_execution::usds_futures::user_data_streams
@@ -0,0 +1,30 @@
#pragma once
#include "binance_execution/usds_futures/user_data_streams/account_update.h"
#include "binance_execution/usds_futures/user_data_streams/order_update.h"
#include "lib/utils/Delegate.h"
#include <memory>
namespace binance_execution::usds_futures::user_data_streams {
/**
* @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams
*/
class UserDataSubscription {
public:
explicit UserDataSubscription(std::string url);
~UserDataSubscription();
void start();
void stop();
bts::core::Delegate<void(AccountUpdate)> &account_update_callback();
bts::core::Delegate<void(OrderUpdate)> &order_update_callback();
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace binance_execution::usds_futures::user_data_streams
+1
View File
@@ -76,6 +76,7 @@ endif()
# linking
target_link_libraries(sparkd PUBLIC
libspark
binance_execution
databento::databento
${emumba-deribit-connector-library_LIBRARIES}
ssl
+34 -3
View File
@@ -1,4 +1,5 @@
#include "Run.h"
#include "binance_execution/execution_gateway_provider.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/config/config.h"
#include "lib/config/protos/ports.pb.h"
@@ -14,6 +15,7 @@
#include "lib/metrics/metrics.h"
#include "lib/metrics/system_metrics.h"
#include "lib/orderman/OrderServiceServer.h"
#include "lib/orderman/gateways/execution_gateway_provider.h"
#include "lib/orderman/gateways/sim/SimExecutionGatewayFactory.h"
#include "lib/orderman/order_factory/DynamicOrderRegistry.h"
#include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h"
@@ -47,6 +49,36 @@
namespace bts::cmds {
using bts::api::ServiceContext;
using bts::md::MDClient;
using bts::md::TradeSummarySignalDispatcher;
using bts::metrics::MetricsContainer;
using bts::orderman::DynamicOrderRegistry;
using bts::orderman::ExecutionGatewayProvider;
using bts::orderman::IExecutionGatewayProvider;
using bts::orderman::OrderServiceServer;
using bts::trace::PerfLog;
using std::unique_ptr;
using std::vector;
static OrderServiceServer create_order_service_server(
ServiceContext &sc,
MDClient &mdclient,
DynamicOrderRegistry &dyn_order_registry,
MetricsContainer &stats,
PerfLog &perf_log,
TradeSummarySignalDispatcher &tss_dispatcher) {
vector<unique_ptr<IExecutionGatewayProvider>> gw_pros;
gw_pros.push_back(std::make_unique<ExecutionGatewayProvider>(
&perf_log, mdclient.oracle()));
gw_pros.push_back(
std::make_unique<binance_execution::ExecutionGatewayProvider>(
mdclient.oracle()));
return OrderServiceServer(
sc, mdclient, dyn_order_registry, std::move(gw_pros), stats, perf_log,
tss_dispatcher);
}
static void execute() {
using namespace bts;
LOG(Gen, Warn,
@@ -56,8 +88,7 @@ static void execute() {
auto qb_cfg = config::get<config::QuickbusCfg>().node();
auto &ports = config::get<config::Ports>();
qb_cfg.set_my_port(ports.qbus_port());
qbus::Quickbus quickbus(
qb_cfg, qbus::QuickbusThreadOptions::DEFERRED_START);
qbus::Quickbus quickbus(qb_cfg, qbus::QuickbusThreadOptions::DEFERRED_START);
api::ServiceContext sc(&quickbus);
trace::PerfLog perf_log;
@@ -90,7 +121,7 @@ static void execute() {
plugin::init_plugins(plugin_services);
// finished initializing order plugins
orderman::OrderServiceServer order_service_server(
auto order_service_server = create_order_service_server(
sc, mdclient, dyn_order_registry, stats, perf_log, tss_dispatcher);
support::ErrorReportServer error_report_service(sc);
admin::AdminServiceServer admin_service(sc);