diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 0646db05e..67103bd59 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -7,7 +7,8 @@ "/usr/local/include", "${workspaceRoot}", "${workspaceFolder}/src", - "${workspaceRoot}/externals/linux/include" + "${workspaceRoot}/externals/linux/include", + "${workspaceRoot}/build/ninja_debug/src" ], "defines": [], "intelliSenseMode": "clang-x64", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index db0a3719c..dac3218c7 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -48,9 +48,9 @@ } }, { - "label": "linux build (Debug)", + "label": "linux_build", "type": "shell", - "command": "cd build/ninja_debug; time ninja", + "command": "docker exec -i ${USER}s_docker bash -c \"cd build/ninja_${input:build_configuration} && ninja\"", "group": { "kind": "build", "isDefault": true @@ -59,5 +59,17 @@ "$gcc" ] } + ], + "inputs": [ + { + "id": "build_configuration", + "type": "pickString", + "default": "debug", + "description": "Configuration:", + "options": [ + "debug", + "release" + ] + } ] } \ No newline at end of file diff --git a/src/lib/md/IntPrice.h b/src/lib/md/IntPrice.h index 73689cb45..3c35a6a11 100644 --- a/src/lib/md/IntPrice.h +++ b/src/lib/md/IntPrice.h @@ -87,6 +87,9 @@ class IntPrice { _exponent = temp._exponent; return *this; } + IntPrice operator -() { + return IntPrice(-_mantissa, _exponent); + } IntPrice operator * (const IntPrice& other) const { return op(other, [](int64_t m1, int64_t m2) { return m1 * m2; }); } diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index 3a102d30a..187c8dfb5 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -421,10 +421,9 @@ void AlgoWrapperOrder::subscribe_book(SecurityKey k) { me->services->subscribe_to_trade_signal(k, [this, k](auto const &ev) { ReportStateGuard _(*this); me->algo->on_trade_summary_signal(k, - ev.signal.ag_side == co2::AggressorSide::AGGRESSOR_SIDE_BUY ? - api::SIDE_BUY : api::SIDE_SELL, - Price{ev.signal.min_price.price()}, - Price{ev.signal.max_price.price()}); + ev.signal.aggressor, + Price{ev.signal.last_price.price()}, + Quantity{ev.signal.last_qty}); }); } diff --git a/src/lib/orderman/orders/algo/IAlgoOrder.h b/src/lib/orderman/orders/algo/IAlgoOrder.h index ad9a228cf..b555ec634 100644 --- a/src/lib/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib/orderman/orders/algo/IAlgoOrder.h @@ -56,7 +56,8 @@ class IAlgoOrder { // more trades between the two price levels specified for a buy or // sell aggressor. virtual void on_trade_summary_signal(SecurityKey, api::Side /* aggressor */, - Price /* min_p */, Price /* max_p */) {} + Price /* last_price */, + Quantity /* last_quantity */) {} // A periodic timer has fired virtual void on_timer() {} diff --git a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp index ed9acd334..e1f14497b 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp @@ -482,8 +482,9 @@ void SniperAlgo::on_book(SecurityKey key, const Book& bk) { } void SniperAlgo::on_trade_summary_signal(SecurityKey, api::Side /* aggressor */, - Price /* min_p */, Price /* max_p */){ -} + Price /* last_price */, + Quantity /* last_quantity */) +{} void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){ LOG(OrderMan, Info, diff --git a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h index 19baca129..ba592f7ed 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h @@ -2,7 +2,13 @@ #include "lib/orderman/orders/algo/IAlgoOrder.h" -namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}} +namespace bts { +namespace api { +namespace orderman { +class CustomOrderDefinition; +} +} // namespace api +} // namespace bts namespace bts { namespace orderman { @@ -22,7 +28,8 @@ class SniperAlgo : public IAlgoOrder { void do_update(api::orderman::Update const &) override; void on_book(SecurityKey, Book const &) override; void on_trade_summary_signal(SecurityKey, api::Side /* aggressor */, - Price /* min_p */, Price /* max_p */) override; + Price /* last_price */, + Quantity /* last_quantity */) override; void on_child_change(OrderKey, ChildState const &) override; void on_status(SecurityKey, co2::TradingStatus) override; void on_timer() override; @@ -31,14 +38,15 @@ class SniperAlgo : public IAlgoOrder { enum class State; struct PImpl; std::unique_ptr me; -private: - IAlgoServices& _algo() const ; - bool _onetime_setup(IBeforeLaunchAlgoServices& serv, - const bts::api::orderman::CustomOrderDefinition& cod); - void _compute_polygon_cross_value(SniperPolygon&); - void _fire_orders(SecData& sec_data); + + private: + IAlgoServices &_algo() const; + bool _onetime_setup(IBeforeLaunchAlgoServices &serv, + const bts::api::orderman::CustomOrderDefinition &cod); + void _compute_polygon_cross_value(SniperPolygon &); + void _fire_orders(SecData &sec_data); // acts on md, cognizant of the state - void _cxl_and_end (bool is_error, const std::string& err_msg); + void _cxl_and_end(bool is_error, const std::string &err_msg); }; } // namespace orderman diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.h index c2d081776..0a63eda6c 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMatcherContext.h @@ -57,7 +57,7 @@ struct FifoMatcherContext { // to initialize/reset the books (i.e., in response to the first callback, // or after reset events). template - inline void reset(MDP3SecurityID sec_id, TIter first, TIter last) { + inline void reset(MDP3SecurityID sec_id, TIter first, TIter last, std::vector* effects = nullptr) { _ensure_sync_implied_candidates(); auto* matcher = _get_or_add_matcher(sec_id); // The 'order' in which orders are added to the matcher matters. @@ -80,6 +80,15 @@ struct FifoMatcherContext { first++; } matcher->matcher()->reset(sorted_orders.begin(), sorted_orders.end()); + if (effects != nullptr) { + effects->emplace_back(MBPUpdate()); + MBPUpdate& mbp = std::get(effects->back()); + mbp.security_id = sec_id; + mbp.mbp_buy = matcher->book(api::Side::SIDE_BUY); + mbp.mbp_sell = matcher->book(api::Side::SIDE_SELL); + mbp.implied_mbp_buy = matcher->implied_book(api::Side::SIDE_BUY); + mbp.implied_mbp_sell = matcher->implied_book(api::Side::SIDE_SELL); + } } }; } // namespace bts::spartan::mdp3 \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp index 9780f1afa..844d7c5be 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp @@ -86,10 +86,14 @@ struct MBPSnapshot { MBPData _initial_mbp; }; -FifoMatcherContext& MergedMatcher::_asset_matcher(MDP3SecurityID sec) { +FifoMatcherContext& MergedMatcher::_asset_matcher(MDP3SecurityID sec, bool add_if_missing) { auto ass_it = _asset_map.find(sec); + if (add_if_missing && ass_it == _asset_map.end()) + ass_it = _asset_map.emplace(sec, std::string()).first; bts_assert(ass_it != _asset_map.end()); auto mat_it = _matcher_map.find(ass_it->second); + if (add_if_missing && mat_it == _matcher_map.end()) + mat_it = _matcher_map.emplace(ass_it->second, FifoMatcherContext({true})).first; bts_assert(mat_it != _matcher_map.end()); return mat_it->second; } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h index e9de675a5..71fae8053 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h @@ -25,7 +25,7 @@ private: size_t _match_count_this_per; google::protobuf::Timestamp _per_start_time; std::string _get_asset(MDP3SecurityID sec) const; - FifoMatcherContext& _asset_matcher(MDP3SecurityID); + FifoMatcherContext& _asset_matcher(MDP3SecurityID, bool add_if_missing = true); public: MergedMatcher (); @@ -54,9 +54,11 @@ public: first++; } } - template - void reset(MDP3SecurityID sec, Iter first, Iter last) { - _asset_matcher(sec).reset(sec, first, last); + + void reset(MDP3SecurityID sec, std::vector orders, std::vector* effects = nullptr) { + for (const auto& o : orders) + _used_order_keys.emplace(o.key); + _asset_matcher(sec).reset(sec, orders.begin(), orders.end(), effects); } void clear(); }; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp index cbbb7a34c..2dfe9614c 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/iceberg_helper.cpp @@ -32,7 +32,6 @@ void add_iceberg_annotation( auto show = std::get<1>(ib.show_qty_at(event_id)); o.iceberg_quantity.value().total = ib.total_qty(); o.iceberg_quantity.value().show = show; - o.quantity = show; bts_assert(o.iceberg_quantity.value().show.value() > 0); //bts_assert(o.iceberg_quantity.value().total >= o.iceberg_quantity.value().show); if (set_exact_state) { diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Sim.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Sim.cpp index 6105786d2..26e26a8ca 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Sim.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Sim.cpp @@ -347,13 +347,12 @@ void Sim::_start_simulation() { matcher.add_def(snap.definition()); if (snap.ready_to_trade()) { simulation.ready_secs.insert(sec_id); - SmallOrderChangeVec matcher_input; - for (auto &p : snap.orders()) - matcher_input.push_back(correct_pcap_order( - OrderChange{p.second, p.second, OrderChange::added}, true)); + std::vector resting_orders; + for (const auto& p : snap.orders()) + resting_orders.emplace_back( + correct_pcap_order({p.second, p.second, OrderChange::added}, true).order); std::vector effects; - simulation.matcher.match(MergedMatcher::SIMULATOR_INPUT, matcher_input, - &effects); + matcher.reset(sec_id, resting_orders, &effects); for (auto &ef : effects) std::visit( overloaded{[&](MBPUpdate &mbp) { @@ -473,7 +472,7 @@ void Sim::clear_simulated_order_book(MDP3TimeStamp transact_time, match_and_send_effects(transact_time, matcher_input); } -SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool) { +SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool set_exact_ib_state) { bts_assert(me->state == State::SIM); bts_assert(me->simulation); auto &key_map = me->simulation->cme_key_map; @@ -502,14 +501,13 @@ SmallOrderChange Sim::correct_pcap_order(OrderChange const &o, bool) { result.order.key = our_key; } } - // if (o.change != OrderChange::ChangeType::removed) { - // // IcebergMap is keyed by exchange keys - // auto ib_it = me->ib_map.find(result.original_key); - // if (ib_it != me->ib_map.end()) - // iceberg_helper::add_iceberg_annotation( - // result.order, result.change, ib_it->second, - // me->event_id.first, set_exact_ib_state); - // } + if (o.change != OrderChange::ChangeType::removed) { + // IcebergMap is keyed by exchange keys + auto ib_it = me->ib_map.find(result.original_key); + if (ib_it != me->ib_map.end()) + iceberg_helper::add_iceberg_annotation( + result.order, result.change, ib_it->second, me->event_id.first, set_exact_ib_state); + } return result; } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp index 1e73e0f9b..3969eb63b 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp @@ -52,27 +52,27 @@ void compute_synthetic_book( Leg() : book(), curr_level(0), curr_level_qty_used(0), leg_qty(0) {} md::IntPrice curr_price() const { - // Assert that the book isn't 'exhausted' yet. + // Assert that the book isn't empty yet. // Note: one could argue that nan be returned in this event, // but since the loop is designed to return "greedily," - // 'curr_price' should not be accessed after exhaustion. + // 'curr_price' should not be accessed after it has been emptied. // This assertion enforces that the behavior is only changed // as a result of deliberate action, rather than an inadvertant // side-effect. - bts_assert(!is_exhausted()); + bts_assert(!is_empty()); return book[curr_level].price; } MDP3Quantity curr_level_qty_rem() const { return book[curr_level].quantity - curr_level_qty_used; } // The implied market's volume is limited by its thinnest market. - // When one leg 'is_exhausted,' the full volume of the implied market + // When one leg 'is_empty,' the full volume of the implied market // has been realized. - bool is_exhausted() const { return curr_level >= book.size(); } + bool is_empty() const { return curr_level >= book.size(); } }; std::vector legs; - for (auto [qty, price_mult, book] : leg_mkts) { + for (const auto& [qty, price_mult, book] : leg_mkts) { // todo: implement price mult logic (for now just assert nan or 1) bts_assert(price_mult.is_nan() || price_mult.price() == 1); auto& l = legs.emplace_back(); @@ -86,21 +86,21 @@ void compute_synthetic_book( return; } - auto any_legs_exhausted = [&]() { + auto any_legs_empty = [&]() { for (auto& l : legs) - if (l.is_exhausted()) + if (l.is_empty()) return true; return false; }; - while (!any_legs_exhausted() && depth-- > 0) { + while (!any_legs_empty() && depth-- > 0) { // legs can't be empty, because it is a 1:1 mapping of leg_mkts, // and it has already been established that the latter isn't empty. int limiting_factor = legs[0].curr_level_qty_rem().value(); for (uint32_t i = 0; i < legs.size(); i++) { if (legs[i].curr_level_qty_rem().value() == 0) { legs[i].curr_level++; - if (legs[i].is_exhausted()) { + if (legs[i].is_empty()) { // book is complete return; } @@ -116,11 +116,16 @@ void compute_synthetic_book( legs[i].curr_level_qty_used += MDP3Quantity(limiting_factor); auto marginal_cost = md::INT_NAN; for (auto& l : legs) { + // The book's prices have already been multiplied by abs(leg_qty); + // Subtract the leg's cost if qty is negative, otherwise add it + // to the synthetic's marginal cost. + auto lcost = l.curr_price(); + if (l.leg_qty.value() < 0) + lcost = -lcost; if (marginal_cost.is_nan()) - marginal_cost = l.curr_price() * l.leg_qty.value(); + marginal_cost = lcost; else - marginal_cost = - marginal_cost + l.curr_price() * l.leg_qty.value(); + marginal_cost += lcost; } dest.emplace_back(marginal_cost, MDP3Quantity(limiting_factor)); } diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h index 1dd490568..f453d7d10 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h @@ -230,47 +230,47 @@ class MDP3Listener : public onixs::HandlerListener, onMessage(onixs::Handler &, const onixs::IncrementalRefreshTradeSummary48Args &args) override { if (_trade_summary_dispatcher) { - std::array levels; - security_key sk = invalid_security_key; + TradeSummarySignal signal; + for (const auto &entry : args.message().entries()) { auto data = _index.get_security(entry.securityId()); + if (!data) - continue; + return; - if (sk == invalid_security_key) - sk = data->handle.key(); + // Get Onix Aggressor Side, bail if its weird + onixs::AggressorSide::Enum onixs_ag_side; + bts_assert(entry.aggressorSide(onixs_ag_side)); + api::Side ag_side; + if (onixs_ag_side == onixs::AggressorSide::Buy) + ag_side = api::SIDE_BUY; + else if (onixs_ag_side == onixs::AggressorSide::Sell) + ag_side = api::SIDE_SELL; else - bts_assert(sk == data->handle.key()); + return; - onixs::AggressorSide::Enum onix_ag_side; - bts_assert(entry.aggressorSide(onix_ag_side)); - - auto agg = map_aggressor(onix_ag_side); - auto idx = (uint)agg; - bts_assert(idx == 1 || idx == 2); - auto &this_level = levels[idx - 1]; - this_level.ts = _packet_arrived; - this_level.sk = sk; - - auto price = convert_price(entry.entryPx(), - data->handle.security().display_factor); - auto &max_price = this_level.max_price; - auto &min_price = this_level.min_price; - if (price > max_price) - max_price = price; - if (price < min_price) - min_price = price; - } - - if (levels[0].ts != levels[1].ts) { - if (levels[0].ts != utils::TimeStamp()) { - _trade_summary_dispatcher->emit(levels[0]); - } - else { - _trade_summary_dispatcher->emit(levels[1]); + // We only process vanilla trade summaries where the + // aggressor is always the same order. The first summary + // we find defines the entire signal. If we see anything + // not vanilla we bail + if (signal.sk == invalid_security_key) { + // First entry defines the side and security + signal.sk = data->handle.key(); + signal.ts = _packet_arrived; + signal.aggressor = ag_side; } + else + if (signal.sk != data->handle.key() || + signal.aggressor != ag_side) + return; + + + signal.last_price = convert_price(entry.entryPx(), + data->handle.security().display_factor); + signal.last_qty = entry.entrySize(); } + _trade_summary_dispatcher->emit(signal); } } diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h b/src/server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h index c0c00fa98..8ff428d3e 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h @@ -11,9 +11,9 @@ namespace md { struct TradeSummarySignal { utils::TimeStamp ts = {}; security_key sk = invalid_security_key; - co2::AggressorSide ag_side = co2::AggressorSide::AGGRESSOR_SIDE_NONE; - IntPrice max_price = IntPrice {std::numeric_limits::max(), -7}; - IntPrice min_price = IntPrice {std::numeric_limits::max(), -7}; + api::Side aggressor = api::SIDE_UNDEFINED; + IntPrice last_price = {} ; + uint last_qty = 0; }; class ITradeSummarySignalListener {