#include "py_security.h" #include "spark/utils/overloaded.h" #include 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(); if (typ.starts_with('S')) { sec.sec_.emplace(); } else if (typ.starts_with('F')) { auto &fut = sec.sec_.emplace(); fut.set_expiration(j.at("exp").get()); } else if (typ.starts_with('O')) { auto &opt = sec.sec_.emplace(); opt.set_expiration(j.at("exp").get()); opt.set_is_call(j.at("cp").get().starts_with('C')); opt.set_strike(j.at("k").get()); } } } // namespace bts::md::py_interop