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

+19
View File
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.23)
project(python_interop)
include(${CMAKE_SOURCE_DIR}/cmake/InitLibrary.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/InitPythonModule.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/InitPythonPlugin.cmake)
# Find Python from the virtual environment
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)
# Shared lib (for c++ to link)
init_library(SHARED)
target_include_directories(${PROJECT_NAME} PUBLIC ${Python3_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PUBLIC ${Python3_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC spark pybind11::embed
${Python3_LIBRARIES})
# CPython module (for .py files to import)
init_python_module()
# Py plugin (for sparkd to load at runtime)
init_python_plugin(PLUGIN_NAME python_interop_plugins)
Whitespace-only changes.
+160
View File
@@ -0,0 +1,160 @@
<!-- Links -->
[pybind11]: https://github.com/pybind/pybind11
[pybind11-stubgen]: https://github.com/sizmailov/pybind11-stubgen
[lib_spark]: ../lib_spark/README.md
# lib_python_interop
This library binds c++ types to python market data and execution plugins which can be loaded at runtime.
## **Dependencies**
* **[Pybind11][pybind11]**
* **[Pybind11][pybind11-stubgen]**
* **[lib_spark][lib_spark]**
## Notes
* Because this is *python*, market data enums (SecurityType, Venue, Side, etc.) are represented as strings
* Parsing is **NOT** case sensitive (though it is conventional to use `UPPER_CASE` strings)
* Type name prefixes (SECURITY_TYPE_, VENUE_, SIDE_ etc.) are **NOT** required
* ***Having said that,*** we recommend UPPER_CASE strings without the type prefix. Here are a few examples:
| Enum | Examples |
|------|---------|
| **SecurityType** | "SPOT", "FUTURE", "OPTION" |
| **Venue** | "BYBIT", "GATEIO", "DERIBIT", "COINBASE" |
| **Side** | "BUY", "SELL" |
| **OrderType** | "LIMIT", "MARKET" |
| **OrderStatus** | "NEW", "PARTIALLY_FILLED", "FILLED", "CANCELED" |
| **SecurityType** | "SPOT", "FUTURE", "OPTION" |
* All .py files in the `python_interop_plugins` dir are copied to a special plugins directory
at build time, so they may be loaded at runtime.
* Type hints for the c++ defined module, `python_interop_module`, should work as long sparkd-debug-clang is built.
> *We use [pybind11-stubgen] to generate stubs as part of the build*
## Examples
### Md Provider Plugin
```python
import time
import python_md_module as pymd
class MdProviderExample(pymd.MdProvider):
def __init__(self):
super(MdProviderExample, self).__init__()
pass
def start(self, sink: pymd.MdSink):
print("Starting MdProviderExample")
# An actual implementation would subscribe to market data here.
# This is just an example to show you how to pass market data
# back to the c++ Spark api.
def add_dummy_security():
# This example populates a dummy security for demonstrative purposes.
# Of course, an actual implementation would populate the security list
# with data received from the exchange (or some other provider).
sec = pymd.PySecurity()
sec.set_venue("GLBX")
sec.set_name("ESH8")
sec.set_asset("ES")
fut = pymd.PyFuture()
fut.set_expiration("2025-03-28")
sec.set_future(fut)
# Note: the example only adds 1 sec, but batch adds are supported.
sec_list = pymd.PySecurityList()
sec_list.append(sec)
sink.on_securities(sec_list)
def add_dummy_trade():
trade = pymd.PyTrade()
trade.set_venue("GLBX")
trade.set_security_type("FUTURE")
trade.set_exchange_security_id("ESH8")
trade.set_price(5684.25)
trade.set_quantity(1)
trade.set_aggressor_side("BUY")
trade.set_timestamp(time.time_ns())
add_dummy_security()
add_dummy_trade()
def stop(self):
print("Stopping MdProviderExample")
```
### Execution Gateway Plugin
```python
import json
import logging
import threading
from typing import Optional
import python_execution_module as pyexe
from py_execution_utils import configure_logger, run_websocket
class ExecutionGatewayExample(pyexe.ExecutionGateway):
def __init__(self):
super(ExecutionGatewayExample, self).__init__()
self.bridge: Optional[pyexe.ExecutionGatewayBridge] = None
self.stop_event = threading.Event()
# Logs to file /home/bts/sparkdata/data/logging.sparkd/<name>_log
# (name is 'py_execution_example' in this case).
self.logger = configure_logger(logging.getLogger("py_execution_example"))
def start_impl(self, bridge: pyexe.ExecutionGatewayBridge):
def on_message(msg: str):
# This example assumes msg has a particular structure. Obviously, each exchange is
# different, so this isn't a one-size-fits-all solution.
data = json.loads(msg)
event = data.get("e")
if event == "executionReport":
upd = json.loads(str(msg))
er = pyexe.PyExecutionReport()
# TODO: Resolve exch key to bts key
# er.set_key(bts_key)
er.set_exchange_security_id(upd["s"])
er.set_security_type("SPOT")
er.set_is_buy(upd["S"] == "BUY")
er.set_price(float(upd["p"]))
er.set_quantity(float(upd["q"]))
er.set_last_filled_price(upd["L"])
er.set_last_filled_quantity(upd["l"])
# ...
# TODO: Fill in remaining fields
bridge.on_execution_report(er)
else:
self.logger.error("Unknown event %s. Full msg %s" % (event, msg))
run_websocket(
"wss://some_exch.com/ws", self.stop_event, self.logger, on_message
)
def start(self, bridge: pyexe.ExecutionGatewayBridge):
# Connect to the exchange, subscribe to order updates, etc. Don't block this thread.
# Spawn new thread(s) for long running operations websocket msg loops, ping loops, etc.
self.logger.info("Starting ExecutionGatewayExample")
self.bridge = bridge
self.ws_thread = threading.Thread(target=lambda: self.start_impl(bridge))
self.ws_thread.start()
def stop(self):
self.logger.info("Stopping ExecutionGatewayExample")
self.stop_event.set()
def create_order(self, req: pyexe.PyCreateOrderRequest):
self.logger.info("create_order. %s" % str(req))
# TODO: Send REST or ws request
def cancel_replace_order(self, req: pyexe.PyCancelReplaceOrderRequest):
self.logger.info("cancel_replace_order. %s" % str(req))
# TODO: Send REST or ws request
def cancel_order(self, req: pyexe.PyCancelOrderRequest):
self.logger.info("cancel_order. %s" % str(req))
# TODO: Send REST or ws request
```
+20
View File
@@ -0,0 +1,20 @@
{
"include": [
".",
],
"exclude": [
"**/__pycache__"
],
"reportMissingImports": true,
"reportMissingTypeStubs": false,
"stubPath": "../../build/sparkd-debug-clang/src/lib_python_interop/stubs",
"extraPaths": [
"../lib_python_interop/python_interop_plugins",
"../../build/sparkd-debug-clang/src/lib_python_interop/stubs",
],
"pythonVersion": "3.10",
"pythonPlatform": "Linux",
"typeCheckingMode": "basic",
"venvPath": "../../",
"venv": ".venv"
}
@@ -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
@@ -0,0 +1,313 @@
import json
import time
from typing import Optional
import uuid
from pybit.unified_trading import WebSocket, WebSocketTrading
from py_execution_utils import load_gateway_config
from spark_core import (
as_bool,
configure_logger,
format_price,
format_quantity,
parse_float,
StructuredLog as SLog,
)
import logging
from python_interop_module import (
ExecutionGateway,
ExecutionGatewayBridge,
PyCreateOrderRequest,
PyCancelReplaceOrderRequest,
PyCancelOrderRequest,
PyExecutionReport,
PySecurity,
PySecurityList,
)
import threading
class OrderData:
def __init__(self, bts_id: int, exch_id: str, price: float, quantity: float):
self.bts_id = bts_id
self.exch_id = exch_id
self.price = price
self.quantity = quantity
self.filled_quantity: float = 0
self.last_filled_quantity: float = 0
self.avg_filled_price: float = 0
self.last_filled_price: float = 0
self.exec_type = "PENDING_NEW"
def on_update(
self,
price: float,
quantity: float,
filled_quantity: float,
avg_filled_price: float,
status: str,
):
self.price = price
self.filled_quantity = 0
self.quantity = quantity
lfq = filled_quantity - self.filled_quantity
if lfq > 0:
if self.filled_quantity == 0:
self.last_filled_quantity = filled_quantity
self.last_filled_price = avg_filled_price
else:
price_diff = avg_filled_price - self.avg_filled_price
lfq_weight = filled_quantity / (self.filled_quantity + filled_quantity)
lfp = self.avg_filled_price + (price_diff / lfq_weight)
self.last_filled_price = lfp
self.last_filled_quantity = lfq
self.filled_quantity = filled_quantity
self.avg_filled_price = avg_filled_price
match status:
case "Filled":
self.exec_type = "FILL"
case "Canceled":
self.exec_type = "CANCELED"
case "Rejected":
self.exec_type = "REJECTED"
case _:
self.exec_type = "NEW" if filled_quantity == 0 else "PARTIAL_FILL"
class BybitExecutionGateway(ExecutionGateway):
def __init__(self, name: str):
super(BybitExecutionGateway, self).__init__()
self.name = name
self.logger = configure_logger(logging.getLogger(name), "OrderMan")
self.logger.info("[bybit_gw_ctr.json]%s" % SLog(name=name))
cfg = load_gateway_config(name)
self.account: str = cfg["account"]
self.api_key: str = cfg["api_key"]
self.secret_key: str = cfg["secret_key"]
self.use_testnet: bool = cfg.get("use_testnet", True)
self.use_trace_logging: bool = cfg.get("use_trace_logging", False)
self.stop_event = threading.Event()
self.stream_cli: Optional[WebSocket] = None
self.trade_cli: Optional[WebSocketTrading] = None
# bts id -> exch id
self.orders: dict[int, OrderData] = {}
self.securities: dict[str, PySecurity] = {}
def _get_order_data(self, exch_id: str):
for _, v in self.orders.items():
if v.exch_id == exch_id:
return v
return None
def _on_order_ack(self, typ, msg: object):
self.logger.info(
'[bybit_gw_ord_ack.json]{ "typ": "%s", "msg": %s}' % (typ, msg)
)
def _on_abort(
self,
key: int,
req_type: str,
rsn: str,
):
self.logger.error(
'[bybit_gw_abort.json]{ "key": %d, "typ": "%s", "rsn": "%s", "ts": %d }'
% (key, req_type, rsn, time.time_ns())
)
def start_impl(self, bridge: ExecutionGatewayBridge):
def on_order_update(msg: dict):
def on_error(err: str):
self.logger.error(
'[bybit_gw_ord_upd_err.json]{ "err": "%s", "ts": %d }'
% (err, time.time_ns())
)
self.logger.info("[bybit_gw_ord_upd.json]%s" % msg)
try:
upds: list[dict] = msg["data"]
for d in upds:
er = PyExecutionReport()
exch_id: str = d["orderLinkId"]
ord_data: Optional[OrderData] = self._get_order_data(exch_id)
if not ord_data:
on_error("Did not resolve exch order id %s" % exch_id)
return
total_filled_qty = parse_float(d["cumExecQty"])
avg_filled_price = parse_float(d["avgPrice"])
status: str = d["orderStatus"]
# brits can't spell
if status == "Cancelled":
status = "Canceled"
exec_type = ord_data.exec_type
match ord_data.exec_type:
case "PARTIAL_FILL":
exec_type = "PARTIALLY_FILLED"
case _:
pass
px = parse_float(d["price"])
qty = parse_float(d["qty"])
ord_data.on_update(
px, qty, total_filled_qty, avg_filled_price, status
)
transact_time = int(d["updatedTime"]) * 1_000_000
er.set_data(
key=ord_data.bts_id,
exchange_order_id=d["orderLinkId"],
venue="BYBIT",
security_type="FUTURE",
exchange_security_id=d["symbol"],
account=self.account,
order_type=d["orderType"],
is_buy=d["side"] == "Buy",
price=px,
quantity=qty,
time_in_force=d["timeInForce"],
exec_type=exec_type,
order_status=status,
last_filled_price=ord_data.last_filled_price,
last_filled_quantity=ord_data.last_filled_quantity,
filled_quantity=total_filled_qty,
avg_filled_price=avg_filled_price,
# There is no transact_id in Bybit, so we use updatedTime
transact_id=str(transact_time),
transact_time=transact_time,
)
self.logger.info("[bybit_gw_exe_rep_out.json]%s" % er)
bridge.on_execution_report(er)
except Exception as e:
self.logger.error(
'[bybit_gw_ord_upd_err.json]{ "err": %s }' % e, exc_info=True
)
def on_exe_report(msg):
self.logger.info("[bybit_gw_exe_rep.json]%s" % msg)
# Stream client (receives order updates)
params = {
"api_key": self.api_key,
"api_secret": self.secret_key,
"testnet": self.use_testnet,
"channel_type": "private",
"trace_logging": False,
}
self.logger.info("[bybit_gw_stream_sub.json]%s" % json.dumps(params))
self.stream_cli = WebSocket(**params)
self.stream_cli.order_stream(on_order_update)
self.stream_cli.execution_stream(on_exe_report)
# Trade client (sends orders)
params = {
"api_key": self.api_key,
"api_secret": self.secret_key,
"testnet": True,
}
self.logger.info("[bybit_gw_start_trd_cli.json]%s" % json.dumps(params))
self.trade_cli = WebSocketTrading(**params)
params = {
"category": "linear",
}
self.bridge.on_is_ready(True)
def start(self, bridge: ExecutionGatewayBridge):
def start_wrapper():
try:
self.start_impl(bridge)
except Exception as e:
self.logger.error(
'[bybit_gw_start_err.json]{ "err": "%s" }' % str(e), exc_info=True
)
self.logger.info("[bybit_gw_start.json]%s" % json.dumps({"ts": time.time_ns()}))
self.bridge = bridge
threading.Thread(
target=start_wrapper,
name=f"{self.name}_main",
daemon=True,
).start()
def stop(self):
self.logger.info("[bybit_gw_stop.json]%s" % json.dumps({"ts": time.time_ns()}))
self.stop_event.set()
def create_order(self, req: PyCreateOrderRequest):
self.logger.info("[bybit_gw_crt_ord.json]%s" % str(req))
if not self.stream_cli or not self.trade_cli or not self.bridge:
self._on_abort(
req.key(),
"crt",
"WebSocket not initialized (stream_cli: %s, trade_cli: %s, bridge: %s)"
% as_bool(self.stream_cli, self.trade_cli, self.bridge),
)
return
exch_ord_id = uuid.uuid1().hex
bts_id = req.key()
px = req.price()
qty = req.quantity()
self.orders[req.key()] = OrderData(
bts_id=bts_id, exch_id=exch_ord_id, price=px, quantity=qty
)
sec_name = req.exchange_security_id()
sec = self.securities.get(sec_name)
if not sec:
return
params = {
"orderLinkId": exch_ord_id,
"category": "linear",
"symbol": sec_name,
"side": "Buy" if req.is_buy() else "Sell",
"orderType": req.order_type().title(),
"price": format_price(sec, px),
"qty": format_quantity(sec, qty),
}
self.logger.info("[bybit_gw_crt_ord_send.json]%s" % json.dumps(params))
self.trade_cli.place_order(lambda msg: self._on_order_ack("crt", msg), **params)
def cancel_replace_order(self, req: PyCancelReplaceOrderRequest):
self.logger.info("[bybit_gw_cxlrep_ord.json]%s" % req)
if not self.stream_cli or not self.trade_cli:
self._on_abort(req.key(), "cxlrep", "WebSocket client not created.")
return
bts_id = req.key()
ord_data = self.orders.get(bts_id)
if not ord_data:
self._on_abort(req.key(), "cxlrep", "Did not resolve exch order id")
return
sec_name = req.exchange_security_id()
sec = self.securities.get(sec_name)
if not sec:
return
params = {
"orderLinkId": ord_data.exch_id,
"category": "linear",
"symbol": req.exchange_security_id(),
"price": format_price(sec, req.price()),
"qty": format_quantity(sec, req.quantity()),
}
self.trade_cli.amend_order(
lambda msg: self._on_order_ack("cxlrep", msg), **params
)
def cancel_order(self, req: PyCancelOrderRequest):
self.logger.info("[bybit_gw_cxl_ord.json]%s" % str(req))
if not self.stream_cli or not self.trade_cli:
self._on_abort(req.key(), "cxl", "WebSocket client not created.")
return
bts_id = req.key()
ord_data = self.orders.get(bts_id)
if not ord_data:
self._on_abort(req.key(), "cxl", "Did not resolve exch order id")
return
params = {
"orderLinkId": ord_data.exch_id,
"category": "linear",
"symbol": req.exchange_security_id(),
}
self.trade_cli.cancel_order(
lambda msg: self._on_order_ack("cxl", msg), **params
)
def on_securities(self, securities: PySecurityList):
self.logger.info(
"[bybit_gw_sec.json][%s]" % ", ".join([str(sec) for sec in securities])
)
self.securities = {sec.name(): sec for sec in securities}
@@ -0,0 +1,263 @@
import json
import numpy as np
import time
import logging
import sys
import threading
import signal
from typing import Optional
from pybit.unified_trading import WebSocket
from python_interop_module import (
MdBridge,
MdProvider,
PyBook,
PyBookList,
PySecurityList,
)
from bybit_conf import CONTRACTS
from spark_core import (
configure_logger,
get_contract_spec,
get_price_info,
get_quantity_info,
get_security,
)
from py_md_utils import PERP_EXP_STRING
import requests
logger = logging.getLogger("bybit_md_provider")
configure_logger(logger, "md")
def normalize_contract_name(contract: str) -> str:
if len(contract) > 4:
return contract[:-4] + "_" + contract[-4:]
return contract
def fetch_contract_specs() -> dict:
default_specs = {
"tick_unit": 1,
"tick_base": 10,
"cash_per_point": 0.1,
"qty_tick_unit": 1,
"qty_tick_base": 10000
}
specs_dict = {contract: default_specs for contract in CONTRACTS}
try:
url = "https://api.bybit.com/v5/market/instruments-info?category=linear"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
found_contracts = set()
for item in data.get("result", {}).get("list", []):
contract = item.get("symbol")
if contract in CONTRACTS:
price_tick = float(item.get("priceFilter", {}).get("tickSize", "0.1"))
lot_size = float(item.get("lotSizeFilter", {}).get("qtyStep", "0.001"))
tick_base = int(1 / price_tick) if price_tick != 0 else 10
if lot_size >= 1:
qty_tick_base = int(lot_size)
else:
qty_tick_base = int(1 / lot_size) if lot_size != 0 else 10000
specs_dict[contract] = {
"tick_unit": 1,
"tick_base": tick_base,
"cash_per_point": price_tick,
"qty_tick_unit": 1,
"qty_tick_base": qty_tick_base
}
found_contracts.add(contract)
for contract in CONTRACTS:
if contract not in found_contracts:
logger.warning(f"Contract {contract} not found in API response, using default specs")
return specs_dict
except Exception as e:
logger.error(f"Failed to fetch contract specs: {e}", exc_info=True)
return specs_dict
class BybitMdProvider(MdProvider):
def __init__(self):
super().__init__()
self.ws = None
self.ws_thread: Optional[threading.Thread] = None
self.bridge: Optional[MdBridge] = None
self.stop_event = threading.Event()
self._setup_signal_handlers()
def _setup_signal_handlers(self):
"""Set up signal handlers for SIGINT, SIGTERM, and SIGTSTP."""
def signal_handler(sig, frame):
logger.info(f"Received signal {sig}, stopping...")
self.stop()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGTSTP, signal_handler)
def start_impl(self, bridge: MdBridge):
self.bridge = bridge
logger.info("Starting BybitMdProvider")
contract_specs = fetch_contract_specs()
sec_list = PySecurityList()
for contract in CONTRACTS:
normalized_contract = normalize_contract_name(contract)
asset, quote = normalized_contract.split("_")
specs = contract_specs.get(contract, {})
try:
qty_info = get_quantity_info(
tick_unit=specs.get("qty_tick_unit", 1),
tick_base=specs.get("qty_tick_base", 10000)
)
except Exception as e:
logger.error(f"Failed to get quantity info for contract: {contract}, error: {e}")
continue
try:
spec = get_contract_spec(
price_info=get_price_info(
tick_unit=specs.get("tick_unit", 1),
tick_base=specs.get("tick_base", 10),
cash_per_point=specs.get("cash_per_point", 0.1)
),
qty_info=qty_info
)
except Exception as e:
logger.error(f"Failed to get contract spec for contract: {contract}, error: {e}")
continue
try:
sec = get_security(
venue="Bybit",
name=contract,
asset=asset,
group=asset,
currency=quote,
expiration=PERP_EXP_STRING,
option_kind=None,
strike=None,
spec=spec,
)
sec_list.append(sec)
logger.info(f"Created security: {contract} with specs: {specs}")
except Exception as e:
logger.error(f"Failed to create security for contract: {contract}, error: {e}")
continue
bridge.on_securities(sec_list)
logger.info("Sent securities list--bridge.on_securities called.")
try:
self.ws = WebSocket(
testnet=False,
channel_type="linear",
ping_interval=20,
)
logger.info("Connecting to Bybit WebSocket (mainnet, linear)")
for contract in CONTRACTS:
try:
self.ws.orderbook_stream(
symbol=contract,
depth=50,
callback=self._on_book_update
)
logger.info(f"Subscribed to orderbook.50.{contract}")
except Exception as e:
logger.error(f"Failed to subscribe to orderbook for {contract}, error: {e}")
logger.info("Subscribed to orderbook channels on Bybit unified trading WS.")
for t in threading.enumerate():
logger.debug(f"Thread (start): Name={t.name}, Alive={t.is_alive()}, Daemon={t.daemon}, Target={t._target}")
while not self.stop_event.is_set():
time.sleep(0.05)
except KeyboardInterrupt:
logger.info("KeyboardInterrupt received — shutting down.")
self.stop()
except Exception as e:
logger.error(f"Bybit WebSocket fatal error: {e}", exc_info=True)
self.stop()
def start(self, bridge: MdBridge):
def start_wrapper():
try:
self.start_impl(bridge)
except Exception as e:
logger.error(f"Error in BybitMdProvider start_impl: {e}", exc_info=True)
self.ws_thread = threading.Thread(target=start_wrapper)
self.ws_thread.start()
def _on_book_update(self, data: dict):
try:
topic = data.get("topic", "")
if not topic.startswith("orderbook.50."):
return
contract = topic.split(".")[-1]
payload = data.get("data", {})
raw_bids = [(float(x[0]), float(x[1])) for x in payload.get("b", [])]
raw_asks = [(float(x[0]), float(x[1])) for x in payload.get("a", [])]
# Sort and take top 5, padding with [float('nan'), float('nan')] if needed
sorted_bids = sorted(raw_bids, key=lambda x: -x[0])[:5]
sorted_bids += [(np.float64('nan'), np.float64('nan'))]
sorted_asks = sorted(raw_asks, key=lambda x: x[0])[:5]
sorted_asks += [(np.float64('nan'), np.float64('nan'))]
logger.debug(f"📘 BOOK DATA for {contract}: bids={sorted_bids} asks={sorted_asks}")
book = PyBook()
book.set_data(
contract,
"Bybit",
"FUTURE",
np.array(sorted_bids, dtype=np.float64),
np.array(sorted_asks, dtype=np.float64),
)
book_list = PyBookList()
book_list.append(book)
if self.bridge:
self.bridge.on_books(book_list)
logger.debug(f"✔ on_books called for {contract}")
else:
logger.warning("Bridge is None, cannot call on_books")
except Exception as e:
logger.error(f"Error processing book update for {contract}: {e}", exc_info=True)
def stop(self):
logger.info("Stopping BybitMdProvider...")
self.stop_event.set()
logger.debug("Threads before stopping WebSocket:")
for t in threading.enumerate():
logger.debug(f"Thread(stop): {t.name}, Alive: {t.is_alive()}, Daemon: {t.daemon}, Target={t._target}")
if self.ws:
try:
self.ws.exit()
logger.info("WebSocket exit called")
# Clean up pybit threads with longer timeout
for t in threading.enumerate():
if t.name in ['Thread-1 (<lambda>)', 'Thread-2 (_send_ping)'] and t.is_alive():
logger.info(f"Joining pybit thread: {t.name}")
t.join(timeout=1.0)
except Exception as e:
logger.error(f"Error while closing WebSocket: {e}", exc_info=True)
self.ws = None
if self.ws_thread:
self.ws_thread.join()
self.bridge = None
logger.info("✔ Stopped BybitMdProvider.")
logger.debug("Threads after stop:")
for t in threading.enumerate():
logger.debug(f"Thread(after stop): {t.name}, Alive: {t.is_alive()}, Daemon: {t.daemon}, Target={t._target}")
import os
os._exit(0)
@@ -0,0 +1,30 @@
# Gate.io WebSocket configuration
# WS_URL = "wss://fx-ws-testnet.gateio.ws/v4/ws/usdt" # Testnet url for perp futures
WS_URL = "wss://fx-ws.gateio.ws/v4/ws/usdt" # Mainnet url for perp futures
# API credentials
API_KEY = "e4a0c6b198549042b13faa7645640d60"
API_SECRET = "527c37877efc247bb0b6158cf3d5d18b84db720da894307f1e2ab88df3aaf0ee"
# Futures contract to subscribe to:
CONTRACTS = ['MELON_USDT', 'CAKE_USDT', 'ETH_USDT', 'KLAY_USDT', 'RON_USDT',
'MANA_USDT', 'BZZ_USDT', 'KEEP_USDT', 'OMG_USDT', 'GMX_USDT', 'ICP_USDT',
'LRC_USDT', 'MINA_USDT', 'ZIL_USDT', 'TURBO_USDT', 'OP_USDT', 'ZRX_USDT',
'VET_USDT', 'AR_USDT', 'IMX_USDT', 'HIGH_USDT', 'ARB_USDT', 'YFI_USDT',
'NEAR_USDT', 'OXY_USDT', 'WAVES_USDT', 'AMP_USDT', 'CELO_USDT', 'CRV_USDT',
'FIL_USDT', 'PEOPLE_USDT', 'IOTX_USDT', 'ATOM_USDT', 'NOT_USDT', 'HBAR_USDT',
'GMT_USDT', 'KAVA_USDT', 'WIF_USDT', 'UNI_USDT', 'GALA_USDT', 'AAVE_USDT',
'CRU_USDT', 'PEPE_USDT', 'TWT_USDT', 'IRIS_USDT', 'APE_USDT', 'BNB_USDT',
'ATLAS_USDT', 'BCHA_USDT', 'ETHW_USDT', 'DOT_USDT', 'TONCOIN_USDT', 'DOGS_USDT',
'MER_USDT', 'ADA_USDT', 'TRIBE_USDT', 'THETA_USDT', 'BTC_USDT', 'ORDI_USDT',
'ANKR_USDT', 'KIN_USDT', 'AMPL_USDT', 'TRX_USDT', 'SAND_USDT', 'SERO_USDT',
'LUNC_USDT', 'BAT_USDT', 'KSM_USDT', 'SOL_USDT', 'AXS_USDT', 'BCD_USDT',
'TIA_USDT', 'WOO_USDT', 'QTUM_USDT', 'OLDLUNA_USDT', 'AVAX_USDT', 'EOS_USDT',
'SNX_USDT', 'MKR_USDT', 'ZKS_USDT', 'XRP_USDT', 'STEP_USDT', 'COMP_USDT',
'UST_USDT', 'WSB_USDT', 'LINK_USDT', 'LUNA_USDT', 'ETC_USDT', 'JST_USDT',
'LON_USDT', 'PEARL_USDT', 'BTT_USDT', 'XLM_USDT', 'GRT_USDT', 'STX_USDT',
'NU_USDT', 'ALGO_USDT', 'ROSE_USDT', 'SHIB_USDT', 'BSV_USDT', 'ENJ_USDT',
'USTC_USDT', 'NEO_USDT', 'FLOW_USDT', 'RUNE_USDT', 'FIL6_USDT', 'APT_USDT',
'DOGE_USDT', 'CHZ_USDT', '1INCH_USDT', 'CSPR_USDT', 'XEC_USDT', 'ENS_USDT',
'IOST_USDT', 'LDO_USDT', 'XTZ_USDT']
@@ -0,0 +1,241 @@
import dotenv
import json
import logging
import math
import sys
import traceback
from typing import Any, Optional
import uuid
import websocket
from binance_client import BinanceClient
import python_interop_module as pyinter
BINANCE_API_KEY = "2RJ9H02ztFGWxeY2oRpnFYZNU8mki4V1yMvKrDU08HCiEhO4VLe2ePSUFqEYTNxc"
BINANCE_USDS_FUTURES_API_KEY = (
"b838c108cd2648e34a4f088a7fc635a9cbccf3c2faa6bcaabcb532ffacc087b0"
)
BINANCE_USDS_FUTURES_SECRET_KEY = (
"a68f0ab27403c42fa0306b6341b2ddd51d99ac42916f849404155449457d0492"
)
def to_bts_exec_type(exec_type: str, ord_status: str):
match exec_type:
case "TRADE_PREVENTION":
return "REJECTED"
case "NONE":
return "UNDEFINED"
case "TRADE":
return "FILL" if ord_status == "FILLED" else "PARTIAL_FILL"
case _:
return exec_type
def to_bts_order_status(ord_status: str):
match ord_status:
case "EXPIRED_IN_MATCH":
return "EXPIRED"
case _:
return ord_status
class DummyExecutionGateway(pyinter.ExecutionGateway):
def __init__(self):
super(DummyExecutionGateway, self).__init__()
self.bridge = None
self.client = None
self.start_th = None
self.ws = None
self.orders = {}
self.logger = logging.getLogger(__name__)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.formatter = logging.Formatter(
"%(asctime)s | %(levelname)7s | %(filename)s:%(lineno)s | %(message)s"
)
self.logger.addHandler(stdout_handler)
self.logger.addHandler(
logging.FileHandler(
"/home/bts/sparkdata/data/logging.python_execution/dummy_log"
)
)
self.logger.setLevel(logging.INFO)
env_path = "/workspaces/spark/tools/binance/binance.env"
self.logger.info("DummyExecutionGateway ctr. Loading env")
dotenv.load_dotenv(env_path, interpolate=False)
def _index_transaction(self, bts_key: int):
exch_key = uuid.uuid1().hex
self.orders[exch_key] = bts_key
return exch_key
def _on_user_data_msg_core(self, ws: websocket.WebSocket, msg: Any):
self.logger.info("Recv usr data: %s", str(msg))
if not msg:
self.logger.error("Recv empty usr data msg")
return
if not self.bridge:
self.logger.error("Cannot process usr data. Bridge not created")
return
upd = json.loads(str(msg))
if "e" in upd:
if upd["e"] == "executionReport":
# Binance spots update
bts_ord_id = self.orders.get(upd["c"])
is_old_ord = False
if not bts_ord_id:
bts_ord_id = self.orders.get(upd["C"])
if not bts_ord_id:
self.logger.warning(
"Received update with unknown order id: %s", msg
)
return
is_old_ord = True
er = pyinter.PyExecutionReport()
er.set_key(bts_ord_id)
er.set_exchange_security_id(upd["s"])
er.set_security_type("SPOT")
er.set_exec_type(to_bts_exec_type(upd["x"], upd["X"]))
er.set_is_buy(upd["S"] == "BUY")
er.set_price(float(upd["p"]))
er.set_quantity(float(upd["q"]))
er.set_last_filled_price(upd["L"])
er.set_last_filled_quantity(upd["l"])
total_filled_qty = float(upd["z"])
er.set_filled_quantity(total_filled_qty)
total_filled_notional = float(upd["Z"])
avg_filled_price = (
total_filled_notional / total_filled_qty
if total_filled_qty > 0
else math.nan
)
er.set_avg_filled_price(avg_filled_price)
er.set_order_status(to_bts_order_status(upd["X"]))
er.set_order_type(upd["o"])
match upd["X"]:
case "CANCELED":
if not is_old_ord:
del self.orders[bts_ord_id]
case "FILLED":
del self.orders[bts_ord_id]
case "REJECTED":
del self.orders[bts_ord_id]
self.bridge.on_execution_report(er)
elif upd["e"] == "ORDER_TRADE_UPDATE":
data = upd["o"]
# Binance Usds Futures order update
bts_ord_id = self.orders.get(data["c"])
is_old_ord = False
if not bts_ord_id:
bts_ord_id = self.orders.get(data["C"])
if not bts_ord_id:
self.logger.warning(
"Received dataate with unknown order id: %s", msg
)
return
is_old_ord = True
def parse_float(s: Optional[str], default_val=math.nan):
if s is not None:
try:
return float(s)
except Exception:
pass
return default_val
er = pyinter.PyExecutionReport()
er.set_key(bts_ord_id)
er.set_venue("BINANCE_USDS_FUTURES")
er.set_security_type("FUTURE")
er.set_time_in_force("GTC")
er.set_exchange_order_id(str(data.get("i") or 0))
# er.set_exchange_order_id(data["i"])
er.set_exchange_security_id(data["s"])
er.set_exec_type(to_bts_exec_type(data["x"], data["X"]))
er.set_is_buy(data["S"] == "BUY")
er.set_price(parse_float(data["p"]))
er.set_quantity(parse_float(data["q"]))
er.set_last_filled_price(parse_float(data.get("L")))
er.set_last_filled_quantity(parse_float(data["l"]))
er.set_filled_quantity(parse_float(data["z"]))
er.set_avg_filled_price(parse_float(data["ap"]))
er.set_order_status(to_bts_order_status(data["X"]))
er.set_order_type(data["o"])
match data["X"]:
case "CANCELED":
if not is_old_ord:
del self.orders[bts_ord_id]
case "FILLED":
del self.orders[bts_ord_id]
case "REJECTED":
del self.orders[bts_ord_id]
print("Firing this exe report: %s" % str(er), flush=True)
self.bridge.on_execution_report(er)
def _on_user_data_msg(self, ws: websocket.WebSocket, msg: Any):
try:
self._on_user_data_msg_core(ws, msg)
except Exception:
self.logger.error("on_user_data_msg. %s" % traceback.format_exc())
def _on_user_data_open(self, ws: websocket.WebSocket):
self.ws = ws
if self.bridge:
self.bridge.on_is_ready(True)
def _on_user_data_err(self, ws: websocket.WebSocket, err: str):
self.logger.error("on_user_data_err: %s", err)
def _on_user_data_close(self, ws: websocket.WebSocket, code: int, msg: str):
self.logger.warning("on_user_data_close: %s %s", str(code), msg)
if self.bridge:
self.bridge.on_is_ready(False)
def start(self, bridge: pyinter.ExecutionGatewayBridge):
print("DummyExecutionGateway.start")
self.bridge = bridge
self.client = BinanceClient(venue="BINANCE_USDS_FUTURES")
print("DummyExecutionGateway.start. Subscribing to binance user data")
self.client.subscribe_user_data(
on_message=self._on_user_data_msg,
on_open=self._on_user_data_open,
on_close=self._on_user_data_close,
)
print("DummyExecutionGateway.start. Subscribed to binance user data")
def cancel_order(self, req: pyinter.PyCancelOrderRequest):
if not self.client:
self.logger.error("Cannot cancel order. BinanceClient not created.")
return
self.logger.info("[cancel_order.json]%s", str(req))
try:
self.client.request(
"CANCEL_ORDER",
symbol=req.exchange_security_id(),
orig_client_order_id=req.exchange_order_id(),
new_client_order_id=self._index_transaction(req.key()),
)
except Exception as ex:
self.logger.error("Create order request threw. %s" % str(ex))
def create_order(self, req: pyinter.PyCreateOrderRequest):
if not self.client:
self.logger.error("Cannot create order. BinanceClient not created.")
return
print("hey bud")
self.logger.info("[create_order.json]%s", str(req))
side = "BUY" if req.is_buy() else "SELL"
try:
self.client.request(
op="CREATE_ORDER",
symbol=req.exchange_security_id(),
price=req.price(),
quantity=f"{req.quantity():.6f}",
side=side,
time_in_force="GTC",
type="LIMIT",
new_client_order_id=self._index_transaction(req.key()),
)
except Exception as ex:
self.logger.error("Create order request threw. %s" % str(ex))
@@ -0,0 +1,79 @@
from python_interop_module import (
MdBridge,
MdProvider,
PySecurityList,
PyBook,
PyBookList,
PyTradeList,
)
from spark_core import (
get_security,
get_contract_spec,
get_price_info,
get_quantity_info,
)
from py_md_utils import get_book_side, get_trade
class DummyMdProvider(MdProvider):
def __init__(self):
super(DummyMdProvider, self).__init__()
pass
def start(self, bridge: MdBridge):
# start subscriptions, bind callbacks etc.
def add_fake_securities():
sec_list = PySecurityList()
sec_list.append(
get_security(
venue="Glbx",
name="ESH8",
asset="ES",
group="ES",
currency="USD",
expiration="2026-03-28",
spec=get_contract_spec(
price_info=get_price_info(
tick_unit=25,
tick_base=1000,
cash_per_point=50,
),
qty_info=get_quantity_info(tick_unit=1, tick_base=1),
),
)
)
bridge.on_securities(sec_list)
def add_fake_books():
bids = get_book_side([(99.75, 8), (99.50, 11)], is_bid_side=True)
asks = get_book_side([(100.25, 3)], is_bid_side=False)
book = PyBook()
book.set_venue("Glbx")
book.set_security_type("FUTURE")
book.set_exchange_security_id("ESH8")
book.set_bids(bids)
book.set_asks(asks)
# pass book to sink
book_list = PyBookList()
book_list.append(book)
bridge.on_books(book_list)
def add_fake_trades():
trade = get_trade(
venue="Glbx",
security_type="FUTURE",
exchange_security_id="ESH8",
price=100,
quantity=2,
aggressor_side="Buy",
)
trade_list = PyTradeList()
trade_list.append(trade)
bridge.on_trades(trade_list)
add_fake_securities()
add_fake_books()
add_fake_trades()
def stop(self):
print("Stopping python DummyMdProvider")
@@ -0,0 +1,226 @@
import json
import numpy as np
import time
import logging
import sys
import threading
import requests
from websocket import create_connection, WebSocketConnectionClosedException
from typing import Optional, Any
from python_interop_module import (
MdBridge,
MdProvider,
PyBook,
PyBookList,
PySecurityList,
)
from config import WS_URL, CONTRACTS
from spark_core import (
configure_logger,
get_contract_spec,
get_price_info,
get_quantity_info,
get_security,
)
logger = logging.getLogger("gateio_md_provider")
configure_logger(logger, "md")
def fetch_contract_specs() -> dict:
"""Fetch specs for all contracts in one request."""
default_specs = {
"tick_unit": 1,
"tick_base": 10,
"cash_per_point": 0.1,
"qty_tick_unit": 1,
"qty_tick_base": 10000
}
specs_dict = {contract: default_specs for contract in CONTRACTS}
try:
url = "https://api.gateio.ws/api/v4/futures/usdt/contracts"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
for item in data:
contract = item.get("name")
if contract in CONTRACTS:
price_tick = float(item.get("price_tick", "0.1"))
contract_size = float(item.get("contract_size", "0.0001"))
tick_base = int(1 / price_tick) if price_tick != 0 else 10
qty_tick_base = int(1 / contract_size) if contract_size != 0 else 10000
specs_dict[contract] = {
"tick_unit": 1,
"tick_base": tick_base,
"cash_per_point": price_tick,
"qty_tick_unit": 1,
"qty_tick_base": qty_tick_base
}
return specs_dict
except Exception as e:
logger.error(f"Failed to fetch batch specs: {e}", exc_info=True)
return specs_dict
class GateIoMdProvider(MdProvider):
def __init__(self):
super().__init__()
self.ws = None
self.ws_thread: Optional[threading.Thread] = None
self.bridge: Optional[MdBridge] = None
self.stop_event = threading.Event()
self.ping_thread = None
def send_ping(self):
"""Send ping messages every 15 seconds to keep connection alive."""
while not self.stop_event.is_set():
try:
ping_msg = {
"time": int(time.time() * 1000),
"channel": "futures.ping",
"event": "",
"payload": []
}
self.ws.send(json.dumps(ping_msg))
logger.debug("Sent ping")
except Exception as e:
logger.error(f"Ping error: {e}", exc_info=True)
break
time.sleep(15)
def start_impl(self, bridge: MdBridge):
self.bridge = bridge
logger.info(f"Connecting to {WS_URL}")
self.ws = create_connection(WS_URL)
self.ws.settimeout(5)
logger.info("Connected.")
# Start ping thread
self.ping_thread = threading.Thread(target=self.send_ping, daemon=True)
self.ping_thread.start()
logger.info("Started ping thread")
# Fetch all contract specs at once
contract_specs = fetch_contract_specs()
# 1) Send security info
sec_list = PySecurityList()
for contract in CONTRACTS:
asset, quote = contract.split("_")
specs = contract_specs[contract]
spec = get_contract_spec(
price_info=get_price_info(
tick_unit=specs["tick_unit"],
tick_base=specs["tick_base"],
cash_per_point=specs["cash_per_point"]
),
qty_info=get_quantity_info(
tick_unit=specs["qty_tick_unit"],
tick_base=specs["qty_tick_base"]
)
)
sec = get_security(
venue="GateIO",
name=contract,
asset=asset,
group=asset,
currency=quote,
expiration=None,
option_kind=None,
strike=None,
spec=spec,
)
sec_list.append(sec)
logger.info(f"Created security: {contract} with specs: {specs}")
bridge.on_securities(sec_list)
logger.info("Sent securities list--sink.on_securities called.")
# 2) Subscribe to order books
for contract in CONTRACTS:
sub_msg = {
"time": int(time.time() * 1000),
"channel": "futures.order_book",
"event": "subscribe",
"payload": [contract, "5", "0"]
}
self.ws.send(json.dumps(sub_msg))
logger.info(f"✔ Sent subscribe for {contract}: {sub_msg}")
# 3) Listen for incoming messages in main thread
logger.info("▶ Listening for data")
try:
while not self.stop_event.is_set():
try:
msg = self.ws.recv()
except Exception as e:
logger.debug(f"⚠ recv() error or timeout: {e}")
continue
logger.debug(f"⟵ raw WS: {msg}")
try:
data = json.loads(msg)
logger.debug(f"Parsed JSON: {data}")
except Exception as e:
logger.error(f"JSON parse error: {e}", exc_info=True)
continue
event = data.get("event")
channel = data.get("channel")
# Handle subscription acknowledgements
if event == "subscribe":
logger.info(f"🔔 Subscribe ACK: {data}")
continue
# Handle order book updates
if channel == "futures.order_book" and event in ["all", "update"]:
logger.debug(f"Handling '{event}' event")
payload = data.get("result", {})
if isinstance(payload, dict) and ("bids" in payload or "asks" in payload):
contract = payload.get("contract", "<no-contract>")
bids = [(np.float64(l["p"]), np.float64(l["s"])) for l in payload.get("bids", [])]
asks = [(np.float64(l["p"]), np.float64(l["s"])) for l in payload.get("asks", [])]
logger.debug(f"📘 BOOK DATA for {contract}: bids={bids} asks={asks}")
book = PyBook()
book.set_data(
contract,
"GateIO",
"SPOT",
np.array(bids, dtype=np.float64),
np.array(asks, dtype=np.float64),
)
book_list = PyBookList()
book_list.append(book)
self.bridge.on_books(book_list)
logger.debug(f"✔ on_books called for {contract}")
else:
logger.debug("… no bids/asks in payload; skipping")
except WebSocketConnectionClosedException:
logger.info("WebSocket closed.")
except Exception as e:
logger.error("Fatal error in listen loop:", exc_info=True)
def start(self, bridge: MdBridge):
self.ws_thread = threading.Thread(target=lambda: self.start_impl(bridge))
self.ws_thread.start()
def stop(self):
"""Disconnect and cleanup."""
logger.info("Stopping GateIoMdProvider...")
self.stop_event.set()
if self.ping_thread and self.ping_thread.is_alive():
self.ping_thread.join()
logger.info("Ping thread stopped")
if self.ws:
try:
self.ws.close()
except Exception:
pass
self.ws = None
if self.ws_thread:
self.ws_thread.join()
self.bridge = None
logger.info("✔ Stopped GateIoMdProvider.")
@@ -0,0 +1,12 @@
import json
import string
import os
def load_gateway_config(name: str, expand_vars: bool = True):
s = ""
with open(f"/home/bts/sparkdata/conf/py_{name}.json") as f:
s = f.read()
if expand_vars:
s = string.Template(s).substitute(os.environ)
return json.loads(s)
@@ -0,0 +1,40 @@
import logging
import os
import sys
import threading
import time
from typing import Optional
from python_interop_module import PyBookSide, PyBookLevel, PyTrade
PERP_EXP_STRING = "2100-01-01T00:00"
def get_book_side(levels: list[tuple[float, float]], is_bid_side: bool):
book_side = PyBookSide()
book_side.set_is_ascending(not is_bid_side)
for price, qty in levels:
book_level = PyBookLevel()
book_level.set_price(price)
book_level.set_quantity(qty)
book_side.set_level(book_level)
return book_side
def get_trade(
venue: str,
security_type: str,
exchange_security_id: str,
price: float,
quantity: float,
aggressor_side: str,
):
trade = PyTrade()
trade.set_venue(venue)
trade.set_security_type(security_type)
trade.set_exchange_security_id(exchange_security_id)
trade.set_price(price)
trade.set_quantity(quantity)
trade.set_aggressor_side(aggressor_side)
return trade
@@ -0,0 +1,372 @@
import json
import math
import string
import logging
import os
import sys
import threading
import time
from typing import Callable, Optional
import websocket
from python_interop_module import (
PyContractPriceInfo,
PyContractPriceInfoList,
PyContractQuantityInfo,
PyContractQuantityInfoList,
PyContractSpecification,
PyFuture,
PyOption,
PySecurity,
PySpot,
)
class JsonElement:
def __init__(self, value):
self.value = value
def __str__(self):
return json.dumps(self.value)
class StructuredLog:
def __init__(self, **kwargs):
self.kwargs = kwargs
if "ts" not in kwargs:
kwargs["ts"] = time.time_ns()
def __str__(self):
return json.dumps(self.kwargs)
def _log_level_name(level: int):
match level:
case logging.DEBUG:
return "Diag"
case logging.INFO:
return "Info"
case logging.WARNING:
return "Warn"
case logging.ERROR:
return "Error"
case logging.CRITICAL:
return "Fatal"
case _:
return str(level)
class FileLogFormatter(logging.Formatter):
def __init__(self, subsystem: str):
self.subsystem = subsystem
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
if datefmt:
return time.strftime(datefmt, ct)
else:
t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
return f"{t}.{int(record.msecs * 1000):06d}"
def format(self, record):
env = "dev"
thread_id = threading.get_ident()
filename_line = f"{record.filename}:{record.lineno}"
timestamp = self.formatTime(record)
timestamp_ns = int(record.created * 1e9)
return f"{timestamp}|{timestamp_ns}|{self.subsystem:<8}|{_log_level_name(record.levelno):<5}|{os.getpid():<3}|{record.threadName:<20}|{thread_id}|{filename_line:<35}|{env}| {record.getMessage()}"
class ConsoleLogFormatter(logging.Formatter):
def __init__(self, subsystem: str):
self.subsystem = subsystem
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
if datefmt:
return time.strftime(datefmt, ct)
else:
t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
return f"{t}.{int(record.msecs * 1000):06d}"
def format(self, record):
filename_line = f"{record.filename}:{record.lineno}"
timestamp = self.formatTime(record)
return f"{timestamp}|{self.subsystem:<8}|{_log_level_name(record.levelno):<5}|{filename_line:<35}| {record.getMessage()}"
def as_bool(*args):
(True if arg else False for arg in args)
def configure_logger(logger: logging.Logger, subsystem: str):
stdout_handler = logging.StreamHandler(sys.stdout)
file_handler = logging.FileHandler(
f"/home/bts/sparkdata/data/logging.sparkd/{logger.name}_log"
)
stdout_handler.formatter = ConsoleLogFormatter(subsystem=subsystem)
file_handler.formatter = FileLogFormatter(subsystem=subsystem)
logger.handlers.clear()
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
return logger
def parse_float(s: Optional[str], default_val=math.nan):
if s is not None:
try:
return float(s)
except Exception:
pass
return default_val
def run_websocket(
url: str,
stop_event: threading.Event,
logger: logging.Logger,
on_message: Callable[[str], None],
send_ping: Optional[Callable[[websocket.WebSocket], None]] = None,
ping_interval_secs: Optional[int] = 25,
):
conn_num = 0
if send_ping is None:
def send_empty_ping(ws):
return ws.ping()
send_ping = send_empty_ping
while not stop_event.is_set():
if conn_num > 0:
# Gradually increase sleep time between attempts, capping at 5min.
sleep_secs = min(conn_num * 5, 300)
logger.warning("Reconnecting to %s in %ds." % (url, sleep_secs))
time.sleep(sleep_secs)
logger.info("Connecting to %s (attempt no %d)" % (url, conn_num))
ws = websocket.create_connection(url)
ws.settimeout(5)
logger.info("Connected to %s. Listening for data" % url)
if ping_interval_secs:
def ping_loop(orig_conn_num):
time.sleep(ping_interval_secs)
while not stop_event.is_set() and conn_num == orig_conn_num:
send_ping(ws)
time.sleep(ping_interval_secs)
threading.Thread(target=ping_loop(conn_num), daemon=True).start()
try:
msg = ws.recv()
if isinstance(msg, bytes):
on_message(msg.decode("utf-8"))
elif isinstance(msg, str):
on_message(msg)
else:
logger.error("Websocket recv unexpected msg type %s" % str(type(msg)))
except Exception:
logging.error("Websocket recv threw.", exc_info=True)
continue
def get_tick_factors(tick_sz: float):
"""
Returns a tuple of (tick unit, tick base) that represents the tick size as a
fraction. For example, if tick_sz is 0.01, returns (1, 100), as 1/100 == 0.01.
"""
if not (tick_sz > 0):
raise ValueError(f"Invalid tick size {tick_sz}")
tick_denom = 1.0 / tick_sz
match tick_denom:
case 1:
return (1, 1)
case 10:
return (10, 100)
case 100:
return (1, 100)
case 1_000:
return (1, 1_000)
case 10_000:
return (1, 10_000)
case 100_000:
return (1, 100_000)
case 1_000_000:
return (1, 1_000_000)
case 10_000_000:
return (1, 10_000_000)
case 100_000_000:
return (1, 100_000_000)
case 1_000_000_000:
return (1, 1_000_000_000)
case 4:
return (25, 100)
case 40:
return (25, 1_000)
case 400:
return (25, 10_000)
case 4_000:
return (25, 100_000)
case 20:
return (5, 100)
case 200:
return (5, 1_000)
case 2_000:
return (5, 10_000)
case 20_000:
return (5, 100_000)
case _:
raise ValueError(f"Invalid tick size {tick_sz}")
def get_num_decimal_places(tick_sz: float):
return math.pow(10, -math.floor(math.log10(tick_sz)))
def get_qty_decimal_places(sec: PySecurity) -> int:
qty_infos = sec.contract_spec().quantity_infos()
if not qty_infos:
return 2
tick_sz = qty_infos[0].tick_unit() / qty_infos[0].tick_base()
return round(-math.floor(math.log10(tick_sz)))
def _get_decimal_places(tick_base: float) -> int:
# tick base of 100 -> 2 decimals, tick base of 1000 -> 3 decimals, etc.
tick_sz = 1 / tick_base
return round(-math.floor(math.log10(tick_sz)))
def get_price_decimal_places(sec: PySecurity) -> int:
price_infos = sec.contract_spec().price_infos()
if not price_infos:
return 2
return _get_decimal_places(price_infos[0].tick_base())
def format_price(sec: PySecurity, value: float) -> str:
decimals = get_price_decimal_places(sec)
return f"{value:.{decimals}f}"
def format_quantity(sec: PySecurity, value: float) -> str:
decimals = get_qty_decimal_places(sec)
return f"{value:.{decimals}f}"
def get_price_info(
tick_unit: float,
tick_base: float,
cash_per_point: Optional[float] = None,
multiplier: Optional[float] = None,
max_price: Optional[float] = None,
):
# Use 'not' checks to account for nans (for which every op evaluates to false)
if not (tick_unit > 0) or not (tick_base >= tick_unit):
raise ValueError(f"Invalid tick factors {tick_unit}/{tick_base}")
price_info = PyContractPriceInfo()
price_info.set_tick_unit(tick_unit)
price_info.set_tick_base(tick_base)
if cash_per_point:
price_info.set_cash_per_point(cash_per_point)
if max_price:
price_info.set_max_price(max_price)
if multiplier:
price_info.set_multiplier(multiplier)
return price_info
def get_quantity_info(
tick_unit: float,
tick_base: float,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
multiplier: Optional[float] = None,
max_price: Optional[float] = None,
):
# Use 'not' checks to account for nans (for which every op evaluates to false)
if not (tick_unit > 0) or not (tick_base >= tick_unit):
raise ValueError(f"Invalid tick factors {tick_unit}/{tick_base}")
if not min_value:
min_value = tick_unit / tick_base
elif min_value < (tick_unit / tick_base):
# min_value should at least be 1 tick
raise ValueError(f"min_value ({min_value}) < tick ({tick_unit}/{tick_base})")
qty_info = PyContractQuantityInfo()
qty_info.set_tick_unit(tick_unit)
qty_info.set_tick_base(tick_base)
qty_info.set_min_value(min_value)
if max_value:
if not (max_value > min_value):
raise ValueError(
f"max_value ({max_value}) not greater than min_value ({min_value})"
)
qty_info.set_max_value(max_value)
if max_price:
qty_info.set_max_price(max_price)
if multiplier:
qty_info.set_multiplier(multiplier)
return qty_info
def get_contract_spec(
price_info: Optional[PyContractPriceInfo] = None,
qty_info: Optional[PyContractQuantityInfo] = None,
notional_qty_info: Optional[PyContractQuantityInfo] = None,
):
price_infos = PyContractPriceInfoList()
if not price_info:
# Default to 3 decimal places
price_info = get_price_info(1, 1000)
price_infos.append(price_info)
qty_infos = PyContractQuantityInfoList()
if not qty_info:
# Default to 3 decimal places
qty_info = get_quantity_info(1, 1000)
qty_infos.append(qty_info)
notional_qty_infos = PyContractQuantityInfoList()
if notional_qty_info:
notional_qty_infos.append(notional_qty_info)
spec = PyContractSpecification()
spec.set_price_infos(price_infos)
spec.set_quantity_infos(qty_infos)
spec.set_notional_quantity_infos(notional_qty_infos)
return spec
def get_security(
venue: str,
name: str,
asset: str,
group: str,
currency: str,
expiration: Optional[str] = None,
option_kind: Optional[str] = None,
strike: Optional[float] = None,
spec: Optional[PyContractSpecification] = None,
):
sec = PySecurity()
sec.set_venue(venue)
sec.set_exchange_id(name)
sec.set_name(name)
sec.set_currency(currency)
sec.set_asset(asset)
sec.set_group(group)
if not spec:
spec = get_contract_spec()
sec.set_contract_spec(spec)
if expiration and strike and option_kind:
opt = PyOption()
opt.set_expiration(expiration)
opt.set_is_call(option_kind[0].lower() == "c")
opt.set_strike(strike)
sec.set_option(opt)
elif expiration and not strike and not option_kind:
fut = PyFuture()
fut.set_expiration(expiration)
sec.set_future(fut)
elif not expiration and not strike and not option_kind:
sec.set_spot(PySpot())
else:
raise ValueError("Invalid parameters (unable to deduce security type)")
return sec