refactor: Consolidate all py interop into single lib

It was just too hard trying to make it work with separate libs. The
main issue was getting the python md and execution modules to
recognize PySecurityList and other types defined in the core module.
This commit is contained in:
John Colvin committed 2025-06-16 11:08:57 -04:00
1 parent 7c9330650d
commit bdd5e08b60
87 files changed
+1093 -2926

No files matched your search

@@ -0,0 +1,7 @@
#include "execution_gateway.h"
namespace python_execution {
ExecutionGateway::~ExecutionGateway() = default;
} // namespace python_execution
@@ -0,0 +1,25 @@
#pragma once
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/py_cancel_order_request.h"
#include "python_interop/execution/py_cancel_replace_order_request.h"
#include "python_interop/execution/py_create_order_request.h"
#include "python_interop/py_security.h"
namespace python_execution {
class ExecutionGateway {
public:
virtual ~ExecutionGateway();
virtual void start(ExecutionGatewayBridge *bridge) = 0;
virtual void create_order(std::shared_ptr<PyCreateOrderRequest> req) = 0;
virtual void cancel_order(std::shared_ptr<PyCancelOrderRequest> req) = 0;
virtual void cancel_replace_order(
std::shared_ptr<PyCancelReplaceOrderRequest> req) = 0;
virtual void on_securities(
const std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>>
&securities) = 0;
virtual void stop() = 0;
};
} // namespace python_execution
@@ -0,0 +1,7 @@
#include "execution_gateway_bridge.h"
namespace python_execution {
ExecutionGatewayBridge::~ExecutionGatewayBridge() = default;
} // namespace python_execution
@@ -0,0 +1,17 @@
#pragma once
#include "python_interop/execution/py_execution_report.h"
#include "python_interop/execution/py_position_info.h"
namespace python_execution {
class ExecutionGatewayBridge {
public:
virtual ~ExecutionGatewayBridge();
// Python interface
virtual void on_execution_report(std::shared_ptr<PyExecutionReport>) {}
virtual void on_position_info(std::shared_ptr<PyPositionInfo>) {}
virtual void on_is_ready(bool) {}
};
} // namespace python_execution
@@ -0,0 +1,192 @@
#include "python_interop/execution/execution_gateway_bridge_impl.h"
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/py_cancel_order_request.h"
#include "python_interop/execution/py_create_order_request.h"
#include "python_interop/execution/py_position_info.h"
#include "python_interop/python_activator.h"
#include "python_interop/python_interpreter.h"
#include "spark/core/TimeSpan.h"
#include "spark/io/json_formatter.h"
#include "spark/logging/logging.h"
#include "spark/logging/trace_log.h"
#include "spark/logging/trace_log_var.h"
#include "spark/orderman/OrderMsg.h"
#include "spark/orderman/log_messages/execution_report_log.h"
#include "spark/orderman/orderman_utils/ExecutionReport.h"
#include "spark/utils/stopwatch.h"
#include <pybind11/pybind11.h>
using std::shared_ptr;
namespace py = pybind11;
namespace python_execution {
using bts::core::Delegate;
using bts::core::TimeSpan;
using bts::io::json_formatter;
using bts::orderman::RFQMsg;
using python_interop::PythonActivatorT;
using std::string;
using std::vector;
#define ASSERT_FN(test, fn, ...) \
bts_assert_msgf(test, "exe_gw_bridge::" fn ". " __VA_ARGS__)
#define LOG_FN(level, fn, ...) \
LOG(OrderMan, level, "exe_gw_bridge::" 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__)
#define _LOG(level, ...) LOG(OrderMan, level, __VA_ARGS__)
#define INFO(...) _LOG(bts::Info, __VA_ARGS__)
#define ERROR(...) _LOG(bts::Error, __VA_ARGS__)
#define _LOG_TRACE(level, name) \
LOG_TRACE(OrderMan, level, std::string("exe_gw_bridge"), std::string(name))
#define INFO_TRACE(name) _LOG_TRACE(bts::Info, name)
#define WARN_TRACE(name) _LOG_TRACE(bts::Warn, name)
#define ERROR_TRACE(name) _LOG_TRACE(bts::Error, name)
#define INFO_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
OrderMan, bts::Info, std::string("exe_gw_bridge"), std::string(name), \
params)
#define WARN_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
OrderMan, bts::Warn, std::string("exe_gw_bridge"), std::string(name), \
params)
ExecutionGatewayBridgeImpl::ExecutionGatewayBridgeImpl(
string name, string target, vector<string> module_paths)
: name(std::move(name)), target(std::move(target)),
module_paths(std::move(module_paths)) {}
void ExecutionGatewayBridgeImpl::on_order_msg(PyOrderRequest msg) {
INFO_TRACE("on_order_msg");
outgoing_reqs.push(std::move(msg));
}
void ExecutionGatewayBridgeImpl::on_execution_report(
std::shared_ptr<PyExecutionReport> er) {
INFO_TRACE_PARAMS("on_execution_report", er->to_string());
incoming_exe_reports.push(std::move(er));
}
void ExecutionGatewayBridgeImpl::on_position_info(
std::shared_ptr<PyPositionInfo> pi) {
INFO_TRACE_PARAMS("on_execution_report", pi->to_string());
incoming_pos_infos.push(std::move(pi));
}
void ExecutionGatewayBridgeImpl::on_is_ready(bool value) {
INFO_TRACE_PARAMS("on_is_ready", json_formatter(value));
is_ready.set_value_if_changed(value);
}
bool ExecutionGatewayBridgeImpl::wait_for_ready(TimeSpan timeout) {
is_ready.wait_for_until_value(timeout, true);
return is_ready.value();
}
bool ExecutionGatewayBridgeImpl::pop_exe_report(
shared_ptr<const PyExecutionReport> &result) {
auto res = incoming_exe_reports.pop();
if (res) {
result = std::move(*res);
return true;
}
result = nullptr;
return false;
}
void ExecutionGatewayBridgeImpl::on_securities(
std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>> securities) {
INFO_TRACE_PARAMS(
"on_securities",
json_formatter(
fmt::format("Received {} securities", this->securities.size())));
this->securities = std::move(securities);
this->is_securities_dirty = true;
}
shared_ptr<const PyExecutionReport>
ExecutionGatewayBridgeImpl::pop_exe_report() {
auto res = incoming_exe_reports.pop();
return res ? *res : nullptr;
}
int ExecutionGatewayBridgeImpl::run_py(volatile bool *keep_going) {
INFO_TRACE_PARAMS("run_py", json_formatter("Begin"));
bts_assert(keep_going != nullptr);
PythonActivatorT<ExecutionGateway> gw_activator{target, module_paths};
gw = gw_activator.get_target(
[&](ExecutionGateway &local_gw) { local_gw.start(this); }, name);
INFO_TRACE_PARAMS("run_py", json_formatter("Waiting for ready..."));
auto sw = bts::utils::Stopwatch::start_new();
while (!wait_for_ready(TimeSpan::from_seconds(1)) && *keep_going) {
INFO_TRACE_PARAMS(
"run", json_formatter(fmt::format(
"PyExecutionGateway still not ready after {:.2f}s",
sw.total_elapsed().total_seconds())));
}
while (is_ready.value() && *keep_going) {
INFO_TRACE_PARAMS("run_py", json_formatter("Waiting for request..."));
if (is_securities_dirty) {
INFO_TRACE_PARAMS(
"run_py",
json_formatter(fmt::format(
"Sending {} securities to py gw", securities.size())));
python_interop::PythonInterpreter::exec(
[&]() { gw->on_securities(securities); });
is_securities_dirty = false;
}
auto req = outgoing_reqs.wait_for_until_pop(
bts::core::TimeSpan::from_seconds(1));
if (!req) {
continue;
}
INFO_TRACE_PARAMS(
"run_py", json_formatter("Forwarding request to py gateway"));
enum ReqType { CREATE = 0, CANCEL_REPLACE = 1, CANCEL = 2 };
static const std::string req_type_names[]{"CREATE", "CXL_REP", "CXL"};
ReqType req_type;
sw.restart();
python_interop::PythonInterpreter::exec([&]() {
std::visit(
bts::overloaded{
[&](std::shared_ptr<PyCreateOrderRequest> req) {
req_type = ReqType::CREATE;
gw->create_order(std::move(req));
},
[&](std::shared_ptr<PyCancelReplaceOrderRequest> req) {
req_type = ReqType::CANCEL_REPLACE;
gw->cancel_replace_order(std::move(req));
},
[&](std::shared_ptr<PyCancelOrderRequest> req) {
req_type = ReqType::CANCEL;
gw->cancel_order(std::move(req));
},
[&](const RFQMsg &) {
bts_assert_msg(false, "RFQ not implemented");
},
},
*req);
});
sw.stop();
INFO_TRACE_PARAMS(
"run_py",
fmt::format(
R"({{ "typ": "{}", "hndl_dur": "{:.3f}ms" }})",
req_type_names[req_type], sw.total_elapsed().total_millis()));
}
python_interop::PythonInterpreter::exec([&]() { gw->stop(); });
return 0;
}
ExecutionGatewayBridgeImpl::~ExecutionGatewayBridgeImpl() = default;
} // namespace python_execution
@@ -0,0 +1,55 @@
#pragma once
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/py_execution_report.h"
#include "python_interop/execution/py_order_request.h"
#include "python_interop/execution/py_position_info.h"
#include "python_interop/py_security.h"
#include "spark/collections/concurrent_queue.h"
#include "spark/utils/Delegate.h"
#include "spark/utils/concurrent_value_store.h"
namespace python_execution {
class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge {
public:
ExecutionGatewayBridgeImpl(
std::string name,
std::string target,
std::vector<std::string> module_paths);
~ExecutionGatewayBridgeImpl() override;
// Python interface
void on_execution_report(std::shared_ptr<PyExecutionReport>) override;
void on_position_info(std::shared_ptr<PyPositionInfo>) override;
void on_is_ready(bool) override;
// C++ interface
bts::core::Delegate<void(std::shared_ptr<const PyExecutionReport>)> &
exe_report_callback();
void on_order_msg(PyOrderRequest msg);
void on_securities(
std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>>
securities);
bool wait_for_ready(bts::core::TimeSpan timeout);
bool pop_exe_report(std::shared_ptr<const PyExecutionReport> &result);
std::shared_ptr<const PyExecutionReport> pop_exe_report();
int run_py(volatile bool *keep_going);
private:
std::string name;
std::string target;
std::vector<std::string> module_paths;
bts::utils::ConcurrentValueStore<bool> is_ready;
bts::collections::ConcurrentQueue<std::shared_ptr<PyExecutionReport>>
incoming_exe_reports;
bts::collections::ConcurrentQueue<std::shared_ptr<PyPositionInfo>>
incoming_pos_infos;
bts::collections::ConcurrentQueue<PyOrderRequest> outgoing_reqs;
std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>> securities;
volatile bool is_securities_dirty = false;
ExecutionGateway *gw;
};
} // namespace python_execution
@@ -0,0 +1,68 @@
#include "py_cancel_order_request.h"
#include "spark/domain_model/Venue.h"
#include "spark/orderman/CancelOrderMsg.h"
#include <nlohmann/json.hpp>
namespace python_execution {
using std::string;
using std::uint32_t;
uint32_t PyCancelOrderRequest::key() const { return key_; }
void PyCancelOrderRequest::set_key(uint32_t value) { key_ = value; }
const string &PyCancelOrderRequest::exchange_order_id() const {
return exchange_order_id_;
}
void PyCancelOrderRequest::set_exchange_order_id(string value) {
exchange_order_id_ = std::move(value);
}
const string &PyCancelOrderRequest::exchange_security_id() const {
return exchange_security_id_;
}
void PyCancelOrderRequest::set_exchange_security_id(string value) {
exchange_security_id_ = std::move(value);
}
const string &PyCancelOrderRequest::venue() const { return venue_; }
void PyCancelOrderRequest::set_venue(string value) {
venue_ = std::move(value);
}
bool PyCancelOrderRequest::is_buy() const { return is_buy_; }
void PyCancelOrderRequest::set_is_buy(bool value) {
is_buy_ = std::move(value);
}
void to_json(nlohmann::json &j, const PyCancelOrderRequest &req) {
j["key"] = req.key();
j["eoid"] = req.exchange_order_id();
j["esid"] = req.exchange_security_id();
j["venue"] = req.venue();
j["side"] = req.is_buy() ? 'B' : 'S';
}
void from_json(const nlohmann::json &j, PyCancelOrderRequest &req) {
j.at("key").get_to(req.key_);
j.at("eoid").get_to(req.exchange_order_id_);
j.at("esid").get_to(req.exchange_security_id_);
j.at("venue").get_to(req.venue_);
const auto str = j.at("side").get<string>();
if (!str.empty()) {
switch (str[0]) {
case 'S':
case 's':
case 'A':
case 'a':
req.is_buy_ = false;
break;
case 'B':
case 'b':
req.is_buy_ = true;
}
}
}
} // namespace python_execution
@@ -0,0 +1,94 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
#include <string>
namespace bts::orderman {
struct CancelOrderMsg;
}
namespace python_execution {
class PyCancelOrderRequest {
public:
std::uint32_t key() const;
void set_key(std::uint32_t value);
const std::string &exchange_order_id() const;
void set_exchange_order_id(std::string value);
const std::string &exchange_security_id() const;
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
bool is_buy() const;
void set_is_buy(bool value);
void set_data(
std::uint32_t key,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
bool is_buy);
std::string to_string() const;
private:
std::uint32_t key_;
std::string exchange_order_id_;
std::string exchange_security_id_;
std::string venue_;
bool is_buy_;
friend void from_json(const nlohmann::json &j, PyCancelOrderRequest &req);
};
void to_json(nlohmann::json &j, const PyCancelOrderRequest &req);
void from_json(const nlohmann::json &j, PyCancelOrderRequest &req);
std::ostream &operator<<(
std::ostream &out, const PyCancelOrderRequest &req) noexcept;
} // namespace python_execution
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
python_execution::PyCancelOrderRequest,
r,
"{}",
bts::io::json_formatter(r));
namespace python_execution {
inline void PyCancelOrderRequest::set_data(
std::uint32_t key,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
bool is_buy) {
key_ = key;
exchange_order_id_ = std::move(exchange_order_id);
exchange_security_id_ = std::move(exchange_security_id);
venue_ = std::move(venue);
is_buy_ = is_buy;
}
inline std::string PyCancelOrderRequest::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyCancelOrderRequest &req) noexcept {
out << fmt::format("{}", req);
return out;
}
} // namespace python_execution
@@ -0,0 +1,80 @@
#include "py_cancel_replace_order_request.h"
#include "spark/domain_model/Venue.h"
#include "spark/orderman/CancelReplaceOrderMsg.h"
#include <nlohmann/json.hpp>
namespace python_execution {
using std::string;
using std::uint32_t;
uint32_t PyCancelReplaceOrderRequest::key() const { return key_; }
void PyCancelReplaceOrderRequest::set_key(uint32_t value) { key_ = value; }
const string &PyCancelReplaceOrderRequest::exchange_order_id() const {
return exchange_order_id_;
}
void PyCancelReplaceOrderRequest::set_exchange_order_id(string value) {
exchange_order_id_ = std::move(value);
}
const string &PyCancelReplaceOrderRequest::exchange_security_id() const {
return exchange_security_id_;
}
void PyCancelReplaceOrderRequest::set_exchange_security_id(string value) {
exchange_security_id_ = std::move(value);
}
const string &PyCancelReplaceOrderRequest::venue() const { return venue_; }
void PyCancelReplaceOrderRequest::set_venue(string value) {
venue_ = std::move(value);
}
bool PyCancelReplaceOrderRequest::is_buy() const { return is_buy_; }
void PyCancelReplaceOrderRequest::set_is_buy(bool value) {
is_buy_ = std::move(value);
}
float PyCancelReplaceOrderRequest::price() const { return price_; }
void PyCancelReplaceOrderRequest::set_price(float value) { price_ = value; }
float PyCancelReplaceOrderRequest::quantity() const { return quantity_; }
void PyCancelReplaceOrderRequest::set_quantity(float value) {
quantity_ = value;
}
void to_json(nlohmann::json &j, const PyCancelReplaceOrderRequest &req) {
j["key"] = req.key();
j["eoid"] = req.exchange_order_id();
j["esid"] = req.exchange_security_id();
j["venue"] = req.venue();
j["side"] = req.is_buy() ? 'B' : 'S';
j["px"] = req.price();
j["qty"] = req.quantity();
}
void from_json(const nlohmann::json &j, PyCancelReplaceOrderRequest &req) {
j.at("key").get_to(req.key_);
j.at("eoid").get_to(req.exchange_order_id_);
j.at("esid").get_to(req.exchange_security_id_);
j.at("venue").get_to(req.venue_);
const auto str = j.at("side").get<string>();
if (!str.empty()) {
switch (str[0]) {
case 'S':
case 's':
case 'A':
case 'a':
req.is_buy_ = false;
break;
case 'B':
case 'b':
req.is_buy_ = true;
}
}
j.at("px").get_to(req.price_);
j.at("qty").get_to(req.quantity_);
}
} // namespace python_execution
@@ -0,0 +1,110 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
#include <string>
namespace bts::orderman {
struct CancelReplaceOrderMsg;
}
namespace python_execution {
class PyCancelReplaceOrderRequest {
public:
std::uint32_t key() const;
void set_key(std::uint32_t value);
const std::string &exchange_order_id() const;
void set_exchange_order_id(std::string value);
const std::string &exchange_security_id() const;
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
bool is_buy() const;
void set_is_buy(bool value);
float price() const;
void set_price(float value);
float quantity() const;
void set_quantity(float value);
void set_data(
std::uint32_t key,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
bool is_buy,
double price,
double quantity);
std::string to_string() const;
private:
std::uint32_t key_;
std::string exchange_order_id_;
std::string exchange_security_id_;
std::string venue_;
float price_;
float quantity_;
bool is_buy_;
friend void from_json(
const nlohmann::json &j, PyCancelReplaceOrderRequest &req);
};
void to_json(nlohmann::json &j, const PyCancelReplaceOrderRequest &req);
void from_json(const nlohmann::json &j, PyCancelReplaceOrderRequest &req);
std::ostream &operator<<(
std::ostream &out, const PyCancelReplaceOrderRequest &req) noexcept;
} // namespace python_execution
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
python_execution::PyCancelReplaceOrderRequest,
r,
"{}",
bts::io::json_formatter(r));
namespace python_execution {
inline void PyCancelReplaceOrderRequest::set_data(
std::uint32_t key,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
bool is_buy,
double price,
double quantity) {
key_ = key;
exchange_order_id_ = std::move(exchange_order_id);
exchange_security_id_ = std::move(exchange_security_id);
venue_ = std::move(venue);
is_buy_ = is_buy;
price_ = price;
quantity_ = quantity;
}
inline std::string PyCancelReplaceOrderRequest::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyCancelReplaceOrderRequest &req) noexcept {
out << fmt::format("{}", req);
return out;
}
} // namespace python_execution
@@ -0,0 +1,101 @@
#include "py_create_order_request.h"
#include "spark/api/services/native_order_type.h"
#include "spark/api/services/time_in_force.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/domain_model/Venue.h"
#include "spark/orderman/CreateOrderMsg.h"
#include <nlohmann/json.hpp>
namespace python_execution {
using std::string;
using std::uint32_t;
uint32_t PyCreateOrderRequest::key() const { return key_; }
void PyCreateOrderRequest::set_key(uint32_t value) { key_ = value; }
const string &PyCreateOrderRequest::exchange_security_id() const {
return exchange_security_id_;
}
void PyCreateOrderRequest::set_exchange_security_id(string value) {
exchange_security_id_ = std::move(value);
}
const string &PyCreateOrderRequest::venue() const { return venue_; }
void PyCreateOrderRequest::set_venue(string value) {
venue_ = std::move(value);
}
float PyCreateOrderRequest::price() const { return price_; }
void PyCreateOrderRequest::set_price(float value) { price_ = value; }
float PyCreateOrderRequest::quantity() const { return quantity_; }
void PyCreateOrderRequest::set_quantity(float value) { quantity_ = value; }
const string &PyCreateOrderRequest::order_type() const { return order_type_; }
void PyCreateOrderRequest::set_order_type(string value) {
order_type_ = std::move(value);
}
const string &PyCreateOrderRequest::time_in_force() const {
return time_in_force_;
}
void PyCreateOrderRequest::set_time_in_force(string value) {
time_in_force_ = std::move(value);
}
bool PyCreateOrderRequest::is_buy() const { return is_buy_; }
void PyCreateOrderRequest::set_is_buy(bool value) {
is_buy_ = std::move(value);
}
void PyCreateOrderRequest::copy_from(const bts::orderman::CreateOrderMsg &msg) {
key_ = msg.key.value();
exchange_security_id_ = msg.security_name();
venue_ = bts::api::venue_name_pretty(msg.venue());
is_buy_ = msg.side == bts::api::SIDE_BUY;
price_ = msg.price.value();
quantity_ = msg.quantity.full_value_float();
order_type_ =
bts::api::orderman::native_order_type_name_pretty(msg.order_type);
time_in_force_ =
bts::api::orderman::time_in_force_name_pretty(msg.time_in_force);
}
void to_json(nlohmann::json &j, const PyCreateOrderRequest &req) {
j["key"] = req.key();
j["esid"] = req.exchange_security_id();
j["venue"] = req.venue();
j["side"] = req.is_buy() ? "B" : "S";
j["px"] = req.price();
j["qty"] = req.quantity();
j["typ"] = req.order_type();
j["tif"] = req.time_in_force();
}
void from_json(const nlohmann::json &j, PyCreateOrderRequest &req) {
j.at("key").get_to(req.key_);
j.at("eid").get_to(req.exchange_security_id_);
j.at("venue").get_to(req.venue_);
const auto str = j.at("side").get<string>();
if (!str.empty()) {
switch (str[0]) {
case 'S':
case 's':
case 'A':
case 'a':
req.is_buy_ = false;
break;
case 'B':
case 'b':
req.is_buy_ = true;
}
}
j.at("px").get_to(req.price_);
j.at("qty").get_to(req.quantity_);
j.at("typ").get_to(req.order_type_);
j.at("tif").get_to(req.time_in_force_);
}
} // namespace python_execution
@@ -0,0 +1,117 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
#include <string>
namespace bts::orderman {
struct CreateOrderMsg;
}
namespace python_execution {
class PyCreateOrderRequest {
public:
std::uint32_t key() const;
void set_key(std::uint32_t value);
const std::string &exchange_security_id() const;
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
bool is_buy() const;
void set_is_buy(bool value);
float price() const;
void set_price(float value);
float quantity() const;
void set_quantity(float value);
const std::string &order_type() const;
void set_order_type(std::string value);
const std::string &time_in_force() const;
void set_time_in_force(std::string value);
void set_data(
std::uint32_t key,
std::string exchange_security_id,
std::string venue,
bool is_buy,
double price,
double quantity,
std::string order_type,
std::string time_in_force);
void copy_from(const bts::orderman::CreateOrderMsg &msg);
std::string to_string() const;
private:
std::uint32_t key_;
float price_;
float quantity_;
std::string exchange_security_id_;
std::string venue_;
std::string order_type_;
std::string time_in_force_;
bool is_buy_;
friend void from_json(const nlohmann::json &j, PyCreateOrderRequest &req);
};
void to_json(nlohmann::json &j, const PyCreateOrderRequest &req);
void from_json(const nlohmann::json &j, PyCreateOrderRequest &req);
std::ostream &operator<<(
std::ostream &out, const PyCreateOrderRequest &req) noexcept;
} // namespace python_execution
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
python_execution::PyCreateOrderRequest,
r,
"{}",
bts::io::json_formatter(r));
namespace python_execution {
inline void PyCreateOrderRequest::set_data(
std::uint32_t key,
std::string exchange_security_id,
std::string venue,
bool is_buy,
double price,
double quantity,
std::string order_type,
std::string time_in_force) {
key_ = key;
exchange_security_id_ = std::move(exchange_security_id);
venue_ = std::move(venue);
is_buy_ = is_buy;
price_ = price;
quantity_ = quantity;
order_type_ = std::move(order_type);
time_in_force_ = std::move(time_in_force);
}
inline std::string PyCreateOrderRequest::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyCreateOrderRequest &req) noexcept {
out << fmt::format("{}", req);
return out;
}
} // namespace python_execution
@@ -0,0 +1,7 @@
#include "py_execution_gateway.h"
namespace python_execution {
PyExecutionGateway::~PyExecutionGateway() = default;
} // namespace python_execution
@@ -0,0 +1,55 @@
#pragma once
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/py_create_order_request.h"
#include <pybind11/pybind11.h>
namespace python_execution {
/**
* An implementation of `ExecutionGateway` that binds to a python object at runtime.
*
* @note The python object must derive from 'python_execution.ExecutionGateway', and define methods:
* * 'start(bridge: python_execution.ExecutionGatewayBridge)'
* * 'stop'
* * 'create_order'
* * 'cancel_order'
* * 'cancel_replace_order'
* @note 'python_execution_module' is a python module defined by us in c++ (see 'py_module.cpp').
*/
class PyExecutionGateway : public ExecutionGateway {
public:
using ExecutionGateway::ExecutionGateway;
~PyExecutionGateway() override;
// cpp calls into py
void start(ExecutionGatewayBridge *bridge) override {
PYBIND11_OVERLOAD_PURE(void, ExecutionGateway, start, bridge);
}
void create_order(std::shared_ptr<PyCreateOrderRequest> req) override {
PYBIND11_OVERLOAD_PURE(void, ExecutionGateway, create_order, req);
}
void cancel_order(std::shared_ptr<PyCancelOrderRequest> req) override {
PYBIND11_OVERLOAD_PURE(void, ExecutionGateway, cancel_order, req);
}
void cancel_replace_order(
std::shared_ptr<PyCancelReplaceOrderRequest> req) override {
PYBIND11_OVERLOAD_PURE(void, ExecutionGateway, cancel_replace_order, req);
}
// cpp calls into py
void on_securities(
const std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>>
&securities) override {
PYBIND11_OVERLOAD_PURE(
void, ExecutionGateway, on_securities, std::move(securities));
}
void stop() override {
PYBIND11_OVERLOAD_PURE(void, ExecutionGateway, stop);
}
};
} // namespace python_execution
@@ -0,0 +1,156 @@
#include "py_execution_report.h"
#include "spark/api/services/exec_type.h"
#include "spark/api/services/native_order_status.h"
#include "spark/api/services/native_order_type.h"
#include "spark/api/services/time_in_force.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/domain_model/SecurityType.h"
#include "spark/domain_model/Venue.h"
#include "spark/domain_model/common.h"
#include "spark/orderman/orderman_utils/ExecutionReport.h"
#include "spark/orderman/orderman_utils/exchange_order_id.h"
#include "spark/orderman/orderman_utils/orderman_common.h"
#include <variant>
namespace python_execution {
using bts::api::security_type_parse_smart;
using bts::api::venue_parse_smart;
using bts::api::orderman::exec_type_parse_smart;
using bts::api::orderman::native_order_status_parse_smart;
using bts::api::orderman::native_order_type_parse_smart;
using bts::api::orderman::time_in_force_parse_smart;
using bts::orderman::ExchangeOrderID;
using bts::orderman::ExchangeTimeStamp64;
using std::uint64_t;
void PyExecutionReport::set_data(
std::uint32_t key,
std::string account,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
std::string security_type,
std::string order_type,
std::string order_status,
std::string time_in_force,
std::string exec_type,
bool is_buy,
double price,
double quantity,
double filled_quantity,
double last_filled_quantity,
double last_filled_price,
double avg_filled_price,
std::string transact_id,
std::int64_t transact_time) {
key_ = key;
account_ = std::move(account);
exchange_order_id_ = std::move(exchange_order_id);
exchange_security_id_ = std::move(exchange_security_id);
venue_ = std::move(venue);
security_type_ = std::move(security_type);
order_type_ = std::move(order_type);
order_status_ = std::move(order_status);
time_in_force_ = std::move(time_in_force);
exec_type_ = std::move(exec_type);
is_buy_ = is_buy;
price_ = price;
quantity_ = quantity;
filled_quantity_ = filled_quantity;
last_filled_quantity_ = last_filled_quantity;
last_filled_price_ = last_filled_price;
avg_filled_price_ = avg_filled_price;
transact_id_ = transact_id;
transact_time_ = transact_time;
quantity_ = quantity;
}
void PyExecutionReport::to_execution_report(
bts::orderman::ExecutionReport &er,
std::function<const bts::api::Security *(
bts::api::Venue, bts::api::SecurityType, const std::string &)>
find_sec) {
*er.order_key = key_;
er.account_name->copy_from(account_);
if (std::holds_alternative<ExchangeOrderID>(er.exchange_order_id)) {
std::get<ExchangeOrderID>(er.exchange_order_id)
->copy_from(exchange_order_id_);
} else {
er.exchange_order_id = ExchangeOrderID{exchange_order_id_};
}
native_order_type_parse_smart(order_type_, &er.order_type);
native_order_status_parse_smart(order_status_, &er.order_status);
time_in_force_parse_smart(time_in_force_, &er.time_in_force);
exec_type_parse_smart(exec_type_, &er.exec_type);
bts::api::Venue venue;
venue_parse_smart(this->venue(), &venue);
bts::api::SecurityType sec_type;
security_type_parse_smart(security_type(), &sec_type);
er.security = find_sec(venue, sec_type, exchange_security_id());
er.side = is_buy_ ? bts::api::SIDE_BUY : bts::api::SIDE_SELL;
*er.price = price_;
*er.stop_price = std::numeric_limits<double>::quiet_NaN();
er.quantity = bts::Quantity::create_new(quantity_);
*er.avg_price = avg_filled_price_;
er.filled_quantity = bts::Quantity::create_new(filled_quantity_);
*er.last_price = last_filled_price_;
er.last_shares = bts::Quantity::create_new(last_filled_quantity_);
er.transact_time =
ExchangeTimeStamp64{static_cast<uint64_t>(transact_time_)};
}
void to_json(nlohmann::json &j, const PyExecutionReport &req) {
j["key"] = req.key();
j["acc"] = req.account();
j["esid"] = req.exchange_security_id();
j["st"] = req.order_status();
j["exec"] = req.exec_type();
j["venue"] = req.venue();
j["styp"] = req.security_type();
j["side"] = req.is_buy() ? "B" : "S";
j["px"] = req.price();
j["qty"] = req.quantity();
j["fqty"] = req.filled_quantity();
j["lfqty"] = req.last_filled_quantity();
j["lfpx"] = req.last_filled_price();
j["afpx"] = req.avg_filled_price();
j["typ"] = req.order_type();
j["tt"] = req.transact_time();
j["tid"] = req.transact_id();
}
void from_json(const nlohmann::json &j, PyExecutionReport &req) {
j.at("key").get_to(req.key_);
j.at("acc").get_to(req.account_);
j.at("esid").get_to(req.exchange_security_id_);
j.at("st").get_to(req.order_status_);
j.at("exec").get_to(req.exec_type_);
j.at("venue").get_to(req.venue_);
j.at("styp").get_to(req.security_type_);
std::string str;
j.at("side").get_to(str);
if (!str.empty()) {
switch (str[0]) {
case 'b':
case 'B':
req.is_buy_ = true;
break;
case 's':
case 'S':
req.is_buy_ = false;
break;
}
}
j.at("px").get_to(req.price_);
j.at("qty").get_to(req.quantity_);
j.at("fqty").get_to(req.filled_quantity_);
j.at("lfqty").get_to(req.last_filled_quantity_);
j.at("lfpx").get_to(req.last_filled_price_);
j.at("afpx").get_to(req.avg_filled_price_);
j.at("typ").get_to(req.order_type_);
j.at("tt").get_to(req.transact_time_);
}
} // namespace python_execution
@@ -0,0 +1,276 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::orderman {
class ExecutionReport;
}
namespace python_execution {
class PyExecutionReport {
public:
std::uint32_t key() const;
void set_key(std::uint32_t value);
const std::string &account() const;
void set_account(std::string value);
const std::string &exchange_order_id() const;
void set_exchange_order_id(std::string value);
const std::string &exchange_security_id() const;
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
const std::string &security_type() const;
void set_security_type(std::string value);
const std::string &order_type() const;
void set_order_type(std::string value);
const std::string &order_status() const;
void set_order_status(std::string value);
const std::string &time_in_force() const;
void set_time_in_force(std::string value);
const std::string &exec_type() const;
void set_exec_type(std::string value);
bool is_buy() const;
void set_is_buy(bool value);
double price() const;
void set_price(double value);
double quantity() const;
void set_quantity(double value);
double filled_quantity() const;
void set_filled_quantity(double value);
double last_filled_quantity() const;
void set_last_filled_quantity(double value);
double last_filled_price() const;
void set_last_filled_price(double value);
double avg_filled_price() const;
void set_avg_filled_price(double value);
const std::string &transact_id() const;
void set_transact_id(std::string value);
std::int64_t transact_time() const;
void set_transact_time(std::int64_t value);
/**
* Sets all data at once (more performant than setting props one at a time
* when calling from python).
*/
void set_data(
std::uint32_t key,
std::string account,
std::string exchange_order_id,
std::string exchange_security_id,
std::string venue,
std::string security_type,
std::string order_type,
std::string order_status,
std::string time_in_force,
std::string exec_type,
bool is_buy,
double price,
double quantity,
double filled_quantity,
double last_filled_quantity,
double last_filled_price,
double avg_filled_price,
std::string transact_id,
std::int64_t transact_time);
void to_execution_report(bts::orderman::ExecutionReport &er);
void to_execution_report(
bts::orderman::ExecutionReport &er,
std::function<const bts::api::Security *(
bts::api::Venue, bts::api::SecurityType, const std::string &)>
find_sec);
std::string to_string() const;
private:
std::uint32_t key_;
std::string account_;
std::string exchange_order_id_;
std::string exchange_security_id_;
std::string venue_;
std::string security_type_;
std::string order_type_;
std::string order_status_;
std::string time_in_force_;
std::string exec_type_;
bool is_buy_;
double price_;
double quantity_;
double filled_quantity_;
double last_filled_quantity_;
double last_filled_price_;
double avg_filled_price_;
std::string transact_id_;
std::int64_t transact_time_;
friend void from_json(const nlohmann::json &j, PyExecutionReport &req);
};
void to_json(nlohmann::json &j, const PyExecutionReport &req);
void from_json(const nlohmann::json &j, PyExecutionReport &req);
std::ostream &operator<<(
std::ostream &out, const PyExecutionReport &req) noexcept;
} // namespace python_execution
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
python_execution::PyExecutionReport, r, "{}", bts::io::json_formatter(r));
namespace python_execution {
inline std::uint32_t PyExecutionReport::key() const { return key_; }
inline void PyExecutionReport::set_key(std::uint32_t value) { key_ = value; }
inline const std::string &PyExecutionReport::account() const {
return account_;
}
inline void PyExecutionReport::set_account(std::string value) {
account_ = std::move(value);
}
inline const std::string &PyExecutionReport::exchange_order_id() const {
return exchange_order_id_;
}
inline void PyExecutionReport::set_exchange_order_id(std::string value) {
exchange_order_id_ = std::move(value);
}
inline const std::string &PyExecutionReport::exchange_security_id() const {
return exchange_security_id_;
}
inline void PyExecutionReport::set_exchange_security_id(std::string value) {
exchange_security_id_ = std::move(value);
}
inline const std::string &PyExecutionReport::venue() const { return venue_; }
inline void PyExecutionReport::set_venue(std::string value) {
venue_ = std::move(value);
}
inline const std::string &PyExecutionReport::security_type() const {
return security_type_;
}
inline void PyExecutionReport::set_security_type(std::string value) {
security_type_ = std::move(value);
}
inline const std::string &PyExecutionReport::order_type() const {
return order_type_;
}
inline void PyExecutionReport::set_order_type(std::string value) {
order_type_ = std::move(value);
}
inline const std::string &PyExecutionReport::order_status() const {
return order_status_;
}
inline void PyExecutionReport::set_order_status(std::string value) {
order_status_ = std::move(value);
}
inline const std::string &PyExecutionReport::time_in_force() const {
return time_in_force_;
}
inline void PyExecutionReport::set_time_in_force(std::string value) {
time_in_force_ = std::move(value);
}
inline const std::string &PyExecutionReport::exec_type() const {
return exec_type_;
}
inline void PyExecutionReport::set_exec_type(std::string value) {
exec_type_ = std::move(value);
}
inline bool PyExecutionReport::is_buy() const { return is_buy_; }
inline void PyExecutionReport::set_is_buy(bool value) { is_buy_ = value; }
inline double PyExecutionReport::price() const { return price_; }
inline void PyExecutionReport::set_price(double value) { price_ = value; }
inline double PyExecutionReport::quantity() const { return quantity_; }
inline void PyExecutionReport::set_quantity(double value) { quantity_ = value; }
inline double PyExecutionReport::filled_quantity() const {
return filled_quantity_;
}
inline void PyExecutionReport::set_filled_quantity(double value) {
filled_quantity_ = value;
}
inline double PyExecutionReport::last_filled_quantity() const {
return last_filled_quantity_;
}
inline void PyExecutionReport::set_last_filled_quantity(double value) {
last_filled_quantity_ = value;
}
inline double PyExecutionReport::last_filled_price() const {
return last_filled_price_;
}
inline void PyExecutionReport::set_last_filled_price(double value) {
last_filled_price_ = value;
}
inline double PyExecutionReport::avg_filled_price() const {
return avg_filled_price_;
}
inline void PyExecutionReport::set_avg_filled_price(double value) {
avg_filled_price_ = value;
}
inline const std::string &PyExecutionReport::transact_id() const {
return transact_id_;
}
inline void PyExecutionReport::set_transact_id(std::string value) {
transact_id_ = std::move(value);
}
inline std::int64_t PyExecutionReport::transact_time() const {
return transact_time_;
}
inline void PyExecutionReport::set_transact_time(std::int64_t value) {
transact_time_ = value;
}
inline std::string PyExecutionReport::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyExecutionReport &req) noexcept {
out << fmt::format("{}", req);
return out;
}
} // namespace python_execution
@@ -0,0 +1,153 @@
#include "py_cancel_order_request.h"
#include "py_cancel_replace_order_request.h"
#include "py_create_order_request.h"
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/py_execution_gateway.h"
#include "python_interop/execution/py_execution_report.h"
#include "python_interop/execution/py_position_info.h"
#include "python_interop/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include "python_interop/md/py_book.h"
#include "python_interop/md/py_book_level.h"
#include "python_interop/md/py_book_side.h"
#include "python_interop/md/py_md_provider.h"
#include "python_interop/md/py_trade.h"
#include <memory>
#include <pybind11/detail/common.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
using bts::md::py_interop::MdBridge;
using bts::md::py_interop::MdProvider;
using bts::md::py_interop::PyBook;
using bts::md::py_interop::PyBookLevel;
using bts::md::py_interop::PyBookSide;
using bts::md::py_interop::PyMdProvider;
using bts::md::py_interop::PyTrade;
using python_execution::ExecutionGateway;
using python_execution::ExecutionGatewayBridge;
using python_execution::PyCancelOrderRequest;
using python_execution::PyCancelReplaceOrderRequest;
using python_execution::PyCreateOrderRequest;
using python_execution::PyExecutionGateway;
using python_execution::PyExecutionReport;
using python_execution::PyPositionInfo;
using std::shared_ptr;
using std::shared_ptr;
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
namespace py = pybind11;
#define PY_ARG(arg) pybind11::arg(#arg)
#define _STR(x) #x
// In general, we bind c++ methods to python methods with the same name verbatim.
// The DEF macro avoids typos in the python method name for this common case.
#define DEF(type, member) def(_STR(member), &type::member)
#define DEF_ARGS(type, member, ...) \
def(_STR(member), &type::member, __VA_ARGS__)
#define EXPLICIT_DEF(type, member, member_type) \
def(_STR(member), (member_type) & type::member)
// The PROP macro defines getter and setter methods according to <name>
// Binds a pseudo property with getter/setter methods.
// Binds getter -> <name>, and setter -> set_<name>.
#define PROP(type, name) \
def(_STR(name), &type::name) \
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
PYBIND11_MODULE(python_execution_module, m) {
py::class_<PyCreateOrderRequest, shared_ptr<PyCreateOrderRequest>>(
m, "PyCreateOrderRequest")
.def(py::init<>())
.PROP(PyCreateOrderRequest, key)
.PROP(PyCreateOrderRequest, exchange_security_id)
.PROP(PyCreateOrderRequest, venue)
.PROP(PyCreateOrderRequest, is_buy)
.PROP(PyCreateOrderRequest, price)
.PROP(PyCreateOrderRequest, quantity)
.PROP(PyCreateOrderRequest, order_type)
.PROP(PyCreateOrderRequest, time_in_force)
.def("__str__", &PyCreateOrderRequest::to_string);
py::class_<PyCancelOrderRequest, shared_ptr<PyCancelOrderRequest>>(
m, "PyCancelOrderRequest")
.def(py::init<>())
.PROP(PyCancelOrderRequest, key)
.PROP(PyCancelOrderRequest, exchange_order_id)
.PROP(PyCancelOrderRequest, exchange_security_id)
.PROP(PyCancelOrderRequest, venue)
.PROP(PyCancelOrderRequest, is_buy)
.def("__str__", &PyCancelOrderRequest::to_string);
py::class_<
PyCancelReplaceOrderRequest, shared_ptr<PyCancelReplaceOrderRequest>>(
m, "PyCancelReplaceOrderRequest")
.def(py::init<>())
.PROP(PyCancelReplaceOrderRequest, key)
.PROP(PyCancelReplaceOrderRequest, exchange_order_id)
.PROP(PyCancelReplaceOrderRequest, exchange_security_id)
.PROP(PyCancelReplaceOrderRequest, venue)
.PROP(PyCancelReplaceOrderRequest, is_buy)
.PROP(PyCancelReplaceOrderRequest, price)
.PROP(PyCancelReplaceOrderRequest, quantity)
.def("__str__", &PyCancelReplaceOrderRequest::to_string);
py::class_<PyExecutionReport, shared_ptr<PyExecutionReport>>(
m, "PyExecutionReport")
.def(py::init<>())
.PROP(PyExecutionReport, key)
.PROP(PyExecutionReport, account)
.PROP(PyExecutionReport, exchange_order_id)
.PROP(PyExecutionReport, exchange_security_id)
.PROP(PyExecutionReport, order_status)
.PROP(PyExecutionReport, time_in_force)
.PROP(PyExecutionReport, exec_type)
.PROP(PyExecutionReport, venue)
.PROP(PyExecutionReport, security_type)
.PROP(PyExecutionReport, is_buy)
.PROP(PyExecutionReport, price)
.PROP(PyExecutionReport, quantity)
.PROP(PyExecutionReport, filled_quantity)
.PROP(PyExecutionReport, last_filled_quantity)
.PROP(PyExecutionReport, last_filled_price)
.PROP(PyExecutionReport, avg_filled_price)
.PROP(PyExecutionReport, order_type)
.PROP(PyExecutionReport, transact_time)
.def(
"set_data", &PyExecutionReport::set_data, py::arg("key"),
py::arg("account"), py::arg("exchange_order_id"),
py::arg("exchange_security_id"), py::arg("venue"),
py::arg("security_type"), py::arg("order_type"),
py::arg("order_status"), py::arg("time_in_force"),
py::arg("exec_type"), py::arg("is_buy"), py::arg("price"),
py::arg("quantity"), py::arg("filled_quantity"),
py::arg("last_filled_quantity"), py::arg("last_filled_price"),
py::arg("avg_filled_price"), py::arg("transact_id"),
py::arg("transact_time"))
.def("__str__", &PyExecutionReport::to_string);
py::class_<PyPositionInfo, shared_ptr<PyPositionInfo>>(m, "PyPositionInfo")
.def(py::init<>())
.PROP(PyPositionInfo, account)
.PROP(PyPositionInfo, exchange_security_id)
.PROP(PyPositionInfo, venue)
.PROP(PyPositionInfo, security_type)
.PROP(PyPositionInfo, long_quantity)
.PROP(PyPositionInfo, long_avg_cost)
.PROP(PyPositionInfo, short_quantity)
.PROP(PyPositionInfo, short_avg_cost)
.PROP(PyPositionInfo, transact_time)
.def("__str__", &PyPositionInfo::to_string);
py::class_<ExecutionGatewayBridge>(m, "ExecutionGatewayBridge")
.def(py::init<>())
.DEF(ExecutionGatewayBridge, on_execution_report)
.DEF(ExecutionGatewayBridge, on_position_info)
.DEF(ExecutionGatewayBridge, on_is_ready);
py::class_<ExecutionGateway, PyExecutionGateway>(m, "ExecutionGateway")
.def(py::init<>())
.DEF_ARGS(ExecutionGateway, start, py::arg("bridge"))
.DEF_ARGS(ExecutionGateway, create_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_replace_order, py::arg("req"))
.DEF(ExecutionGateway, stop);
}
@@ -0,0 +1,16 @@
#pragma once
#include "python_interop/execution/py_cancel_order_request.h"
#include "python_interop/execution/py_cancel_replace_order_request.h"
#include "python_interop/execution/py_create_order_request.h"
#include <variant>
namespace python_execution {
using PyOrderRequest = std::variant<
std::shared_ptr<PyCreateOrderRequest>,
std::shared_ptr<PyCancelReplaceOrderRequest>,
std::shared_ptr<PyCancelOrderRequest>>;
} // namespace python_execution
@@ -0,0 +1,29 @@
#include "py_position_info.h"
namespace python_execution {
void to_json(nlohmann::json &j, const PyPositionInfo &pi) {
j["acc"] = pi.account();
j["esid"] = pi.exchange_security_id();
j["styp"] = pi.security_type();
j["venue"] = pi.venue();
j["lacost"] = pi.long_avg_cost();
j["lqty"] = pi.long_quantity();
j["sacost"] = pi.short_avg_cost();
j["sqty"] = pi.short_quantity();
j["tt"] = pi.transact_time();
}
void from_json(const nlohmann::json &j, PyPositionInfo &pi) {
j.at("acc").get_to(pi.account_);
j.at("esid").get_to(pi.exchange_security_id_);
j.at("styp").get_to(pi.security_type_);
j.at("venue").get_to(pi.venue_);
j.at("lacost").get_to(pi.long_avg_cost_);
j.at("lqty").get_to(pi.long_quantity_);
j.at("sacost").get_to(pi.short_avg_cost_);
j.at("sqty").get_to(pi.short_quantity_);
j.at("tt").get_to(pi.transact_time_);
}
} // namespace python_execution
@@ -0,0 +1,134 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <string>
namespace python_execution {
class PyPositionInfo {
public:
const std::string &account() const;
void set_account(std::string value);
const std::string &exchange_security_id() const;
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
const std::string &security_type() const;
void set_security_type(std::string value);
float long_quantity() const;
void set_long_quantity(float value);
float long_avg_cost() const;
void set_long_avg_cost(float value);
float short_quantity() const;
void set_short_quantity(float value);
float short_avg_cost() const;
void set_short_avg_cost(float value);
std::int64_t transact_time() const;
void set_transact_time(std::int64_t value);
std::string to_string() const;
private:
std::string account_;
std::string exchange_security_id_;
std::string venue_;
std::string security_type_;
float long_quantity_;
float long_avg_cost_;
float short_quantity_;
float short_avg_cost_;
std::int64_t transact_time_;
friend void from_json(const nlohmann::json &j, PyPositionInfo &req);
};
void to_json(nlohmann::json &j, const PyPositionInfo &req);
void from_json(const nlohmann::json &j, PyPositionInfo &req);
std::ostream &operator<<(std::ostream &out, const PyPositionInfo &req) noexcept;
} // namespace python_execution
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(python_execution::PyPositionInfo, r, "{}", bts::io::json_formatter(r));
namespace python_execution {
inline const std::string &PyPositionInfo::account() const { return account_; }
inline void PyPositionInfo::set_account(std::string value) {
account_ = std::move(value);
}
inline const std::string &PyPositionInfo::exchange_security_id() const {
return exchange_security_id_;
}
inline void PyPositionInfo::set_exchange_security_id(std::string value) {
exchange_security_id_ = std::move(value);
}
inline const std::string &PyPositionInfo::venue() const { return venue_; }
inline void PyPositionInfo::set_venue(std::string value) {
venue_ = std::move(value);
}
inline const std::string &PyPositionInfo::security_type() const {
return security_type_;
}
inline void PyPositionInfo::set_security_type(std::string value) {
security_type_ = std::move(value);
}
inline float PyPositionInfo::long_quantity() const { return long_quantity_; }
inline void PyPositionInfo::set_long_quantity(float value) {
long_quantity_ = value;
}
inline float PyPositionInfo::long_avg_cost() const { return long_avg_cost_; }
inline void PyPositionInfo::set_long_avg_cost(float value) {
long_avg_cost_ = value;
}
inline float PyPositionInfo::short_quantity() const { return short_quantity_; }
inline void PyPositionInfo::set_short_quantity(float value) {
short_quantity_ = value;
}
inline float PyPositionInfo::short_avg_cost() const { return short_avg_cost_; }
inline void PyPositionInfo::set_short_avg_cost(float value) {
short_avg_cost_ = value;
}
inline std::int64_t PyPositionInfo::transact_time() const {
return transact_time_;
}
inline void PyPositionInfo::set_transact_time(std::int64_t value) {
transact_time_ = value;
}
inline std::string PyPositionInfo::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyPositionInfo &req) noexcept {
out << fmt::format("{}", req);
return out;
}
} // namespace python_execution
@@ -0,0 +1,7 @@
#include "md_bridge.h"
namespace bts::md::py_interop {
MdBridge::~MdBridge() = default;
} // namespace bts::md::py_interop
@@ -0,0 +1,23 @@
#pragma once
#include "python_interop/md/py_book.h"
#include "python_interop/md/py_trade.h"
#include "python_interop/py_security.h"
#include <vector>
namespace bts::md::py_interop {
class MdBridge {
public:
virtual ~MdBridge();
// Python interface
virtual void on_securities(
const std::vector<std::shared_ptr<PySecurity>> &securities) = 0;
virtual void on_books(const std::vector<std::shared_ptr<PyBook>> &books) = 0;
virtual void on_trades(
const std::vector<std::shared_ptr<PyTrade>> &trades) = 0;
};
} // namespace bts::md::py_interop
@@ -0,0 +1,128 @@
#include "python_interop/md/md_bridge_impl.h"
#include "python_interop/md/md_provider.h"
#include "python_interop/python_activator.h"
#include "spark/io/json_formatter.h"
#include "spark/logging/logging.h"
#include "spark/logging/trace_log.h"
#include "spark/utils/Delegate.h"
#include "spark/utils/stopwatch.h"
#include <pybind11/pybind11.h>
#include <thread>
using std::shared_ptr;
namespace py = pybind11;
namespace bts::md::py_interop {
using bts::core::Delegate;
using bts::io::json_formatter;
using bts::utils::Stopwatch;
using python_interop::PythonActivatorT;
using std::string;
using std::vector;
#define ASSERT_FN(test, fn, ...) \
bts_assert_msgf(test, "md_bridge::" fn ". " __VA_ARGS__)
#define LOG_FN(level, fn, ...) LOG(md, level, "md_bridge::" 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__)
#define _LOG(level, ...) LOG(OrderMan, level, __VA_ARGS__)
#define INFO(...) _LOG(bts::Info, __VA_ARGS__)
#define ERROR(...) _LOG(bts::Error, __VA_ARGS__)
#define _LOG_TRACE(level, name) \
LOG_TRACE(md, level, std::string("md_bridge"), std::string(name))
#define INFO_TRACE(name) _LOG_TRACE(bts::Info, name)
#define WARN_TRACE(name) _LOG_TRACE(bts::Warn, name)
#define ERROR_TRACE(name) _LOG_TRACE(bts::Error, name)
#define DIAG_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
md, bts::Diag, std::string("md_bridge"), std::string(name), params)
#define INFO_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
md, bts::Info, std::string("md_bridge"), std::string(name), params)
#define WARN_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
md, bts::Warn, std::string("md_bridge"), std::string(name), params)
MdBridgeImpl::MdBridgeImpl(string target, vector<string> module_paths)
: target(std::move(target)), module_paths(std::move(module_paths)) {}
void MdBridgeImpl::on_securities(
const std::vector<std::shared_ptr<PySecurity>> &securities) {
INFO_TRACE_PARAMS("on_securities", securities.size());
incoming_securities.push_many(securities.begin(), securities.end());
}
void MdBridgeImpl::on_books(const std::vector<std::shared_ptr<PyBook>> &books) {
DIAG_TRACE_PARAMS("on_books", json_formatter(books.size()));
incoming_books.push_many(books.begin(), books.end());
}
void MdBridgeImpl::on_trades(
const std::vector<std::shared_ptr<PyTrade>> &trades) {
DIAG_TRACE_PARAMS("on_trades", json_formatter(trades.size()));
incoming_trades.push_many(trades.begin(), trades.end());
}
bool MdBridgeImpl::pop_security(shared_ptr<const PySecurity> &result) {
auto res = incoming_securities.pop();
if (res) {
result = std::move(*res);
return true;
}
result = nullptr;
return false;
}
bool MdBridgeImpl::pop_book(shared_ptr<const PyBook> &result) {
auto res = incoming_books.pop();
if (res) {
result = std::move(*res);
return true;
}
result = nullptr;
return false;
}
bool MdBridgeImpl::pop_trade(shared_ptr<const PyTrade> &result) {
auto res = incoming_trades.pop();
if (res) {
result = std::move(*res);
return true;
}
result = nullptr;
return false;
}
int MdBridgeImpl::run_py(volatile bool *keep_going) {
bts_assert(keep_going != nullptr);
PythonActivatorT<MdProvider> provider_activator{target, module_paths};
provider = provider_activator.get_target([&](MdProvider &pro) {
// This will start another thread in python, and call back into c++ from there.
auto sw = Stopwatch::start_new();
pro.start(this);
INFO_TRACE_PARAMS(
"run_py",
json_formatter(fmt::format(
"MdProvider.start took {:.2f}ms", sw.elapsed().total_millis())));
});
// Release gil to let python run
py::gil_scoped_release gil_rel;
while (*keep_going) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
MdBridgeImpl::~MdBridgeImpl() = default;
} // namespace bts::md::py_interop
@@ -0,0 +1,36 @@
#pragma once
#include "python_interop/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include "spark/collections/concurrent_queue.h"
namespace bts::md::py_interop {
class MdBridgeImpl : public MdBridge {
public:
MdBridgeImpl(std::string target, std::vector<std::string> module_paths);
~MdBridgeImpl();
// Python interface
void on_securities(
const std::vector<std::shared_ptr<PySecurity>> &securities) override;
void on_books(const std::vector<std::shared_ptr<PyBook>> &books) override;
void on_trades(const std::vector<std::shared_ptr<PyTrade>> &trades) override;
// C++ interface
bool pop_security(std::shared_ptr<const PySecurity> &result);
bool pop_book(std::shared_ptr<const PyBook> &result);
bool pop_trade(std::shared_ptr<const PyTrade> &result);
int run_py(volatile bool *keep_going);
private:
std::string target;
std::vector<std::string> module_paths;
collections::ConcurrentQueue<std::shared_ptr<const PySecurity>>
incoming_securities;
collections::ConcurrentQueue<std::shared_ptr<const PyBook>> incoming_books;
collections::ConcurrentQueue<std::shared_ptr<const PyTrade>> incoming_trades;
MdProvider *provider;
};
} // namespace bts::md::py_interop
@@ -0,0 +1,7 @@
#include "python_interop/md/md_provider.h"
namespace bts::md::py_interop {
MdProvider::~MdProvider() = default;
} // namespace bts::md::py_interop
@@ -0,0 +1,14 @@
#pragma once
#include "python_interop/md/md_bridge.h"
namespace bts::md::py_interop {
class MdProvider {
public:
virtual ~MdProvider();
virtual void start(MdBridge *bridge) = 0;
virtual void stop() = 0;
};
} // namespace bts::md::py_interop
@@ -0,0 +1,51 @@
#include "py_book.h"
#include "python_interop/md/py_book_level.h"
namespace bts::md::py_interop {
static const void validate_depth_array(const pybind11::buffer_info &info) {
if (info.ndim != 2 || info.shape[0] < 0 || info.shape[1] != 2) {
throw std::runtime_error(fmt::format(
"Expected array of shape (10, 2). Actually got ({}, {}, {})",
info.ndim, info.shape[0], info.shape[1]));
}
}
static void to_book_side(
PyBookSide &dest, const PyBook::PyDepthArray &src, std::size_t count) {
auto r = src.unchecked<2>();
PyBookLevel lvl;
for (std::size_t i = 0; i < count; i++) {
double px = r(i, 0);
double qty = r(i, 1);
if (std::isnan(px) || !(qty > 0)) {
break;
}
lvl.set_price(r(i, 0));
lvl.set_quantity(r(i, 1));
dest.set_level(lvl);
}
}
void PyBook::set_data(
std::string exchange_security_id,
std::string venue,
std::string security_type,
PyDepthArray bids,
PyDepthArray asks) {
const auto bids_info = bids.request();
const auto asks_info = asks.request();
validate_depth_array(bids_info);
validate_depth_array(asks_info);
exchange_security_id_ = std::move(exchange_security_id);
venue_ = std::move(venue);
security_type_ = std::move(security_type);
bids_.set_is_ascending(false);
asks_.set_is_ascending(true);
const size_t n_bids = bids_info.size / 2;
const size_t n_asks = asks_info.size / 2;
to_book_side(bids_, bids, n_bids);
to_book_side(asks_, asks, n_asks);
}
} // namespace bts::md::py_interop
@@ -0,0 +1,160 @@
#pragma once
#include "python_interop/md/py_book_level.h"
#include "python_interop/md/py_book_side.h"
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include "spark/utils/bts_assert.h"
#include <cmath>
#include <limits>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <sstream>
#include <string>
namespace bts::md::py_interop {
/**
* @brief An L2 Order Book (both sides of the market).
*/
class PyBook {
public:
// A more performant alternative to PyBookSide. Populate bid/ask numpy arrays
// in python, then init everything in a single call (set_data).
using PyDepthArray = pybind11::
array_t<double, pybind11::array::c_style | pybind11::array::forcecast>;
/**
* Gets the exchange security id. This is typically the security's name
* (e.g., "BTS-PERPETUAL", or "ESH5").
*
* @note Should be unique for each security at a particular venue (i.e., a single
* venue cannot have 2 securities with the same name). MUST be unique for each security
* of the same type (e.g., SPOT, FUTURE, OPTION) at a particular venue.
*
* @note There is no "security_id" member because that is an internal id that
* we generate elsewhere in c++ code. The python plugins just need to set the
* venue and exchange_security_id.
*/
const std::string &exchange_security_id() const;
/**
* @brief Sets the exchange security id.
*
* @param value The new exchange security id.
*/
void set_exchange_security_id(std::string value);
/**
* @brief Gets the name of the venue (i.e., exchange).
*
* @return const std::string& The name of the venue.
*/
const std::string &venue() const;
void set_venue(std::string value);
/**
* @brief Gets the name of the security type (e.g., "SPOT", "FUTURE", "OPTION").
*
* @return const std::string& The name of the security type.
*/
const std::string &security_type() const;
/**
* @brief Sets the security type.
*
* @param value The new security type (should be "SPOT", "FUTURE", or "OPTION").
*/
void set_security_type(std::string value);
PyBookSide &bids();
const PyBookSide &bids() const;
void set_bids(PyBookSide bids);
PyBookSide &asks();
const PyBookSide &asks() const;
void set_asks(PyBookSide asks);
/**
* Sets all data at once (more performant than setting props one at a time
* when calling from python).
*/
void set_data(
std::string exchange_security_id,
std::string venue,
std::string security_type,
PyDepthArray bids,
PyDepthArray asks);
/**
* @brief Gets the number of levels in the book (i.e., the number of prices
* with resting orders).
*/
constexpr int size() const;
std::string to_string() const;
private:
std::string venue_;
std::string security_type_;
std::string exchange_security_id_;
PyBookSide bids_;
PyBookSide asks_;
};
void to_json(nlohmann::json &j, const PyBook &bk);
void from_json(const nlohmann::json &j, PyBook &bk);
} // namespace bts::md::py_interop
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(bts::md::py_interop::PyBook, bs, "{}", bts::io::json_formatter(bs));
namespace bts::md::py_interop {
inline const std::string &PyBook::venue() const { return venue_; }
inline void PyBook::set_venue(std::string value) { venue_ = std::move(value); }
inline const std::string &PyBook::security_type() const {
return security_type_;
}
inline void PyBook::set_security_type(std::string value) {
security_type_ = std::move(value);
}
inline const std::string &PyBook::exchange_security_id() const {
return exchange_security_id_;
}
inline void PyBook::set_exchange_security_id(std::string value) {
exchange_security_id_ = std::move(value);
}
inline PyBookSide &PyBook::bids() { return bids_; }
inline const PyBookSide &PyBook::bids() const { return bids_; }
inline void PyBook::set_bids(PyBookSide bids) { bids_ = std::move(bids); }
inline PyBookSide &PyBook::asks() { return asks_; }
inline const PyBookSide &PyBook::asks() const { return asks_; }
inline void PyBook::set_asks(PyBookSide asks) { asks_ = std::move(asks); }
constexpr int PyBook::size() const { return bids_.size() + asks_.size(); }
inline std::string PyBook::to_string() const {
return fmt::format("{}", *this);
}
inline void to_json(nlohmann::json &j, const PyBook &bk) {
j["bids"] = bk.bids();
j["asks"] = bk.asks();
}
inline void from_json(const nlohmann::json &j, PyBook &bk) {
bk.set_exchange_security_id(j.at("sec").get<std::string>());
bk.set_venue(j.at("venue").get<std::string>());
bk.set_security_type(j.at("typ").get<std::string>());
bk.set_bids(j.at("bids").get<PyBookSide>());
bk.set_asks(j.at("asks").get<PyBookSide>());
}
} // namespace bts::md::py_interop
@@ -0,0 +1,70 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <limits>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::md::py_interop {
class PyBookLevel {
public:
constexpr PyBookLevel() = default;
constexpr double price() const;
void set_price(double value);
constexpr double quantity() const;
void set_quantity(double value);
std::string to_string() const;
private:
double price_{std::numeric_limits<double>::quiet_NaN()};
double quantity_{0};
};
std::ostream &operator<<(std::ostream &out, const PyBookLevel &l) noexcept;
void to_json(nlohmann::json &j, const PyBookLevel &l);
void from_json(const nlohmann::json &j, PyBookLevel &l);
} // namespace bts::md::py_interop
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(bts::md::py_interop::PyBookLevel, l, "{}", bts::io::json_formatter(l));
namespace bts::md::py_interop {
constexpr double PyBookLevel::price() const { return price_; }
inline void PyBookLevel::set_price(double value) { price_ = value; }
constexpr double PyBookLevel::quantity() const { return quantity_; }
inline void PyBookLevel::set_quantity(double value) { quantity_ = value; }
inline std::string PyBookLevel::to_string() const {
return fmt::format("{}", *this);
}
inline void to_json(nlohmann::json &j, const PyBookLevel &l) {
j["p"] = l.price();
j["q"] = l.quantity();
}
inline void from_json(const nlohmann::json &j, PyBookLevel &l) {
l.set_price(j.at("p").get<double>());
l.set_quantity(j.at("q").get<double>());
}
inline std::ostream &operator<<(
std::ostream &out, const PyBookLevel &l) noexcept {
out << fmt::format("{}", l);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,67 @@
#include "py_book_side.h"
#include <algorithm>
#include <cmath>
namespace bts::md::py_interop {
void PyBookSide::set_level(PyBookLevel level) {
if (std::isnan(level.price())) {
std::cerr << "PyBookLevel invalid price (nan)" << std::endl;
return;
}
if (level.quantity() < 0) {
std::cerr << "PyBookLevel invalid quantity (less than 0)" << std::endl;
return;
}
auto it = std::lower_bound(
levels_.begin(), levels_.end(), level,
[](const auto &lhs, const auto &rhs) {
return lhs.price() < rhs.price();
});
if (it == levels_.end()) {
if (level.quantity() == 0) {
std::cerr << "PyBookLevel " << to_string()
<< " indicates remove op, but the price level does not exist"
<< std::endl;
return;
}
levels_.push_back(std::move(level));
} else if (it->price() == level.price()) {
if (level.quantity() == 0) {
levels_.erase(it);
} else {
*it = std::move(level);
}
} else {
levels_.insert(it, std::move(level));
}
}
std::string PyBookSide::to_string_pretty() const {
std::string rv;
if (is_ascending_) {
bool is_first = true;
for (const auto &level : levels_) {
if (is_first) {
is_first = false;
} else {
rv.append(", ");
}
rv.append(level.to_string());
}
} else {
bool is_first = true;
for (auto it = levels_.rbegin(); it != levels_.rend(); it++) {
if (is_first) {
is_first = false;
} else {
rv.append(", ");
}
rv.append(it->to_string());
}
}
return rv;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,93 @@
#pragma once
#include "python_interop/md/py_book_level.h"
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <iostream>
#include <vector>
namespace bts::md::py_interop {
/**
* @brief L2 Order Book for one side of the market (i.e., bids or offers).
*
* @details Bid market levels should be in DESCENDING order by price, while Ask
* levels (offers) should be in ASCENDING order. Another way to think about it
* is the price levels are sorted by their distance from the opposite market.
*/
class PyBookSide {
public:
constexpr bool is_ascending();
void set_is_ascending(bool is_ascending);
constexpr PyBookLevel get_level(int index);
constexpr PyBookLevel get_level(int index) const;
void set_level(PyBookLevel level);
constexpr int size() const;
std::string to_string() const;
std::string to_string_pretty() const;
private:
std::vector<PyBookLevel> levels_;
bool is_ascending_;
friend void to_json(nlohmann::json &j, const PyBookSide &bs);
friend void from_json(const nlohmann::json &j, PyBookSide &bs);
};
void to_json(nlohmann::json &j, const PyBookSide &bs);
void from_json(const nlohmann::json &j, PyBookSide &bs);
} // namespace bts::md::py_interop
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
bts::md::py_interop::PyBookSide, bs, "{}", bts::io::json_formatter(bs));
namespace bts::md::py_interop {
constexpr bool PyBookSide::is_ascending() { return is_ascending_; }
inline void PyBookSide::set_is_ascending(bool is_ascending) {
is_ascending_ = is_ascending;
}
constexpr PyBookLevel PyBookSide::get_level(int index) {
if (!is_ascending_) {
index = levels_.size() - 1 - index;
}
return levels_.at(index);
}
constexpr PyBookLevel PyBookSide::get_level(int index) const {
if (!is_ascending_) {
index = levels_.size() - 1 - index;
}
return levels_.at(index);
}
constexpr int PyBookSide::size() const { return levels_.size(); }
inline std::string PyBookSide::to_string() const {
return fmt::format("{}", *this);
}
inline void to_json(nlohmann::json &j, const PyBookSide &bs) { j = bs.levels_; }
inline void from_json(const nlohmann::json &j, PyBookSide &bs) {
j.get_to(bs.levels_);
}
inline std::ostream &operator<<(
std::ostream &out, const PyBookSide &bs) noexcept {
out << fmt::format("{}", bs);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,7 @@
#include "py_md_provider.h"
namespace bts::md::py_interop {
PyMdProvider::~PyMdProvider() = default;
} // namespace bts::md::py_interop
@@ -0,0 +1,31 @@
#pragma once
#include "python_interop/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include <pybind11/pybind11.h>
namespace bts::md::py_interop {
/**
* An implementation of `bts::md::py_interop::MdProvider` that binds to a python object at runtime.
*
* @note The python object must derive from 'python_md_module.MdProvider', and define methods:
* 1. 'start(md_bridge: python_md_module.MdBridge)'
* 2. 'stop'
* @note 'python_md_module' is a python module defined by us in c++ (see 'py_module.cpp').
*/
class PyMdProvider : public MdProvider {
public:
using MdProvider::MdProvider;
~PyMdProvider() override;
void start(MdBridge *bridge) override {
PYBIND11_OVERLOAD_PURE(void, MdProvider, start, bridge);
}
void stop() override { PYBIND11_OVERLOAD_PURE(void, MdProvider, stop); }
};
} // namespace bts::md::py_interop
@@ -0,0 +1,97 @@
#include "md_provider.h"
#include "python_interop/md/py_book_level.h"
#include "python_interop/md/py_book_side.h"
#include "python_interop/md/py_md_provider.h"
#include "python_interop/md/py_trade.h"
#include <pybind11/detail/common.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
using bts::md::py_interop::MdBridge;
using bts::md::py_interop::MdProvider;
using bts::md::py_interop::PyBook;
using bts::md::py_interop::PyBookLevel;
using bts::md::py_interop::PyBookSide;
using bts::md::py_interop::PyMdProvider;
using bts::md::py_interop::PyTrade;
using std::shared_ptr;
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
namespace py = pybind11;
#define _STR(x) #x
// In general, we bind c++ methods to python methods with the same name verbatim.
// The DEF macro avoids typos in the python method name for this common case.
#define DEF(type, member) def(_STR(member), &type::member)
#define DEF_ARGS(type, member, ...) \
def(_STR(member), &type::member, __VA_ARGS__)
#define EXPLICIT_DEF(type, member, member_type) \
def(_STR(member), (member_type) & type::member)
// The PROP macro defines getter and setter methods according to <name>
// Binds a pseudo property with getter/setter methods.
// Binds getter -> <name>, and setter -> set_<name>.
#define PROP(type, name) \
def(_STR(name), &type::name) \
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
// Define python module with c++ classes.
PYBIND11_MODULE(python_md_module, m) {
using BookSideGetter = PyBookSide &(PyBook::*)();
using BookLevelGetter = PyBookLevel (PyBookSide::*)(int) const;
// book
py::class_<PyBookLevel>(m, "PyBookLevel")
.def(py::init<>())
.PROP(PyBookLevel, price)
.PROP(PyBookLevel, quantity);
py::class_<PyBookSide>(m, "PyBookSide")
.def(py::init<>())
.EXPLICIT_DEF(PyBookSide, get_level, BookLevelGetter)
.DEF(PyBookSide, set_level)
.PROP(PyBookSide, is_ascending)
.def("__len__", &PyBookSide::size)
.def("__str__", &PyBookSide::to_string);
py::class_<PyBook, shared_ptr<PyBook>>(m, "PyBook")
.def(py::init<>())
.PROP(PyBook, venue)
.PROP(PyBook, security_type)
.PROP(PyBook, exchange_security_id)
.EXPLICIT_DEF(PyBook, bids, BookSideGetter)
.DEF(PyBook, set_bids)
.EXPLICIT_DEF(PyBook, asks, BookSideGetter)
.DEF(PyBook, set_asks)
.def(
"set_data", &PyBook::set_data, py::arg("exch_sec_id"),
py::arg("venue"), py::arg("sec_type"), py::arg("bids"),
py::arg("asks"))
.def("__len__", &PyBook::size)
.def("__str__", &PyBook::to_string);
py::bind_vector<std::vector<shared_ptr<PyBook>>>(m, "PyBookList");
// trade
py::class_<PyTrade, shared_ptr<PyTrade>>(m, "PyTrade")
.def(py::init<>())
.PROP(PyTrade, venue)
.PROP(PyTrade, security_type)
.PROP(PyTrade, exchange_security_id)
.PROP(PyTrade, price)
.PROP(PyTrade, quantity)
.PROP(PyTrade, aggressor_side)
.PROP(PyTrade, timestamp);
py::bind_vector<std::vector<shared_ptr<PyTrade>>>(m, "PyTradeList");
// md bridge
py::class_<MdBridge>(m, "MdBridge")
.DEF_ARGS(MdBridge, on_securities, py::arg("securities"))
.DEF_ARGS(MdBridge, on_books, py::arg("books"))
.DEF_ARGS(MdBridge, on_trades, py::arg("trades"));
// md provider
py::class_<MdProvider, PyMdProvider>(m, "MdProvider")
.def(py::init<>())
.DEF_ARGS(MdProvider, start, py::arg("bridge"))
.DEF(MdProvider, stop);
}
#undef PROP
#undef EXPLICIT_DEF
#undef DEF
#undef _STR
@@ -0,0 +1,157 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
namespace bts::md::py_interop {
class PyTrade {
public:
/**
* Gets the exchange security id. This is typically the security's name
* (e.g., "BTS-PERPETUAL", or "ESH5").
*
* @note Should be unique for each security at a particular venue (i.e., a single
* venue cannot have 2 securities with the same name). MUST be unique for each security
* of the same type (e.g., SPOT, FUTURE, OPTION) at a particular venue.
*
* @note There is no "security_id" member because that is an internal id that
* we generate elsewhere in c++ code. The python plugins just need to set the
* venue and exchange_security_id.
*/
const std::string &exchange_security_id() const;
/**
* Sets the exchange security id.
*
* @param value The new exchange security id.
*/
void set_exchange_security_id(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
const std::string &security_type() const;
void set_security_type(std::string value);
constexpr double price() const;
void set_price(double value);
constexpr double quantity() const;
void set_quantity(double value);
const std::string &aggressor_side() const;
void set_aggressor_side(std::string value);
/**
* @brief The time at which the trade was executed (nanos since unix epoch).
*/
constexpr std::int64_t timestamp() const;
void set_timestamp(std::int64_t value);
std::string to_string_pretty() const;
std::string to_string() const;
private:
std::string venue_;
std::string security_type_;
std::string exchange_security_id_;
double price_;
double quantity_;
std::string aggressor_side_;
std::int64_t timestamp_;
};
void to_json(nlohmann::json &j, const PyTrade &t);
void from_json(const nlohmann::json &j, PyTrade &t);
std::ostream &operator<<(std::ostream &out, const PyTrade &t) noexcept;
} // namespace bts::md::py_interop
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(bts::md::py_interop::PyTrade, t, "{}", bts::io::json_formatter(t));
namespace bts::md::py_interop {
inline const std::string &PyTrade::venue() const { return venue_; }
inline void PyTrade::set_venue(std::string value) { venue_ = std::move(value); }
inline const std::string &PyTrade::security_type() const {
return security_type_;
}
inline void PyTrade::set_security_type(std::string value) {
security_type_ = std::move(value);
}
inline const std::string &PyTrade::exchange_security_id() const {
return exchange_security_id_;
}
inline void PyTrade::set_exchange_security_id(std::string value) {
exchange_security_id_ = std::move(value);
}
constexpr double PyTrade::price() const { return price_; }
inline void PyTrade::set_price(double value) { price_ = value; }
constexpr double PyTrade::quantity() const { return quantity_; }
inline void PyTrade::set_quantity(double value) { quantity_ = value; }
inline const std::string &PyTrade::aggressor_side() const {
return aggressor_side_;
}
inline void PyTrade::set_aggressor_side(std::string value) {
aggressor_side_ = std::move(value);
}
constexpr std::int64_t PyTrade::timestamp() const { return timestamp_; }
inline void PyTrade::set_timestamp(std::int64_t value) { timestamp_ = value; }
inline std::string PyTrade::to_string() const {
return fmt::format("{}", *this);
}
inline std::string PyTrade::to_string_pretty() const {
std::stringstream str;
if (!aggressor_side_.empty()) {
str << aggressor_side_ << " ";
}
str << quantity_ << exchange_security_id_ << " @ " << price_ << " ("
<< venue_ << " ts " << timestamp_ << ")";
return str.str();
}
inline void to_json(nlohmann::json &j, const PyTrade &t) {
j["sec"] = t.exchange_security_id();
j["venue"] = t.venue();
j["typ"] = t.security_type();
j["agg"] = t.aggressor_side();
j["p"] = t.price();
j["q"] = t.quantity();
j["ts"] = t.timestamp();
}
inline void from_json(const nlohmann::json &j, PyTrade &t) {
t.set_exchange_security_id(j.at("sec").get<std::string>());
t.set_venue(j.at("venue").get<std::string>());
t.set_security_type(j.at("typ").get<std::string>());
t.set_aggressor_side(j.at("agg").get<std::string>());
t.set_price(j.at("p").get<double>());
t.set_quantity(j.at("q").get<double>());
t.set_timestamp(j.at("ts").get<std::int64_t>());
}
inline std::ostream &operator<<(std::ostream &out, const PyTrade &t) noexcept {
out << fmt::format("{}", t);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,153 @@
#pragma once
#include <fmt/format.h>
#include <limits>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
namespace bts::md::py_interop {
/**
* Price metadata for a particular contract within a continuous price range.
*
* @note If there are multiple PyContractPriceInfo items, they should be added
* to a list in ascending order by "max_price". Then the "min_price" for each
* item simply the "max_price" of the previous item. Hence the omission of a
* "min_price" function.
*/
class PyContractPriceInfo {
public:
constexpr PyContractPriceInfo() = default;
float tick_unit() const;
void set_tick_unit(float value);
float tick_base() const;
void set_tick_base(float value);
float multiplier() const;
void set_multiplier(float value);
float cash_per_point() const;
void set_cash_per_point(float value);
float max_price() const;
void set_max_price(float value);
std::string to_string() const;
private:
float tick_unit_ = std::numeric_limits<float>::quiet_NaN();
float tick_base_ = std::numeric_limits<float>::quiet_NaN();
float multiplier_ = 1;
float cash_per_point_ = 1;
float max_price_ = std::numeric_limits<float>::max();
friend void to_json(nlohmann::json &j, const PyContractPriceInfo &cpi);
friend void from_json(const nlohmann::json &j, PyContractPriceInfo &cpi);
};
void to_json(nlohmann::json &j, const PyContractPriceInfo &cpi);
void from_json(const nlohmann::json &j, PyContractPriceInfo &cpi);
std::ostream &operator<<(
std::ostream &out, const PyContractPriceInfo &qty_info) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline float PyContractPriceInfo::tick_unit() const { return tick_unit_; }
inline void PyContractPriceInfo::set_tick_unit(float value) {
tick_unit_ = value;
}
inline float PyContractPriceInfo::tick_base() const { return tick_base_; }
inline void PyContractPriceInfo::set_tick_base(float value) {
tick_base_ = value;
}
inline float PyContractPriceInfo::multiplier() const { return multiplier_; }
inline void PyContractPriceInfo::set_multiplier(float value) {
multiplier_ = value;
}
inline float PyContractPriceInfo::cash_per_point() const {
return cash_per_point_;
}
inline void PyContractPriceInfo::set_cash_per_point(float value) {
cash_per_point_ = value;
}
inline float PyContractPriceInfo::max_price() const { return max_price_; }
inline void PyContractPriceInfo::set_max_price(float value) {
max_price_ = value;
}
inline std::string PyContractPriceInfo::to_string() const {
std::stringstream str;
str << *this;
return str.str();
}
inline void to_json(nlohmann::json &j, const PyContractPriceInfo &cpi) {
j["tick_unit"] = cpi.tick_unit_;
j["tick_base"] = cpi.tick_base_;
j["multiplier"] = cpi.multiplier_;
j["cash_per_point"] = cpi.cash_per_point_;
j["max_price"] = cpi.max_price_;
}
inline void from_json(const nlohmann::json &j, PyContractPriceInfo &cpi) {
auto it = j.find("tick_unit");
if (it != j.end()) {
it->get_to(cpi.tick_unit_);
}
it = j.find("tick_base");
if (it != j.end()) {
it->get_to(cpi.tick_base_);
}
it = j.find("multiplier");
if (it != j.end()) {
it->get_to(cpi.multiplier_);
}
it = j.find("cash_per_point");
if (it != j.end()) {
it->get_to(cpi.cash_per_point_);
}
it = j.find("max_price");
if (it != j.end()) {
it->get_to(cpi.max_price_);
}
}
} // namespace bts::md::py_interop
template <>
struct fmt::formatter<bts::md::py_interop::PyContractPriceInfo> {
constexpr auto parse(fmt ::format_parse_context &ctx)
-> decltype(ctx.end()) {
return ctx.end();
}
template <typename FormatContext>
auto format(
const bts::md::py_interop::PyContractPriceInfo &cpi,
FormatContext &ctx) const -> decltype(ctx.out()) {
return fmt::format_to(
ctx.out(),
R"( "tick_unit": {}, "tick_base": {}, "multiplier": {}, "cash_per_point": {}, "max_price": {} )",
cpi.tick_unit(), cpi.tick_base(), cpi.multiplier(),
cpi.cash_per_point(), cpi.max_price());
}
};
namespace bts::md::py_interop {
inline std::ostream &operator<<(
std::ostream &out, const PyContractPriceInfo &qty_info) noexcept {
out << fmt::format("{}", qty_info);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,166 @@
#pragma once
#include <fmt/format.h>
#include <limits>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
namespace bts::md::py_interop {
/**
* Quantity metadata for a particular contract within a continuous price range.
* Most contracts use the same quantity metadata for all prices. For those that
* don't, the "min price" is interpretted as the "max_price" of the previous
* item in the list of quantity metadatas (presumably sorted by "max_price").
*/
class PyContractQuantityInfo {
public:
constexpr PyContractQuantityInfo() = default;
float min_value() const;
void set_min_value(float value);
float max_value() const;
void set_max_value(float value);
float tick_unit() const;
void set_tick_unit(float value);
float tick_base() const;
void set_tick_base(float value);
float multiplier() const;
void set_multiplier(float value);
float max_price() const;
void set_max_price(float value);
std::string to_string() const;
private:
float min_value_ = std::numeric_limits<float>::min();
float max_value_ = std::numeric_limits<float>::max();
float tick_unit_ = std::numeric_limits<float>::quiet_NaN();
float tick_base_ = std::numeric_limits<float>::quiet_NaN();
float multiplier_ = 1;
float max_price_ = std::numeric_limits<float>::max();
friend void to_json(
nlohmann::json &j, const PyContractQuantityInfo &qty_info);
friend void from_json(
const nlohmann::json &j, PyContractQuantityInfo &qty_info);
};
void to_json(nlohmann::json &j, const PyContractQuantityInfo &qty_info);
void from_json(const nlohmann::json &j, PyContractQuantityInfo &qty_info);
std::ostream &operator<<(
std::ostream &out, const PyContractQuantityInfo &qty_info) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline float PyContractQuantityInfo::min_value() const { return min_value_; }
inline void PyContractQuantityInfo::set_min_value(float value) {
min_value_ = value;
}
inline float PyContractQuantityInfo::max_value() const { return max_value_; }
inline void PyContractQuantityInfo::set_max_value(float value) {
max_value_ = value;
}
inline float PyContractQuantityInfo::tick_unit() const { return tick_unit_; }
inline void PyContractQuantityInfo::set_tick_unit(float value) {
tick_unit_ = value;
}
inline float PyContractQuantityInfo::tick_base() const { return tick_base_; }
inline void PyContractQuantityInfo::set_tick_base(float value) {
tick_base_ = value;
}
inline float PyContractQuantityInfo::multiplier() const { return multiplier_; }
inline void PyContractQuantityInfo::set_multiplier(float value) {
multiplier_ = value;
}
inline float PyContractQuantityInfo::max_price() const { return max_price_; }
inline void PyContractQuantityInfo::set_max_price(float value) {
max_price_ = value;
}
inline std::string PyContractQuantityInfo::to_string() const {
std::stringstream str;
str << *this;
return str.str();
}
inline void to_json(nlohmann::json &j, const PyContractQuantityInfo &qty_info) {
j["min_value"] = qty_info.min_value_;
j["max_value"] = qty_info.max_value_;
j["tick_unit"] = qty_info.tick_unit_;
j["tick_base"] = qty_info.tick_base_;
j["multiplier"] = qty_info.multiplier_;
j["max_price"] = qty_info.max_price_;
}
inline void from_json(
const nlohmann::json &j, PyContractQuantityInfo &qty_info) {
auto it = j.find("min_value");
if (it != j.end()) {
it->get_to(qty_info.min_value_);
}
it = j.find("max_value");
if (it != j.end()) {
it->get_to(qty_info.max_value_);
}
it = j.find("tick_unit");
if (it != j.end()) {
it->get_to(qty_info.tick_unit_);
}
it = j.find("tick_base");
if (it != j.end()) {
it->get_to(qty_info.tick_base_);
}
it = j.find("multiplier");
if (it != j.end()) {
it->get_to(qty_info.multiplier_);
}
it = j.find("max_price");
if (it != j.end()) {
it->get_to(qty_info.max_price_);
}
}
} // namespace bts::md::py_interop
template <>
struct fmt::formatter<bts::md::py_interop::PyContractQuantityInfo> {
constexpr auto parse(fmt ::format_parse_context &ctx)
-> decltype(ctx.end()) {
return ctx.end();
}
template <typename FormatContext>
auto format(
const bts::md::py_interop::PyContractQuantityInfo &cqi,
FormatContext &ctx) const -> decltype(ctx.out()) {
return fmt::format_to(
ctx.out(),
R"( "min_value": {}, "max_value": {}, "tick_unit": {}, "tick_base": {}, "multiplier": {}, "max_price": {} )",
cqi.min_value(), cqi.max_value(), cqi.tick_unit(), cqi.tick_base(),
cqi.multiplier(), cqi.max_price());
}
};
namespace bts::md::py_interop {
inline std::ostream &operator<<(
std::ostream &out, const PyContractQuantityInfo &qty_info) noexcept {
out << fmt::format("{}", qty_info);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,158 @@
#pragma once
#include "python_interop/py_contract_price_info.h"
#include "python_interop/py_contract_quantity_info.h"
#include "spark/core/FMT_IMPL.h"
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace bts::md::py_interop {
/**
* Quantity and price metadata for a particular contract.
*/
class PyContractSpecification {
public:
constexpr PyContractSpecification() = default;
const std::vector<PyContractPriceInfo> &price_infos() const;
std::vector<PyContractPriceInfo> &mutable_price_infos();
void set_price_infos(std::vector<PyContractPriceInfo> value);
const std::vector<PyContractQuantityInfo> &quantity_infos() const;
std::vector<PyContractQuantityInfo> &mutable_quantity_infos();
void set_quantity_infos(std::vector<PyContractQuantityInfo> value);
const std::vector<PyContractQuantityInfo> &notional_quantity_infos() const;
std::vector<PyContractQuantityInfo> &mutable_notional_quantity_infos();
void set_notional_quantity_infos(std::vector<PyContractQuantityInfo> value);
std::string to_string() const;
private:
std::vector<PyContractPriceInfo> price_infos_;
std::vector<PyContractQuantityInfo> quantity_infos_;
std::vector<PyContractQuantityInfo> notional_quantity_infos_;
friend void to_json(nlohmann::json &j, const PyContractSpecification &spec);
friend void from_json(
const nlohmann::json &j, PyContractSpecification &spec);
};
std::ostream &operator<<(
std::ostream &out, const PyContractSpecification &opt) noexcept;
void to_json(nlohmann::json &j, const PyContractSpecification &spec);
void from_json(const nlohmann::json &j, PyContractSpecification &spec);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
} // namespace bts::md::py_interop
FMT_IMPL(
bts::md::py_interop::PyContractSpecification,
s,
R"({{ "price_infos": [{}], "quantity_infos": [{}], "notional_quantity_infos": [{}] }})",
fmt::format("{}", fmt::join(s.price_infos(), ", ")),
fmt::format("{}", fmt::join(s.quantity_infos(), ", ")),
fmt::format("{}", fmt::join(s.notional_quantity_infos(), ", ")));
// template <>
// struct fmt::formatter<bts::md::py_interop::PyContractSpecification> {
// constexpr auto parse(fmt ::format_parse_context &ctx)
// -> decltype(ctx.end()) {
// return ctx.end();
// }
// template <typename FormatContext>
// auto format(
// const bts::md::py_interop::PyContractSpecification &spec,
// FormatContext &ctx) const -> decltype(ctx.out()) {
// return fmt::format_to(
// ctx.out(),
// R"({{ "price_infos": [{}], "quantity_infos": [{}], "notional_quantity_infos": [{}] }})",
// fmt::format("{}", fmt::join(spec.price_infos().items(), ", ")),
// fmt::format("{}", fmt::join(spec.quantity_infos().items(), ", ")),
// fmt::format(
// "{}", fmt::join(spec.notional_quantity_infos().items(), ", ")));
// }
// };
namespace bts::md::py_interop {
inline const std::vector<PyContractPriceInfo> &
PyContractSpecification::price_infos() const {
return price_infos_;
}
inline std::vector<PyContractPriceInfo> &
PyContractSpecification::mutable_price_infos() {
return price_infos_;
}
inline void PyContractSpecification::set_price_infos(
std::vector<PyContractPriceInfo> value) {
price_infos_ = std::move(value);
}
inline const std::vector<PyContractQuantityInfo> &
PyContractSpecification::quantity_infos() const {
return quantity_infos_;
}
inline std::vector<PyContractQuantityInfo> &
PyContractSpecification::mutable_quantity_infos() {
return quantity_infos_;
}
inline void PyContractSpecification::set_quantity_infos(
std::vector<PyContractQuantityInfo> value) {
quantity_infos_ = std::move(value);
}
inline const std::vector<PyContractQuantityInfo> &
PyContractSpecification::notional_quantity_infos() const {
return notional_quantity_infos_;
}
inline std::vector<PyContractQuantityInfo> &
PyContractSpecification::mutable_notional_quantity_infos() {
return notional_quantity_infos_;
}
inline void PyContractSpecification::set_notional_quantity_infos(
std::vector<PyContractQuantityInfo> value) {
notional_quantity_infos_ = std::move(value);
}
inline std::string PyContractSpecification::to_string() const {
return fmt::format("{}", *this);
}
inline void to_json(nlohmann::json &j, const PyContractSpecification &spec) {
j["price_infos"] = spec.price_infos();
j["quantity_infos"] = spec.quantity_infos();
j["notional_quantity_infos"] = spec.notional_quantity_infos();
}
inline void from_json(const nlohmann::json &j, PyContractSpecification &spec) {
auto it = j.find("price_infos");
if (it != j.end()) {
it->get_to(spec.price_infos_);
}
it = j.find("quantity_infos");
if (it != j.end()) {
it->get_to(spec.quantity_infos_);
}
it = j.find("notional_quantity_infos");
if (it != j.end()) {
it->get_to(spec.notional_quantity_infos_);
}
}
inline std::ostream &operator<<(
std::ostream &out, const PyContractSpecification &spec) noexcept {
out << fmt::format("{}", spec);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,56 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include <iostream>
#include <nlohmann/json_fwd.hpp>
#include <string>
namespace bts::md::py_interop {
/**
* Future contract expiration.
*/
class PyFuture {
public:
const std::string &expiration() const;
void set_expiration(std::string value);
std::string to_string() const;
private:
std::string expiration_;
};
void to_json(nlohmann::json &j, const PyFuture &fut);
void from_json(const nlohmann::json &j, PyFuture &fut);
std::ostream &operator<<(std::ostream &out, const PyFuture &fut) noexcept;
} // namespace bts::md::py_interop
FMT_IMPL(
bts::md::py_interop::PyFuture, f, R"({{ "exp" : "{}" }})", f.expiration());
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
namespace bts::md::py_interop {
inline const std::string &PyFuture::expiration() const { return expiration_; }
inline void PyFuture::set_expiration(std::string value) {
expiration_ = std::move(value);
}
inline std::string PyFuture::to_string() const {
return fmt::format("{}", *this);
}
inline std::ostream &operator<<(
std::ostream &out, const PyFuture &fut) noexcept {
out << fmt::format("{}", fut);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,103 @@
#pragma once
#include "spark/core/FMT_IMPL.h"
#include <iostream>
#include <limits>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::md::py_interop {
/**
* Option contract expiration, strike, and call/put.
*/
class PyOption {
public:
constexpr PyOption() = default;
const std::string &expiration() const;
void set_expiration(std::string value);
constexpr bool is_call() const;
void set_is_call(bool value);
constexpr double strike() const;
void set_strike(double value);
std::string to_string() const;
private:
std::string expiration_;
bool is_call_{false};
double strike_{std::numeric_limits<double>::quiet_NaN()};
friend void to_json(nlohmann::json &j, const PyOption &opt);
friend void from_json(const nlohmann::json &j, PyOption &opt);
};
void to_json(nlohmann::json &j, const PyOption &opt);
void from_json(const nlohmann::json &j, PyOption &opt);
std::ostream &operator<<(std::ostream &out, const PyOption &opt) noexcept;
} // namespace bts::md::py_interop
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
FMT_IMPL(
bts::md::py_interop::PyOption,
f,
R"({{ "exp" : "{}", "k": {}, "cp": "{}" }})",
f.expiration(),
f.strike(),
f.is_call() ? 'C' : 'P');
namespace bts::md::py_interop {
inline const std::string &PyOption::expiration() const { return expiration_; }
inline void PyOption::set_expiration(std::string value) {
expiration_ = std::move(value);
}
constexpr bool PyOption::is_call() const { return is_call_; }
inline void PyOption::set_is_call(bool value) { is_call_ = value; }
constexpr double PyOption::strike() const { return strike_; }
inline void PyOption::set_strike(double value) { strike_ = value; }
inline std::string PyOption::to_string() const {
return fmt::format("{}", *this);
}
inline void to_json(nlohmann::json &j, const PyOption &opt) {
j["exp"] = opt.expiration();
j["k"] = opt.strike();
j["cp"] = opt.is_call() ? "C" : "P";
}
inline void from_json(const nlohmann::json &j, PyOption &opt) {
auto it = j.find("exp");
if (it != j.end()) {
it->get_to(opt.expiration_);
}
it = j.find("k");
if (it != j.end()) {
it->get_to(opt.strike_);
}
it = j.find("cp");
if (it != j.end()) {
const std::string cp = it->get<std::string>();
opt.is_call_ = !cp.empty() && (cp[0] == 'C' || cp[0] == 'c');
}
}
inline std::ostream &operator<<(
std::ostream &out, const PyOption &opt) noexcept {
out << fmt::format("{}", opt);
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,84 @@
#include "py_security.h"
#include "spark/utils/overloaded.h"
#include <nlohmann/json.hpp>
namespace bts::md::py_interop {
using std::string;
using std::uint32_t;
void to_json(nlohmann::json &j, const PySecurity &sec) {
j["id"] = sec.id_;
j["eid"] = sec.exchange_id_;
j["name"] = sec.name_;
j["venue"] = sec.venue_;
j["cxy"] = sec.currency_;
j["grp"] = sec.group_;
j["ass"] = sec.asset_;
j["spec"] = sec.contract_spec_;
std::visit(
overloaded{
[&](const PySpot &) { j["typ"] = "SPOT"; },
[&](const PyFuture &fut) {
j["typ"] = "FUT";
j["exp"] = fut.expiration();
},
[&](const PyOption &opt) {
j["typ"] = "OPT";
j["exp"] = opt.expiration();
j["k"] = opt.strike();
j["cp"] = opt.is_call() ? "C" : "P";
},
},
sec.sec_);
}
void from_json(const nlohmann::json &j, PySecurity &sec) {
auto it = j.find("id");
if (it != j.end()) {
it->get_to(sec.id_);
}
it = j.find("eid");
if (it != j.end()) {
it->get_to(sec.exchange_id_);
}
it = j.find("name");
if (it != j.end()) {
it->get_to(sec.name_);
}
it = j.find("venue");
if (it != j.end()) {
it->get_to(sec.venue_);
}
it = j.find("cxy");
if (it != j.end()) {
it->get_to(sec.currency_);
}
it = j.find("grp");
if (it != j.end()) {
it->get_to(sec.group_);
}
it = j.find("ass");
if (it != j.end()) {
it->get_to(sec.asset_);
}
it = j.find("spec");
if (it != j.end()) {
it->get_to(sec.contract_spec_);
}
const auto typ = j.at("typ").get<string>();
if (typ.starts_with('S')) {
sec.sec_.emplace<PySpot>();
} else if (typ.starts_with('F')) {
auto &fut = sec.sec_.emplace<PyFuture>();
fut.set_expiration(j.at("exp").get<string>());
} else if (typ.starts_with('O')) {
auto &opt = sec.sec_.emplace<PyOption>();
opt.set_expiration(j.at("exp").get<string>());
opt.set_is_call(j.at("cp").get<string>().starts_with('C'));
opt.set_strike(j.at("k").get<double>());
}
}
} // namespace bts::md::py_interop
@@ -0,0 +1,160 @@
#pragma once
#include "python_interop/py_contract_specification.h"
#include "python_interop/py_future.h"
#include "python_interop/py_option.h"
#include "python_interop/py_spot.h"
#include "spark/io/json_formatter.h"
#include <cstdint>
#include <fmt/format.h>
#include <nlohmann/json_fwd.hpp>
#include <stdexcept>
#include <string>
#include <variant>
namespace bts::md::py_interop {
class PySecurity {
public:
constexpr std::uint32_t id() const;
void set_id(std::uint32_t value);
const std::string &exchange_id() const;
void set_exchange_id(std::string value);
const std::string &name() const;
void set_name(std::string value);
const std::string &venue() const;
void set_venue(std::string value);
const std::string &currency() const;
void set_currency(std::string value);
const std::string &group() const;
void set_group(std::string value);
const std::string &asset() const;
void set_asset(std::string value);
const PySpot &spot() const;
void set_spot(PySpot);
constexpr bool is_spot() const;
const PyFuture &future() const;
void set_future(PyFuture);
constexpr bool is_future() const;
const PyOption &option() const;
void set_option(PyOption);
constexpr bool is_option() const;
const PyContractSpecification &contract_spec() const;
void set_contract_spec(PyContractSpecification value);
std::string to_string() const;
private:
std::uint32_t id_;
std::string exchange_id_;
std::string venue_;
std::string group_;
std::string asset_;
std::string name_;
std::string currency_;
PyContractSpecification contract_spec_;
std::variant<PySpot, PyFuture, PyOption> sec_;
friend struct fmt::formatter<PySecurity>;
friend void to_json(nlohmann::json &j, const PySecurity &sec);
friend void from_json(const nlohmann::json &j, PySecurity &sec);
};
void to_json(nlohmann::json &j, const PySecurity &sec);
void from_json(const nlohmann::json &j, PySecurity &sec);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
constexpr std::uint32_t PySecurity::id() const { return id_; }
inline void PySecurity::set_id(std::uint32_t value) { id_ = value; }
inline const std::string &PySecurity::exchange_id() const {
return exchange_id_;
}
inline void PySecurity::set_exchange_id(std::string value) {
exchange_id_ = std::move(value);
}
inline const std::string &PySecurity::name() const { return name_; }
inline void PySecurity::set_name(std::string value) {
name_ = std::move(value);
}
inline const std::string &PySecurity::venue() const { return venue_; }
inline void PySecurity::set_venue(std::string value) {
venue_ = std::move(value);
}
inline const std::string &PySecurity::currency() const { return currency_; }
inline void PySecurity::set_currency(std::string value) {
currency_ = std::move(value);
}
inline const std::string &PySecurity::group() const { return group_; }
inline void PySecurity::set_group(std::string value) {
group_ = std::move(value);
}
inline const std::string &PySecurity::asset() const { return asset_; }
inline void PySecurity::set_asset(std::string value) {
asset_ = std::move(value);
}
inline const PySpot &PySecurity::spot() const {
if (std::holds_alternative<PySpot>(sec_)) {
return std::get<PySpot>(sec_);
}
throw std::runtime_error{"PySecurity is not a spot"};
}
inline void PySecurity::set_spot(PySpot value) { sec_ = std::move(value); }
constexpr bool PySecurity::is_spot() const {
return std::holds_alternative<PySpot>(sec_);
}
inline const PyFuture &PySecurity::future() const {
if (std::holds_alternative<PyFuture>(sec_)) {
return std::get<PyFuture>(sec_);
}
throw std::runtime_error{"PySecurity is not a future"};
}
inline void PySecurity::set_future(PyFuture value) { sec_ = std::move(value); }
constexpr bool PySecurity::is_future() const {
return std::holds_alternative<PyFuture>(sec_);
}
inline const PyOption &PySecurity::option() const {
if (std::holds_alternative<PyOption>(sec_)) {
return std::get<PyOption>(sec_);
}
throw std::runtime_error{"PySecurity is not an option"};
}
inline void PySecurity::set_option(PyOption value) { sec_ = std::move(value); }
constexpr bool PySecurity::is_option() const {
return std::holds_alternative<PyOption>(sec_);
}
inline const PyContractSpecification &PySecurity::contract_spec() const {
return contract_spec_;
}
inline void PySecurity::set_contract_spec(PyContractSpecification value) {
contract_spec_ = std::move(value);
}
inline std::string PySecurity::to_string() const { return name_; }
} // namespace bts::md::py_interop
FMT_IMPL(bts::md::py_interop::PySecurity, s, "{}", bts::io::json_formatter(s));
@@ -0,0 +1,28 @@
#pragma once
#include <string>
namespace bts::md::py_interop {
/**
* Placeholder object that represents a Spot contract.
*/
class PySpot {
public:
std::string to_string() const;
};
std::ostream &operator<<(std::ostream &out, const PySpot &spot) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline std::string PySpot::to_string() const { return ""; }
inline std::ostream &operator<<(
std::ostream &out, const PySpot & /*spot*/) noexcept {
return out;
}
} // namespace bts::md::py_interop
@@ -0,0 +1,93 @@
#pragma once
#include "python_interop/python_activator_impl.h"
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <string>
#include <vector>
namespace python_interop {
class PythonActivator {
public:
PythonActivator(std::string target, std::vector<std::string> module_paths)
: me_(new PythonActivator_Impl(
std::move(target), std::move(module_paths))) {}
PythonActivator() {
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
delete me;
}
pybind11::object &get_target_obj() {
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
return me->get_target_obj();
}
pybind11::object &get_target_obj(
std::function<void(pybind11::object &)> init) {
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
return me->get_target_obj(init);
}
template <typename... Args>
pybind11::object &get_target_obj(
std::function<void(pybind11::object &)> init, Args &&...args) {
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
return me->get_target_obj<Args...>(init, std::forward<Args>(args)...);
}
void reset() {
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
me->reset();
}
private:
void *me_;
};
template <typename Target>
class PythonActivatorT {
public:
PythonActivatorT(std::string target, std::vector<std::string> module_paths)
: activator_(std::move(target), std::move(module_paths)) {}
pybind11::object &get_target_obj() { return activator_.get_target_obj(); }
Target *get_target() {
if (target_ == nullptr) {
target_ = activator_.get_target_obj().cast<Target *>();
}
return target_;
}
Target *get_target(std::function<void(Target &)> init) {
if (target_ == nullptr) {
activator_.get_target_obj([&](pybind11::object &obj) {
target_ = obj.cast<Target *>();
init(*target_);
});
}
return target_;
}
template <typename... Args>
Target *get_target(std::function<void(Target &)> init, Args &&...args) {
if (target_ == nullptr) {
activator_.get_target_obj<Args...>(
[&](pybind11::object &obj) {
target_ = obj.cast<Target *>();
init(*target_);
},
std::forward<Args>(args)...);
}
return target_;
}
private:
PythonActivator activator_;
Target *target_ = nullptr;
};
} // namespace python_interop
@@ -0,0 +1,118 @@
#pragma once
#include "python_interop/python_interpreter.h"
#include "spark/core/TimeStamp.h"
#include "spark/io/json_formatter.h"
#include "spark/logging/logging.h"
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <string>
#include <vector>
namespace python_interop {
struct PythonActivator_Impl {
std::string target;
std::vector<std::string> module_paths;
std::optional<pybind11::object> target_obj;
PythonActivator_Impl(
std::string target, std::vector<std::string> module_paths)
: target(std::move(target)), module_paths(std::move(module_paths)) {}
~PythonActivator_Impl() { reset(); }
pybind11::object &get_target_obj() {
return get_target_obj([](pybind11::object &) {});
}
template <typename... Args>
pybind11::object &get_target_obj(
std::function<void(pybind11::object &)> init, Args &&...args) {
if (!target_obj) {
// Prevent python from overwriting our signal handler (so ctrl+c
// actually terminates the process).
auto orig_signal_handler = std::signal(SIGINT, SIG_DFL);
target_obj = create_target_with_args<Args...>(
init, std::forward<Args>(args)...);
std::signal(SIGINT, orig_signal_handler);
}
return *target_obj;
}
// Undef after use (this is a header)
#define LOG_EVENT(ev) \
LOG(OrderMan, bts::Info, \
R"([py_activator.json]{{ "tgt": "{}", "path": {}, "ev": )" ev \
R"(, "ts": {} }})", \
target, bts::io::json_formatter(module_paths), \
bts::core::TimeStamp::now_ns())
#define LOG_ERROR(err) \
LOG(OrderMan, bts::Info, \
R"([py_activator.json]{{ "tgt": "{}", "path": {}, "ev": "ERROR", "err": "{}", "ts": {} }})", \
target, bts::io::json_formatter(module_paths), err, \
bts::core::TimeStamp::now_ns())
#define LOG_CREATE LOG_EVENT("CREATE")
#define LOG_RESET LOG_EVENT("RESET")
void reset() {
LOG_RESET;
target_obj.reset();
}
private:
template <typename... Args>
pybind11::object create_target_with_args(
std::function<void(pybind11::object &)> init, Args &&...args) {
LOG_CREATE;
return PythonInterpreter::exec_t<pybind11::object>(
[this, &init,
args_tuple =
std::make_tuple(std::forward<Args>(args)...)]() mutable {
std::string target_module_name;
std::string target_type_name;
PythonInterpreter::parse_target(
target, &target_module_name, &target_type_name);
pybind11::object obj;
try {
// Prepare module path
pybind11::module sys = pybind11::module::import("sys");
for (const auto &module_path : module_paths) {
sys.attr("path").attr("append")(module_path.c_str());
}
// Import target module
pybind11::module target_module =
pybind11::module::import(target_module_name.c_str());
// Instantiate target using tuple expansion
obj = std::apply(
[&](auto &&...unpacked_args) {
return target_module.attr(target_type_name.c_str())(
std::forward<decltype(unpacked_args)>(
unpacked_args)...);
},
std::move(args_tuple));
init(obj);
} catch (const std::exception &ex) {
LOG_ERROR(std::string{ex.what()});
}
return obj;
});
}
pybind11::object create_target() {
return create_target_with_args([](pybind11::object &) {});
}
pybind11::object create_target(
std::function<void(pybind11::object &)> init) {
return create_target_with_args(init);
}
#undef LOG_EVENT
#undef LOG_ERROR
#undef LOG_CREATE
#undef LOG_RESET
};
} // namespace python_interop
@@ -0,0 +1,276 @@
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.h"
#include "python_interop/execution/py_cancel_order_request.h"
#include "python_interop/execution/py_cancel_replace_order_request.h"
#include "python_interop/execution/py_create_order_request.h"
#include "python_interop/execution/py_execution_gateway.h"
#include "python_interop/execution/py_position_info.h"
#include "python_interop/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include "python_interop/md/py_book.h"
#include "python_interop/md/py_book_level.h"
#include "python_interop/md/py_book_side.h"
#include "python_interop/md/py_md_provider.h"
#include "python_interop/md/py_trade.h"
#include "python_interop/py_contract_price_info.h"
#include "python_interop/py_contract_quantity_info.h"
#include "python_interop/py_contract_specification.h"
#include "python_interop/py_security.h"
#include <memory>
#include <pybind11/detail/common.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
using bts::md::py_interop::MdBridge;
using bts::md::py_interop::MdProvider;
using bts::md::py_interop::PyBook;
using bts::md::py_interop::PyBookLevel;
using bts::md::py_interop::PyBookSide;
using bts::md::py_interop::PyContractPriceInfo;
using bts::md::py_interop::PyContractQuantityInfo;
using bts::md::py_interop::PyContractSpecification;
using bts::md::py_interop::PyFuture;
using bts::md::py_interop::PyMdProvider;
using bts::md::py_interop::PyOption;
using bts::md::py_interop::PySecurity;
using bts::md::py_interop::PySpot;
using bts::md::py_interop::PyTrade;
using python_execution::ExecutionGateway;
using python_execution::ExecutionGatewayBridge;
using python_execution::PyCancelOrderRequest;
using python_execution::PyCancelReplaceOrderRequest;
using python_execution::PyCreateOrderRequest;
using python_execution::PyExecutionGateway;
using python_execution::PyExecutionReport;
using python_execution::PyPositionInfo;
using std::shared_ptr;
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
namespace py = pybind11;
#define _STR(x) #x
// In general, we bind c++ methods to python methods with the same name verbatim.
// The DEF macro avoids typos in the python method name for this common case.
#define DEF(type, member) def(_STR(member), &type::member)
#define DEF_ARGS(type, member, ...) \
def(_STR(member), &type::member, __VA_ARGS__)
#define EXPLICIT_DEF(type, member, member_type) \
def(_STR(member), (member_type) & type::member)
// Defines get/set methods for a pseudo property
#define PROP(type, name) \
def(_STR(name), &type::name) \
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
// Define python module with c++ classes.
PYBIND11_MODULE(python_interop_module, m) {
// price info
py::class_<PyContractPriceInfo>(m, "PyContractPriceInfo")
.def(py::init<>())
.PROP(PyContractPriceInfo, tick_unit)
.PROP(PyContractPriceInfo, tick_base)
.PROP(PyContractPriceInfo, multiplier)
.PROP(PyContractPriceInfo, cash_per_point)
.PROP(PyContractPriceInfo, max_price)
.def("__str__", &PyContractPriceInfo::to_string);
py::bind_vector<std::vector<PyContractPriceInfo>>(
m, "PyContractPriceInfoList");
// quantity info
py::class_<PyContractQuantityInfo>(m, "PyContractQuantityInfo")
.def(py::init<>())
.PROP(PyContractQuantityInfo, min_value)
.PROP(PyContractQuantityInfo, max_value)
.PROP(PyContractQuantityInfo, tick_unit)
.PROP(PyContractQuantityInfo, tick_base)
.PROP(PyContractQuantityInfo, multiplier)
.PROP(PyContractQuantityInfo, max_price)
.def("__str__", &PyContractQuantityInfo::to_string);
py::bind_vector<std::vector<PyContractQuantityInfo>>(
m, "PyContractQuantityInfoList");
// contract spec
py::class_<PyContractSpecification>(m, "PyContractSpecification")
.def(py::init<>())
.PROP(PyContractSpecification, price_infos)
.PROP(PyContractSpecification, quantity_infos)
.PROP(PyContractSpecification, notional_quantity_infos)
.def("__str__", &PyContractSpecification::to_string);
// security
py::class_<PySpot>(m, "PySpot")
.def(py::init<>())
.def("__str__", &PySpot::to_string);
py::class_<PyFuture>(m, "PyFuture")
.def(py::init<>())
.PROP(PyFuture, expiration)
.def("__str__", &PyFuture::to_string);
py::class_<PyOption>(m, "PyOption")
.def(py::init<>())
.PROP(PyOption, expiration)
.PROP(PyOption, is_call)
.PROP(PyOption, strike)
.def("__str__", &PyOption::to_string);
py::class_<PySecurity, shared_ptr<PySecurity>>(m, "PySecurity")
.def(py::init<>())
.PROP(PySecurity, id)
.PROP(PySecurity, exchange_id)
.PROP(PySecurity, venue)
.PROP(PySecurity, name)
.PROP(PySecurity, currency)
.PROP(PySecurity, group)
.PROP(PySecurity, asset)
.PROP(PySecurity, contract_spec)
.PROP(PySecurity, spot)
.PROP(PySecurity, future)
.PROP(PySecurity, option)
.def("__str__", &PySecurity::to_string);
py::bind_vector<std::vector<shared_ptr<PySecurity>>>(m, "PySecurityList");
//////////////////////////////////////////////////
// Md
//////////////////////////////////////////////////
using BookSideGetter = PyBookSide &(PyBook::*)();
using BookLevelGetter = PyBookLevel (PyBookSide::*)(int) const;
// book
py::class_<PyBookLevel>(m, "PyBookLevel")
.def(py::init<>())
.PROP(PyBookLevel, price)
.PROP(PyBookLevel, quantity);
py::class_<PyBookSide>(m, "PyBookSide")
.def(py::init<>())
.EXPLICIT_DEF(PyBookSide, get_level, BookLevelGetter)
.DEF(PyBookSide, set_level)
.PROP(PyBookSide, is_ascending)
.def("__len__", &PyBookSide::size)
.def("__str__", &PyBookSide::to_string);
py::class_<PyBook, shared_ptr<PyBook>>(m, "PyBook")
.def(py::init<>())
.PROP(PyBook, venue)
.PROP(PyBook, security_type)
.PROP(PyBook, exchange_security_id)
.EXPLICIT_DEF(PyBook, bids, BookSideGetter)
.DEF(PyBook, set_bids)
.EXPLICIT_DEF(PyBook, asks, BookSideGetter)
.DEF(PyBook, set_asks)
.def(
"set_data", &PyBook::set_data, py::arg("exch_sec_id"),
py::arg("venue"), py::arg("sec_type"), py::arg("bids"),
py::arg("asks"))
.def("__len__", &PyBook::size)
.def("__str__", &PyBook::to_string);
py::bind_vector<std::vector<shared_ptr<PyBook>>>(m, "PyBookList");
// trade
py::class_<PyTrade, shared_ptr<PyTrade>>(m, "PyTrade")
.def(py::init<>())
.PROP(PyTrade, venue)
.PROP(PyTrade, security_type)
.PROP(PyTrade, exchange_security_id)
.PROP(PyTrade, price)
.PROP(PyTrade, quantity)
.PROP(PyTrade, aggressor_side)
.PROP(PyTrade, timestamp);
py::bind_vector<std::vector<shared_ptr<PyTrade>>>(m, "PyTradeList");
// md bridge
py::class_<MdBridge>(m, "MdBridge")
.DEF_ARGS(MdBridge, on_securities, py::arg("securities"))
.DEF_ARGS(MdBridge, on_books, py::arg("books"))
.DEF_ARGS(MdBridge, on_trades, py::arg("trades"));
// md provider
py::class_<MdProvider, PyMdProvider>(m, "MdProvider")
.def(py::init<>())
.DEF_ARGS(MdProvider, start, py::arg("bridge"))
.DEF(MdProvider, stop);
//////////////////////////////////////////////////
// Execution
//////////////////////////////////////////////////
py::class_<PyCreateOrderRequest, shared_ptr<PyCreateOrderRequest>>(
m, "PyCreateOrderRequest")
.def(py::init<>())
.PROP(PyCreateOrderRequest, key)
.PROP(PyCreateOrderRequest, exchange_security_id)
.PROP(PyCreateOrderRequest, venue)
.PROP(PyCreateOrderRequest, is_buy)
.PROP(PyCreateOrderRequest, price)
.PROP(PyCreateOrderRequest, quantity)
.PROP(PyCreateOrderRequest, order_type)
.PROP(PyCreateOrderRequest, time_in_force)
.def("__str__", &PyCreateOrderRequest::to_string);
py::class_<PyCancelOrderRequest, shared_ptr<PyCancelOrderRequest>>(
m, "PyCancelOrderRequest")
.def(py::init<>())
.PROP(PyCancelOrderRequest, key)
.PROP(PyCancelOrderRequest, exchange_order_id)
.PROP(PyCancelOrderRequest, exchange_security_id)
.PROP(PyCancelOrderRequest, venue)
.PROP(PyCancelOrderRequest, is_buy)
.def("__str__", &PyCancelOrderRequest::to_string);
py::class_<
PyCancelReplaceOrderRequest, shared_ptr<PyCancelReplaceOrderRequest>>(
m, "PyCancelReplaceOrderRequest")
.def(py::init<>())
.PROP(PyCancelReplaceOrderRequest, key)
.PROP(PyCancelReplaceOrderRequest, exchange_order_id)
.PROP(PyCancelReplaceOrderRequest, exchange_security_id)
.PROP(PyCancelReplaceOrderRequest, venue)
.PROP(PyCancelReplaceOrderRequest, is_buy)
.PROP(PyCancelReplaceOrderRequest, price)
.PROP(PyCancelReplaceOrderRequest, quantity)
.def("__str__", &PyCancelReplaceOrderRequest::to_string);
py::class_<PyExecutionReport, shared_ptr<PyExecutionReport>>(
m, "PyExecutionReport")
.def(py::init<>())
.PROP(PyExecutionReport, key)
.PROP(PyExecutionReport, account)
.PROP(PyExecutionReport, exchange_order_id)
.PROP(PyExecutionReport, exchange_security_id)
.PROP(PyExecutionReport, order_status)
.PROP(PyExecutionReport, time_in_force)
.PROP(PyExecutionReport, exec_type)
.PROP(PyExecutionReport, venue)
.PROP(PyExecutionReport, security_type)
.PROP(PyExecutionReport, is_buy)
.PROP(PyExecutionReport, price)
.PROP(PyExecutionReport, quantity)
.PROP(PyExecutionReport, filled_quantity)
.PROP(PyExecutionReport, last_filled_quantity)
.PROP(PyExecutionReport, last_filled_price)
.PROP(PyExecutionReport, avg_filled_price)
.PROP(PyExecutionReport, order_type)
.PROP(PyExecutionReport, transact_time)
.def(
"set_data", &PyExecutionReport::set_data, py::arg("key"),
py::arg("account"), py::arg("exchange_order_id"),
py::arg("exchange_security_id"), py::arg("venue"),
py::arg("security_type"), py::arg("order_type"),
py::arg("order_status"), py::arg("time_in_force"),
py::arg("exec_type"), py::arg("is_buy"), py::arg("price"),
py::arg("quantity"), py::arg("filled_quantity"),
py::arg("last_filled_quantity"), py::arg("last_filled_price"),
py::arg("avg_filled_price"), py::arg("transact_id"),
py::arg("transact_time"))
.def("__str__", &PyExecutionReport::to_string);
py::class_<PyPositionInfo, shared_ptr<PyPositionInfo>>(m, "PyPositionInfo")
.def(py::init<>())
.PROP(PyPositionInfo, account)
.PROP(PyPositionInfo, exchange_security_id)
.PROP(PyPositionInfo, venue)
.PROP(PyPositionInfo, security_type)
.PROP(PyPositionInfo, long_quantity)
.PROP(PyPositionInfo, long_avg_cost)
.PROP(PyPositionInfo, short_quantity)
.PROP(PyPositionInfo, short_avg_cost)
.PROP(PyPositionInfo, transact_time)
.def("__str__", &PyPositionInfo::to_string);
py::class_<ExecutionGatewayBridge>(m, "ExecutionGatewayBridge")
.def(py::init<>())
.DEF(ExecutionGatewayBridge, on_execution_report)
.DEF(ExecutionGatewayBridge, on_position_info)
.DEF(ExecutionGatewayBridge, on_is_ready);
py::class_<ExecutionGateway, PyExecutionGateway>(m, "ExecutionGateway")
.def(py::init<>())
.DEF_ARGS(ExecutionGateway, start, py::arg("bridge"))
.DEF_ARGS(ExecutionGateway, create_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_replace_order, py::arg("req"))
.DEF(ExecutionGateway, stop);
}
@@ -0,0 +1,50 @@
#include "python_interpreter.h"
#include "spark/logging/logging.h"
#include "spark/utils/string_utils.h"
#include <csignal>
#include <mutex>
#include <pybind11/embed.h>
#include <pybind11/gil.h>
namespace python_interop {
using bts::system::string_utils::snake_to_pascal_case;
using bts::system::string_utils::split;
std::mutex PythonInterpreter::mx;
std::optional<pybind11::scoped_interpreter> PythonInterpreter::interpreter;
void PythonInterpreter::init_interpreter() {
if (interpreter.has_value()) {
return;
}
static std::mutex local_mx;
std::lock_guard<std::mutex> lock{local_mx};
if (interpreter.has_value()) {
return;
}
LOG(Gen, bts::Info, "Creating global py interpreter");
// Don't let python handle the signals (otherwise it'll hang on ctrl+c)
auto orig_signal_handler = std::signal(SIGINT, SIG_DFL);
interpreter.emplace(false);
std::signal(SIGINT, orig_signal_handler);
}
bool PythonInterpreter::parse_target(
const std::string &target,
std::string *module_name,
std::string *type_name) {
auto parts = split(target, ".");
if (parts.empty()) {
return false;
}
if (parts.size() == 1) {
snake_to_pascal_case(parts.emplace_back(parts[0]));
}
*module_name = parts[0];
*type_name = parts[1];
return !module_name->empty() && !type_name->empty();
}
} // namespace python_interop
@@ -0,0 +1,125 @@
#pragma once
#include "spark/utils/bts_assert.h"
#include "spark/utils/string_utils.h"
#include <mutex>
#include <pybind11/embed.h>
#include <pybind11/gil.h>
#include <pybind11/pybind11.h>
#include <string>
namespace python_interop {
class PythonInterpreter {
static std::mutex mx;
static std::optional<pybind11::scoped_interpreter> interpreter;
static void init_interpreter();
public:
/**
* Parses module and type name from a \p target arg. The following formats are supported:
* 1) <module_name>.<type_name>
* 2) <module_name>
* In this case, the type name defaults to the PascalCase equivalent of the module name.
* For example, the module "foo_bar" would have type name "FooBar".
*/
static bool parse_target(
const std::string &target,
std::string *module_name,
std::string *type_name);
static void exec(std::function<void()> fn) {
init_interpreter();
std::lock_guard<std::mutex> lock{mx};
fn();
}
template <typename T>
static T exec_t(std::function<T()> fn) {
init_interpreter();
std::lock_guard<std::mutex> lock{mx};
pybind11::gil_scoped_acquire gil;
T rv = fn();
pybind11::gil_scoped_release gil_rel;
return rv;
}
template <typename... Args>
static pybind11::object create_object(
const std::vector<std::string> &module_paths,
const std::string &target_name,
Args &&...args) {
std::string target_module_name;
std::string target_type_name;
bts_assert(
parse_target(target_name, &target_module_name, &target_type_name));
return create_object(
module_paths, target_module_name, target_type_name,
std::forward<Args>(args)...);
}
template <typename... Args>
static pybind11::object create_object(
const std::vector<std::string> &module_paths,
const std::string &target_type_name,
const std::string &target_module_name,
Args &&...args) {
namespace py = pybind11;
return exec_t<py::object>([&]() {
// Prepare module path
py::module sys = py::module::import("sys");
for (const auto &module_path : module_paths) {
sys.attr("path").attr("append")(module_path.c_str());
}
// Import target module
py::module target_module =
py::module::import(target_module_name.c_str());
// Instantiate target
return target_module.attr(target_type_name.c_str())(
std::forward<Args>(args)...);
});
}
template <typename... Args>
static pybind11::object create_object_with_init(
const std::vector<std::string> &module_paths,
const std::string &target_name,
std::function<void(pybind11::object &)> init_fn,
Args &&...args) {
std::string target_module_name;
std::string target_type_name;
bts_assert(
parse_target(target_name, &target_module_name, &target_type_name));
return create_object_with_init(
module_paths, target_module_name, target_type_name, init_fn,
std::forward<Args>(args)...);
}
template <typename... Args>
static pybind11::object create_object_with_init(
const std::vector<std::string> &module_paths,
const std::string &target_type_name,
const std::string &target_module_name,
std::function<void(pybind11::object &)> init_fn,
Args &&...args) {
namespace py = pybind11;
return exec_t<py::object>([&]() {
// Prepare module path
py::module sys = py::module::import("sys");
for (const auto &module_path : module_paths) {
sys.attr("path").attr("append")(module_path.c_str());
}
// Import target module
py::module target_module =
py::module::import(target_module_name.c_str());
// Instantiate target
py::object obj = target_module.attr(target_type_name.c_str())(
std::forward<Args>(args)...);
init_fn(obj);
return obj;
});
}
};
} // namespace python_interop