From bdac6bc3fa1103e3b2dd5fa50953ee7588b45232 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Tue, 10 Nov 2020 11:35:45 -0500 Subject: [PATCH] Callbacks in the AlgoBridge are routed back --- .../simulator/Bridge/AlgoServicesBridge.cpp | 98 ++++++++++++++----- .../simulator/Bridge/BridgeCommon.h | 7 +- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp index 89c127254..f0bed7e45 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.cpp @@ -5,7 +5,7 @@ #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 "BridgeCommon.h" using namespace bts::spartan::mdp3; @@ -16,8 +16,10 @@ namespace bts { namespace orderman { struct OrderState{ - uint32_t total_filled_qty; + // spartan outgoing order MDP3Order order; + // spark callback object + ChildState child_state; }; struct AlgoServicesBridge::PImpl { @@ -37,6 +39,35 @@ struct AlgoServicesBridge::PImpl { } }; +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; + } +} + MDP3SecurityID get_spartan_sec_id(uint32_t){ // TODO fill in MDP3SecurityID rv(0); @@ -86,7 +117,9 @@ bool AlgoServicesBridge::is_shutting_down() const { return false; } -std::vector const& AlgoServicesBridge::current_positions() const { +const std::vector& +AlgoServicesBridge::current_positions() const { + bts_assert_msg(false, "Not implemented"); return me->pos; } @@ -167,19 +200,28 @@ OrderKey AlgoServicesBridge::launch_child( 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{0, MDP3Order { - ord_key, - sec_key, - ord_def.side(), - MDP3OrderPriority{}, - md::IntPrice_from_double<7>(p.value()), - MDP3Quantity(q.value()), - std::nullopt - }}}); + 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); + SmallOrderChange ord_change(ord_state.order, + OrderChange::ChangeType::added); me->sim_services.send(ord_change); return map_ord_key(ord_key); } @@ -194,11 +236,15 @@ void AlgoServicesBridge::update_child( OrderState& ord_state = it->second; ord_state.order.quantity = MDP3Quantity(u.quantity()); ord_state.order.price = md::IntPrice_from_double<7>(u.price()); - SmallOrderChange ord_change(ord_state.order, OrderChange::ChangeType::updated); + 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); + LOG(OrderMan, Warn, "AlgoServicesBridge::update_child OrderKey:{} " + "does not exist", k); } void AlgoServicesBridge::cancel_child(OrderKey k) { @@ -206,11 +252,13 @@ void AlgoServicesBridge::cancel_child(OrderKey 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); + 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); + LOG(OrderMan, Warn, "AlgoServicesBridge::cancel_child OrderKey:{} " + "does not exist", k); } void AlgoServicesBridge::detach_child(OrderKey) { @@ -238,7 +286,8 @@ void AlgoServicesBridge::set_ladder_visibility(bool) { bts_assert_msg(false, "Not implemented"); } -void AlgoServicesBridge::update_custom_state(const api::orderman::DynamicOrderState&) { +void AlgoServicesBridge::update_custom_state( + const api::orderman::DynamicOrderState&) { bts_assert_msg(false, "Not implemented"); } @@ -258,15 +307,19 @@ void AlgoServicesBridge::on_notification( 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); + 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 " @@ -275,6 +328,5 @@ void AlgoServicesBridge::on_notification( } - } // namespace orderman } // namespace bts diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BridgeCommon.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BridgeCommon.h index 19bbae2bc..ba55f7ab9 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BridgeCommon.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/BridgeCommon.h @@ -14,8 +14,9 @@ namespace bts{ namespace orderman{ -void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in, +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) @@ -28,13 +29,13 @@ void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in, md::PriceSize (bl.price,bl.quantity.value()); } -void fill_future(const bts::spartan::mdp3::InstrumentDefinition&, +inline void fill_future(const bts::spartan::mdp3::InstrumentDefinition&, bts::api::Security& out_sec){ // TODO, set future expiration dates out_sec.mutable_future(); } -bts::api::Security convert_to_spark_security ( +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){