106 lines
3.3 KiB
C++
106 lines
3.3 KiB
C++
#include "Security.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
|
#include "lib/utils/datetime.h"
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <google/protobuf/text_format.h>
|
|
|
|
namespace bts {
|
|
namespace securities {
|
|
|
|
std::string to_str(api::Security const &sec) {
|
|
std::string serialized;
|
|
google::protobuf::TextFormat::PrintToString(sec, &serialized);
|
|
return serialized;
|
|
}
|
|
|
|
std::pair<bool, api::Security> from_str(std::string const &str) {
|
|
api::Security result;
|
|
auto success = google::protobuf::TextFormat::ParseFromString(str, &result);
|
|
return std::make_pair(success, result);
|
|
}
|
|
|
|
std::string descriptor(api::Security const &sec) {
|
|
if (sec.has_glbx())
|
|
return sec.glbx().security_desc();
|
|
else if (sec.has_ice())
|
|
return sec.ice().security_desc();
|
|
else
|
|
return "No descriptor for non glbx / ice exchange";
|
|
}
|
|
|
|
api::SecurityType security_type(api::Security const &sec) {
|
|
if (sec.has_future())
|
|
return api::SecurityType::SECURITYTYPE_FUTURE;
|
|
else if (sec.has_option())
|
|
return api::SecurityType::SECURITYTYPE_OPTION;
|
|
else if (sec.has_spread())
|
|
return api::SecurityType::SECURITYTYPE_SPREAD;
|
|
else if (sec.has_synthetic())
|
|
return api::SecurityType::SECURITYTYPE_SYNTHETIC;
|
|
else
|
|
return api::SecurityType::SECURITYTYPE_UNSET;
|
|
}
|
|
|
|
boost::gregorian::date smallest_leg(
|
|
::google::protobuf::RepeatedPtrField<::bts::api::ContractLeg> const &l) {
|
|
bts_assert(l.size());
|
|
auto smallest = expiration(l[0].security());
|
|
for (const auto &leg : l) {
|
|
auto this_date = expiration(leg.security());
|
|
if (this_date < smallest) {
|
|
smallest = this_date;
|
|
}
|
|
}
|
|
return smallest;
|
|
}
|
|
|
|
boost::gregorian::date expiration(api::Security const &security) {
|
|
switch (security.Contract_case()) {
|
|
case api::Security::kFuture:
|
|
return {
|
|
boost::gregorian::greg_year(security.future().expiration_year()),
|
|
boost::gregorian::greg_month(security.future().expiration_month()),
|
|
boost::gregorian::greg_day(security.future().expiration_day())};
|
|
case api::Security::kOption:
|
|
return {
|
|
boost::gregorian::greg_year(security.option().expiration_year()),
|
|
boost::gregorian::greg_month(security.option().expiration_month()),
|
|
boost::gregorian::greg_day(security.option().expiration_day())};
|
|
case api::Security::kSpread:
|
|
return smallest_leg(security.spread().legs());
|
|
case api::Security::kSynthetic:
|
|
return smallest_leg(security.synthetic().legs());
|
|
default:
|
|
return {};
|
|
}
|
|
}
|
|
|
|
bool is_expired(
|
|
api::Security const &security, datetime::PTime const &now, uint32_t sod_hh,
|
|
uint32_t sod_mm) {
|
|
auto e = expiration(security);
|
|
if (e == boost::gregorian::date())
|
|
return false;
|
|
else {
|
|
auto etime = datetime::PTime{
|
|
e, boost::posix_time::time_duration(sod_hh, sod_mm, 0)};
|
|
return etime < now;
|
|
}
|
|
}
|
|
|
|
bool is_expired(api::Security const &security, datetime::PTime const &now) {
|
|
const auto &sod = config::get<config::SODTime>();
|
|
return is_expired(security, now, sod.sod_hh(), sod.sod_mm());
|
|
}
|
|
|
|
bool is_expired(api::Security const &security) {
|
|
return is_expired(security, datetime::local_now());
|
|
}
|
|
|
|
} // namespace securities
|
|
} // namespace bts
|