From 02fa07552b4ebdbbc1e255355cecbf26fec0cd51 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 24 Dec 2020 10:44:08 -0500 Subject: [PATCH] Added expiration to instrument definition. --- .../md/mdp3/back_testing/algos/SniperModel.cpp | 14 +++++--------- .../spartan/md/mdp3/back_testing/sim/Sim.cpp | 2 +- .../md/mdp3/common/InstrumentDefinition.cpp | 14 ++++++++++++-- .../md/mdp3/common/InstrumentDefinition.h | 1 + .../cmds/spartan/md/mdp3/utils/mdp3_utils.cpp | 17 ++++++++++++++--- .../cmds/spartan/md/mdp3/utils/mdp3_utils.h | 3 ++- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/SniperModel.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/SniperModel.cpp index 414248d0b..b246888b5 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/SniperModel.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/SniperModel.cpp @@ -6,7 +6,10 @@ #include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h" #include "server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h" #include "server/sparkd/cmds/spartan/md/mdp3/utils/polygon_utils.h" +#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h" + #include +#include namespace bts::spartan::mdp3 { @@ -63,14 +66,7 @@ struct SniperModel::PImpl { double max_shot_loss; EventHeader current_event; size_t streak_id = 0; - - double get_min_cumm_edge(api::Side side) { - if (side == api::SIDE_BUY) - return -min_cumm_edge; - else - return min_cumm_edge; - } - + State state = State::WAIT_OPPORTUNITY; std::unordered_map books = {}; std::vector> targets = {}; @@ -351,7 +347,7 @@ bool SniperModel::within_safety_margin(Target const &t) const { std::tuple_size::value); auto cumm_qty = 0ul; - auto min_price = me->get_min_cumm_edge(t.side); + auto min_price = me->min_cumm_edge; // This is not right, exactly, we are assuming all the legs are // expressed in the same terms as the first. diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/sim/Sim.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/sim/Sim.cpp index 8a0be2e06..12406f837 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/sim/Sim.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/sim/Sim.cpp @@ -751,7 +751,7 @@ Sim::calculate_trading_window(std::string const &mkt_date, auto start_time = adjust_day(start); auto stop_time = adjust_day(stop); bts_assert(stop_time > start_time); - return {cme_ts_from_ptime(start_time), cme_ts_from_ptime(stop_time)}; + return {to_mdp3time(start_time), to_mdp3time(stop_time)}; } } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.cpp index 7c5ae9430..bde7e3a4e 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.cpp @@ -4,6 +4,9 @@ #include "lib/config/protos/cme_reference_data.pb.h" #include "lib/logging/logging.h" #include "lib/utils/helper_macros.h" +#include +#include "lib/utils/datetime.h" +#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h" namespace bts::spartan::mdp3 { @@ -38,9 +41,14 @@ struct InstrumentDefinition::PImpl { cme_security_type = str_from_fixed_array(m.securityType(), m.securityTypeEncodingLength()); + m.noEvents().forEach([&](auto const &ev) { + if (ev.eventType() == ::mdp3::EventType::LastEligibleTradeDate) { + expiration = MDP3TimeStamp(ev.eventTime()); + } + + }); // As in the constructor we have to skip over all the arrays // that preceed ours - m.noEvents().forEach([](auto const &) {}); m.noMDFeedTypes().forEach([](auto const &) {}); m.noInstAttrib().forEach([this](auto &attr) { implied_matching = attr.instAttribValue().impliedMatchingEligibility(); @@ -53,7 +61,6 @@ struct InstrumentDefinition::PImpl { std::string symbol; std::string security_group; std::string asset; - MDP3TimeStamp last_update_time; MDP3SecurityID security_id; std::string match_algorithm; md::IntPrice min_price_increment; @@ -61,6 +68,7 @@ struct InstrumentDefinition::PImpl { bool implied_matching = false; api::SecurityType security_type; std::string cme_security_type; + std::optional expiration; }; InstrumentDefinition::InstrumentDefinition() : me (new PImpl()) {} @@ -148,6 +156,8 @@ std::string const &InstrumentDefinition::security_group() const { } std::string const &InstrumentDefinition::asset() const { return me->asset; } +std::optional InstrumentDefinition::expiration() const {return me->expiration;} + std::string InstrumentDefinition::match_algorithm() const { return me->match_algorithm; } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h index 024ebda3c..d64fa45de 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h @@ -33,6 +33,7 @@ class InstrumentDefinition { std::string const &symbol() const ; std::string const &security_group() const ; std::string const &asset() const ; + std::optional expiration() const; std::string match_algorithm() const ; double display_factor() const {return display_factor_;} double min_price_increment() const; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp index 845e0ed01..41545d874 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp @@ -48,8 +48,9 @@ std::string drop_extensions(std::string const &str) { return result; } +auto one_billion = 1000000000; + void cme_ts_to_time_str(uint64_t cme_ts, std::stringstream *out) { - auto one_billion = 1000000000; time_t unix_ts = cme_ts / one_billion; auto boost_ts = boost::posix_time::from_time_t(unix_ts); int hours = boost_ts.time_of_day().hours(); @@ -62,7 +63,6 @@ void cme_ts_to_time_str(uint64_t cme_ts, std::stringstream *out) { } void cme_ts_to_date_time_str(uint64_t cme_ts, std::stringstream *out) { - auto one_billion = 1000000000; time_t unix_ts = cme_ts / one_billion; auto boost_ts = boost::posix_time::from_time_t(unix_ts); auto year = boost_ts.date().year(); @@ -79,13 +79,24 @@ void cme_ts_to_date_time_str(uint64_t cme_ts, std::stringstream *out) { << cme_ts - (unix_ts * one_billion); } -MDP3TimeStamp cme_ts_from_ptime(datetime::PTime const &pt) { +MDP3TimeStamp to_mdp3time(datetime::PTime const &pt) { using namespace boost::posix_time; static ptime epoch(boost::gregorian::date(1970, 1, 1)); auto tns = (pt - epoch).total_nanoseconds(); return MDP3TimeStamp{(size_t)tns}; } +datetime::PTime to_ptime(MDP3TimeStamp cme_ts) { + time_t unix_ts = cme_ts.value() / one_billion; + long residual_nano_seconds = cme_ts.value() - (unix_ts * one_billion); + auto temp_result = boost::posix_time::from_time_t(unix_ts); + auto t_of_day_secs = temp_result.time_of_day(); + auto t_of_day_nsecs = datetime::PDuration {t_of_day_secs.hours(),t_of_day_secs.minutes(), + t_of_day_secs.seconds(), residual_nano_seconds }; + + return datetime::PTime {temp_result.date(), t_of_day_nsecs}; +} + std::string to_time_string(MDP3TimeStamp ts) { std::stringstream str; cme_ts_to_time_str(ts.value(), &str); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h index 6a24f3dc6..02c0d354f 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h @@ -14,7 +14,8 @@ namespace bts::spartan::mdp3 { // CME time stamps to friendly strings std::string to_time_string(MDP3TimeStamp ts); std::string to_datetime_string(MDP3TimeStamp ts); -MDP3TimeStamp cme_ts_from_ptime(datetime::PTime const &) ; +MDP3TimeStamp to_mdp3time(datetime::PTime const &) ; +datetime::PTime to_ptime(MDP3TimeStamp) ; // Details the ip/port used for each of the incremental a/b, snapshot // and instrument feeds in a pcap.