From f7ea0cd25411281b54421fcf6b09fbc4d2e67a95 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 9 Dec 2020 14:51:58 -0500 Subject: [PATCH] Removing old sim code part 3. --- .../run_sim/SimSessionOptions.proto | 82 +++++ .../md/mdp3/back_testing/run_sim/run_sim.cpp | 213 ++++++++++++ .../md/mdp3/back_testing/run_sim/run_sim.h | 15 + .../mdp3/back_testing/simulator/IAlgoModel.h | 29 ++ .../spark_bridge/AlgoServicesBridge.cpp | 312 ++++++++++++++++++ .../spark_bridge/AlgoServicesBridge.h | 103 ++++++ .../spark_bridge/BeforeLaunchBridge.cpp | 120 +++++++ .../spark_bridge/BeforeLaunchBridge.h | 49 +++ .../simulator/spark_bridge/BridgeCommon.h | 99 ++++++ 9 files changed, 1022 insertions(+) create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/SimSessionOptions.proto create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.cpp create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.h create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.cpp create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.h create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.cpp create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.h create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BridgeCommon.h diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/SimSessionOptions.proto b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/SimSessionOptions.proto new file mode 100644 index 000000000..7928b6954 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/SimSessionOptions.proto @@ -0,0 +1,82 @@ +syntax="proto3"; +package bts.spartan.protos; + +/* +algo: "Sniper" +n_cores: 4 + +base_algo_latency_ns: 1000 +base_algo_latency_ns: 5000 +base_algo_latency_ns: 10000 +transaction_latency_ns: 50000 + +param {key: "" value {item: "1" item: "2"}} +parameter {key: "Qty" value {item: "1" item: "2"}} +*/ + +// The parameters for an entire simulation session, consisting of +// multiple runs. +message SimSessionOptions { + // The algo we are testing + string algo = 1; + + // the channel we are testing + string channel = 2; + + // Wheter we log every fill/book update/order. Use with care, this + // results in large quantities of log information. + bool log_all_transactions = 10; + + // The latest pcap event we will process before we terminate the + // simulation. This is usefull to speed initial prototyping of algos + // or analysis tools. Use 0 (the default) to indicate all events + // should be processed. + uint64 last_eid = 3; + + // The max number of cores to use for the simulation. By default all + // cores will be used. + uint32 n_cores = 4 ; + + // the days we are testing this out + repeated string market_date = 5; + + // A base latency added to any orders produced by an algo. By + // default 22000. + repeated uint64 base_algo_latency_ns = 6; + + // An estimate for the average transaction latency at the + // exchange. We fallback on this value when we cannot use the send + // time of a packet to estimate transaction duration. By default + // 60000 + repeated uint64 transaction_latency_ns = 7; + + // Wheter we should use the first packet or the last packet send + // times when calculating when an algo would see market data + repeated bool use_first_packet_st = 9; + + // Algo specific parameters used for this session. The simulation + // wll run through all combinations of parameters, recording the + // outcome of each. + message Param { + string name = 1; + string value = 2; + } + repeated Param param = 8; +} + +// The parameters for a single simulation run. +message SimRunOptions { + string algo = 1; + string channel = 2; + string market_date = 3; + bool log_all_transactions = 9; + bool use_first_packet_st = 8; + uint64 base_algo_latency_ns = 4; + uint64 last_eid = 5; + uint64 transaction_latency_ns = 6; + map param = 7; +} + +message DayFiles { + repeated string file = 1; +} diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.cpp new file mode 100644 index 000000000..3a70020cb --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.cpp @@ -0,0 +1,213 @@ +#include "run_sim.h" + +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/PolyObserver2.h" +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper2.h" +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/sim_db/SimDB.h" +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/PCapEventSource.h" +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Sim.h" + +#include "lib/utils/PBUtils.h" +#include +#include +#include + +namespace bts::spartan::mdp3 { + +std::string pcap_location(std::string const &channel, std::string const &date) { + auto base_path = os::bts_data_path(); + auto pcap_path = fmt::format("{}pcaps/{}/{}_{}_24h.pcap", base_path, channel, + date, channel); + auto xz_pcap_path = pcap_path + ".xz"; + if (boost::filesystem::exists(pcap_path)) + return pcap_path; + else if (boost::filesystem::exists(xz_pcap_path)) + return xz_pcap_path; + else { + bts_assert_msg(false, + fmt::format("Could not find pcap for channel {} on {}", + channel, date)); + return ""; + } +} + +std::vector +calc_param_dimensions(protos::SimSessionOptions const &options) { + std::vector dimensions; + dimensions.push_back(options.market_date().size()); + dimensions.push_back(options.base_algo_latency_ns().size()); + dimensions.push_back(options.transaction_latency_ns().size()); + dimensions.push_back(options.use_first_packet_st().size()); + std::map param_dims; + for (auto const &p : options.param()) + param_dims[p.name()]++; + for (auto const &p : param_dims) { + dimensions.push_back(p.second); + } + return dimensions; +} + +void generate_param_combinations(std::vector const &dimensionality, + std::vector ¤t_combination, + size_t current_idx, + std::vector> *out) { + bts_assert(dimensionality.size() == current_combination.size()); + if (current_idx == dimensionality.size()) + out->push_back(current_combination); + else { + if (dimensionality[current_idx] == 0) + generate_param_combinations(dimensionality, current_combination, + current_idx + 1, out); + else + for (size_t i = 0; i < dimensionality[current_idx]; i++) { + current_combination[current_idx] = i; + generate_param_combinations(dimensionality, current_combination, + current_idx + 1, out); + } + } +} + +std::vector> +gen_param_combinations(protos::SimSessionOptions const &options) { + auto dimensions = calc_param_dimensions(options); + std::vector first_combination(dimensions.size(), -1); + std::vector> all_combinations; + generate_param_combinations(dimensions, first_combination, 0, + &all_combinations); + return all_combinations; +} + +std::vector +gen_runs(protos::SimSessionOptions const &options) { + auto combinations = gen_param_combinations(options); + std::vector runs; + for (auto &c : combinations) { + + auto &this_run = runs.emplace_back(); + this_run.set_algo(options.algo()); + this_run.set_channel(options.channel()); + this_run.set_last_eid(options.last_eid()); + this_run.set_log_all_transactions(options.log_all_transactions()); + + + this_run.set_market_date(options.market_date(c[0])); + + size_t param_idx = 1; + + // base algo latency + if (c[param_idx] == -1) + this_run.set_base_algo_latency_ns(22000); + else + this_run.set_base_algo_latency_ns( + options.base_algo_latency_ns(c[param_idx])); + param_idx++; + + // transaction latency + if (c[param_idx] == -1) + this_run.set_transaction_latency_ns(60000); + else + this_run.set_transaction_latency_ns( + options.transaction_latency_ns(c[param_idx])); + param_idx++; + + // use first packet st + if (c[param_idx] == -1) + this_run.set_use_first_packet_st(false); + else + this_run.set_use_first_packet_st( + options.use_first_packet_st(c[param_idx])); + param_idx++; + + std::map> algo_params; + for (auto const &p : options.param()) + algo_params[p.name()].push_back(p.value()); + + for (auto const &[name, values] : algo_params) { + auto this_parameter_name = name; + auto this_parameter_value = values[c[param_idx++]]; + this_run.mutable_param()->insert( + {this_parameter_name, this_parameter_value}); + } + } + return runs; +} + +void run_sim(SimDB &db, SimDB::SessionID sid, size_t remaining, + protos::SimRunOptions const &raw_options) { + using namespace simulator; + + Sniper2 algo; + auto options = algo.fill_defaults(raw_options); + auto rid = db.begin_run(sid, options); + LOG(Spartan, Warn, + "Starting preload for run {}. {} still queued" + "----------------------------------------------", + rid, remaining); + auto logger = [rid, &db](size_t sim_sec, std::string const &src, auto ts, + auto const &header, auto const &entry_type, + auto const &args) { + db.log(rid, sim_sec, src, ts, header, entry_type, args); + }; + + auto pcap_loc = pcap_location(options.channel(), options.market_date()); + PCapEventSource source(pcap_loc); + LOG(Spartan, Warn, + "Starting simulation run {} " + "----------------------------------------------", + rid.value()); + auto event_gen = source.get_event_generator(); + + Sim sim(algo, options, source.instruments(), source.icebergs(), + source.polygons(), event_gen, logger); + auto pnl = sim.run(); + db.end_run(rid, pnl.cash , pnl.commission, pnl.position); + LOG(Spartan, Warn, + "Final PandL for run {}. Cash: {} Pos: {} " + "----------------------------------------------", + rid.value(), pnl.cash, pnl.position); +} + +void run_sim(std::string const &path_to_session_options) { + spartan::protos::SimSessionOptions options; + std::ifstream proto_in(path_to_session_options); + std::string str((std::istreambuf_iterator(proto_in)), + std::istreambuf_iterator()); + bts_assert(utils::pb::from_string(str, &options)); + + if (options.last_eid() == 0) + options.set_last_eid(std::numeric_limits::max()); + + SimDB db; + auto sid = db.create_session(options.algo()); + + std::mutex lock; + std::vector workers(options.n_cores() ? options.n_cores() + : os::num_cores()); + auto run_options = gen_runs(options); + std::queue runs; + for (auto &ru : run_options) + runs.push(ru); + + for (auto &w : workers) { + w = std::thread{[&lock, &runs, &db, sid]() { + while (true) { + spartan::protos::SimRunOptions these_options; + size_t remaining; + { + std::lock_guard l(lock); + if (runs.size()) { + these_options = runs.front(); + runs.pop(); + remaining = runs.size(); + } else + return; + } + run_sim(db, sid, remaining, these_options); + } + }}; + } + + for (auto &w : workers) + w.join(); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.h new file mode 100644 index 000000000..4f9f2c076 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/run_sim/run_sim.h @@ -0,0 +1,15 @@ +#pragma once + +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h" +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/SimLogger.h" +#include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h" +#include "server/sparkd/cmds/spartan/md/mdp3/utils/polygon_utils.h" + +namespace bts::spartan::mdp3 { +class MDP3EventGenerator; +class AlgoModel; + +void run_sim(protos::SimRunOptions const&); +void run_sim(std::string const&path_to_run_options); + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h new file mode 100644 index 000000000..cbec895a5 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/orchestrator/SimSessionOptions.pb.h" + +namespace bts::spartan::mdp3{ +struct EventHeader; +struct MBPUpdate; +struct OrderUpdate; +} + +namespace bts::spartan::mdp3::simulator { + +class ISim; + +class IAlgoModel { + public: + virtual ~IAlgoModel() {} + virtual protos::SimRunOptions fill_defaults (protos::SimRunOptions const&in) {return in;} + virtual void init(ISim &, protos::SimRunOptions const&) = 0; + virtual void finit() {} + virtual void on_replay_start(EventHeader const &, + std::vector const &) {} + virtual void on_order_update(EventHeader const &, OrderUpdate const &) {} + virtual void on_mbp(EventHeader const &, MBPUpdate const &) {} + virtual void on_time() {} +}; + +} diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.cpp new file mode 100644 index 000000000..b5f2f37d4 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.cpp @@ -0,0 +1,312 @@ +// #include "lib/orderman/orders/algo/IAlgoOrder.h" +// #include "lib/orderman/orders/algo/IAlgoServices.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/ISimServices.h" +// #include "AlgoServicesBridge.h" +// #include "lib/orderman/actors/order_manager/OrderBuilder.h" +// #include "lib/orderman/orders/algo/ChildTemplate.h" +// #include "lib/orderman/orderman_utils/Book.h" +// #include "BridgeCommon.h" +// #include "BeforeLaunchBridge.h" + + +// using namespace bts::spartan::mdp3; +// using namespace bts::api; +// using namespace bts::api::orderman; + +// namespace bts { +// namespace orderman { + +// struct OrderState{ +// // spartan outgoing order +// MDP3Order order; +// // spark callback object +// ChildState child_state; +// }; + +// struct AlgoServicesBridge::PImpl { +// std::vector pos; +// BeforeLaunchBridge& bl_bridge; +// IAlgoOrder& algo_order; +// ISimServices& sim_services; +// std::unordered_map book_map; +// std::unordered_map order_map; +// OrderDefinition order_def; +// uint32_t running_ok = 0; + +// PImpl(BeforeLaunchBridge& bl_bridge, +// IAlgoOrder& algo_order, +// ISimServices& sim_services) +// : +// bl_bridge(bl_bridge), +// algo_order(algo_order), +// sim_services(sim_services), +// order_def(bl_bridge.definition()) +// { +// } +// }; + +// void absorb_order_callback(const OrderUpdate& new_update, +// ChildState& curr_state){ +// uint32_t tot_fills = new_update.last_fills.value() + +// curr_state.total_fills.value(); +// double avg_price = 0; +// if (tot_fills>0){ +// avg_price = +// curr_state.avg_price.value()*curr_state.total_fills.value() + +// new_update.last_price.price()*new_update.last_fills.value(); +// avg_price = avg_price / tot_fills; +// } +// curr_state.total_fills = Quantity(tot_fills); +// curr_state.avg_price = Price(avg_price); +// curr_state.last_fills = Quantity(new_update.last_fills.value()); +// curr_state.last_price = Price(new_update.last_price.price()); +// switch (new_update.status) +// { +// case OrderUpdate::Status::FILLED: +// curr_state.status = ChildStatus::COMPLETED; +// break; +// case OrderUpdate::Status::PARTIALLY_FILLED: +// curr_state.status = ChildStatus::WORKING; +// break; +// case OrderUpdate::Status::CANCELED: +// curr_state.status = ChildStatus::CANCELED; +// break; +// } +// } + +// AlgoServicesBridge::AlgoServicesBridge(BeforeLaunchBridge& bl_bridge, +// IAlgoOrder& algo_order, +// ISimServices& sim_services) +// : +// me(new PImpl(bl_bridge,algo_order, sim_services)) { +// } + +// Price AlgoServicesBridge::price() const{ +// return Price(0); +// } + +// Quantity AlgoServicesBridge::quantity() const { +// return Quantity(0); +// } + +// Quantity AlgoServicesBridge::total_fills() const{ +// return Quantity(0); +// } + +// bool AlgoServicesBridge::is_shutting_down() const { +// return false; +// } + +// const std::vector& +// AlgoServicesBridge::current_positions() const { +// bts_assert_msg(false, "Not implemented"); +// return me->pos; +// } + +// trace::CorID AlgoServicesBridge::get_correlation_id() const { +// trace::CorID cid; +// return cid; +// } + +// void AlgoServicesBridge::subscribe_book(SecurityKey k) { +// auto st_sec_key = map_sec_key(k); +// if(me->book_map.find(st_sec_key) == me->book_map.end()) +// me->book_map.insert({st_sec_key,Book()}); +// } + +// void AlgoServicesBridge::unsubscribe_book(SecurityKey k) { +// auto st_sec_key = map_sec_key(k); +// if(me->book_map.find(st_sec_key) != me->book_map.end()) +// me->book_map.erase(st_sec_key); +// } + +// void AlgoServicesBridge::subscribe_settlement(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_settlement(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::subscribe_child_change(OrderKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_child_change(OrderKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::subscribe_timer() { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_timer() { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::subscribe_ticker(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_ticker(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::subscribe_status(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_status(SecurityKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::subscribe_position() { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::unsubscribe_position() { +// bts_assert_msg(false, "Not implemented"); +// } + +// OrderKey AlgoServicesBridge::launch_child(ChildTemplate &, +// Price p, Quantity q, +// bool subscribe) { +// return launch_child(me->order_def,p,q,subscribe); +// } + +// OrderKey AlgoServicesBridge::launch_child( +// const api::orderman::OrderDefinition& ord_def, +// Price p, Quantity q, +// bool) { +// auto ord_key = me->sim_services.generate_order_key(); +// auto sec_key = map_sec_key(SecurityKey(ord_def.security_key())); +// auto ret = me->order_map.insert( +// { +// ord_key, +// OrderState{ +// MDP3Order { +// ord_key, +// sec_key, +// ord_def.side(), +// MDP3OrderPriority{}, +// md::IntPrice_from_double<7>(p.value()), +// MDP3Quantity(q.value()), +// std::nullopt +// }, +// ChildState{ +// p,q,Quantity(0),Price(0),Quantity(0),Price(0), +// ChildStatus::WORKING,Quantity(0) +// } +// }}); +// bts_assert(ret.second); +// OrderState& ord_state = ret.first->second; +// SmallOrderChange ord_change(ord_state.order, +// OrderChange::ChangeType::added); +// me->sim_services.send(ord_change); +// return map_ord_key(ord_key); +// } + +// // Child order maintenance +// void AlgoServicesBridge::update_child( +// OrderKey k, +// const api::orderman::Update& u){ +// auto sp_ord_key = map_ord_key(k); +// auto it = me->order_map.find(sp_ord_key); +// if(it != me->order_map.end()){ +// OrderState& ord_state = it->second; +// ord_state.order.quantity = MDP3Quantity(u.quantity()); +// ord_state.order.price = md::IntPrice_from_double<7>(u.price()); +// ord_state.child_state.price = Price(u.price()); +// ord_state.child_state.quantity = Quantity(u.quantity()); +// SmallOrderChange ord_change(ord_state.order, +// OrderChange::ChangeType::updated); +// me->sim_services.send(ord_change); +// } +// else +// LOG(OrderMan, Warn, "AlgoServicesBridge::update_child OrderKey:{} " +// "does not exist", k); +// } + +// void AlgoServicesBridge::cancel_child(OrderKey k) { +// auto sp_ord_key = map_ord_key(k); +// auto it = me->order_map.find(sp_ord_key); +// if(it != me->order_map.end()){ +// OrderState& ord_state = it->second; +// SmallOrderChange ord_change(ord_state.order, +// OrderChange::ChangeType::removed); +// me->sim_services.send(ord_change); +// } +// else +// LOG(OrderMan, Warn, "AlgoServicesBridge::cancel_child OrderKey:{} " +// "does not exist", k); +// } + +// void AlgoServicesBridge::detach_child(OrderKey) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::detach_all_children() { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::cancel(const std::string&) { +// for (const auto& p : me->order_map) +// me->sim_services.send( +// SmallOrderChange(p.second.order, OrderChange::ChangeType::removed)); +// } + +// void AlgoServicesBridge::terminate_error(const std::string& msg) { +// cancel(msg); +// } + +// void AlgoServicesBridge::complete() { +// } + +// void AlgoServicesBridge::set_ladder_visibility(bool) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::update_custom_state( +// const api::orderman::DynamicOrderState&) { +// bts_assert_msg(false, "Not implemented"); +// } + +// void AlgoServicesBridge::update_fill_state(Quantity, Price) { +// } + +// // IAlgoModel + +// void AlgoServicesBridge::init(bts::spartan::mdp3::ISimServices&, +// bts::spartan::protos::SimRunOptions const&){ +// } + +// void AlgoServicesBridge::on_notification( +// const bts::spartan::mdp3::AlgoNotification& ev){ +// if(std::holds_alternative(ev.event)){ +// // book update +// const MBPUpdate& new_bk = std::get(ev.event); +// auto it = me->book_map.find(new_bk.security_id); +// if(it != me->book_map.end()){ +// Book& bk = it->second; +// translate_book(new_bk,bk); +// me->algo_order.on_book(map_sec_key(new_bk.security_id), bk); +// } +// } +// else{ +// const OrderUpdate& u = std::get(ev.event); +// auto it = me->order_map.find(u.key); +// if(it != me->order_map.end()){ +// OrderState& ord_state = it->second; +// absorb_order_callback(u,ord_state.child_state); +// me->algo_order.on_child_change( +// map_ord_key(ord_state.order.key),ord_state.child_state); +// } +// else +// LOG(OrderMan, Error, "AlgoServicesBridge::on_notification " +// "OrderKey:{} does not exist", u.key); +// } + +// } + +// } // namespace orderman +// } // namespace bts diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.h new file mode 100644 index 000000000..2298d0e85 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/AlgoServicesBridge.h @@ -0,0 +1,103 @@ +// #pragma once + +// #include "lib/orderman/orders/algo/IAlgoOrder.h" +// #include "lib/orderman/orders/algo/IAlgoServices.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h" + +// namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}} +// namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}} + +// namespace bts::spartan::protos { class SimRunOptions; } + +// namespace bts { +// namespace orderman { + +// class ChildTemplate; +// class IAlgoOrder; +// class BeforeLaunchBridge; + +// class AlgoServicesBridge : public IAlgoServices, public bts::spartan::mdp3::IAlgoModel{ +// public: +// AlgoServicesBridge(BeforeLaunchBridge& bl_bridge, +// IAlgoOrder& algo_order, +// bts::spartan::mdp3::ISimServices&); +// ~AlgoServicesBridge(){} + +// Price price() const override; + +// Quantity quantity() const override; + +// Quantity total_fills() const override; + +// bool is_shutting_down() const override; + +// std::vector const& current_positions() const override; + +// trace::CorID get_correlation_id() const override; + +// void subscribe_book(SecurityKey) override; + +// void unsubscribe_book(SecurityKey) override; + +// void subscribe_settlement(SecurityKey) override; + +// void unsubscribe_settlement(SecurityKey) override; + +// void subscribe_child_change(OrderKey) override; + +// void unsubscribe_child_change(OrderKey) override; + +// void subscribe_timer() override; + +// void unsubscribe_timer() override; + +// void subscribe_ticker(SecurityKey) override; + +// void unsubscribe_ticker(SecurityKey) override; + +// void subscribe_status(SecurityKey) override; + +// void unsubscribe_status(SecurityKey) override; + +// void subscribe_position() override; + +// void unsubscribe_position() override; + +// OrderKey launch_child(ChildTemplate &, Price, Quantity, +// bool subscribe = true) override; + +// OrderKey launch_child(api::orderman::OrderDefinition const &, Price, +// Quantity, bool subscribe = true) override; + +// // Child order maintenance +// void update_child(OrderKey child, api::orderman::Update const &) override; +// void cancel_child(OrderKey child) override; +// void detach_child(OrderKey child) override; +// void detach_all_children() override; + +// void cancel(std::string const &msg = "") override; + +// void terminate_error(std::string const &msg) override; + +// void complete() override; + +// void set_ladder_visibility(bool is_visible) override; + +// void update_custom_state(api::orderman::DynamicOrderState const&) override; + +// void update_fill_state(Quantity total_fills, Price last_price) override; + +// // IAlgoModel + +// void init(bts::spartan::mdp3::ISimServices&, +// bts::spartan::protos::SimRunOptions const&) override; + +// void on_notification(bts::spartan::mdp3::AlgoNotification const&) override; + +// private: +// struct PImpl; +// std::unique_ptr me; +// }; + +// } // namespace orderman +// } // namespace bts diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.cpp new file mode 100644 index 000000000..fa18533e0 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.cpp @@ -0,0 +1,120 @@ +// #include "BeforeLaunchBridge.h" +// #include "../ISimServices.h" +// #include "BridgeCommon.h" +// #include "lib/orderman/actors/order_manager/OrderBuilder.h" +// #include "lib/orderman/orders/algo/ChildTemplate.h" + + +// namespace bts { +// namespace orderman { + +// using namespace bts::spartan::mdp3; +// using namespace bts::api::orderman; + +// typedef std::map > InsMap; + +// struct BeforeLaunchBridge::PImpl { +// OrderKey ok; +// MDP3SecurityID sec_id; +// ISimServices& sim_services; +// const api::orderman::OrderDefinition& order_def; +// ChildTemplate* child_temp = nullptr; +// api::Security security; +// TimeStamp ts; +// std::map,OrderDefinition> _child_temp_map; +// InsMap sec_map = InsMap([](const auto& a, const auto& b) { +// return a->security_id().value() < b->security_id().value(); +// }); + +// PImpl( +// OrderKey ok, +// MDP3SecurityID sec_id, +// ISimServices& sim_services, +// const api::orderman::OrderDefinition& order_def) +// : +// ok(ok), +// sec_id(sec_id), +// sim_services(sim_services), +// order_def(order_def) { +// } +// }; + +// // TODO: +// // order def translation (ex sniper) +// // ChildTemplate +// // timestamp + +// BeforeLaunchBridge::BeforeLaunchBridge( +// ISimServices& sim_services, +// const api::orderman::OrderDefinition& order_def) +// : +// me(new PImpl(OrderKey(0),MDP3SecurityID(0),sim_services,order_def)){ +// for (const auto& p : me->sim_services.instruments()){ +// auto mdp3_sec_ptr = p.second; +// api::Security sec = convert_to_spark_security( +// mdp3_sec_ptr, +// me->sim_services.instruments()); +// me->sec_map.insert({mdp3_sec_ptr,sec}); +// } +// bool found = false; +// for (const auto& p : me->sec_map) +// if(p.second.id() == order_def.security_key()){ +// me->security = p.second; +// found = true; +// } +// bts_assert(found); +// } + +// const api::orderman::OrderDefinition& BeforeLaunchBridge::definition() const{ +// return me->order_def; +// } + +// const api::Security& BeforeLaunchBridge::security() const{ +// return me->security; +// } + +// OrderKey BeforeLaunchBridge::order_key() const{ +// return me->ok; +// } + +// OrderKey BeforeLaunchBridge::parent_order_key() const{ +// // not supported in spartan sim +// return order_key(); +// } + +// api::Security const* BeforeLaunchBridge::lookup_security(SecurityKey k) const{ +// for (const auto& p : me->sec_map) +// if(p.second.id() == k.value()) +// return &p.second; +// return nullptr; +// } + +// std::vector BeforeLaunchBridge::known_securities() const{ +// std::vector rv; +// for (const auto& p : me->sec_map) +// rv.push_back(&p.second); +// return rv; +// } + +// bool BeforeLaunchBridge::is_attaching() const{ +// return false; +// } + +// ChildTemplate& BeforeLaunchBridge::make_child_template( +// const api::orderman::OrderDefinition& d, +// size_t){ +// auto ret = me->_child_temp_map.insert(std::make_pair(std::make_unique(),d)); +// bts_assert(ret.second); +// const std::unique_ptr& rv = ret.first->first; +// return *rv; +// } + +// // Get the current time +// TimeStamp BeforeLaunchBridge::get_time() const{ +// // TODO +// return me->ts; +// } + +// }} diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.h new file mode 100644 index 000000000..20d8d40e2 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BeforeLaunchBridge.h @@ -0,0 +1,49 @@ +// #pragma once + +// #include "lib/orderman/orders/algo/IAlgoOrder.h" +// #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" + + +// namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}} +// namespace bts {namespace spartan {namespace mdp3{class ISimServices;}}} + +// namespace bts { +// namespace orderman { + +// class ChildTemplate; + +// class BeforeLaunchBridge : public IBeforeLaunchAlgoServices{ +// public: +// BeforeLaunchBridge( +// bts::spartan::mdp3::ISimServices&, +// const api::orderman::OrderDefinition&); +// virtual ~BeforeLaunchBridge(){} + +// api::orderman::OrderDefinition const &definition() const override; + +// api::Security const &security() const override; + +// OrderKey order_key() const override; + +// OrderKey parent_order_key() const override; + +// api::Security const *lookup_security(SecurityKey k) const override; + +// std::vector known_securities() const override; + +// bool is_attaching() const override; + +// ChildTemplate & +// make_child_template(api::orderman::OrderDefinition const &child_def, +// size_t reserve = 1) override; + +// TimeStamp get_time() const override; + +// private: +// struct PImpl; +// //std::unique_ptr me; +// PImpl* me; +// }; + +// } // namespace orderman +// } // namespace bts diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BridgeCommon.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BridgeCommon.h new file mode 100644 index 000000000..98916b7bd --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/spark_bridge/BridgeCommon.h @@ -0,0 +1,99 @@ +// #pragma once + +// #include "lib/orderman/orders/algo/IAlgoOrder.h" +// //#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" +// //#include "../ISimServices.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentDefinition.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentMap.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h" +// #include "lib/orderman/orderman_utils/Book.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/AlgoEvent.h" +// #include "server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/IAlgoModel.h" + +// namespace bts{ +// namespace orderman{ + +// inline OrderKey map_ord_key(bts::spartan::mdp3::MDP3OrderKey k){ +// // uint64_t -> uint32_t +// return OrderKey(static_cast(k.value())); +// } + +// inline bts::spartan::mdp3::MDP3OrderKey map_ord_key(OrderKey k){ +// // uint32_t -> uint64_t +// return bts::spartan::mdp3::MDP3OrderKey(static_cast(k.value())); +// } + +// inline SecurityKey map_sec_key(bts::spartan::mdp3::MDP3SecurityID k){ +// // int -> uint32_t +// return SecurityKey(static_cast(k.value())); +// } + +// inline bts::spartan::mdp3::MDP3SecurityID map_sec_key(SecurityKey k){ +// // uint32_t -> int +// return bts::spartan::mdp3::MDP3SecurityID(static_cast(k.value())); +// } + +// inline void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in, +// bts::orderman::Book& b_out){ +// // TODO: implieds +// size_t i=-1; +// for (auto const& bl : u_in.mbp_buy) +// if(++i<5) +// b_out.side(api::Side::SIDE_BUY)[i] = +// md::PriceSize (bl.price,bl.quantity.value()); +// i=-1; +// for (auto const& bl : u_in.mbp_sell) +// if(++i<5) +// b_out.side(api::Side::SIDE_SELL)[i] = +// md::PriceSize (bl.price,bl.quantity.value()); +// } + +// inline void fill_future(const bts::spartan::mdp3::InstrumentDefinition&, +// bts::api::Security& out_sec){ +// // TODO, set future expiration dates +// out_sec.mutable_future(); +// } + +// inline bts::api::Security convert_to_spark_security ( +// bts::spartan::mdp3::InstrumentDefinition const* def_ptr, +// const bts::spartan::mdp3::InstrumentMap& sec_map){ +// auto fill_common = [](const auto& def, auto out_sec){ +// out_sec.set_id(map_sec_key(def.security_id()).value()); +// out_sec.set_symbol(def.security_group()); +// out_sec.set_exchange(bts::api::VENUE_GLBX); +// out_sec.set_currency(bts::api::CURRENCY_USD); +// out_sec.mutable_contract_spec()->set_low_tick(def.min_price_increment_ammount()); +// out_sec.mutable_contract_spec()->set_high_tick(def.min_price_increment_ammount()); +// }; +// const auto& def = *def_ptr; +// bts::api::Security rv_sec; +// fill_common(def,rv_sec); +// switch(def.security_type()){ +// case bts::api::SECURITYTYPE_FUTURE: +// fill_future(def,rv_sec); +// break; +// case bts::api::SECURITYTYPE_SPREAD:{ +// auto& spr = *rv_sec.mutable_spread(); +// for(const auto& l : def.legs()){ +// // getting inst def for the leg +// auto it = sec_map.find(l.security_id); +// bts_assert(it != sec_map.end()); +// const auto& leg_insdef = *it->second; +// // creating spread +// bts::api::ContractLeg& spr_leg = *spr.add_legs(); +// auto& leg_sec = *spr_leg.mutable_security(); +// fill_common(leg_insdef, leg_sec); +// fill_future(leg_insdef,leg_sec); +// spr_leg.set_quantity(l.ratio.value()); +// spr_leg.set_side(l.side); +// spr_leg.set_price_multiplier(spr_leg.quantity()); +// } +// break; +// } +// default: +// break; +// } +// return rv_sec; +// } +// }}