ADD: Support for Spreads on Bybit. Gateio fixes.
This commit is contained in:
1 parent
1b5f1bfb27
commit
ca27903f63
7 files changed
+1192
-191
No files matched your search
@@ -30,6 +30,12 @@ void to_json(nlohmann::json &j, const PySecurity &sec) {
|
||||
j["k"] = opt.strike();
|
||||
j["cp"] = opt.is_call() ? "C" : "P";
|
||||
},
|
||||
[&](const PySpread &spr) {
|
||||
j["typ"] = "SPR";
|
||||
j["exp"] = spr.expiration();
|
||||
j["spr_type"] = spr.spread_type();
|
||||
j["legs"] = spr.legs();
|
||||
},
|
||||
},
|
||||
sec.sec_);
|
||||
}
|
||||
@@ -78,6 +84,11 @@ void from_json(const nlohmann::json &j, PySecurity &sec) {
|
||||
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>());
|
||||
} else if (typ.starts_with('S') && typ != "SPOT") {
|
||||
auto &spr = sec.sec_.emplace<PySpread>();
|
||||
spr.set_expiration(j.at("exp").get<string>());
|
||||
spr.set_spread_type(j.at("spr_type").get<string>());
|
||||
spr.set_legs(j.at("legs").get<std::vector<string>>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "python_interop/py_future.h"
|
||||
#include "python_interop/py_option.h"
|
||||
#include "python_interop/py_spot.h"
|
||||
#include "python_interop/py_spread.h"
|
||||
#include "spark/io/json_formatter.h"
|
||||
|
||||
#include <cstdint>
|
||||
@@ -50,6 +51,10 @@ class PySecurity {
|
||||
void set_option(PyOption);
|
||||
constexpr bool is_option() const;
|
||||
|
||||
const PySpread &spread() const;
|
||||
void set_spread(PySpread);
|
||||
constexpr bool is_spread() const;
|
||||
|
||||
const PyContractSpecification &contract_spec() const;
|
||||
void set_contract_spec(PyContractSpecification value);
|
||||
|
||||
@@ -65,7 +70,7 @@ class PySecurity {
|
||||
std::string name_;
|
||||
std::string currency_;
|
||||
PyContractSpecification contract_spec_;
|
||||
std::variant<PySpot, PyFuture, PyOption> sec_;
|
||||
std::variant<PySpot, PyFuture, PyOption, PySpread> sec_;
|
||||
|
||||
friend struct fmt::formatter<PySecurity>;
|
||||
friend void to_json(nlohmann::json &j, const PySecurity &sec);
|
||||
@@ -180,6 +185,18 @@ constexpr bool PySecurity::is_option() const {
|
||||
return std::holds_alternative<PyOption>(sec_);
|
||||
}
|
||||
|
||||
inline const PySpread &PySecurity::spread() const {
|
||||
if (std::holds_alternative<PySpread>(sec_)) {
|
||||
return std::get<PySpread>(sec_);
|
||||
}
|
||||
throw std::runtime_error{"PySecurity is not a spread"};
|
||||
}
|
||||
inline void PySecurity::set_spread(PySpread value) { sec_ = std::move(value); }
|
||||
constexpr bool PySecurity::is_spread() const {
|
||||
return std::holds_alternative<PySpread>(sec_);
|
||||
}
|
||||
|
||||
|
||||
inline const PyContractSpecification &PySecurity::contract_spec() const {
|
||||
return contract_spec_;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include "python_interop/py_contract_quantity_info.h"
|
||||
#include "python_interop/py_contract_specification.h"
|
||||
#include "python_interop/py_security.h"
|
||||
#include "python_interop/py_spread.h"
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <pybind11/detail/common.h>
|
||||
@@ -35,6 +37,7 @@ 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::PySpread;
|
||||
using bts::md::py_interop::PyTrade;
|
||||
using python_execution::ExecutionGateway;
|
||||
using python_execution::ExecutionGatewayBridge;
|
||||
@@ -129,6 +132,19 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PyOption, is_call)
|
||||
.PROP(PyOption, strike)
|
||||
.def("__str__", &PyOption::to_string);
|
||||
py::class_<PySpread>(m, "PySpread")
|
||||
.def(py::init<>())
|
||||
.PROP(PySpread, spread_type)
|
||||
.def("legs", &PySpread::legs) // ← Changed this line
|
||||
.def("set_legs", [](PySpread& self, py::list legs_list) { // ← Custom binding
|
||||
std::vector<std::string> legs;
|
||||
for (auto item : legs_list) {
|
||||
legs.push_back(py::cast<std::string>(item));
|
||||
}
|
||||
self.set_legs(legs);
|
||||
})
|
||||
.PROP(PySpread, expiration)
|
||||
.def("__str__", &PySpread::to_string);
|
||||
py::class_<PySecurity, shared_ptr<PySecurity>>(m, "PySecurity")
|
||||
.def(py::init<>())
|
||||
.PROP(PySecurity, id)
|
||||
@@ -142,11 +158,35 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PySecurity, spot)
|
||||
.PROP(PySecurity, future)
|
||||
.PROP(PySecurity, option)
|
||||
.PROP(PySecurity, spread)
|
||||
.def("set_spread_type", [](PySecurity& self, const std::string& spread_type) {
|
||||
if (self.is_spread()) {
|
||||
const_cast<PySpread&>(self.spread()).set_spread_type(spread_type);
|
||||
}
|
||||
})
|
||||
.def("set_legs", [](PySecurity& self, py::list legs_list) {
|
||||
if (self.is_spread()) {
|
||||
std::vector<std::string> legs;
|
||||
for (auto item : legs_list) {
|
||||
legs.push_back(py::cast<std::string>(item));
|
||||
}
|
||||
const_cast<PySpread&>(self.spread()).set_legs(legs);
|
||||
}
|
||||
})
|
||||
.def("set_expiration", [](PySecurity& self, const std::string& expiration) {
|
||||
if (self.is_spread()) {
|
||||
const_cast<PySpread&>(self.spread()).set_expiration(expiration);
|
||||
}
|
||||
})
|
||||
.def("is_spread", &PySecurity::is_spread)
|
||||
.def("__str__", &PySecurity::to_string)
|
||||
.def("__format__", &PySecurity::format);
|
||||
py::bind_vector<std::vector<shared_ptr<PySecurity>>>(m, "PySecurityList")
|
||||
.PY_LIST_TO_STRING(PySecurity);
|
||||
|
||||
py::bind_vector<std::vector<std::string>>(m, "StringVector");
|
||||
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Md
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
Reference in new issue
Block a user