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.
85 lines
2.1 KiB
C++
85 lines
2.1 KiB
C++
#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
|