From e8ccd265ca8317dccf5a49e64fcd0194332649c0 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Tue, 10 Nov 2020 08:48:56 -0500 Subject: [PATCH] More bridge work --- .../simulator/Bridge/AlgoServicesBridge.cpp | 139 +++++++++++++++--- .../simulator/Bridge/AlgoServicesBridge.h | 2 +- .../simulator/Bridge/BridgeCommon.h | 25 +++- 3 files changed, 142 insertions(+), 24 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 f3fc39575..89c127254 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 @@ -1,24 +1,34 @@ #include "lib/orderman/orders/algo/IAlgoOrder.h" #include "lib/orderman/orders/algo/IAlgoServices.h" -// /#include "BeforeLaunchBridge.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" using namespace bts::spartan::mdp3; using namespace bts::api; +using namespace bts::api::orderman; namespace bts { namespace orderman { +struct OrderState{ + uint32_t total_filled_qty; + MDP3Order order; +}; + struct AlgoServicesBridge::PImpl { std::vector pos; IAlgoOrder& algo_order; ISimServices& sim_services; - std::unordered_map _book_map; + std::unordered_map book_map; + std::unordered_map order_map; + OrderDefinition order_def; + uint32_t running_ok = 0; + PImpl(IAlgoOrder& algo_order, ISimServices& sim_services) : @@ -27,6 +37,32 @@ struct AlgoServicesBridge::PImpl { } }; +MDP3SecurityID get_spartan_sec_id(uint32_t){ + // TODO fill in + MDP3SecurityID rv(0); + return rv; +} + +OrderKey map_ord_key(MDP3OrderKey k){ + // uint64_t -> uint32_t + return OrderKey(static_cast(k.value())); +} + +MDP3OrderKey map_ord_key(OrderKey k){ + // uint32_t -> uint64_t + return MDP3OrderKey(static_cast(k.value())); +} + +SecurityKey map_sec_key(MDP3SecurityID k){ + // int -> uint32_t + return SecurityKey(static_cast(k.value())); +} + +MDP3SecurityID map_sec_key(SecurityKey k){ + // uint32_t -> int + return MDP3SecurityID(static_cast(k.value())); +} + AlgoServicesBridge::AlgoServicesBridge(IAlgoOrder& algo_order, ISimServices& sim_services) : @@ -60,13 +96,15 @@ trace::CorID AlgoServicesBridge::get_correlation_id() const { } void AlgoServicesBridge::subscribe_book(SecurityKey k) { - if(me->_book_map.find(k) == me->_book_map.end()) - me->_book_map.insert({k,Book()}); + 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) { - if(me->_book_map.find(k) != me->_book_map.end()) - me->_book_map.erase(k); +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) { @@ -118,30 +156,65 @@ void AlgoServicesBridge::unsubscribe_position() { } OrderKey AlgoServicesBridge::launch_child(ChildTemplate &, - Price, Quantity, - bool) { - OrderKey ok(0); - return ok; + Price p, Quantity q, + bool subscribe) { + return launch_child(me->order_def,p,q,subscribe); } OrderKey AlgoServicesBridge::launch_child( - api::orderman::OrderDefinition const &, - Price, Quantity, + const api::orderman::OrderDefinition& ord_def, + Price p, Quantity q, bool) { - OrderKey ok(0); - return ok; + 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 + }}}); + 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, - api::orderman::Update const &){ + 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()); + 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) { +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) { +void AlgoServicesBridge::detach_child(OrderKey) { + bts_assert_msg(false, "Not implemented"); } void AlgoServicesBridge::detach_all_children() { @@ -149,9 +222,13 @@ void AlgoServicesBridge::detach_all_children() { } 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&) { +void AlgoServicesBridge::terminate_error(const std::string& msg) { + cancel(msg); } void AlgoServicesBridge::complete() { @@ -175,7 +252,27 @@ void AlgoServicesBridge::init(bts::spartan::mdp3::ISimServices&, } void AlgoServicesBridge::on_notification( - const bts::spartan::mdp3::AlgoNotification&){ + 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()){ + } + else + LOG(OrderMan, Error, "AlgoServicesBridge::on_notification " + "OrderKey:{} does not exist", u.key); + } + } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.h index 07f4a302d..c1a34c0b6 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/simulator/Bridge/AlgoServicesBridge.h @@ -14,7 +14,7 @@ namespace orderman { class ChildTemplate; class IAlgoOrder; -class BeforeLaunchBridge; +//class BeforeLaunchBridge; class AlgoServicesBridge : public IAlgoServices, public bts::spartan::mdp3::IAlgoModel{ public: 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 3590a311e..19bbae2bc 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 @@ -1,12 +1,33 @@ +#pragma once + #include "lib/orderman/orders/algo/IAlgoOrder.h" -#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" -#include "../ISimServices.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{ +void translate_book(const bts::spartan::mdp3::MBPUpdate& u_in, + bts::orderman::Book& b_out){ + 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()); +} + void fill_future(const bts::spartan::mdp3::InstrumentDefinition&, bts::api::Security& out_sec){ // TODO, set future expiration dates