diff --git a/src/lib_python_interop/python_interop/py_spread.h b/src/lib_python_interop/python_interop/py_spread.h new file mode 100644 index 000000000..e597d45f4 --- /dev/null +++ b/src/lib_python_interop/python_interop/py_spread.h @@ -0,0 +1,98 @@ +#pragma once + +#include "spark/core/FMT_IMPL.h" + +#include +#include +#include +#include + +namespace bts::md::py_interop { + +/** + * Spread contract definition. + */ +class PySpread { + public: + const std::string &spread_type() const; + void set_spread_type(std::string value); + + const std::vector &legs() const; + void set_legs(std::vector value); + + const std::string &expiration() const; + void set_expiration(std::string value); + + std::string to_string() const; + + private: + std::string spread_type_; + std::vector legs_; + std::string expiration_; +}; + +void to_json(nlohmann::json &j, const PySpread &spread); +void from_json(const nlohmann::json &j, PySpread &spread); + +std::ostream &operator<<(std::ostream &out, const PySpread &spread) noexcept; + +} // namespace bts::md::py_interop + +FMT_IMPL( + bts::md::py_interop::PySpread, s, + R"({{ "type" : "{}", "legs" : "{}", "exp" : "{}" }})", + s.spread_type(), fmt::join(s.legs(), ","), s.expiration()); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +namespace bts::md::py_interop { + +inline const std::string &PySpread::spread_type() const { return spread_type_; } +inline void PySpread::set_spread_type(std::string value) { + spread_type_ = std::move(value); +} + +inline const std::vector &PySpread::legs() const { return legs_; } +inline void PySpread::set_legs(std::vector value) { + legs_ = std::move(value); +} + +inline const std::string &PySpread::expiration() const { return expiration_; } +inline void PySpread::set_expiration(std::string value) { + expiration_ = std::move(value); +} + +inline std::string PySpread::to_string() const { + return fmt::format("{}", *this); +} + +inline std::ostream &operator<<( + std::ostream &out, const PySpread &spread) noexcept { + out << fmt::format("{}", spread); + return out; +} + +// JSON serialization implementation +inline void to_json(nlohmann::json &j, const PySpread &spread) { + j = nlohmann::json{ + {"spread_type", spread.spread_type()}, + {"legs", spread.legs()}, + {"expiration", spread.expiration()} + }; +} + +inline void from_json(const nlohmann::json &j, PySpread &spread) { + if (j.contains("spread_type")) { + spread.set_spread_type(j["spread_type"].get()); + } + if (j.contains("legs")) { + spread.set_legs(j["legs"].get>()); + } + if (j.contains("expiration")) { + spread.set_expiration(j["expiration"].get()); + } +} + +} // namespace bts::md::py_interop \ No newline at end of file