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.
104 lines
2.6 KiB
C++
104 lines
2.6 KiB
C++
#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
|