From 8f1eab57b77a2840cc4d2bba6e42ab14455ab4aa Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 17 May 2019 18:22:36 -0400 Subject: [PATCH 001/153] Basic functions in, need to be hooked --- .../orders/spreader_orders/SpreaderOrder.cpp | 18 +++++++- .../orders/spreader_orders/SpreaderOrder.h | 7 ++++ .../spreader/SpreaderBookkeepingData.cpp | 4 ++ .../spreader/SpreaderBookkeepingData.h | 9 ++++ .../spreader_orders/spreader/SpreaderUtils.h | 41 +++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 24ec42c67..6b9cf169a 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -366,6 +366,22 @@ void SpreaderOrder::_send_hedge(size_t hedge_leg_index, hedge_price, hedge_qty); } +void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, + Price filled_price, + double filled_quantity){ + SpreaderBookkeepingData& bk_data = _bookkeeping_data(); + const LegPropertyData& q_leg_data = + bk_data.get_leg_data(filled_leg_index).leg_property_data(); + const ISpreaderManager& spreader = *this; + SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), + bk_data.effective_price(), + bk_data.n_legs(), + q_leg_data, + filled_price, + filled_quantity, + spreader, + bk_data.nlegs_crossed_hedges()); +} void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, Price fill_price, double fill_quantity, @@ -386,8 +402,6 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, hedge_quantity); else{ const QuoteLegDriver& driver_data = filled_leg_data.quoter().driver_data(quote_order_price, hedge_leg_index); - // QuoteLegDriver driver_data; - // driver_data.set(quote_order_price,Quantity(1),Quantity(1)); SpreaderUtils::compute_hedge_price_size_n_legs (filled_leg_data.leg_property_data(), fill_price, fill_quantity, diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index f49e9c755..e57b53861 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -233,6 +233,13 @@ private: void _send_hedge(size_t hedge_leg_index, Price hedge_price, Quantity hedge_qty); + // computes the hedges in the case of nlegs + // from crossing the hedge markets. If the + // crossing doesnt result in better hedge prices + // 0's are stored in the qty's in + void _compute_nlegs_crossed_hedges(size_t filled_leg_index, + Price filled_price, + double filled_quantity); void _compute_hedge_price_quantity(size_t filled_leg_index, Price filled_price, double filled_quantity, diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp index 0bf2e588e..8ad85b95b 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp @@ -1,4 +1,5 @@ #include "SpreaderBookkeepingData.h" +#include "lib/os/os.h" namespace bts{ namespace orderman{ @@ -18,12 +19,14 @@ SpreaderBookkeepingData::SpreaderBookkeepingData ( _inited_new (inited_new), _n_legs(legs.size()), _n_quoting_legs(_calc_n_quoting_legs(legs)), +_side(order_def.side()), _order_def(order_def), _reported_state(){ LOG(OrderMan, Info, "SpreaderBookkeepingData.cons started " "key: {} n_legs: {}", spreader_order_key.value(), _n_legs); _leg_data.reserve(_n_legs); + _nlegs_crossed_hedges.reserve(_n_legs); size_t i=0; for (const auto& leg_property_data : legs){ _leg_data.emplace_back(spreader_pao_services, @@ -33,6 +36,7 @@ _reported_state(){ quoter_ord_defs[i], hedger_ord_defs[i], leg_property_data); + _nlegs_crossed_hedges.emplace_back(std::pair(os::nan(),0.0)); bts_assert (_sec_key_to_index.find(leg_property_data.security_key()) == _sec_key_to_index.end()); _sec_key_to_index.insert(std::pair( diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h index 994b401d0..54b7b595d 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h @@ -24,6 +24,7 @@ private: const bool _inited_new; const size_t _n_legs; const size_t _n_quoting_legs; + const api::Side _side; // when the order is not created from the disk // (ie created live) this is set to true const api::orderman::OrderDefinition _order_def; @@ -35,6 +36,10 @@ private: Price _input_price; Price _effective_price; Quantity _quantity; + // this is used to pass in the calcuated hedge price/qty + // (preallocating so we dont have to allocate + // memory when calculating and calculating values) + std::vector> _nlegs_crossed_hedges; private: size_t _calc_n_quoting_legs(const std::vector& legs); public: @@ -61,10 +66,14 @@ public: ///////////////////////////////// size_t n_legs() const { return _n_legs; } bool all_legs_resolved() const {return n_legs() > 0;} + api::Side side() const {return _side;} const api::orderman::OrderDefinition& order_def() const {return _order_def;} Price input_price () const {return _input_price;} Price effective_price () const {return _effective_price;} Quantity quantity () const {return _quantity;} + std::vector>& nlegs_crossed_hedges(){ + return _nlegs_crossed_hedges; + } // gets leg data from leg's sec key SpreaderLegData& get_leg_data (SecurityKey security_key){ boost::container::flat_map::const_iterator l_it = _sec_key_to_index.find(security_key.value()); diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index aa890d3d1..2c7873a91 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -790,6 +790,47 @@ public: quote_fill_quantity, hedge_leg); } + static bool compute_nlegs_crossed_hedges( + api::Side spr_side, + Price spr_price, + size_t n_legs, + const LegPropertyData& q_leg, + Price q_fill_price, + double q_fill_qty, + const ISpreaderManager& spreader, + std::vector>& out_drivers){ + // spr_cross_price is the spreader price computed by + // crossing all the hedges ie + // spr_cross_price=sum(ratio*price) where for the filled leg + // it is the fill price and for the hedging legs + // it is the cross that satisifies the size + double spr_cross_price=q_fill_price.value()*q_leg.price_multiplier(); + for (size_t i=0;i Date: Fri, 17 May 2019 19:51:37 -0400 Subject: [PATCH 002/153] code complete, testing remains --- .../orders/spreader_orders/SpreaderOrder.cpp | 30 +++++++++++++------ .../SpreaderWorkingStateWorker.cpp | 9 ++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 6b9cf169a..326d317ba 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -9,6 +9,7 @@ #include "lib/orderman/events/SettlementUpdateEvent.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/helper_macros.h" +#include "lib/os/os.h" namespace bts { namespace orderman { @@ -373,7 +374,7 @@ void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, const LegPropertyData& q_leg_data = bk_data.get_leg_data(filled_leg_index).leg_property_data(); const ISpreaderManager& spreader = *this; - SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), + bool is_succ = SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), bk_data.effective_price(), bk_data.n_legs(), q_leg_data, @@ -381,6 +382,9 @@ void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, filled_quantity, spreader, bk_data.nlegs_crossed_hedges()); + if(!is_succ) + for(auto& p : bk_data.nlegs_crossed_hedges()) + p.second = os::nan(); } void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, Price fill_price, @@ -401,14 +405,22 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, computed_hedge_price, hedge_quantity); else{ - const QuoteLegDriver& driver_data = filled_leg_data.quoter().driver_data(quote_order_price, hedge_leg_index); - SpreaderUtils::compute_hedge_price_size_n_legs (filled_leg_data.leg_property_data(), - fill_price, - fill_quantity, - hedged_leg_data.leg_property_data(), - driver_data, - computed_hedge_price, - hedge_quantity); + auto& crossed_hedge = + _bookkeeping_data().nlegs_crossed_hedges()[hedge_leg_index]; + if(!os::is_nan(crossed_hedge.second)){ + hedge_price = crossed_hedge.first; + hedge_quantity = crossed_hedge.second; + } + else{ + const QuoteLegDriver& driver_data = filled_leg_data.quoter().driver_data(quote_order_price, hedge_leg_index); + SpreaderUtils::compute_hedge_price_size_n_legs (filled_leg_data.leg_property_data(), + fill_price, + fill_quantity, + hedged_leg_data.leg_property_data(), + driver_data, + computed_hedge_price, + hedge_quantity); + } } hedge_price = SpreaderUtils::apply_pre_hedge_parameters (hedged_leg_data.leg_property_data().leg_params().pre_hedge_params(), hedged_leg_data.book(), diff --git a/src/lib/orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp b/src/lib/orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp index f501298e0..6d81f0a30 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp @@ -99,6 +99,15 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data) // getting overfilled Quantity filled_qty=cw.comp_fill_qty(updated_order); bts_assert (filled_qty.value() > 0); + // this is a round about way to do this, so we + // dont have to change the structure of this code. + // this function fills in the value of the cross hedges + // in _bookkeeping_data().nlegs_crossed_hedges(), which + // is later retreived in _compute_hedge_price_quantity + _spreader->_compute_nlegs_crossed_hedges( + leg_data.leg_property_data().leg_index(), + updated_order.last_update_filled_price(), + filled_qty.value()); for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data().n_legs(); q_idx++){ if (q_idx!=leg_data.leg_property_data().leg_index()){ SpreaderLegData& other_leg_data = From 1fd66a17ca5b3b10ae12c17968394afebbc4cc83 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Mon, 20 May 2019 19:57:18 -0400 Subject: [PATCH 003/153] better tests --- .vscode/launch.json | 4 +- .../spreader_orders/spreader/SpreaderUtils.h | 24 +- .../spreader_orders/tests/spreader_tests.cpp | 900 ++++++++++-------- 3 files changed, 540 insertions(+), 388 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 71b5a8e9e..11df8fec7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -135,13 +135,13 @@ "name": "Spreader Tests", "type": "cppdbg", "request": "launch", - "program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd", + "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ "test", "[spreader]" ], "stopAtEntry": false, - "cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/", + "cwd": "/home/bts/spark/build/ninja_debug/server/", "environment": [], "externalConsole": true, "MIMode": "gdb", diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index 2c7873a91..3b8110ae3 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -792,19 +792,18 @@ public: } static bool compute_nlegs_crossed_hedges( api::Side spr_side, - Price spr_price, + Price spr_price, size_t n_legs, const LegPropertyData& q_leg, Price q_fill_price, double q_fill_qty, const ISpreaderManager& spreader, std::vector>& out_drivers){ - // spr_cross_price is the spreader price computed by - // crossing all the hedges ie - // spr_cross_price=sum(ratio*price) where for the filled leg - // it is the fill price and for the hedging legs - // it is the cross that satisifies the size - double spr_cross_price=q_fill_price.value()*q_leg.price_multiplier(); + // spr_cross_price = q_leg_ratio*q_fill_price + + // sum(hedge_leg_ratio*hedge_cross_price) + // where hedge_cross_price is the hedge price found by + // crossing the hedge market to aquire the hedge qty + double spr_cross_price = q_fill_price.value()*q_leg.price_multiplier(); for (size_t i=0;i legs; + legs.push_back(leg_1);legs.push_back(leg_2);legs.push_back(leg_3);legs.push_back(leg_4); + Security sec; + fill_synthetic_security (sec, 2000, "4legs", legs); + // execution params/order def + LegSpreaderParams lsp_1, lsp_2, lsp_3, lsp_4; + fill_leg_spreader_params(lsp_1, 1.0, false, 1.0);fill_leg_spreader_params(lsp_2, 1.0, false, 1.0); + fill_leg_spreader_params(lsp_3, 1.0, false, 1.0);fill_leg_spreader_params(lsp_4, -1.0, true, 1.0); + // // post hedge time out here + std::vector params; + params.push_back(lsp_1);params.push_back(lsp_2); + params.push_back(lsp_3);params.push_back(lsp_4); + bts::api::orderman::OrderDefinition order_def; + fill_spreader_order_definition(order_def, Side::SIDE_BUY, sec, params); + // putting it together in SpreaderBookkeepingData + SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices + testonly_spreader_pao_services = _create_spreader_pao_services(sec); + SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services); + std::vector leg_props; + std::vector quoter_ord_defs, + hedger_ord_defs; + SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs); + SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs); + SpreaderBookkeepingData& bookkeeping_data = spreader_manager.bookkeeping_data(); + SECTION ("buy"){ + fill_book(spreader_manager, 0, new double[5]{65.31,65.32,65.34,65.36, 65.40}, + new uint16_t[5]{12,18,31,19,20}, + bts::api::Side::SIDE_SELL, + true); + fill_book(spreader_manager, 1, new double[5]{64.40,64.41,64.43,64.45,64.50}, + new uint16_t[5]{35,5,20,12,28}, + bts::api::Side::SIDE_SELL, + true); + fill_book(spreader_manager, 2, new double[5]{64.10,64.18,64.19,64.20,64.21}, + new uint16_t[5]{11,3,2,4,80}, + bts::api::Side::SIDE_SELL, + true); + // selling at 0.02 + // 65.34 + 64.41 + 64.21 - 189.8 = 0.02 + std::vector> drivers; + drivers.reserve(4); + // not enough to hedge + bool res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_BUY, + Price(-0.03), + 4, + spreader_manager.get_leg_data(3), + Price(193.99), + 101, + spreader_manager, + drivers); + REQUIRE(!res); + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_BUY, + Price(-0.03), + 4, + spreader_manager.get_leg_data(3), + Price(193.99), + 40, + spreader_manager, + drivers); + REQUIRE(res); + REQUIRE(securities::is_same(drivers[0].first,Price(65.34))); + REQUIRE(securities::is_same(drivers[1].first,Price(64.41))); + REQUIRE(securities::is_same(drivers[2].first,Price(64.21))); + REQUIRE(securities::is_same(Price(drivers[0].second),Price(40))); + REQUIRE(securities::is_same(Price(drivers[1].second),Price(40))); + REQUIRE(securities::is_same(Price(drivers[2].second),Price(40))); + // quote gets better fill + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_BUY, + Price(-0.03), + 4, + spreader_manager.get_leg_data(3), + Price(193.99+0.01), + 40, + spreader_manager, + drivers); + REQUIRE(res); + // quote gets worse fill + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_BUY, + Price(-0.03), + 4, + spreader_manager.get_leg_data(3), + Price(193.99-0.01), + 40, + spreader_manager, + drivers); + REQUIRE(!res); + // // generalize the above + for (int i=0;i<1000;i++){ + double quote_fill_price=gen_rand(193.99-1.0,193.99+1.0,0.01); + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_BUY, + Price(-0.03), + 4, + spreader_manager.get_leg_data(3), + Price(quote_fill_price), + 40, + spreader_manager, + drivers); + if(quote_fill_price>=193.99) + REQUIRE(res); + else + REQUIRE(!res); + } + } + SECTION ("sell"){ + fill_book(spreader_manager, 0, new double[5]{62.1, 62, 61.99, 61.95, 61.93}, + new uint16_t[5]{18,12,30,19,21}, + bts::api::Side::SIDE_BUY, + true); + fill_book(spreader_manager, 1, new double[5]{63.2, 63.15, 62.99, 62.93, 61.90}, + new uint16_t[5]{11,15,2,32,40}, + bts::api::Side::SIDE_BUY, + true); + fill_book(spreader_manager, 2, new double[5]{65.2, 65.18, 64.98, 64.93, 64.90}, + new uint16_t[5]{11,12,19,9,50}, + bts::api::Side::SIDE_BUY, + true); + // selling at 0.02 + // 61.99 + 62.93 + 64.93 - 189.8 = 0.02 + std::vector> drivers; + drivers.reserve(4); + // not enough to hedge + bool res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_SELL, + Price(0.02), + 4, + spreader_manager.get_leg_data(3), + Price(189.83), + 101, + spreader_manager, + drivers); + REQUIRE(!res); + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_SELL, + Price(0.02), + 4, + spreader_manager.get_leg_data(3), + Price(189.83), + 50, + spreader_manager, + drivers); + REQUIRE(res); + REQUIRE(securities::is_same(drivers[0].first,Price(61.99))); + REQUIRE(securities::is_same(drivers[1].first,Price(62.93))); + REQUIRE(securities::is_same(drivers[2].first,Price(64.93))); + REQUIRE(securities::is_same(Price(drivers[0].second),Price(50))); + REQUIRE(securities::is_same(Price(drivers[1].second),Price(50))); + REQUIRE(securities::is_same(Price(drivers[2].second),Price(50))); + // quote gets better fill + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_SELL, + Price(0.02), + 4, + spreader_manager.get_leg_data(3), + Price(189.83-0.01), + 50, + spreader_manager, + drivers); + REQUIRE(res); + // quote gets worse fill + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_SELL, + Price(0.02), + 4, + spreader_manager.get_leg_data(3), + Price(189.83+0.01), + 50, + spreader_manager, + drivers); + REQUIRE(!res); + // generalize the above + for (int i=0;i<1000;i++){ + double quote_fill_price=gen_rand(189.83-1.0,189.83+1.0,0.01); + res = SpreaderUtils::compute_nlegs_crossed_hedges( + api::Side::SIDE_SELL, + Price(0.02), + 4, + spreader_manager.get_leg_data(3), + Price(quote_fill_price), + 50, + spreader_manager, + drivers); + if(quote_fill_price<=189.83) + REQUIRE(res); + else + REQUIRE(!res); + } + } +} TEST_CASE ("quote driver caching", "[spreader]"){ SECTION("zero_search"){ QuoteDriverSetHistory driver_cache(5,10,0.05); @@ -2832,384 +3032,330 @@ TEST_CASE ("spreader_perf_2legs", "[spreader]") std::cout << "2 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl; } } -TEST_CASE ("spreader_perf_4legs", "[spreader]") -{ - bts::orderman::OrderKey order_key(0); - Price spreader_price = Price (40); - Quantity spreader_quantity = Quantity (250); - ContractLeg leg_1, leg_2, leg_3, qleg_4; - fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX, - 2019, 1, 15, - 1.0, 1.0, - api::Side::SIDE_BUY, - 1.0, 0.005); - fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX, - 2019, 2, 15, - 1.0, 1.0, - api::Side::SIDE_BUY, - 1.0, 0.005); - fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX, - 2019, 3, 15, - 1.0, 1.0, - api::Side::SIDE_BUY, - 1.0, 0.005); - fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX, - 2019, 4, 15, - -1.0, -1.0, - api::Side::SIDE_SELL, - 1.0, 0.005); - std::vector contract_legs; - contract_legs.push_back(leg_1); - contract_legs.push_back(leg_2); - contract_legs.push_back(leg_3); - contract_legs.push_back(qleg_4); - Security synthetic_security; - fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs); - // execution params/order def - LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4; - fill_leg_spreader_params(lsp_1, 1.0, false, 1.0); - fill_leg_spreader_params(lsp_2, 1.0, false, 1.0); - fill_leg_spreader_params(lsp_3, 1.0, false, 1.0); - fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0); - std::vector spreader_params; - spreader_params.push_back(lsp_1); - spreader_params.push_back(lsp_2); - spreader_params.push_back(lsp_3); - spreader_params.push_back(qlsp_4); - bts::api::orderman::OrderDefinition order_def; - fill_spreader_order_definition(order_def, Side::SIDE_BUY, - synthetic_security, - spreader_params); - // putting it together in SpreaderBookkeepingData - SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices - testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security); - SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services); - std::vector leg_props; - std::vector quoter_ord_defs, - hedger_ord_defs; - SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs); - SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs); - SpreaderOrder spreader; - REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START); - // on_init_new and activate - spreader.test_only_on_init_new (order_def, pao_services); - SpreaderOrderServices order_services (nullptr); - spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services); - SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data(); - verify_basic_spreader_state (spreader, - SpreaderState::WORKING, - spreader_price, - spreader_quantity); - SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); - verify_complete_quoter_state (qleg_data_4.quoter(), - QuoterState::WAITING_FOR_SIZE, - false, - spreader_price, - spreader_quantity, - Quantity (0), - Quantity(0), - Price(0), - Quantity(0), - spreader_quantity, - false); - Book book_1 = get_book(SecurityKey (leg_1.security().id()), - 5, - new double[5] {20, 19.995, 19.990, 19.985, 19.980}, - new uint16_t[5] {100, 50, 75, 150, 25}, - new double[5] {20.005, 20.010, 20.015, 20.020, 20.025}, - new uint16_t[5] {25,125,175,225,100}, - true), - book_1_alt = get_book(SecurityKey (leg_1.security().id()), - 5, - new double[5] {20.005, 19.990, 19.890, 19.885, 19.780}, - new uint16_t[5] {10, 25, 75, 15, 25}, - new double[5] {20.010, 20.015, 20.025, 20.030, 20.035}, - new uint16_t[5] {10, 25, 75, 15, 25}, - true), - book_2 = get_book(SecurityKey (leg_2.security().id()), - 5, - new double[5] {20.030,20.025,20.020,20.015,20.010}, - new uint16_t[5] {100, 100, 200, 250, 75}, - new double[5] {20.035, 20.040, 20.045, 20.050, 20.055}, - new uint16_t[5] {250,275,100,5,15}, - true), - book_2_alt = get_book(SecurityKey (leg_2.security().id()), - 5, - new double[5] {20.050,20.035,20.030,20.035,20.030}, - new uint16_t[5] {10, 10, 20, 250, 75}, - new double[5] {20.055, 20.060, 20.065, 20.070, 20.085}, - new uint16_t[5] {25,275,100,5,15}, - true), - book_3 = get_book(SecurityKey (leg_3.security().id()), - 5, - new double[5] {19.985,19.980,19.975,19.970,19.965}, - new uint16_t[5] {75,250,125,100,300}, - new double[5] {19.990,19.995,20.000,20.005,20.010}, - new uint16_t[5] {100,150,250,25,125}, - true), - book_3_alt = get_book(SecurityKey (leg_3.security().id()), - 5, - new double[5] {19.885,19.875,19.870,19.765,19.760}, - new uint16_t[5] {75,25,125,100,300}, - new double[5] {19.890,19.895,19.900,19.910,19.915}, - new uint16_t[5] {100,50,50,125,125}, - true); - spreader.test_only_on_book_update (BookUpdateEvent(book_1, - trace::CorID())); - spreader.test_only_on_book_update (BookUpdateEvent(book_2, - trace::CorID())); - spreader.test_only_on_book_update (BookUpdateEvent(book_3, - trace::CorID())); - verify_complete_quoter_state (qleg_data_4.quoter(), - QuoterState::WAITING_FOR_FIRST_ACK, - false, - spreader_price, - spreader_quantity, - Quantity (0), - Quantity(0), - Price(20.045), - spreader_quantity, - spreader_quantity, - false); - spreader.test_only_on_child_order_update (qleg_data_4.order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (0), - Price (0)); - verify_complete_quoter_state (qleg_data_4.quoter(), - QuoterState::WORKING, - false, - spreader_price, - spreader_quantity, - Quantity (0), - Quantity(0), - Price(20.045), - spreader_quantity, - spreader_quantity, - false); - SECTION ("Testing Hedge Reuse"){ - Quantity t_qty; - Price t_price; - // to keep the compiler warning happy - LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(), - book_2_alt.n_levels(), - book_3_alt.n_levels()); - // this sends fills, makes sure the hedgers are - // working, and on fill and recycled - // the hedge prices used are - // 20.015, 20.035 and 19.995 - SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); - // lets concentrate on the hedges sent on leg 1 - SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0); - auto& hm_1 = hleg_1.hedger_manager(); - REQUIRE(hm_1.n_active_hedgers()==0); - // send a 1 lot quoter fill ///////// - ///////////////////////////////////// - spreader.test_only_on_child_order_update (qleg_data_4.order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (1), - Price (20.045)); - REQUIRE(hm_1.n_active_hedgers()==1); - // legs 2 and 3 - REQUIRE(securities::test_price_equality( - bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), - 20.035, - 0.005)); - REQUIRE(securities::test_price_equality( - bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), - 19.995, - 0.005)); - // back to leg1's hedger, this is what will - // be mainly tested here on - { - auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); - REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); - REQUIRE(securities::test_price_equality( - hedger_1_1.order_state().out_price().value(), - 20.015, - 0.005)); - REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); - spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (0), - Price (0)); - REQUIRE(hedger_1_1.state()==HedgerState::WORKING); - // fill h1 and it goes back to start //// - ///////////////////////////////////////// - spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, - Quantity (1), - Price (20.015)); - REQUIRE(hedger_1_1.state()==HedgerState::START); - } - REQUIRE(hm_1.n_active_hedgers()==0); - hm_1.total_fills(t_qty, t_price); - REQUIRE(t_qty.value() == 1); - REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005)); - //////////////////////////// - // send another 1 lot q fill - spreader.test_only_on_child_order_update (qleg_data_4.order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (2), - Price (20.045)); - REQUIRE(hm_1.n_active_hedgers()==1); - // since 2 and 3 were never filled, they should have 2 hedgers - REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2); - REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2); - { - auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); - REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); - REQUIRE(securities::test_price_equality( - hedger_1_1.order_state().out_price().value(), - 20.015, - 0.005)); - REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); - spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (0), - Price (0)); - REQUIRE(hedger_1_1.state()==HedgerState::WORKING); - // fill h1 /////// - ////////////////// - spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, - Quantity (1), - Price (20.025)); - REQUIRE(hedger_1_1.state()==HedgerState::START); - } - REQUIRE(hm_1.n_active_hedgers()==0); - hm_1.total_fills(t_qty, t_price); - REQUIRE(t_qty.value() == 2); - REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005)); - // send 4 more q fills ///////// - //////////////////////////////// - for(size_t f=4; f <= 10; f+= 2) - spreader.test_only_on_child_order_update (qleg_data_4.order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (f), - Price (20.045)); - REQUIRE(hm_1.n_active_hedgers()==4); - REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6); - REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6); - REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0); - for (size_t i=0;i<4;i++){ - auto& h = hm_1.test_only_get_hedger_at(i); - REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK); - REQUIRE(h.order_state().out_quantity().value()==2); - REQUIRE(securities::test_price_equality( - h.order_state().out_price().value(), - 20.015, - 0.005)); - } - // send order acks ////////// - ///////////////////////////// - for (size_t i=0;i<4;i++){ - auto& h = hm_1.test_only_get_hedger_at(i); - spreader.test_only_on_child_order_update (h.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (0), - Price (0)); - REQUIRE(h.state()==HedgerState::WORKING); - REQUIRE(h.order_state().out_quantity().value()==2); - REQUIRE(securities::test_price_equality( - h.order_state().out_price().value(), - 20.015, - 0.005)); - } - REQUIRE(hm_1.n_active_hedgers()==4); - // fills 1 lot on each order // - /////////////////////////////// - for (size_t i=0;i<4;i++){ - auto& h = hm_1.test_only_get_hedger_at(i); - // previous hedger fills were 20.015, 20.025 - spreader.test_only_on_child_order_update (h.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_WORKING, - Quantity (1), - Price (20.035)); - REQUIRE(h.state()==HedgerState::WORKING); - REQUIRE(h.order_state().out_quantity().value()==2); - REQUIRE(securities::test_price_equality( - h.order_state().out_price().value(), - 20.015, - 0.005)); - } - REQUIRE(hm_1.n_active_hedgers()==4); - hm_1.total_fills(t_qty, t_price); - REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); - REQUIRE(t_qty.value()==6); +// TEST_CASE ("spreader_perf_4legs", "[spreader]") +// { +// bts::orderman::OrderKey order_key(0); +// Price spreader_price = Price (40); +// Quantity spreader_quantity = Quantity (250); +// ContractLeg leg_1, leg_2, leg_3, qleg_4; +// fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX, +// 2019, 1, 15, +// 1.0, 1.0, +// api::Side::SIDE_BUY, +// 1.0, 0.005); +// fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX, +// 2019, 2, 15, +// 1.0, 1.0, +// api::Side::SIDE_BUY, +// 1.0, 0.005); +// fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX, +// 2019, 3, 15, +// 1.0, 1.0, +// api::Side::SIDE_BUY, +// 1.0, 0.005); +// fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX, +// 2019, 4, 15, +// -1.0, -1.0, +// api::Side::SIDE_SELL, +// 1.0, 0.005); +// std::vector contract_legs; +// contract_legs.push_back(leg_1); +// contract_legs.push_back(leg_2); +// contract_legs.push_back(leg_3); +// contract_legs.push_back(qleg_4); +// Security synthetic_security; +// fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs); +// // execution params/order def +// LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4; +// fill_leg_spreader_params(lsp_1, 1.0, false, 1.0); +// fill_leg_spreader_params(lsp_2, 1.0, false, 1.0); +// fill_leg_spreader_params(lsp_3, 1.0, false, 1.0); +// fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0); +// std::vector spreader_params; +// spreader_params.push_back(lsp_1); +// spreader_params.push_back(lsp_2); +// spreader_params.push_back(lsp_3); +// spreader_params.push_back(qlsp_4); +// bts::api::orderman::OrderDefinition order_def; +// fill_spreader_order_definition(order_def, Side::SIDE_BUY, +// synthetic_security, +// spreader_params); +// // putting it together in SpreaderBookkeepingData +// SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices +// testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security); +// SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services); +// std::vector leg_props; +// std::vector quoter_ord_defs, +// hedger_ord_defs; +// SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs); +// SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs); +// SpreaderOrder spreader; +// REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START); +// // on_init_new and activate +// spreader.test_only_on_init_new (order_def, pao_services); +// SpreaderOrderServices order_services (nullptr); +// spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services); +// SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data(); +// verify_basic_spreader_state (spreader, +// SpreaderState::WORKING, +// spreader_price, +// spreader_quantity); +// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); +// verify_complete_quoter_state (qleg_data_4.quoter(), +// QuoterState::WAITING_FOR_SIZE, +// false, +// spreader_price, +// spreader_quantity, +// Quantity (0), +// Quantity(0), +// Price(0), +// Quantity(0), +// spreader_quantity, +// false); +// Book book_1 = get_book(SecurityKey (leg_1.security().id()), +// 5, +// new double[5] {20, 19.995, 19.990, 19.985, 19.980}, +// new uint16_t[5] {100, 50, 75, 150, 25}, +// new double[5] {20.005, 20.010, 20.015, 20.020, 20.025}, +// new uint16_t[5] {25,125,175,225,100}, +// true), +// book_1_alt = get_book(SecurityKey (leg_1.security().id()), +// 5, +// new double[5] {20.005, 19.990, 19.890, 19.885, 19.780}, +// new uint16_t[5] {10, 25, 75, 15, 25}, +// new double[5] {20.010, 20.015, 20.025, 20.030, 20.035}, +// new uint16_t[5] {10, 25, 75, 15, 25}, +// true), +// book_2 = get_book(SecurityKey (leg_2.security().id()), +// 5, +// new double[5] {20.030,20.025,20.020,20.015,20.010}, +// new uint16_t[5] {100, 100, 200, 250, 75}, +// new double[5] {20.035, 20.040, 20.045, 20.050, 20.055}, +// new uint16_t[5] {250,275,100,5,15}, +// true), +// book_2_alt = get_book(SecurityKey (leg_2.security().id()), +// 5, +// new double[5] {20.050,20.035,20.030,20.035,20.030}, +// new uint16_t[5] {10, 10, 20, 250, 75}, +// new double[5] {20.055, 20.060, 20.065, 20.070, 20.085}, +// new uint16_t[5] {25,275,100,5,15}, +// true), +// book_3 = get_book(SecurityKey (leg_3.security().id()), +// 5, +// new double[5] {19.985,19.980,19.975,19.970,19.965}, +// new uint16_t[5] {75,250,125,100,300}, +// new double[5] {19.990,19.995,20.000,20.005,20.010}, +// new uint16_t[5] {100,150,250,25,125}, +// true), +// book_3_alt = get_book(SecurityKey (leg_3.security().id()), +// 5, +// new double[5] {19.885,19.875,19.870,19.765,19.760}, +// new uint16_t[5] {75,25,125,100,300}, +// new double[5] {19.890,19.895,19.900,19.910,19.915}, +// new uint16_t[5] {100,50,50,125,125}, +// true); +// spreader.test_only_on_book_update (BookUpdateEvent(book_1, +// trace::CorID())); +// spreader.test_only_on_book_update (BookUpdateEvent(book_2, +// trace::CorID())); +// spreader.test_only_on_book_update (BookUpdateEvent(book_3, +// trace::CorID())); +// verify_complete_quoter_state (qleg_data_4.quoter(), +// QuoterState::WAITING_FOR_FIRST_ACK, +// false, +// spreader_price, +// spreader_quantity, +// Quantity (0), +// Quantity(0), +// Price(20.045), +// spreader_quantity, +// spreader_quantity, +// false); +// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (0), +// Price (0)); +// verify_complete_quoter_state (qleg_data_4.quoter(), +// QuoterState::WORKING, +// false, +// spreader_price, +// spreader_quantity, +// Quantity (0), +// Quantity(0), +// Price(20.045), +// spreader_quantity, +// spreader_quantity, +// false); +// SECTION ("Testing Hedge Reuse"){ +// Quantity t_qty; +// Price t_price; +// // to keep the compiler warning happy +// LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(), +// book_2_alt.n_levels(), +// book_3_alt.n_levels()); +// // this sends fills, makes sure the hedgers are +// // working, and on fill and recycled +// // the hedge prices used are +// // 20.015, 20.035 and 19.995 +// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); +// // lets concentrate on the hedges sent on leg 1 +// SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0); +// auto& hm_1 = hleg_1.hedger_manager(); +// REQUIRE(hm_1.n_active_hedgers()==0); +// // send a 1 lot quoter fill ///////// +// ///////////////////////////////////// +// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (1), +// Price (20.045)); +// REQUIRE(hm_1.n_active_hedgers()==1); +// // legs 2 and 3 +// REQUIRE(securities::test_price_equality( +// bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), +// 20.035, +// 0.005)); +// REQUIRE(securities::test_price_equality( +// bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), +// 19.995, +// 0.005)); +// // back to leg1's hedger, this is what will +// // be mainly tested here on +// { +// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); +// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); +// REQUIRE(securities::test_price_equality( +// hedger_1_1.order_state().out_price().value(), +// 20.015, +// 0.005)); +// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); +// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (0), +// Price (0)); +// REQUIRE(hedger_1_1.state()==HedgerState::WORKING); +// // fill h1 and it goes back to start //// +// ///////////////////////////////////////// +// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, +// Quantity (1), +// Price (20.015)); +// REQUIRE(hedger_1_1.state()==HedgerState::START); +// } +// REQUIRE(hm_1.n_active_hedgers()==0); +// hm_1.total_fills(t_qty, t_price); +// REQUIRE(t_qty.value() == 1); +// REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005)); +// //////////////////////////// +// // send another 1 lot q fill +// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (2), +// Price (20.045)); +// REQUIRE(hm_1.n_active_hedgers()==1); +// // since 2 and 3 were never filled, they should have 2 hedgers +// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2); +// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2); +// { +// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); +// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); +// REQUIRE(securities::test_price_equality( +// hedger_1_1.order_state().out_price().value(), +// 20.015, +// 0.005)); +// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); +// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (0), +// Price (0)); +// REQUIRE(hedger_1_1.state()==HedgerState::WORKING); +// // fill h1 /////// +// ////////////////// +// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, +// Quantity (1), +// Price (20.025)); +// REQUIRE(hedger_1_1.state()==HedgerState::START); +// } +// REQUIRE(hm_1.n_active_hedgers()==0); +// hm_1.total_fills(t_qty, t_price); +// REQUIRE(t_qty.value() == 2); +// REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005)); +// // send 4 more q fills ///////// +// //////////////////////////////// +// for(size_t f=4; f <= 10; f+= 2) +// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (f), +// Price (20.045)); +// REQUIRE(hm_1.n_active_hedgers()==4); +// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6); +// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6); +// REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0); +// for (size_t i=0;i<4;i++){ +// auto& h = hm_1.test_only_get_hedger_at(i); +// REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK); +// REQUIRE(h.order_state().out_quantity().value()==2); +// REQUIRE(securities::test_price_equality( +// h.order_state().out_price().value(), +// 20.015, +// 0.005)); +// } +// // send order acks ////////// +// ///////////////////////////// +// for (size_t i=0;i<4;i++){ +// auto& h = hm_1.test_only_get_hedger_at(i); +// spreader.test_only_on_child_order_update (h.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (0), +// Price (0)); +// REQUIRE(h.state()==HedgerState::WORKING); +// REQUIRE(h.order_state().out_quantity().value()==2); +// REQUIRE(securities::test_price_equality( +// h.order_state().out_price().value(), +// 20.015, +// 0.005)); +// } +// REQUIRE(hm_1.n_active_hedgers()==4); +// // fills 1 lot on each order // +// /////////////////////////////// +// for (size_t i=0;i<4;i++){ +// auto& h = hm_1.test_only_get_hedger_at(i); +// // previous hedger fills were 20.015, 20.025 +// spreader.test_only_on_child_order_update (h.order_state().order_key(), +// api::orderman::OrderStatus::ORDER_STATUS_WORKING, +// Quantity (1), +// Price (20.035)); +// REQUIRE(h.state()==HedgerState::WORKING); +// REQUIRE(h.order_state().out_quantity().value()==2); +// REQUIRE(securities::test_price_equality( +// h.order_state().out_price().value(), +// 20.015, +// 0.005)); +// } +// REQUIRE(hm_1.n_active_hedgers()==4); +// hm_1.total_fills(t_qty, t_price); +// REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); +// REQUIRE(t_qty.value()==6); - // fills additional 1 lot on each, /////// - // which should complete the orders ////// - ////////////////////////////////////////// - for (size_t i=0;i<4;i++){ - auto& h = hm_1.test_only_get_hedger_at(i); - // previous hedger fills were 20.015, 20.025 - spreader.test_only_on_child_order_update (h.order_state().order_key(), - api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, - Quantity (2), - Price (20.03)); - REQUIRE(h.state()==HedgerState::START); - REQUIRE(h.order_state().out_quantity().value()==0); - } - REQUIRE(hm_1.n_active_hedgers()==0); - hm_1.total_fills(t_qty, t_price); - REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); - REQUIRE(t_qty.value()==10); - } - // SECTION ("N_requotes_1_hedge_M_times"){ - // // N book updates followed by M fills - // // (does the hedge time stays the same as - // // in a tight loop in the "hedges" test ?) - // uint32_t n_runs=5, - // n_book_updates=200, - // n_hedges = 5; - // // since its the same quoting order we are filling - // // and the code downstream expects accumulated filled qty - // uint32_t total_filled_qty = 0, hi = 0; - // for (uint32_t i=0; i hedge_perfs; - // hedge_perfs.reserve(n_hedges); - // auto t0=utils::TimeStamp::now(); - // for (uint32_t b=0; b Date: Thu, 23 May 2019 12:37:30 -0400 Subject: [PATCH 004/153] tests pass --- src/lib/config/protos/spreader.proto | 1 + .../orders/spreader_orders/SpreaderOrder.cpp | 46 +- .../orders/spreader_orders/SpreaderOrder.h | 11 +- .../spreader_orders/tests/spreader_tests.cpp | 646 +++++++++--------- 4 files changed, 360 insertions(+), 344 deletions(-) diff --git a/src/lib/config/protos/spreader.proto b/src/lib/config/protos/spreader.proto index 9c1c80ac5..25d345c5f 100644 --- a/src/lib/config/protos/spreader.proto +++ b/src/lib/config/protos/spreader.proto @@ -13,4 +13,5 @@ message Spreader{ optional double fill_period_high = 30 [default=50]; } optional CacheWarming cache_warming = 20; + optional bool n_legs_recomp_hedges_on_fill = 30 [default=false]; } \ No newline at end of file diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 326d317ba..41c538beb 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -21,6 +21,9 @@ _cancelled (false), _report_dirty(false), _active(true), _cancel_hedges_on_cancel(false), +_n_legs_recomp_hedges_on_fill( + config::get().n_legs_recomp_hedges_on_fill()), +_n_legs_last_recomp_hedges_on_fill_succ(false), _cw_helper(), _cancel_timeout (5000),// TODO:get it from config _check_termination_period (250), // should also be a setting/config like above @@ -331,7 +334,7 @@ bool SpreaderOrder::_all_hedger_orders_in_terminal_state(){ } bool SpreaderOrder::_any_quoter_in_irrecoverable_error() const{ for (size_t q_idx = 0; q_idx < _bookkeeping_data().n_legs(); q_idx++){ - const SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx); + const SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx); if (leg_data.leg_property_data().is_active_quoting()) if (leg_data.quoter().irrecoverable_error()) return true; @@ -370,21 +373,23 @@ void SpreaderOrder::_send_hedge(size_t hedge_leg_index, void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, Price filled_price, double filled_quantity){ - SpreaderBookkeepingData& bk_data = _bookkeeping_data(); - const LegPropertyData& q_leg_data = - bk_data.get_leg_data(filled_leg_index).leg_property_data(); - const ISpreaderManager& spreader = *this; - bool is_succ = SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), - bk_data.effective_price(), - bk_data.n_legs(), - q_leg_data, - filled_price, - filled_quantity, - spreader, - bk_data.nlegs_crossed_hedges()); - if(!is_succ) - for(auto& p : bk_data.nlegs_crossed_hedges()) - p.second = os::nan(); + _n_legs_last_recomp_hedges_on_fill_succ = false; + if(_n_legs_recomp_hedges_on_fill && + _bookkeeping_data().n_legs() > 2){ + SpreaderBookkeepingData& bk_data = _bookkeeping_data(); + const LegPropertyData& q_leg_data = + bk_data.get_leg_data(filled_leg_index).leg_property_data(); + const ISpreaderManager& spreader = *this; + _n_legs_last_recomp_hedges_on_fill_succ = + SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), + bk_data.effective_price(), + bk_data.n_legs(), + q_leg_data, + filled_price, + filled_quantity, + spreader, + bk_data.nlegs_crossed_hedges()); + } } void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, Price fill_price, @@ -404,11 +409,12 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, hedged_leg_data.leg_property_data(), computed_hedge_price, hedge_quantity); - else{ - auto& crossed_hedge = + else{ + if(_n_legs_recomp_hedges_on_fill && + _n_legs_last_recomp_hedges_on_fill_succ){ + auto& crossed_hedge = _bookkeeping_data().nlegs_crossed_hedges()[hedge_leg_index]; - if(!os::is_nan(crossed_hedge.second)){ - hedge_price = crossed_hedge.first; + computed_hedge_price = crossed_hedge.first; hedge_quantity = crossed_hedge.second; } else{ diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index e57b53861..fb3574a24 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -62,8 +62,17 @@ private: const bool _active; // this is the main data bool _cancel_hedges_on_cancel; + // if this is true, then in the case of nlegs + // (>2) re-comp the hedge prices on a fill + const bool _n_legs_recomp_hedges_on_fill; + // this tells that in the n leg case the last + // call to compute cross hedges on a quoting fill + // was successful or not (TODO:need to put this, + // _n_legs_recomp_hedges_on_fill and SBKD::_nlegs_crossed_hedges + // in its own class) + bool _n_legs_last_recomp_hedges_on_fill_succ; // helps with forced calls, and all the artificiality of these - // calls are trapped here + // calls are trapped here CacheWarmingState _cw_helper; // timeout before cancel waiting goes to end const int64_t _cancel_timeout; diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index b7dbffc46..5ffa28931 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -3032,330 +3032,330 @@ TEST_CASE ("spreader_perf_2legs", "[spreader]") std::cout << "2 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl; } } -// TEST_CASE ("spreader_perf_4legs", "[spreader]") -// { -// bts::orderman::OrderKey order_key(0); -// Price spreader_price = Price (40); -// Quantity spreader_quantity = Quantity (250); -// ContractLeg leg_1, leg_2, leg_3, qleg_4; -// fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX, -// 2019, 1, 15, -// 1.0, 1.0, -// api::Side::SIDE_BUY, -// 1.0, 0.005); -// fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX, -// 2019, 2, 15, -// 1.0, 1.0, -// api::Side::SIDE_BUY, -// 1.0, 0.005); -// fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX, -// 2019, 3, 15, -// 1.0, 1.0, -// api::Side::SIDE_BUY, -// 1.0, 0.005); -// fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX, -// 2019, 4, 15, -// -1.0, -1.0, -// api::Side::SIDE_SELL, -// 1.0, 0.005); -// std::vector contract_legs; -// contract_legs.push_back(leg_1); -// contract_legs.push_back(leg_2); -// contract_legs.push_back(leg_3); -// contract_legs.push_back(qleg_4); -// Security synthetic_security; -// fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs); -// // execution params/order def -// LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4; -// fill_leg_spreader_params(lsp_1, 1.0, false, 1.0); -// fill_leg_spreader_params(lsp_2, 1.0, false, 1.0); -// fill_leg_spreader_params(lsp_3, 1.0, false, 1.0); -// fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0); -// std::vector spreader_params; -// spreader_params.push_back(lsp_1); -// spreader_params.push_back(lsp_2); -// spreader_params.push_back(lsp_3); -// spreader_params.push_back(qlsp_4); -// bts::api::orderman::OrderDefinition order_def; -// fill_spreader_order_definition(order_def, Side::SIDE_BUY, -// synthetic_security, -// spreader_params); -// // putting it together in SpreaderBookkeepingData -// SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices -// testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security); -// SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services); -// std::vector leg_props; -// std::vector quoter_ord_defs, -// hedger_ord_defs; -// SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs); -// SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs); -// SpreaderOrder spreader; -// REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START); -// // on_init_new and activate -// spreader.test_only_on_init_new (order_def, pao_services); -// SpreaderOrderServices order_services (nullptr); -// spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services); -// SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data(); -// verify_basic_spreader_state (spreader, -// SpreaderState::WORKING, -// spreader_price, -// spreader_quantity); -// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); -// verify_complete_quoter_state (qleg_data_4.quoter(), -// QuoterState::WAITING_FOR_SIZE, -// false, -// spreader_price, -// spreader_quantity, -// Quantity (0), -// Quantity(0), -// Price(0), -// Quantity(0), -// spreader_quantity, -// false); -// Book book_1 = get_book(SecurityKey (leg_1.security().id()), -// 5, -// new double[5] {20, 19.995, 19.990, 19.985, 19.980}, -// new uint16_t[5] {100, 50, 75, 150, 25}, -// new double[5] {20.005, 20.010, 20.015, 20.020, 20.025}, -// new uint16_t[5] {25,125,175,225,100}, -// true), -// book_1_alt = get_book(SecurityKey (leg_1.security().id()), -// 5, -// new double[5] {20.005, 19.990, 19.890, 19.885, 19.780}, -// new uint16_t[5] {10, 25, 75, 15, 25}, -// new double[5] {20.010, 20.015, 20.025, 20.030, 20.035}, -// new uint16_t[5] {10, 25, 75, 15, 25}, -// true), -// book_2 = get_book(SecurityKey (leg_2.security().id()), -// 5, -// new double[5] {20.030,20.025,20.020,20.015,20.010}, -// new uint16_t[5] {100, 100, 200, 250, 75}, -// new double[5] {20.035, 20.040, 20.045, 20.050, 20.055}, -// new uint16_t[5] {250,275,100,5,15}, -// true), -// book_2_alt = get_book(SecurityKey (leg_2.security().id()), -// 5, -// new double[5] {20.050,20.035,20.030,20.035,20.030}, -// new uint16_t[5] {10, 10, 20, 250, 75}, -// new double[5] {20.055, 20.060, 20.065, 20.070, 20.085}, -// new uint16_t[5] {25,275,100,5,15}, -// true), -// book_3 = get_book(SecurityKey (leg_3.security().id()), -// 5, -// new double[5] {19.985,19.980,19.975,19.970,19.965}, -// new uint16_t[5] {75,250,125,100,300}, -// new double[5] {19.990,19.995,20.000,20.005,20.010}, -// new uint16_t[5] {100,150,250,25,125}, -// true), -// book_3_alt = get_book(SecurityKey (leg_3.security().id()), -// 5, -// new double[5] {19.885,19.875,19.870,19.765,19.760}, -// new uint16_t[5] {75,25,125,100,300}, -// new double[5] {19.890,19.895,19.900,19.910,19.915}, -// new uint16_t[5] {100,50,50,125,125}, -// true); -// spreader.test_only_on_book_update (BookUpdateEvent(book_1, -// trace::CorID())); -// spreader.test_only_on_book_update (BookUpdateEvent(book_2, -// trace::CorID())); -// spreader.test_only_on_book_update (BookUpdateEvent(book_3, -// trace::CorID())); -// verify_complete_quoter_state (qleg_data_4.quoter(), -// QuoterState::WAITING_FOR_FIRST_ACK, -// false, -// spreader_price, -// spreader_quantity, -// Quantity (0), -// Quantity(0), -// Price(20.045), -// spreader_quantity, -// spreader_quantity, -// false); -// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (0), -// Price (0)); -// verify_complete_quoter_state (qleg_data_4.quoter(), -// QuoterState::WORKING, -// false, -// spreader_price, -// spreader_quantity, -// Quantity (0), -// Quantity(0), -// Price(20.045), -// spreader_quantity, -// spreader_quantity, -// false); -// SECTION ("Testing Hedge Reuse"){ -// Quantity t_qty; -// Price t_price; -// // to keep the compiler warning happy -// LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(), -// book_2_alt.n_levels(), -// book_3_alt.n_levels()); -// // this sends fills, makes sure the hedgers are -// // working, and on fill and recycled -// // the hedge prices used are -// // 20.015, 20.035 and 19.995 -// SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); -// // lets concentrate on the hedges sent on leg 1 -// SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0); -// auto& hm_1 = hleg_1.hedger_manager(); -// REQUIRE(hm_1.n_active_hedgers()==0); -// // send a 1 lot quoter fill ///////// -// ///////////////////////////////////// -// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (1), -// Price (20.045)); -// REQUIRE(hm_1.n_active_hedgers()==1); -// // legs 2 and 3 -// REQUIRE(securities::test_price_equality( -// bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), -// 20.035, -// 0.005)); -// REQUIRE(securities::test_price_equality( -// bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), -// 19.995, -// 0.005)); -// // back to leg1's hedger, this is what will -// // be mainly tested here on -// { -// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); -// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); -// REQUIRE(securities::test_price_equality( -// hedger_1_1.order_state().out_price().value(), -// 20.015, -// 0.005)); -// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); -// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (0), -// Price (0)); -// REQUIRE(hedger_1_1.state()==HedgerState::WORKING); -// // fill h1 and it goes back to start //// -// ///////////////////////////////////////// -// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, -// Quantity (1), -// Price (20.015)); -// REQUIRE(hedger_1_1.state()==HedgerState::START); -// } -// REQUIRE(hm_1.n_active_hedgers()==0); -// hm_1.total_fills(t_qty, t_price); -// REQUIRE(t_qty.value() == 1); -// REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005)); -// //////////////////////////// -// // send another 1 lot q fill -// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (2), -// Price (20.045)); -// REQUIRE(hm_1.n_active_hedgers()==1); -// // since 2 and 3 were never filled, they should have 2 hedgers -// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2); -// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2); -// { -// auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); -// REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); -// REQUIRE(securities::test_price_equality( -// hedger_1_1.order_state().out_price().value(), -// 20.015, -// 0.005)); -// REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); -// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (0), -// Price (0)); -// REQUIRE(hedger_1_1.state()==HedgerState::WORKING); -// // fill h1 /////// -// ////////////////// -// spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, -// Quantity (1), -// Price (20.025)); -// REQUIRE(hedger_1_1.state()==HedgerState::START); -// } -// REQUIRE(hm_1.n_active_hedgers()==0); -// hm_1.total_fills(t_qty, t_price); -// REQUIRE(t_qty.value() == 2); -// REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005)); -// // send 4 more q fills ///////// -// //////////////////////////////// -// for(size_t f=4; f <= 10; f+= 2) -// spreader.test_only_on_child_order_update (qleg_data_4.order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (f), -// Price (20.045)); -// REQUIRE(hm_1.n_active_hedgers()==4); -// REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6); -// REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6); -// REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0); -// for (size_t i=0;i<4;i++){ -// auto& h = hm_1.test_only_get_hedger_at(i); -// REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK); -// REQUIRE(h.order_state().out_quantity().value()==2); -// REQUIRE(securities::test_price_equality( -// h.order_state().out_price().value(), -// 20.015, -// 0.005)); -// } -// // send order acks ////////// -// ///////////////////////////// -// for (size_t i=0;i<4;i++){ -// auto& h = hm_1.test_only_get_hedger_at(i); -// spreader.test_only_on_child_order_update (h.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (0), -// Price (0)); -// REQUIRE(h.state()==HedgerState::WORKING); -// REQUIRE(h.order_state().out_quantity().value()==2); -// REQUIRE(securities::test_price_equality( -// h.order_state().out_price().value(), -// 20.015, -// 0.005)); -// } -// REQUIRE(hm_1.n_active_hedgers()==4); -// // fills 1 lot on each order // -// /////////////////////////////// -// for (size_t i=0;i<4;i++){ -// auto& h = hm_1.test_only_get_hedger_at(i); -// // previous hedger fills were 20.015, 20.025 -// spreader.test_only_on_child_order_update (h.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_WORKING, -// Quantity (1), -// Price (20.035)); -// REQUIRE(h.state()==HedgerState::WORKING); -// REQUIRE(h.order_state().out_quantity().value()==2); -// REQUIRE(securities::test_price_equality( -// h.order_state().out_price().value(), -// 20.015, -// 0.005)); -// } -// REQUIRE(hm_1.n_active_hedgers()==4); -// hm_1.total_fills(t_qty, t_price); -// REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); -// REQUIRE(t_qty.value()==6); +TEST_CASE ("spreader_perf_4legs", "[spreader]") +{ + bts::orderman::OrderKey order_key(0); + Price spreader_price = Price (40); + Quantity spreader_quantity = Quantity (250); + ContractLeg leg_1, leg_2, leg_3, qleg_4; + fill_future_contract_leg (leg_1, 2468, "SI", bts::api::Venue::VENUE_GLBX, + 2019, 1, 15, + 1.0, 1.0, + api::Side::SIDE_BUY, + 1.0, 0.005); + fill_future_contract_leg (leg_2, 2469, "SI", bts::api::Venue::VENUE_GLBX, + 2019, 2, 15, + 1.0, 1.0, + api::Side::SIDE_BUY, + 1.0, 0.005); + fill_future_contract_leg (leg_3, 2470, "SI", bts::api::Venue::VENUE_GLBX, + 2019, 3, 15, + 1.0, 1.0, + api::Side::SIDE_BUY, + 1.0, 0.005); + fill_future_contract_leg (qleg_4, 2471, "SI", bts::api::Venue::VENUE_GLBX, + 2019, 4, 15, + -1.0, -1.0, + api::Side::SIDE_SELL, + 1.0, 0.005); + std::vector contract_legs; + contract_legs.push_back(leg_1); + contract_legs.push_back(leg_2); + contract_legs.push_back(leg_3); + contract_legs.push_back(qleg_4); + Security synthetic_security; + fill_synthetic_security (synthetic_security, 2000, "synthetic_security", contract_legs); + // execution params/order def + LegSpreaderParams lsp_1, lsp_2, lsp_3, qlsp_4; + fill_leg_spreader_params(lsp_1, 1.0, false, 1.0); + fill_leg_spreader_params(lsp_2, 1.0, false, 1.0); + fill_leg_spreader_params(lsp_3, 1.0, false, 1.0); + fill_leg_spreader_params(qlsp_4, -1.0, true, 1.0); + std::vector spreader_params; + spreader_params.push_back(lsp_1); + spreader_params.push_back(lsp_2); + spreader_params.push_back(lsp_3); + spreader_params.push_back(qlsp_4); + bts::api::orderman::OrderDefinition order_def; + fill_spreader_order_definition(order_def, Side::SIDE_BUY, + synthetic_security, + spreader_params); + // putting it together in SpreaderBookkeepingData + SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices + testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security); + SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services); + std::vector leg_props; + std::vector quoter_ord_defs, + hedger_ord_defs; + SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,quoter_ord_defs,hedger_ord_defs); + SpreaderManagerForTest spreader_manager (order_key, order_def, pao_services, leg_props,quoter_ord_defs,hedger_ord_defs); + SpreaderOrder spreader; + REQUIRE(spreader.test_only_func_spreader_state() == SpreaderState::START); + // on_init_new and activate + spreader.test_only_on_init_new (order_def, pao_services); + SpreaderOrderServices order_services (nullptr); + spreader.test_only_on_activate(spreader_price, spreader_quantity, order_services); + SpreaderBookkeepingData& bookkeeping_data = spreader.test_only_func_spreader_full_data(); + verify_basic_spreader_state (spreader, + SpreaderState::WORKING, + spreader_price, + spreader_quantity); + SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); + verify_complete_quoter_state (qleg_data_4.quoter(), + QuoterState::WAITING_FOR_SIZE, + false, + spreader_price, + spreader_quantity, + Quantity (0), + Quantity(0), + Price(0), + Quantity(0), + spreader_quantity, + false); + Book book_1 = get_book(SecurityKey (leg_1.security().id()), + 5, + new double[5] {20, 19.995, 19.990, 19.985, 19.980}, + new uint16_t[5] {100, 50, 75, 150, 25}, + new double[5] {20.005, 20.010, 20.015, 20.020, 20.025}, + new uint16_t[5] {25,125,175,225,100}, + true), + book_1_alt = get_book(SecurityKey (leg_1.security().id()), + 5, + new double[5] {20.005, 19.990, 19.890, 19.885, 19.780}, + new uint16_t[5] {10, 25, 75, 15, 25}, + new double[5] {20.010, 20.015, 20.025, 20.030, 20.035}, + new uint16_t[5] {10, 25, 75, 15, 25}, + true), + book_2 = get_book(SecurityKey (leg_2.security().id()), + 5, + new double[5] {20.030,20.025,20.020,20.015,20.010}, + new uint16_t[5] {100, 100, 200, 250, 75}, + new double[5] {20.035, 20.040, 20.045, 20.050, 20.055}, + new uint16_t[5] {250,275,100,5,15}, + true), + book_2_alt = get_book(SecurityKey (leg_2.security().id()), + 5, + new double[5] {20.050,20.035,20.030,20.035,20.030}, + new uint16_t[5] {10, 10, 20, 250, 75}, + new double[5] {20.055, 20.060, 20.065, 20.070, 20.085}, + new uint16_t[5] {25,275,100,5,15}, + true), + book_3 = get_book(SecurityKey (leg_3.security().id()), + 5, + new double[5] {19.985,19.980,19.975,19.970,19.965}, + new uint16_t[5] {75,250,125,100,300}, + new double[5] {19.990,19.995,20.000,20.005,20.010}, + new uint16_t[5] {100,150,250,25,125}, + true), + book_3_alt = get_book(SecurityKey (leg_3.security().id()), + 5, + new double[5] {19.885,19.875,19.870,19.765,19.760}, + new uint16_t[5] {75,25,125,100,300}, + new double[5] {19.890,19.895,19.900,19.910,19.915}, + new uint16_t[5] {100,50,50,125,125}, + true); + spreader.test_only_on_book_update (BookUpdateEvent(book_1, + trace::CorID())); + spreader.test_only_on_book_update (BookUpdateEvent(book_2, + trace::CorID())); + spreader.test_only_on_book_update (BookUpdateEvent(book_3, + trace::CorID())); + verify_complete_quoter_state (qleg_data_4.quoter(), + QuoterState::WAITING_FOR_FIRST_ACK, + false, + spreader_price, + spreader_quantity, + Quantity (0), + Quantity(0), + Price(20.045), + spreader_quantity, + spreader_quantity, + false); + spreader.test_only_on_child_order_update (qleg_data_4.order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (0), + Price (0)); + verify_complete_quoter_state (qleg_data_4.quoter(), + QuoterState::WORKING, + false, + spreader_price, + spreader_quantity, + Quantity (0), + Quantity(0), + Price(20.045), + spreader_quantity, + spreader_quantity, + false); + SECTION ("Testing Hedge Reuse"){ + Quantity t_qty; + Price t_price; + // to keep the compiler warning happy + LOG(OrderMan, Info, "{} {} {}", book_1_alt.n_levels(), + book_2_alt.n_levels(), + book_3_alt.n_levels()); + // this sends fills, makes sure the hedgers are + // working, and on fill and recycled + // the hedge prices used are + // 20.015, 20.035 and 19.995 + SpreaderLegData& qleg_data_4 = bookkeeping_data.get_leg_data (3); + // lets concentrate on the hedges sent on leg 1 + SpreaderLegData& hleg_1 = bookkeeping_data.get_leg_data (0); + auto& hm_1 = hleg_1.hedger_manager(); + REQUIRE(hm_1.n_active_hedgers()==0); + // send a 1 lot quoter fill ///////// + ///////////////////////////////////// + spreader.test_only_on_child_order_update (qleg_data_4.order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (1), + Price (20.045)); + REQUIRE(hm_1.n_active_hedgers()==1); + // legs 2 and 3 + REQUIRE(securities::test_price_equality( + bookkeeping_data.get_leg_data (1).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), + 20.035, + 0.005)); + REQUIRE(securities::test_price_equality( + bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), + 19.995, + 0.005)); + // back to leg1's hedger, this is what will + // be mainly tested here on + { + auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); + REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); + REQUIRE(securities::test_price_equality( + hedger_1_1.order_state().out_price().value(), + 20.015, + 0.005)); + REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); + spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (0), + Price (0)); + REQUIRE(hedger_1_1.state()==HedgerState::WORKING); + // fill h1 and it goes back to start //// + ///////////////////////////////////////// + spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, + Quantity (1), + Price (20.015)); + REQUIRE(hedger_1_1.state()==HedgerState::START); + } + REQUIRE(hm_1.n_active_hedgers()==0); + hm_1.total_fills(t_qty, t_price); + REQUIRE(t_qty.value() == 1); + REQUIRE(securities::test_price_equality(t_price.value(),20.015,0.005)); + //////////////////////////// + // send another 1 lot q fill + spreader.test_only_on_child_order_update (qleg_data_4.order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (2), + Price (20.045)); + REQUIRE(hm_1.n_active_hedgers()==1); + // since 2 and 3 were never filled, they should have 2 hedgers + REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==2); + REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==2); + { + auto& hedger_1_1 = hm_1.test_only_get_hedger_at (0); + REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); + REQUIRE(securities::test_price_equality( + hedger_1_1.order_state().out_price().value(), + 20.015, + 0.005)); + REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); + spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (0), + Price (0)); + REQUIRE(hedger_1_1.state()==HedgerState::WORKING); + // fill h1 /////// + ////////////////// + spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, + Quantity (1), + Price (20.025)); + REQUIRE(hedger_1_1.state()==HedgerState::START); + } + REQUIRE(hm_1.n_active_hedgers()==0); + hm_1.total_fills(t_qty, t_price); + REQUIRE(t_qty.value() == 2); + REQUIRE(securities::test_price_equality(t_price.value(),20.020,0.005)); + // send 4 more q fills ///////// + //////////////////////////////// + for(size_t f=4; f <= 10; f+= 2) + spreader.test_only_on_child_order_update (qleg_data_4.order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (f), + Price (20.045)); + REQUIRE(hm_1.n_active_hedgers()==4); + REQUIRE(bookkeeping_data.get_leg_data (1).hedger_manager().n_active_hedgers()==6); + REQUIRE(bookkeeping_data.get_leg_data (2).hedger_manager().n_active_hedgers()==6); + REQUIRE(bookkeeping_data.get_leg_data (3).hedger_manager().n_active_hedgers()==0); + for (size_t i=0;i<4;i++){ + auto& h = hm_1.test_only_get_hedger_at(i); + REQUIRE(h.state()==HedgerState::WAITING_FOR_FIRST_ACK); + REQUIRE(h.order_state().out_quantity().value()==2); + REQUIRE(securities::test_price_equality( + h.order_state().out_price().value(), + 20.015, + 0.005)); + } + // send order acks ////////// + ///////////////////////////// + for (size_t i=0;i<4;i++){ + auto& h = hm_1.test_only_get_hedger_at(i); + spreader.test_only_on_child_order_update (h.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (0), + Price (0)); + REQUIRE(h.state()==HedgerState::WORKING); + REQUIRE(h.order_state().out_quantity().value()==2); + REQUIRE(securities::test_price_equality( + h.order_state().out_price().value(), + 20.015, + 0.005)); + } + REQUIRE(hm_1.n_active_hedgers()==4); + // fills 1 lot on each order // + /////////////////////////////// + for (size_t i=0;i<4;i++){ + auto& h = hm_1.test_only_get_hedger_at(i); + // previous hedger fills were 20.015, 20.025 + spreader.test_only_on_child_order_update (h.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_WORKING, + Quantity (1), + Price (20.035)); + REQUIRE(h.state()==HedgerState::WORKING); + REQUIRE(h.order_state().out_quantity().value()==2); + REQUIRE(securities::test_price_equality( + h.order_state().out_price().value(), + 20.015, + 0.005)); + } + REQUIRE(hm_1.n_active_hedgers()==4); + hm_1.total_fills(t_qty, t_price); + REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); + REQUIRE(t_qty.value()==6); -// // fills additional 1 lot on each, /////// -// // which should complete the orders ////// -// ////////////////////////////////////////// -// for (size_t i=0;i<4;i++){ -// auto& h = hm_1.test_only_get_hedger_at(i); -// // previous hedger fills were 20.015, 20.025 -// spreader.test_only_on_child_order_update (h.order_state().order_key(), -// api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, -// Quantity (2), -// Price (20.03)); -// REQUIRE(h.state()==HedgerState::START); -// REQUIRE(h.order_state().out_quantity().value()==0); -// } -// REQUIRE(hm_1.n_active_hedgers()==0); -// hm_1.total_fills(t_qty, t_price); -// REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); -// REQUIRE(t_qty.value()==10); -// } -// } + // fills additional 1 lot on each, /////// + // which should complete the orders ////// + ////////////////////////////////////////// + for (size_t i=0;i<4;i++){ + auto& h = hm_1.test_only_get_hedger_at(i); + // previous hedger fills were 20.015, 20.025 + spreader.test_only_on_child_order_update (h.order_state().order_key(), + api::orderman::OrderStatus::ORDER_STATUS_COMPLETED, + Quantity (2), + Price (20.03)); + REQUIRE(h.state()==HedgerState::START); + REQUIRE(h.order_state().out_quantity().value()==0); + } + REQUIRE(hm_1.n_active_hedgers()==0); + hm_1.total_fills(t_qty, t_price); + REQUIRE(securities::test_price_equality(t_price.value(),20.03,0.005)); + REQUIRE(t_qty.value()==10); + } +} TEST_CASE ("simple book addition", "[spreader]"){ SECTION ("direct, book"){ SecurityKey key(12345); From 5135a21dc24a470415053210ce7c350804a6e92a Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 23 May 2019 15:27:41 -0400 Subject: [PATCH 005/153] ProRata and Scratch Algos --- src/lib/api/services/OrderService.proto | 18 ++ .../orderman/events/OrderStateUpdateEvent.h | 6 + .../orderman/order_factory/OrderFactory.cpp | 10 + .../orders/algo/algo_orders/ProRataAlgo.cpp | 218 ++++++++++++++++++ .../orders/algo/algo_orders/ProRataAlgo.h | 37 +++ .../orders/algo/algo_orders/ScratchAlgo.cpp | 210 +++++++++++++++++ .../orders/algo/algo_orders/ScratchAlgo.h | 36 +++ 7 files changed, 535 insertions(+) create mode 100644 src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h create mode 100644 src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index b16c4d584..9e4ffa534 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -100,6 +100,8 @@ message CustomOrderDefinition OCOOrderDefinition oco = 25; PricerOrderDefinition pricer = 26; CompositeOrderDefinition composite = 27; + ScratchOrderDefinition scratch = 28; + ProRataOrderDefinition prorata = 29; DynamicOrderDefinition dynamic = 1000; } @@ -197,6 +199,18 @@ message CompositeOrderDefinition { uint32 algo_template_key = 1; CustomOrderDefinition definition = 2; } +message ScratchOrderDefinition { + spark.QuantityMeasure scratch_threshold = 1; + CustomOrderDefinition child = 10; +} +message ProRataOrderDefinition { + spark.QuantityMeasure fill_limit = 1; + spark.QuantityMeasure scratch_threshold = 2; + spark.QuantityMeasure stay_in_threshold = 3; + bool enable_scratch_on_overfill = 4; + bool enable_stay_in = 5; + CustomOrderDefinition child = 10; +} message DynamicOrderDefinition { string dynamic_order_type = 1; map order_params = 2; @@ -249,6 +263,8 @@ message OrderState { QueueHolderState queue_holder = 30; OCOOrderState oco = 25; PricerOrderState pricer = 26; + ScratchOrderState scratch = 27; + ProRataOrderState prorata = 28; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } @@ -324,6 +340,8 @@ message CoverOrderState {} message OffsetOrderState {} message OCOOrderState {} message PricerOrderState {} +message ScratchOrderState {} +message ProRataOrderState {} message DynamicOrderState {} message QueueHolderState {} diff --git a/src/lib/orderman/events/OrderStateUpdateEvent.h b/src/lib/orderman/events/OrderStateUpdateEvent.h index 7556ce6b4..af86ddf3a 100644 --- a/src/lib/orderman/events/OrderStateUpdateEvent.h +++ b/src/lib/orderman/events/OrderStateUpdateEvent.h @@ -146,6 +146,12 @@ class OrderStateUpdateEvent : public mercury::Event { case api::orderman::OrderState::kPricer: result.mutable_pricer(); break; + case api::orderman::OrderState::kScratch: + result.mutable_scratch(); + break; + case api::orderman::OrderState::kProrata: + result.mutable_prorata(); + break; case api::orderman::OrderState::kDynamic: result.mutable_dynamic(); break; diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index 2cbac4d4b..b83c35e57 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -19,6 +19,8 @@ #include "lib/orderman/orders/algo/algo_orders/TopAlgo.h" #include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h" #include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" #include "lib/orderman/orders/book_algos/OCOOrder.h" #include "lib/orderman/orders/book_algos/OffsetOrder.h" #include "lib/orderman/orders/book_algos/PricerOrder.h" @@ -193,6 +195,14 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, case CustomOrderDefinition::kPricer: order_ptr.reset(new orderman::PricerOrder()); break; + case CustomOrderDefinition::kScratch: + order_ptr.reset( + new orderman::AlgoWrapperOrder(std::make_unique())); + break; + case CustomOrderDefinition::kProrata: + order_ptr.reset( + new orderman::AlgoWrapperOrder(std::make_unique())); + break; case CustomOrderDefinition::kDynamic: { order_ptr = me->dynamic_order_registry.build( def.custom_def().dynamic().dynamic_order_type()); diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp new file mode 100644 index 000000000..14666bcbf --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -0,0 +1,218 @@ +#include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" + +#include "lib/domain_model/Security.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/utils/PBUtils.h" + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +enum class ProRataAlgo::Status { WORKING, WAITING_FOR_HEDGES, DONE }; +const char *ProRataAlgo::status_name(Status st) { + switch (st) { + case Status::WORKING: + return "WORKING"; + case Status::WAITING_FOR_HEDGES: + return "WAITING_FOR_HEDGES"; + case Status::DONE: + return "Done"; + } + return nullptr; +} +struct ProRataAlgo::PImpl { + IAlgoServices *algo; + OrderKey child_key = null_order_key(); + Price price_improvement_delta; + ChildTemplate *child_template = nullptr; + ChildTemplate *hedge_template = nullptr; + Quantity hedged_fills = Quantity(0); + Status status = Status::WORKING; + std::vector hedges; +}; + +ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} +ProRataAlgo::~ProRataAlgo() {} +IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } + +OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { + auto &def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_cover()); + + // Validate the cover part of the definition + auto &cover = def.custom_def().cover(); + if (!cover.has_child() && + OrderKey(def.attach_target_order_key()) == null_order_key()) { + return OrderOpResult{ + ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Cover order must have either a child order definition or an " + "order to attach to"}; + } + + if (!cover.has_hedge()) + return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Cover order must have a hedge order definition"}; + if (!serv.is_attaching()) { + // if we are not attaching create a child template so we can + // quickly launch our child when the time comes. + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().cover().child(); + me->child_template = &(serv.make_child_template(child_def)); + } + + init_hedge_template(serv, cover.hedge()); + set_price_improvement_delta(serv, cover); + return {}; +} + +void ProRataAlgo::do_launch(IAlgoServices &algo) { + me->algo = &algo; + LOG(OrderMan, Diag, "Starting ProRataAlgo {}, {} {}@{}. Will cover {} ticks", + algo.order_key().value(), + securities::side_name(algo.definition().side()), algo.quantity().value(), + algo.price().value(), + algo.definition().custom_def().cover().hedge_ticks()); + + LOG(OrderMan, Diag, "ProRataAlgo {} launching child", + algo.order_key().value()); + me->child_key = + algo.launch_child(*me->child_template, algo.price(), algo.quantity()); +} + +void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, + ChildState st) { + me->algo = &algo; + me->child_key = k; + me->hedged_fills = st.total_fills; + on_child_change(k, st); +} + +void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) { + if (k == me->child_key) { + if (st.total_fills.value() > me->hedged_fills.value()) { + auto hedge_qty = + Quantity(st.total_fills.value() - me->hedged_fills.value()); + auto hedge_price = securities::improve_price( + algo().definition().side(), st.last_price, + me->price_improvement_delta); + + auto hedge = + algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); + me->hedges.push_back(hedge); + + me->hedged_fills = + Quantity(hedge_qty.value() + me->hedged_fills.value()); + LOG(OrderMan, Diag, + "ProRataAlgo {}, launched new hedge in response to child fill. " + "Hedge: " + "{} {}@{}", + algo().order_key().value(), + securities::side_name(algo().definition().side()), + hedge_qty.value(), hedge_price.value()); + } + algo().update_fill_state(st.total_fills, st.last_price); + switch (st.status) { + case ChildStatus::WORKING: + break; + case ChildStatus::COMPLETED: + LOG(OrderMan, Info, + "ProRataAlgo {}. Child is completed, waiting for hedges ", + algo().order_key().value()); + me->status = Status::WAITING_FOR_HEDGES; + break; + case ChildStatus::CANCELED: + LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, terminating", + algo().order_key().value()); + me->status = Status::DONE; + algo().terminate_error("Unexpected child cancel"); + break; + } + } else { + switch (st.status) { + case ChildStatus::WORKING: + break; + case ChildStatus::COMPLETED: + if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); + it != me->hedges.end()) + me->hedges.erase(it); + break; + case ChildStatus::CANCELED: + LOG(OrderMan, Info, "ProRataAlgo {}. Hedge was canceled, terminating", + algo().order_key().value()); + me->status = Status::DONE; + algo().terminate_error("Unexpected child cancel"); + break; + } + } + check_terminate(); +} + +void ProRataAlgo::do_update(api::orderman::Update const &update) { + if (me->status == Status::WORKING) { + LOG(OrderMan, Diag, "ProRataAlgo order {} receiving update: {}", + algo().order_key().value(), utils::pb::to_string(update)); + + if (update.has_custom() && !update.custom().has_cover()) { + algo().terminate_error("Invalid update"); + return; + } + + Update child_update; + child_update.set_price(update.price()); + child_update.set_quantity(update.quantity()); + + // process custom part of the udpate if any + if (update.has_custom()) { + auto &cover_update = update.custom().cover(); + set_price_improvement_delta(algo(), cover_update); + if (cover_update.has_child()) { + *child_update.mutable_custom() = cover_update.child(); + } + if (cover_update.has_hedge()) + init_hedge_template(algo(), cover_update.hedge()); + } + algo().update_child(me->child_key, child_update); + } else { + LOG(OrderMan, Info, + "ProRataAlgo {}. Received update after child has completed. Ignoring " + "it.", + algo().order_key().value()); + } +} + +void orderman::ProRataAlgo::init_hedge_template( + IBeforeLaunchAlgoServices &algo, + api::orderman::CustomOrderDefinition const &hedge_cdef) { + auto hedge_def = algo.definition(); + hedge_def.set_side(securities::other_side(algo.definition().side())); + hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); + hedge_def.clear_attach_target_order_key(); + *hedge_def.mutable_custom_def() = hedge_cdef; + me->hedge_template = &(algo.make_child_template(hedge_def)); +} + +void ProRataAlgo::set_price_improvement_delta( + IBeforeLaunchAlgoServices &serv, + api::orderman::CoverOrderDefinition const &cover) { + me->price_improvement_delta = Price( + securities::tick_size(serv.security()).value() * cover.hedge_ticks()); +} + +void ProRataAlgo::check_terminate() { + LOG(OrderMan, Diag, + "ProRataAlgo {} check_terminate. State: {} Hedge count: {}", + algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, + me->hedges.size()); + + if (me->status == Status::WAITING_FOR_HEDGES && me->hedges.empty()) { + LOG(OrderMan, Info, "ProRataAlgo {}. Last hedge completed, terminating", + algo().order_key().value()); + algo().complete(); + } +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h new file mode 100644 index 000000000..71b68b465 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -0,0 +1,37 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { + +// Cover initializes the child order as normal. Fills trigger hedge +// orders to the opposite side of the market, n ticks away. + +class ProRataAlgo : public IAlgoOrder { +public: + ProRataAlgo(); + ~ProRataAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_child_change(OrderKey, ChildState const &) override; + +private: + struct PImpl; + enum class Status; + std::unique_ptr me; + IAlgoServices &algo() const; + void + set_price_improvement_delta(IBeforeLaunchAlgoServices &, + api::orderman::CoverOrderDefinition const &); + void init_hedge_template(IBeforeLaunchAlgoServices &, + api::orderman::CustomOrderDefinition const &); + void check_terminate(); + const char *status_name(Status st); +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp new file mode 100644 index 000000000..8d3936a08 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -0,0 +1,210 @@ +#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" + +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "lib/os/os.h" +#include "lib/utils/PBUtils.h" + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +enum class ScratchAlgo::Status { WAITING_FOR_MD, WAITING_TO_SCRATCH, SCRATCHED }; + +struct ScratchAlgo::PImpl +{ + IAlgoServices *algo; + Quantity scratch_threshold; + Status status = Status::WAITING_FOR_MD; + OrderKey child_key = null_order_key(); + ChildTemplate *child_template = nullptr; + Book last_book; + Price scratch_price=Price(os::nan()); +}; + +ScratchAlgo::ScratchAlgo() : me(new PImpl()) {} +ScratchAlgo::~ScratchAlgo() {} +IAlgoServices &ScratchAlgo::algo() const { return *me->algo; } + +OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) +{ + auto &def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_scratch()); + + // Validate the scratch part of the definition + auto &scratch = def.custom_def().scratch(); + if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) + { + return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Scratch order must have either a child order definition or an order to attach to"}; + } + if (!serv.is_attaching()) + { + // if we are not attaching create a child template so we can + // quickly launch our child when the time comes. + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().scratch().child(); + me->child_template = &(serv.make_child_template(child_def)); + } + return {}; +} + +void ScratchAlgo::do_launch(IAlgoServices &algo) +{ + me->algo = &algo; + update_scratch_threshold(); + bts_assert(me->child_template); + me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); + algo.subscribe_book(SecurityKey(algo.security().id())); +} + +void ScratchAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) +{ + me->algo = &algo; + update_scratch_threshold(); + me->child_key = k; + algo.subscribe_book(SecurityKey(algo.security().id())); + on_child_change(k, st); +} + +void ScratchAlgo::do_update(api::orderman::Update const &update) +{ + LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); + if (update.has_custom() && !update.custom().has_scratch()) + { + algo().terminate_error("Invalid update"); + return; + } + + Update child_update; + Price price_to_pass = get_price_to_pass(Price(update.price())); + child_update.set_price(price_to_pass.value()); + child_update.set_quantity(update.quantity()); + if (update.has_custom()) + { + update_scratch_threshold(); + auto &scratch_update = update.custom().scratch(); + *child_update.mutable_custom() = scratch_update.child(); + } + algo().update_child(me->child_key, child_update); + + // If we were not yet scratched it is possible we now can, since we + // could have changed the scratch_threshold + if (me->status == Status::WAITING_TO_SCRATCH) + try_to_scratch(); +} + +void ScratchAlgo::on_book(SecurityKey, Book const &b) +{ + me->last_book = b; + + LOG(OrderMan, Diag, "ScratchOrder order {} on book. BID: {}@{} ASK: {}@{}", + algo().order_key().value(), b.side(api::SIDE_BUY)[0].size(), + b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), + b.side(api::SIDE_SELL)[0].price()); + + switch (me->status) + { + case Status::WAITING_FOR_MD: + { + auto other_side = securities::other_side(algo().definition().side()); + auto &other_side_top = me->last_book.side(other_side)[0]; + + if (other_side_top.size() > 0) + { + me->status = Status::WAITING_TO_SCRATCH; + LOG(OrderMan, Diag, + "ScratchOrder order {} receiving first book: {} Target: {}", + algo().order_key().value(), me->scratch_threshold.value(), + other_side_top.price()); + try_to_scratch(); + } + } break; + case Status::SCRATCHED: + break; + case Status::WAITING_TO_SCRATCH: + try_to_scratch(); + break; + } +} + +void ScratchAlgo::try_to_scratch() +{ + bts_assert(me->status == Status::WAITING_TO_SCRATCH); + auto my_side = algo().definition().side(); + auto other_side = securities::other_side(my_side); + + Quantity my_quan; + bool found = SpreaderUtils::find_qty_upto_price_level(me->last_book, my_side, algo().price(), /*unused*/ 0.0, my_quan); + auto &my_side_psz = me->last_book.side(my_side)[0]; + if (found && my_quan.value() <= me->scratch_threshold.value()) + { + Price scratch_price; + Quantity scratch_quan; + SpreaderUtils::traverse_depth_for_quantity(me->last_book, other_side, algo().quantity(), scratch_price, scratch_quan); + LOG(OrderMan, Diag, "ScratchOrder order {} scratching: {}@{} {}", + algo().order_key().value(), algo().quantity().value(), scratch_price, scratch_quan); + + // Time to scratch + Update child_update; + me->scratch_price = scratch_price; + child_update.set_price(scratch_price.value()); + //we still want to try to scratch all of it + child_update.set_quantity(algo().quantity().value()); + algo().update_child(me->child_key, child_update); + me->status = Status::SCRATCHED; + } + else + { + LOG(OrderMan, Diag, + "ScratchOrder order {} still waiting to scratch. Order: {}@{} " + "My side: {}@{}" + "Thresh : {}@{}", + algo().order_key().value(), algo().quantity().value(), algo().price().value(), + my_side_psz.size(), my_side_psz.price(), + me->scratch_threshold.value(), my_quan); + } +} + +Price ScratchAlgo::get_price_to_pass (Price price) const +{ + Price rv; + switch (me->status) { + case Status::WAITING_FOR_MD: + case Status::WAITING_TO_SCRATCH: + rv = price; + break; + case Status::SCRATCHED: + rv = me->scratch_price; + break; + } + return rv; +} + +void ScratchAlgo::update_scratch_threshold() +{ + me->scratch_threshold = SpreaderUtils::_quantity_threshold( + algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); +} + +void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) +{ + algo().update_fill_state(st.total_fills, st.last_price); + switch (st.status) + { + case ChildStatus::WORKING: + return; + case ChildStatus::COMPLETED: + algo().complete(); + return; + case ChildStatus::CANCELED: + algo().cancel("Unexpected child cancel"); + return; + } +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h new file mode 100644 index 000000000..d86487366 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h @@ -0,0 +1,36 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { + +// Scratch monitors the cumulative size on the same side of the market of it's child +// if that size falls below the threshold it will cross the market, paying through enough levels to do all of it's size + +class ScratchAlgo : public IAlgoOrder +{ +public: + ScratchAlgo(); + ~ScratchAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices&) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices&,OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + +private: + enum class Status; + struct PImpl; + std::unique_ptr me; + + IAlgoServices &algo() const ; + void update_scratch_threshold(); + bool has_running_child() const; + void try_to_scratch(); + Price get_price_to_pass (Price price) const; +}; + +}} From 91bc135c61f50f34a3d31069680aa0a8e489aff3 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 23 May 2019 15:49:12 -0400 Subject: [PATCH 006/153] Fix web ui build --- src/web/ui/src/Accounts/View.fs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index be6133fbf..f931c6de0 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -28,7 +28,7 @@ let isDirty model = account <> model.scratch | None -> false -let saveButton model dispatch = +let saveButton (model:Model) dispatch = let isNew = isNewAccount model.scratch let buttonText = if isNew then "Create" else "Save" let dirty = isDirty model || isNew @@ -50,7 +50,7 @@ let accountControls model (dispatch:Msg->unit) = // // General // -let generalView model dispatch = +let generalView (model:Model) dispatch = let account = model.scratch @@ -549,7 +549,7 @@ let lazyProductLimitsView model dispatch = productLimits model dispatch -let securityLimitsView model dispatch = +let securityLimitsView (model:Model) dispatch = let headerRow:TableControl.row = { cells = @@ -737,7 +737,7 @@ let ConnectionsView (model:Accounts.Types.Model) dispatch = let creditLimitsView model dispatch = div [] [str "lorem ipsum"] -let exchangeSettings model dispatch = +let exchangeSettings (model:Model) dispatch = let cme = model.scratch.exchangeAccountData.cmeAccountData let sim = model.scratch.exchangeAccountData.simAccountData let send = UpdateCME>>dispatch @@ -760,7 +760,7 @@ let exchangeSettings model dispatch = accountControls model dispatch ] -let tabContent model dispatch = +let tabContent (model:Model) dispatch = div [Key (string model.scratch.id); ClassName "is-tab-content"] [ // (match model.tab with (match model.tab, (accountIsSelected model.scratch Access.Accounts.empty) with From 3a516df5931a1c39fb9b2337791e85db1b74fbfc Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 23 May 2019 18:16:20 -0400 Subject: [PATCH 007/153] more prorata --- src/lib/api/services/OrderService.proto | 14 +- .../orders/algo/algo_orders/ProRataAlgo.cpp | 264 +++++++++++------- .../orders/algo/algo_orders/ProRataAlgo.h | 14 +- .../orders/algo/algo_orders/ScratchAlgo.cpp | 7 +- 4 files changed, 191 insertions(+), 108 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 9e4ffa534..75ed9f7b6 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -204,12 +204,16 @@ message ScratchOrderDefinition { CustomOrderDefinition child = 10; } message ProRataOrderDefinition { - spark.QuantityMeasure fill_limit = 1; - spark.QuantityMeasure scratch_threshold = 2; - spark.QuantityMeasure stay_in_threshold = 3; - bool enable_scratch_on_overfill = 4; - bool enable_stay_in = 5; + uint32 hedge_ticks = 1; + spark.QuantityMeasure fill_limit = 2; + spark.QuantityMeasure scratch_threshold = 3; + spark.QuantityMeasure stay_in_threshold = 4; + bool enable_scratch_on_overfill = 5; + bool enable_stay_in = 6; + CustomOrderDefinition child = 10; + CustomOrderDefinition hedge = 20; + CustomOrderDefinition overfill_hedge = 30; } message DynamicOrderDefinition { string dynamic_order_type = 1; diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 14666bcbf..731002886 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -3,6 +3,7 @@ #include "lib/domain_model/Security.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" #include "lib/utils/PBUtils.h" namespace bts { @@ -10,152 +11,224 @@ namespace orderman { using namespace api::orderman; -enum class ProRataAlgo::Status { WORKING, WAITING_FOR_HEDGES, DONE }; -const char *ProRataAlgo::status_name(Status st) { - switch (st) { - case Status::WORKING: - return "WORKING"; - case Status::WAITING_FOR_HEDGES: - return "WAITING_FOR_HEDGES"; +enum class ProRataAlgo::Status { WAITING_FOR_MD, UNDERFILLED, OVERFILLED, DONE }; +const char *ProRataAlgo::status_name(Status st) +{ + switch (st) + { + case Status::WAITING_FOR_MD: + return "WAITING_FOR_MD"; + case Status::UNDERFILLED: + return "UNDERFILLED"; + case Status::OVERFILLED: + return "OVERFILLED"; case Status::DONE: - return "Done"; + return "DONE"; } return nullptr; } -struct ProRataAlgo::PImpl { +struct ProRataAlgo::PImpl +{ IAlgoServices *algo; OrderKey child_key = null_order_key(); - Price price_improvement_delta; + Quantity fill_limit; + Quantity scratch_threshold; + Quantity stay_in_threshold; ChildTemplate *child_template = nullptr; + + Price hedge_ticks; ChildTemplate *hedge_template = nullptr; + ChildTemplate *overfill_hedge_template = nullptr; + Quantity hedged_fills = Quantity(0); - Status status = Status::WORKING; + Status status = Status::WAITING_FOR_MD; std::vector hedges; + std::vector overfill_hedges; + + Book last_book; + Book book_at_inception; }; ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} ProRataAlgo::~ProRataAlgo() {} IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } -OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { +OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) +{ auto &def = serv.definition(); bts_assert(def.has_custom_def()); - bts_assert(def.custom_def().has_cover()); + bts_assert(def.custom_def().has_prorata()); - // Validate the cover part of the definition - auto &cover = def.custom_def().cover(); - if (!cover.has_child() && - OrderKey(def.attach_target_order_key()) == null_order_key()) { - return OrderOpResult{ - ORDER_ERROR_INVALID_ORDER_DEFINITON, - "Cover order must have either a child order definition or an " - "order to attach to"}; + // Validate the prorata part of the definition + auto &prorata = def.custom_def().prorata(); + if (!prorata.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) + { + return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, + "ProRata order must have either a child order definition or an order to attach to"}; } - if (!cover.has_hedge()) + if (!prorata.has_hedge()) return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, - "Cover order must have a hedge order definition"}; - if (!serv.is_attaching()) { + "ProRata order must have a hedge order definition"}; + if (!serv.is_attaching()) + { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. auto child_def = def; - *child_def.mutable_custom_def() = def.custom_def().cover().child(); + *child_def.mutable_custom_def() = def.custom_def().prorata().child(); me->child_template = &(serv.make_child_template(child_def)); } - init_hedge_template(serv, cover.hedge()); - set_price_improvement_delta(serv, cover); + init_hedge_template(serv, prorata.hedge()); + init_hedge_template(serv, prorata.overfill_hedge()); + set_hedge_ticks(serv, prorata); return {}; } -void ProRataAlgo::do_launch(IAlgoServices &algo) { +void ProRataAlgo::do_launch(IAlgoServices &algo) +{ me->algo = &algo; LOG(OrderMan, Diag, "Starting ProRataAlgo {}, {} {}@{}. Will cover {} ticks", algo.order_key().value(), securities::side_name(algo.definition().side()), algo.quantity().value(), algo.price().value(), - algo.definition().custom_def().cover().hedge_ticks()); + algo.definition().custom_def().prorata().hedge_ticks()); - LOG(OrderMan, Diag, "ProRataAlgo {} launching child", - algo.order_key().value()); - me->child_key = - algo.launch_child(*me->child_template, algo.price(), algo.quantity()); + LOG(OrderMan, Diag, "ProRataAlgo {} launching child", algo.order_key().value()); + me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); + algo.subscribe_book(SecurityKey(algo.security().id())); } -void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, - ChildState st) { +void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) +{ me->algo = &algo; me->child_key = k; me->hedged_fills = st.total_fills; + algo.subscribe_book(SecurityKey(algo.security().id())); on_child_change(k, st); } -void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) { - if (k == me->child_key) { - if (st.total_fills.value() > me->hedged_fills.value()) { - auto hedge_qty = - Quantity(st.total_fills.value() - me->hedged_fills.value()); - auto hedge_price = securities::improve_price( - algo().definition().side(), st.last_price, - me->price_improvement_delta); - auto hedge = - algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); - me->hedges.push_back(hedge); +void ProRataAlgo::on_book(SecurityKey, Book const &b) +{ + me->last_book = b; + + LOG(OrderMan, Diag, "ProRataAlgo order {} on book. BID: {}@{} ASK: {}@{}", + algo().order_key().value(), b.side(api::SIDE_BUY)[0].size(), + b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), + b.side(api::SIDE_SELL)[0].price()); - me->hedged_fills = - Quantity(hedge_qty.value() + me->hedged_fills.value()); + switch (me->status) + { + case Status::WAITING_FOR_MD: + { + auto other_side = securities::other_side(algo().definition().side()); + auto &other_side_top = me->last_book.side(other_side)[0]; + + if (other_side_top.size() > 0) + { + me->status = Status::UNDERFILLED; + me->book_at_inception = b; LOG(OrderMan, Diag, - "ProRataAlgo {}, launched new hedge in response to child fill. " - "Hedge: " - "{} {}@{}", - algo().order_key().value(), - securities::side_name(algo().definition().side()), - hedge_qty.value(), hedge_price.value()); + "ProRataAlgo order {} receiving first book: {} Target: {}", + algo().order_key().value(), me->scratch_threshold.value(), + other_side_top.price()); + } + } break; + case Status::UNDERFILLED: + break; + case Status::OVERFILLED: + //TODO bail.... + break; + case Status::DONE: + break; + } +} + +void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) +{ + if (k == me->child_key) + { + if (st.total_fills.value() > me->hedged_fills.value()) + { + auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); + me->hedged_fills = Quantity(hedge_qty.value() + me->hedged_fills.value()); + auto hedge_price = securities::improve_price(algo().definition().side(), st.last_price, me->hedge_ticks); + if(me->hedged_fills < me->fill_limit) + { + auto hedge = algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); + me->hedges.push_back(hedge); + LOG(OrderMan, Diag, + "ProRataAlgo {}, launched new hedge in response to child fill. " + "Hedge: " + "{} {}@{}", + algo().order_key().value(), + securities::side_name(algo().definition().side()), + hedge_qty.value(), hedge_price.value()); + } + else + { + auto hedge = algo().launch_child(*me->overfill_hedge_template, hedge_price, hedge_qty); + me->overfill_hedges.push_back(hedge); + LOG(OrderMan, Diag, + "ProRataAlgo {}, launched new OVERFILL hedge in response to child fill. " + "Hedge: " + "{} {}@{}", + algo().order_key().value(), + securities::side_name(algo().definition().side()), + hedge_qty.value(), hedge_price.value()); + me->status = Status::OVERFILLED; + } } algo().update_fill_state(st.total_fills, st.last_price); - switch (st.status) { + switch (st.status) + { case ChildStatus::WORKING: break; case ChildStatus::COMPLETED: LOG(OrderMan, Info, - "ProRataAlgo {}. Child is completed, waiting for hedges ", - algo().order_key().value()); - me->status = Status::WAITING_FOR_HEDGES; + "ProRataAlgo {}. Child is completed, waiting for hedges ", algo().order_key().value()); break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, terminating", - algo().order_key().value()); + LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, terminating", algo().order_key().value()); me->status = Status::DONE; algo().terminate_error("Unexpected child cancel"); break; } - } else { - switch (st.status) { + } + else //it's a hedge + { + switch (st.status) + { case ChildStatus::WORKING: break; case ChildStatus::COMPLETED: - if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); - it != me->hedges.end()) + if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); it != me->hedges.end()) me->hedges.erase(it); + if (auto oit = std::find(me->overfill_hedges.begin(), me->overfill_hedges.end(), k); oit != me->overfill_hedges.end()) + me->overfill_hedges.erase(oit); break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "ProRataAlgo {}. Hedge was canceled, terminating", - algo().order_key().value()); - me->status = Status::DONE; - algo().terminate_error("Unexpected child cancel"); + LOG(OrderMan, Info, "ProRataAlgo {}. Hedge was canceled, removing", algo().order_key().value()); + if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); it != me->hedges.end()) + me->hedges.erase(it); + if (auto oit = std::find(me->overfill_hedges.begin(), me->overfill_hedges.end(), k); oit != me->overfill_hedges.end()) + me->overfill_hedges.erase(oit); break; } } check_terminate(); } -void ProRataAlgo::do_update(api::orderman::Update const &update) { - if (me->status == Status::WORKING) { +void ProRataAlgo::do_update(api::orderman::Update const &update) +{ + if (me->status != Status::DONE) + { LOG(OrderMan, Diag, "ProRataAlgo order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - if (update.has_custom() && !update.custom().has_cover()) { + if (update.has_custom() && !update.custom().has_prorata()) + { algo().terminate_error("Invalid update"); return; } @@ -165,27 +238,31 @@ void ProRataAlgo::do_update(api::orderman::Update const &update) { child_update.set_quantity(update.quantity()); // process custom part of the udpate if any - if (update.has_custom()) { - auto &cover_update = update.custom().cover(); - set_price_improvement_delta(algo(), cover_update); - if (cover_update.has_child()) { - *child_update.mutable_custom() = cover_update.child(); - } - if (cover_update.has_hedge()) - init_hedge_template(algo(), cover_update.hedge()); + if (update.has_custom()) + { + //TODO MORE + auto &prorata_update = update.custom().prorata(); + set_hedge_ticks(algo(), prorata_update); + if (prorata_update.has_child()) + *child_update.mutable_custom() = prorata_update.child(); + if (prorata_update.has_hedge()) + init_hedge_template(algo(), prorata_update.hedge()); + if (prorata_update.has_overfill_hedge()) + init_hedge_template(algo(), prorata_update.overfill_hedge()); } algo().update_child(me->child_key, child_update); - } else { + } + else + { LOG(OrderMan, Info, - "ProRataAlgo {}. Received update after child has completed. Ignoring " - "it.", + "ProRataAlgo {}. Received update after child has completed. Ignoring it.", algo().order_key().value()); } } -void orderman::ProRataAlgo::init_hedge_template( - IBeforeLaunchAlgoServices &algo, - api::orderman::CustomOrderDefinition const &hedge_cdef) { +void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, + api::orderman::CustomOrderDefinition const &hedge_cdef) +{ auto hedge_def = algo.definition(); hedge_def.set_side(securities::other_side(algo.definition().side())); hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); @@ -194,22 +271,21 @@ void orderman::ProRataAlgo::init_hedge_template( me->hedge_template = &(algo.make_child_template(hedge_def)); } -void ProRataAlgo::set_price_improvement_delta( - IBeforeLaunchAlgoServices &serv, - api::orderman::CoverOrderDefinition const &cover) { - me->price_improvement_delta = Price( - securities::tick_size(serv.security()).value() * cover.hedge_ticks()); +void ProRataAlgo::set_hedge_ticks(IBeforeLaunchAlgoServices &serv, api::orderman::CoverOrderDefinition const &cover) +{ + me->hedge_ticks = Price(securities::tick_size(serv.security()).value() * cover.hedge_ticks()); } -void ProRataAlgo::check_terminate() { +void ProRataAlgo::check_terminate() +{ LOG(OrderMan, Diag, "ProRataAlgo {} check_terminate. State: {} Hedge count: {}", algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, me->hedges.size()); - if (me->status == Status::WAITING_FOR_HEDGES && me->hedges.empty()) { - LOG(OrderMan, Info, "ProRataAlgo {}. Last hedge completed, terminating", - algo().order_key().value()); + if (me->status != Status::DONE && me->hedges.empty() && me->overfill_hedges.empty()) + { + LOG(OrderMan, Info, "ProRataAlgo {}. Last hedge completed, terminating", algo().order_key().value()); algo().complete(); } } diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index 71b68b465..497fa4531 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -5,10 +5,11 @@ namespace bts { namespace orderman { -// Cover initializes the child order as normal. Fills trigger hedge -// orders to the opposite side of the market, n ticks away. +// ProRata initializes the child order as normal. Fills trigger one type of hedge, overfills (fills > limit) trigger another +// type of hedge. Overfills also optionally trigger overfill_hedge scratching and cancelling children dependent on book state -class ProRataAlgo : public IAlgoOrder { +class ProRataAlgo : public IAlgoOrder +{ public: ProRataAlgo(); ~ProRataAlgo(); @@ -24,11 +25,8 @@ private: enum class Status; std::unique_ptr me; IAlgoServices &algo() const; - void - set_price_improvement_delta(IBeforeLaunchAlgoServices &, - api::orderman::CoverOrderDefinition const &); - void init_hedge_template(IBeforeLaunchAlgoServices &, - api::orderman::CustomOrderDefinition const &); + void set_hedge_ticks(IBeforeLaunchAlgoServices &, api::orderman::CoverOrderDefinition const &); + void init_hedge_template(IBeforeLaunchAlgoServices &, api::orderman::CustomOrderDefinition const &); void check_terminate(); const char *status_name(Status st); }; diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 8d3936a08..766b37d31 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -21,6 +21,7 @@ struct ScratchAlgo::PImpl OrderKey child_key = null_order_key(); ChildTemplate *child_template = nullptr; Book last_book; + Book book_at_inception; Price scratch_price=Price(os::nan()); }; @@ -116,6 +117,7 @@ void ScratchAlgo::on_book(SecurityKey, Book const &b) if (other_side_top.size() > 0) { me->status = Status::WAITING_TO_SCRATCH; + me->book_at_inception = b; LOG(OrderMan, Diag, "ScratchOrder order {} receiving first book: {} Target: {}", algo().order_key().value(), me->scratch_threshold.value(), @@ -140,7 +142,10 @@ void ScratchAlgo::try_to_scratch() Quantity my_quan; bool found = SpreaderUtils::find_qty_upto_price_level(me->last_book, my_side, algo().price(), /*unused*/ 0.0, my_quan); auto &my_side_psz = me->last_book.side(my_side)[0]; - if (found && my_quan.value() <= me->scratch_threshold.value()) + auto &other_side_psz = me->last_book.side(other_side)[0]; + bool market_moved = other_side_psz.price() != my_side_psz.price(); + + if (market_moved || found && my_quan.value() <= me->scratch_threshold.value()) { Price scratch_price; Quantity scratch_quan; From e1f590127eef4640ed5da527a63760464bf80f95 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 23 May 2019 23:55:26 -0400 Subject: [PATCH 008/153] ready to test --- src/lib/api/services/OrderService.proto | 1 - .../orders/algo/algo_orders/ProRataAlgo.cpp | 140 +++++++++++------- .../orders/algo/algo_orders/ProRataAlgo.h | 11 +- .../orders/algo/algo_orders/ScratchAlgo.cpp | 2 +- 4 files changed, 95 insertions(+), 59 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 75ed9f7b6..d2bc605bc 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -206,7 +206,6 @@ message ScratchOrderDefinition { message ProRataOrderDefinition { uint32 hedge_ticks = 1; spark.QuantityMeasure fill_limit = 2; - spark.QuantityMeasure scratch_threshold = 3; spark.QuantityMeasure stay_in_threshold = 4; bool enable_scratch_on_overfill = 5; bool enable_stay_in = 6; diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 731002886..2bdd18e55 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -11,17 +11,15 @@ namespace orderman { using namespace api::orderman; -enum class ProRataAlgo::Status { WAITING_FOR_MD, UNDERFILLED, OVERFILLED, DONE }; +enum class ProRataAlgo::Status { WAITING_FOR_MD, WORKING, DONE }; //this is for the child const char *ProRataAlgo::status_name(Status st) { switch (st) { case Status::WAITING_FOR_MD: return "WAITING_FOR_MD"; - case Status::UNDERFILLED: - return "UNDERFILLED"; - case Status::OVERFILLED: - return "OVERFILLED"; + case Status::WORKING: + return "WORKING"; case Status::DONE: return "DONE"; } @@ -29,14 +27,20 @@ const char *ProRataAlgo::status_name(Status st) } struct ProRataAlgo::PImpl { + PImpl() : + is_overfilled(false) + { + } IAlgoServices *algo; OrderKey child_key = null_order_key(); + + Price hedge_price_diff; Quantity fill_limit; - Quantity scratch_threshold; Quantity stay_in_threshold; - ChildTemplate *child_template = nullptr; + bool enable_scratch_on_overfill; + bool enable_stay_in; - Price hedge_ticks; + ChildTemplate *child_template = nullptr; ChildTemplate *hedge_template = nullptr; ChildTemplate *overfill_hedge_template = nullptr; @@ -47,6 +51,7 @@ struct ProRataAlgo::PImpl Book last_book; Book book_at_inception; + bool is_overfilled; }; ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} @@ -68,8 +73,7 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) } if (!prorata.has_hedge()) - return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, - "ProRata order must have a hedge order definition"}; + return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, "ProRata order must have a hedge order definition"}; if (!serv.is_attaching()) { // if we are not attaching create a child template so we can @@ -81,7 +85,7 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) init_hedge_template(serv, prorata.hedge()); init_hedge_template(serv, prorata.overfill_hedge()); - set_hedge_ticks(serv, prorata); + set_parameters(serv, prorata); return {}; } @@ -109,7 +113,7 @@ void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState } -void ProRataAlgo::on_book(SecurityKey, Book const &b) +void ProRataAlgo::on_book(SecurityKey, const Book& b) { me->last_book = b; @@ -127,25 +131,58 @@ void ProRataAlgo::on_book(SecurityKey, Book const &b) if (other_side_top.size() > 0) { - me->status = Status::UNDERFILLED; + me->status = Status::WORKING; me->book_at_inception = b; LOG(OrderMan, Diag, "ProRataAlgo order {} receiving first book: {} Target: {}", - algo().order_key().value(), me->scratch_threshold.value(), + algo().order_key().value(), me->fill_limit.value(), other_side_top.price()); } - } break; - case Status::UNDERFILLED: - break; - case Status::OVERFILLED: - //TODO bail.... + } + break; + case Status::WORKING: + check_overfill(); break; case Status::DONE: + check_overfill(); break; } } -void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) +void ProRataAlgo::check_overfill() +{ + if(me->is_overfilled) + { + if(me->enable_stay_in) + { + auto my_side = algo().definition().side(); + auto other_side = securities::other_side(my_side); + + Quantity my_quan; + bool found = SpreaderUtils::find_qty_upto_price_level(me->last_book, my_side, algo().price(), /*unused*/ 0.0, my_quan); + auto &my_side_psz = me->last_book.side(my_side)[0]; + auto &other_side_psz = me->last_book.side(other_side)[0]; + bool market_moved = other_side_psz.price() != my_side_psz.price(); + + if (market_moved || !found || (found && my_quan.value() <= me->stay_in_threshold.value()) ) + get_out(); + } + else + get_out(); + } +} + +void ProRataAlgo::get_out() +{ + if(me->enable_scratch_on_overfill) + { + //scratch all my overs TODO + me->enable_scratch_on_overfill = false; + } + algo().cancel_child(me->child_key); +} + +void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) { if (k == me->child_key) { @@ -153,31 +190,30 @@ void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) { auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); me->hedged_fills = Quantity(hedge_qty.value() + me->hedged_fills.value()); - auto hedge_price = securities::improve_price(algo().definition().side(), st.last_price, me->hedge_ticks); + auto hedge_price = securities::improve_price(algo().definition().side(), st.last_price, me->hedge_price_diff); if(me->hedged_fills < me->fill_limit) { auto hedge = algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); me->hedges.push_back(hedge); LOG(OrderMan, Diag, "ProRataAlgo {}, launched new hedge in response to child fill. " - "Hedge: " - "{} {}@{}", + "Hedge: {} {}@{}", algo().order_key().value(), securities::side_name(algo().definition().side()), hedge_qty.value(), hedge_price.value()); } else - { + { auto hedge = algo().launch_child(*me->overfill_hedge_template, hedge_price, hedge_qty); me->overfill_hedges.push_back(hedge); LOG(OrderMan, Diag, "ProRataAlgo {}, launched new OVERFILL hedge in response to child fill. " - "Hedge: " - "{} {}@{}", + "Hedge: {} {}@{}", algo().order_key().value(), securities::side_name(algo().definition().side()), - hedge_qty.value(), hedge_price.value()); - me->status = Status::OVERFILLED; + hedge_qty.value(), hedge_price.value()); + me->is_overfilled = true; + check_overfill(); } } algo().update_fill_state(st.total_fills, st.last_price); @@ -186,13 +222,12 @@ void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) case ChildStatus::WORKING: break; case ChildStatus::COMPLETED: - LOG(OrderMan, Info, - "ProRataAlgo {}. Child is completed, waiting for hedges ", algo().order_key().value()); + LOG(OrderMan, Info, "ProRataAlgo {}. Child is completed, waiting for hedges ", algo().order_key().value()); + me->status = Status::DONE; break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, terminating", algo().order_key().value()); - me->status = Status::DONE; - algo().terminate_error("Unexpected child cancel"); + LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, waiting on hedges", algo().order_key().value()); + me->status = Status::DONE; break; } } @@ -201,15 +236,9 @@ void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) switch (st.status) { case ChildStatus::WORKING: - break; - case ChildStatus::COMPLETED: - if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); it != me->hedges.end()) - me->hedges.erase(it); - if (auto oit = std::find(me->overfill_hedges.begin(), me->overfill_hedges.end(), k); oit != me->overfill_hedges.end()) - me->overfill_hedges.erase(oit); - break; + break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "ProRataAlgo {}. Hedge was canceled, removing", algo().order_key().value()); + case ChildStatus::COMPLETED: if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); it != me->hedges.end()) me->hedges.erase(it); if (auto oit = std::find(me->overfill_hedges.begin(), me->overfill_hedges.end(), k); oit != me->overfill_hedges.end()) @@ -220,7 +249,7 @@ void ProRataAlgo::on_child_change(OrderKey k, ChildState const &st) check_terminate(); } -void ProRataAlgo::do_update(api::orderman::Update const &update) +void ProRataAlgo::do_update(const api::orderman::Update& update) { if (me->status != Status::DONE) { @@ -240,14 +269,14 @@ void ProRataAlgo::do_update(api::orderman::Update const &update) // process custom part of the udpate if any if (update.has_custom()) { - //TODO MORE auto &prorata_update = update.custom().prorata(); - set_hedge_ticks(algo(), prorata_update); - if (prorata_update.has_child()) + set_parameters(algo(), prorata_update); + + if(prorata_update.has_child()) *child_update.mutable_custom() = prorata_update.child(); - if (prorata_update.has_hedge()) + if(prorata_update.has_hedge()) init_hedge_template(algo(), prorata_update.hedge()); - if (prorata_update.has_overfill_hedge()) + if(prorata_update.has_overfill_hedge()) init_hedge_template(algo(), prorata_update.overfill_hedge()); } algo().update_child(me->child_key, child_update); @@ -261,7 +290,7 @@ void ProRataAlgo::do_update(api::orderman::Update const &update) } void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, - api::orderman::CustomOrderDefinition const &hedge_cdef) + const api::orderman::CustomOrderDefinition& hedge_cdef) { auto hedge_def = algo.definition(); hedge_def.set_side(securities::other_side(algo.definition().side())); @@ -271,19 +300,24 @@ void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, me->hedge_template = &(algo.make_child_template(hedge_def)); } -void ProRataAlgo::set_hedge_ticks(IBeforeLaunchAlgoServices &serv, api::orderman::CoverOrderDefinition const &cover) +void ProRataAlgo::set_parameters(IBeforeLaunchAlgoServices &serv, const api::orderman::ProRataOrderDefinition& prorata) { - me->hedge_ticks = Price(securities::tick_size(serv.security()).value() * cover.hedge_ticks()); + me->hedge_price_diff = Price(securities::tick_size(serv.security()).value() * prorata.hedge_ticks()); + if(prorata.has_fill_limit()) + me->fill_limit = SpreaderUtils::_quantity_threshold(prorata.fill_limit(), algo().quantity()); + if(prorata.has_stay_in_threshold()) + me->stay_in_threshold = SpreaderUtils::_quantity_threshold(prorata.stay_in_threshold(), algo().quantity()); + me->enable_scratch_on_overfill = prorata.enable_scratch_on_overfill(); + me->enable_stay_in = prorata.enable_stay_in(); } void ProRataAlgo::check_terminate() { LOG(OrderMan, Diag, "ProRataAlgo {} check_terminate. State: {} Hedge count: {}", - algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, - me->hedges.size()); + algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, me->hedges.size()); - if (me->status != Status::DONE && me->hedges.empty() && me->overfill_hedges.empty()) + if (me->status == Status::DONE && me->hedges.empty() && me->overfill_hedges.empty()) { LOG(OrderMan, Info, "ProRataAlgo {}. Last hedge completed, terminating", algo().order_key().value()); algo().complete(); diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index 497fa4531..9ed33bdea 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -17,16 +17,19 @@ public: OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; void do_launch(IAlgoServices &) override; void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; - void do_update(api::orderman::Update const &) override; - void on_child_change(OrderKey, ChildState const &) override; + void do_update(const api::orderman::Update&) override; + void on_book(SecurityKey, const Book&) override; + void on_child_change(OrderKey, const ChildState&) override; private: struct PImpl; enum class Status; std::unique_ptr me; IAlgoServices &algo() const; - void set_hedge_ticks(IBeforeLaunchAlgoServices &, api::orderman::CoverOrderDefinition const &); - void init_hedge_template(IBeforeLaunchAlgoServices &, api::orderman::CustomOrderDefinition const &); + void set_parameters(IBeforeLaunchAlgoServices &, const api::orderman::ProRataOrderDefinition&); + void init_hedge_template(IBeforeLaunchAlgoServices &, const api::orderman::CustomOrderDefinition&); + void get_out(); + void check_overfill(); void check_terminate(); const char *status_name(Status st); }; diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 766b37d31..443b200a2 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -145,7 +145,7 @@ void ScratchAlgo::try_to_scratch() auto &other_side_psz = me->last_book.side(other_side)[0]; bool market_moved = other_side_psz.price() != my_side_psz.price(); - if (market_moved || found && my_quan.value() <= me->scratch_threshold.value()) + if (market_moved || !found || (found && my_quan.value() <= me->scratch_threshold.value()) ) { Price scratch_price; Quantity scratch_quan; From c8c6aea1882e508d5eee6df07b4e2f1b30130795 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 24 May 2019 15:50:46 -0400 Subject: [PATCH 009/153] gui for haris orders --- .../Orders/OrderTypeVMs/DynamicOrderHelper.cs | 17 ++++++ .../OrderTypeVMs/DynamicOrderParamVM.cs | 38 +++++++++++++ .../Orders/OrderTypeVMs/DynamicOrderVM.cs | 6 ++ src/lib/api/services/OrderService.proto | 2 + .../orders/algo/algo_orders/ProRataAlgo.cpp | 25 +++++++++ .../orders/algo/algo_orders/ProRataAlgo.h | 1 + .../orders/algo/algo_orders/ScratchAlgo.cpp | 19 ++++++- .../orders/algo/algo_orders/ScratchAlgo.h | 4 +- src/server/sparkd/cmds/Run.cpp | 55 +++++++++++++++++++ 9 files changed, 163 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs index 8587e9e92..f08137c66 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs @@ -71,6 +71,7 @@ namespace mbts.spark { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef: param.AsOrderDef = new CustomOrderDefinition(); break; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: param.AsString = string.Empty; break; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: param.AsUint32 = 0u; break; + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: param.AsQuantity = new bts.spark.QuantityMeasure(); break; } break; case DynamicOrderTypeDescriptor.DescriptorOneofCase.Structure: param.AsStruct = new DynamicOrderStruct(); break; @@ -87,5 +88,21 @@ namespace mbts.spark { } param.AsEnumerated.EnumValue = value ?? string.Empty; } + + internal static bts.spark.QuantityMeasureImmutable? get_quantity_value(DynamicOrderParam param) + { + return new bts.spark.QuantityMeasureImmutable(param?.AsQuantity); + } + internal static void set_quantity_value(DynamicOrderParam param, bts.spark.QuantityMeasureImmutable? value) + { + if (param == null) + { + return; + } + else if (param.ValueCase != DynamicOrderParam.ValueOneofCase.AsQuantity) + { + param.AsQuantity = value?.CloneToMutable(); + } + } } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs index 9cf2c4962..def02cd07 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs @@ -1,6 +1,7 @@ using bts.api.orderman; using Google.Protobuf.Reflection; using mbts.ui.shell; +using bts.spark; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -116,4 +117,41 @@ namespace mbts.spark { void _sync_elements() => elements.sync_with(_get_elements(descriptor)); } + + + class DynamicOrderQuantityVM : DynamicOrderParamVM, IDynamicOrderParamVM + { + static readonly FieldDescriptor _field_desc = DynamicOrderParam.Descriptor.FindFieldByNumber(DynamicOrderParam.AsQuantityFieldNumber); + QuantityMeasureImmutable? _value; + public DynamicOrderQuantityVM(DynamicOrderParamDescriptor descriptor) + : base(descriptor) + { + } + + public QuantityMeasureImmutable? value + { + get => _value; + set + { + _value = value; + _field_desc.Accessor.SetValue(param, _value?.CloneToMutable()); + RaisePropertyChanged(); + } + } + + public IObservable subscribe_value() => + Observable.Create(o => + { + PropertyChanged += on_prop_changed; + return Disposable.Create(() => PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) => + o.OnNext(value); + }); + + protected override void on_param_changed() + { + _value = new QuantityMeasureImmutable( (QuantityMeasure) _field_desc.Accessor.GetValue(param)); + RaisePropertyChanged(nameof(value)); + } + } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs index c54208b68..848602c12 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs @@ -5,6 +5,7 @@ using mbts.collections; using mbts.spark.utilities; using mbts.ui.utility; using mbts.utils; +using bts.spark; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -73,6 +74,11 @@ namespace mbts.spark { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return mk(DynamicOrderParam.AsStringFieldNumber, v => v, v => v); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: + { + var qm = new DynamicOrderQuantityVM(descriptor); + return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasureImmutable(new QuantityMeasure { Ratio = 1.0, Absolute = 1 })); + } default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.Primitive, nameof(descriptor)); } case DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated: diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index d2bc605bc..f90925449 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -563,6 +563,7 @@ enum DynamicOrderPrimitiveDescriptor { DYNAMIC_ORDER_PRIMITIVE_STRING = 3; DYNAMIC_ORDER_PRIMITIVE_BOOL = 4; DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF = 5; + DYNAMIC_ORDER_PRIMITIVE_QUANTITY = 6; } message DynamicOrderTypeDescriptor { @@ -611,6 +612,7 @@ message DynamicOrderParam { DynamicOrderArray as_array = 7; DynamicOrderStruct as_struct = 8; DynamicOrderEnumerated as_enumerated = 9; + spark.QuantityMeasure as_quantity = 10; } } diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 2bdd18e55..5ad8dfc96 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -58,6 +58,31 @@ ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} ProRataAlgo::~ProRataAlgo() {} IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } +void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) +{ + if (def.has_custom_def() && def.custom_def().has_dynamic()) + { + auto& dprorata = def.custom_def().dynamic(); + auto& mutable_def = const_cast(def); //TODO Terrible hack + auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); + + if(dprorata.order_params().count("hedge_ticks") > 0) + prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); + if(dprorata.order_params().count("fill_limit") > 0) + *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); + if(dprorata.order_params().count("stay_in_threshold") > 0) + *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); + if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) + prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); + if(dprorata.order_params().count("enable_stay_in") > 0) + prorata.set_enable_stay_in(dprorata.order_params().at("enable_stay_in").as_bool()); + + *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); + *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); + *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); + } +} + OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { auto &def = serv.definition(); diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index 9ed33bdea..f4c19960a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -32,6 +32,7 @@ private: void check_overfill(); void check_terminate(); const char *status_name(Status st); + void check_dynamic(const api::orderman::OrderDefinition&); }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 443b200a2..ea3ad3744 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -29,12 +29,27 @@ ScratchAlgo::ScratchAlgo() : me(new PImpl()) {} ScratchAlgo::~ScratchAlgo() {} IAlgoServices &ScratchAlgo::algo() const { return *me->algo; } +void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) +{ + if (def.has_custom_def() && def.custom_def().has_dynamic()) + { + auto& dscratch = def.custom_def().dynamic(); + auto& mutable_def = const_cast(def); //TODO Terrible hack + auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); + + if(dscratch.order_params().count("scratch_threshold") > 0) + *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); + + *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); + } +} + OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { - auto &def = serv.definition(); + const api::orderman::OrderDefinition& def = serv.definition(); + check_dynamic(def); bts_assert(def.has_custom_def()); bts_assert(def.custom_def().has_scratch()); - // Validate the scratch part of the definition auto &scratch = def.custom_def().scratch(); if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h index d86487366..76c220a80 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h @@ -27,10 +27,10 @@ private: std::unique_ptr me; IAlgoServices &algo() const ; - void update_scratch_threshold(); - bool has_running_child() const; + void update_scratch_threshold(); void try_to_scratch(); Price get_price_to_pass (Price price) const; + void check_dynamic(const api::orderman::OrderDefinition&); }; }} diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 3c9b1ec1c..14b2faaf6 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -34,6 +34,10 @@ #include "server/sparkd/user/AdminServiceServer.h" #include "server/sparkd/user/AuthServiceServer.h" #include "server/sparkd/algo/AlgoDefinitionServiceServer.h" + +#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" + #include namespace bts { @@ -76,6 +80,57 @@ int Run::execute(std::vector const &) { SparkServices plugin_services(dyn_order_registry); plugin::init_plugins(plugin_services); // finished initializing order plugins + + //TODO these will go away once Haris approves algo + using namespace api::orderman; + { + DynamicOrderDescriptor desc; + desc.set_dynamic_order_type("Scratch"); + auto param = desc.add_param_desc(); + param->set_param_name("scratch_threshold"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); + + param = desc.add_param_desc(); + param->set_param_name("child"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); + plugin_services.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + } + + { + DynamicOrderDescriptor desc; + desc.set_dynamic_order_type("ProRata"); + auto param = desc.add_param_desc(); + param->set_param_name("hedge_ticks"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); + param = desc.add_param_desc(); + param->set_param_name("fill_limit"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); + + param = desc.add_param_desc(); + param->set_param_name("stay_in_threshold"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); + param = desc.add_param_desc(); + param->set_param_name("enable_scratch_on_overfill"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_BOOL); + param = desc.add_param_desc(); + param->set_param_name("enable_stay_in"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_BOOL); + + param = desc.add_param_desc(); + param->set_param_name("child"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); + param = desc.add_param_desc(); + param->set_param_name("hedge"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); + param = desc.add_param_desc(); + param->set_param_name("overfill_hedge"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); + + + plugin_services.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + } orderman::OrderServiceServer order_service_server( sc, mdclient, dyn_order_registry, stats, perf_log); From 117e1071162c8bfa23ff5a0155a8e4b97c122cca Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 24 May 2019 22:50:12 -0400 Subject: [PATCH 010/153] dynamics work again --- .../DynamicOrderTemplateSelector.cs | 2 ++ .../Orders/OrderTypeVMs/DynamicOrderVM.cs | 7 +++++-- .../Order/AlgoContentTemplateSelector.xaml | 21 +++++++++++++++++++ .../orders/algo/algo_orders/ScratchAlgo.cpp | 5 ++++- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs index e09a3acab..cdf5a4906 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs @@ -19,6 +19,7 @@ namespace mbts.ui.spark public DataTemplate UIntTemplate { get; set; } public DataTemplate StringTemplate { get; set; } public DataTemplate OrderDefTemplate { get; set; } + public DataTemplate QuantityMeasureTemplate { get; set; } public DataTemplate EnumTemplate { get; set; } @@ -39,6 +40,7 @@ namespace mbts.ui.spark case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return UIntTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return StringTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef: return OrderDefTemplate; + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: return QuantityMeasureTemplate; default: return null; } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs index 848602c12..892e07fa0 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs @@ -47,10 +47,13 @@ namespace mbts.spark { protected override void on_model_internal() => _sync_dynamic_params(); - protected override IEnumerable get_selector_datum() => - OrderDataHelper.get_child_algo_data(new CustomOrderDefinition { + protected override IEnumerable get_selector_datum() + { + var data = OrderDataHelper.get_child_algo_data(new CustomOrderDefinition { Dynamic = model?.dynamic_helper?.create_def(dyn_type) ?? new DynamicOrderDefinition() }); + return data; + } void _sync_dynamic_params() { if (model != null) { diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 6706baf48..96db2855a 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1094,6 +1094,27 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index ea3ad3744..fb6c7dc03 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -40,7 +40,10 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dscratch.order_params().count("scratch_threshold") > 0) *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); - *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); + if(dscratch.order_params().count("child") > 0) + *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); + else + scratch.mutable_child()->mutable_limit(); } } From 5f125790cf74164322dc31e6ce239e7c0c827bd2 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sat, 25 May 2019 10:50:27 -0400 Subject: [PATCH 011/153] dynamics can now have QuantityMeasure --- .../Orders/OrderTypeVMs/DynamicOrderHelper.cs | 7 +---- .../OrderTypeVMs/DynamicOrderParamVM.cs | 28 ++++++++++--------- .../Orders/OrderTypeVMs/DynamicOrderVM.cs | 2 +- .../Order/AlgoContentTemplateSelector.xaml | 7 +++-- .../orders/algo/algo_orders/ProRataAlgo.cpp | 10 +++++-- .../orders/algo/algo_orders/ScratchAlgo.cpp | 7 ++++- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs index f08137c66..9bae979d3 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs @@ -96,13 +96,8 @@ namespace mbts.spark { internal static void set_quantity_value(DynamicOrderParam param, bts.spark.QuantityMeasureImmutable? value) { if (param == null) - { return; - } - else if (param.ValueCase != DynamicOrderParam.ValueOneofCase.AsQuantity) - { - param.AsQuantity = value?.CloneToMutable(); - } + param.AsQuantity = value?.CloneToMutable(); } } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs index def02cd07..80e0c6edb 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs @@ -122,36 +122,38 @@ namespace mbts.spark { class DynamicOrderQuantityVM : DynamicOrderParamVM, IDynamicOrderParamVM { static readonly FieldDescriptor _field_desc = DynamicOrderParam.Descriptor.FindFieldByNumber(DynamicOrderParam.AsQuantityFieldNumber); - QuantityMeasureImmutable? _value; public DynamicOrderQuantityVM(DynamicOrderParamDescriptor descriptor) : base(descriptor) { } - public QuantityMeasureImmutable? value + QuantityMeasureImmutable? IDynamicOrderParamVM.value { get => quantity; set => quantity= value; } + public QuantityMeasureImmutable? quantity { - get => _value; - set + get => DynamicOrderHelper.get_quantity_value(param); + set { - _value = value; - _field_desc.Accessor.SetValue(param, _value?.CloneToMutable()); - RaisePropertyChanged(); + var orig = quantity; + DynamicOrderHelper.set_quantity_value(param, value); + if (quantity != orig) + RaisePropertyChanged(); } - } + } - public IObservable subscribe_value() => - Observable.Create(o => + public IObservable subscribe_value() + { + return Observable.Create(o => { PropertyChanged += on_prop_changed; return Disposable.Create(() => PropertyChanged -= on_prop_changed); void on_prop_changed(object sender, PropertyChangedEventArgs e) => - o.OnNext(value); + o.OnNext(quantity); }); + } protected override void on_param_changed() { - _value = new QuantityMeasureImmutable( (QuantityMeasure) _field_desc.Accessor.GetValue(param)); - RaisePropertyChanged(nameof(value)); + RaisePropertyChanged(nameof(quantity)); } } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs index 892e07fa0..76af9879d 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs @@ -80,7 +80,7 @@ namespace mbts.spark { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: { var qm = new DynamicOrderQuantityVM(descriptor); - return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasureImmutable(new QuantityMeasure { Ratio = 1.0, Absolute = 1 })); + return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasure { Ratio = 1.0, Absolute = 1 }); } default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.Primitive, nameof(descriptor)); } diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 96db2855a..dc159595c 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1102,8 +1102,11 @@ - - + + diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 5ad8dfc96..49f973079 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -65,7 +65,12 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) auto& dprorata = def.custom_def().dynamic(); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); - + + std::string keys; + for(auto& pair : dprorata.order_params()) + { + keys += pair.first; + } if(dprorata.order_params().count("hedge_ticks") > 0) prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); if(dprorata.order_params().count("fill_limit") > 0) @@ -85,7 +90,8 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { - auto &def = serv.definition(); + const api::orderman::OrderDefinition& def = serv.definition(); + check_dynamic(def); bts_assert(def.has_custom_def()); bts_assert(def.custom_def().has_prorata()); diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index fb6c7dc03..9adaaf79c 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -36,7 +36,12 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) auto& dscratch = def.custom_def().dynamic(); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); - + + std::string keys; + for(auto& pair : dscratch.order_params()) + { + keys += pair.first; + } if(dscratch.order_params().count("scratch_threshold") > 0) *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); From 6fc3c0b6016c2c862c6626fb992f6d7dbc22154a Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sat, 25 May 2019 17:48:54 -0400 Subject: [PATCH 012/153] the bug turned out to be in my brain --- .../OrderTypeVMs/DynamicOrderParamVM.cs | 4 +--- .../orderman/orders/algo/AlgoWrapperOrder.cpp | 3 ++- .../orders/algo/algo_orders/ProRataAlgo.cpp | 20 +++++++++++++++---- .../orders/algo/algo_orders/ScratchAlgo.cpp | 5 +++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs index 80e0c6edb..d80ae2413 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs @@ -123,9 +123,7 @@ namespace mbts.spark { { static readonly FieldDescriptor _field_desc = DynamicOrderParam.Descriptor.FindFieldByNumber(DynamicOrderParam.AsQuantityFieldNumber); public DynamicOrderQuantityVM(DynamicOrderParamDescriptor descriptor) - : base(descriptor) - { - } + : base(descriptor) { } QuantityMeasureImmutable? IDynamicOrderParamVM.value { get => quantity; set => quantity= value; } public QuantityMeasureImmutable? quantity diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index b14e7c7ef..f1c7dc58e 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -306,7 +306,8 @@ orderman::OrderOpResult AlgoWrapperOrder::on_cancel() { // IBeforeLaunchAlgoServices // -------------------------------------------------------------------------------- api::orderman::OrderDefinition const &AlgoWrapperOrder::definition() const { - return *def(); + //return *def(); + return me->definition; } api::Security const &AlgoWrapperOrder::security() const { diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 49f973079..6670084b0 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -62,7 +62,8 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) { if (def.has_custom_def() && def.custom_def().has_dynamic()) { - auto& dprorata = def.custom_def().dynamic(); + //copy + api::orderman::DynamicOrderDefinition dprorata = def.custom_def().dynamic(); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); @@ -82,9 +83,20 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dprorata.order_params().count("enable_stay_in") > 0) prorata.set_enable_stay_in(dprorata.order_params().at("enable_stay_in").as_bool()); - *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); - *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); - *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); + if(dprorata.order_params().count("child") > 0) + *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); + else + prorata.mutable_child()->mutable_limit(); + + if(dprorata.order_params().count("hedge") > 0) + *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); + else + prorata.mutable_hedge()->mutable_limit(); + + if(dprorata.order_params().count("overfill_hedge") > 0) + *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); + else + prorata.mutable_overfill_hedge()->mutable_limit(); } } diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 9adaaf79c..c3ab411c0 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -33,7 +33,8 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) { if (def.has_custom_def() && def.custom_def().has_dynamic()) { - auto& dscratch = def.custom_def().dynamic(); + //copy + api::orderman::DynamicOrderDefinition dscratch = def.custom_def().dynamic(); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); @@ -54,7 +55,7 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { - const api::orderman::OrderDefinition& def = serv.definition(); + auto& def = serv.definition(); check_dynamic(def); bts_assert(def.has_custom_def()); bts_assert(def.custom_def().has_scratch()); From ea232fbaf1a637bb3bae645e85f0f1059dc44bcc Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sat, 25 May 2019 23:27:54 -0400 Subject: [PATCH 013/153] ScratchAlgo works --- src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp | 3 +-- .../orders/algo/algo_orders/ProRataAlgo.cpp | 15 +++++++-------- .../orders/algo/algo_orders/ScratchAlgo.cpp | 15 +++++++++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index f1c7dc58e..b14e7c7ef 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -306,8 +306,7 @@ orderman::OrderOpResult AlgoWrapperOrder::on_cancel() { // IBeforeLaunchAlgoServices // -------------------------------------------------------------------------------- api::orderman::OrderDefinition const &AlgoWrapperOrder::definition() const { - //return *def(); - return me->definition; + return *def(); } api::Security const &AlgoWrapperOrder::security() const { diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 6670084b0..c72a97ff0 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -63,7 +63,8 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if (def.has_custom_def() && def.custom_def().has_dynamic()) { //copy - api::orderman::DynamicOrderDefinition dprorata = def.custom_def().dynamic(); + api::orderman::DynamicOrderDefinition dprorata; + dprorata.CopyFrom(def.custom_def().dynamic()); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); @@ -200,14 +201,12 @@ void ProRataAlgo::check_overfill() { auto my_side = algo().definition().side(); auto other_side = securities::other_side(my_side); - - Quantity my_quan; - bool found = SpreaderUtils::find_qty_upto_price_level(me->last_book, my_side, algo().price(), /*unused*/ 0.0, my_quan); - auto &my_side_psz = me->last_book.side(my_side)[0]; + + auto &my_side_psz = me->last_book.side(my_side)[0]; auto &other_side_psz = me->last_book.side(other_side)[0]; - bool market_moved = other_side_psz.price() != my_side_psz.price(); - - if (market_moved || !found || (found && my_quan.value() <= me->stay_in_threshold.value()) ) + bool market_moved = !securities::is_same(Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); + bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); + if (market_moved || (on_market && my_side_psz.size() <= me->stay_in_threshold.value()) ) get_out(); } else diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index c3ab411c0..1e015968d 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -22,6 +22,7 @@ struct ScratchAlgo::PImpl ChildTemplate *child_template = nullptr; Book last_book; Book book_at_inception; + Quantity total_fills = Quantity(0); Price scratch_price=Price(os::nan()); }; @@ -33,8 +34,10 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) { if (def.has_custom_def() && def.custom_def().has_dynamic()) { + //copy - api::orderman::DynamicOrderDefinition dscratch = def.custom_def().dynamic(); + api::orderman::DynamicOrderDefinition dscratch; + dscratch.CopyFrom(def.custom_def().dynamic()); auto& mutable_def = const_cast(def); //TODO Terrible hack auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); @@ -164,12 +167,11 @@ void ScratchAlgo::try_to_scratch() auto other_side = securities::other_side(my_side); Quantity my_quan; - bool found = SpreaderUtils::find_qty_upto_price_level(me->last_book, my_side, algo().price(), /*unused*/ 0.0, my_quan); auto &my_side_psz = me->last_book.side(my_side)[0]; auto &other_side_psz = me->last_book.side(other_side)[0]; - bool market_moved = other_side_psz.price() != my_side_psz.price(); - - if (market_moved || !found || (found && my_quan.value() <= me->scratch_threshold.value()) ) + bool market_moved = !securities::is_same(Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); + bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); + if (market_moved || (on_market && my_side_psz.size() <= me->scratch_threshold.value()) ) { Price scratch_price; Quantity scratch_quan; @@ -182,7 +184,7 @@ void ScratchAlgo::try_to_scratch() me->scratch_price = scratch_price; child_update.set_price(scratch_price.value()); //we still want to try to scratch all of it - child_update.set_quantity(algo().quantity().value()); + child_update.set_quantity(algo().quantity().value() - me->total_fills.value()); algo().update_child(me->child_key, child_update); me->status = Status::SCRATCHED; } @@ -222,6 +224,7 @@ void ScratchAlgo::update_scratch_threshold() void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) { algo().update_fill_state(st.total_fills, st.last_price); + me->total_fills = st.total_fills; switch (st.status) { case ChildStatus::WORKING: From e19bb7c6218f9132dd8b27b9ef18b4297b77dd43 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sat, 25 May 2019 23:45:17 -0400 Subject: [PATCH 014/153] chase if worse --- src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 1e015968d..86d064979 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -169,7 +169,8 @@ void ScratchAlgo::try_to_scratch() Quantity my_quan; auto &my_side_psz = me->last_book.side(my_side)[0]; auto &other_side_psz = me->last_book.side(other_side)[0]; - bool market_moved = !securities::is_same(Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); + bool market_moved = !securities::is_equal_or_better(other_side, + Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); if (market_moved || (on_market && my_side_psz.size() <= me->scratch_threshold.value()) ) { From 729986d53d710932acec8c86f465d5619f4d79de Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sun, 26 May 2019 23:30:55 -0400 Subject: [PATCH 015/153] need to test get_out function --- .../orders/algo/algo_orders/ProRataAlgo.cpp | 149 +++++++++++------- .../orders/algo/algo_orders/ProRataAlgo.h | 5 +- .../orders/algo/algo_orders/ScratchAlgo.cpp | 9 +- 3 files changed, 100 insertions(+), 63 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index c72a97ff0..20d489edc 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -11,7 +11,7 @@ namespace orderman { using namespace api::orderman; -enum class ProRataAlgo::Status { WAITING_FOR_MD, WORKING, DONE }; //this is for the child +enum class ProRataAlgo::Status { WAITING_FOR_MD, WORKING, DONE_WORKING, GETTING_OUT }; //this is for the child const char *ProRataAlgo::status_name(Status st) { switch (st) @@ -20,8 +20,10 @@ const char *ProRataAlgo::status_name(Status st) return "WAITING_FOR_MD"; case Status::WORKING: return "WORKING"; - case Status::DONE: - return "DONE"; + case Status::DONE_WORKING: + return "DONE_WORKING"; + case Status::GETTING_OUT: + return "GETTING_OUT"; } return nullptr; } @@ -47,7 +49,7 @@ struct ProRataAlgo::PImpl Quantity hedged_fills = Quantity(0); Status status = Status::WAITING_FOR_MD; std::vector hedges; - std::vector overfill_hedges; + std::map overfill_hedges; Book last_book; Book book_at_inception; @@ -68,11 +70,6 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) auto& mutable_def = const_cast(def); //TODO Terrible hack auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); - std::string keys; - for(auto& pair : dprorata.order_params()) - { - keys += pair.first; - } if(dprorata.order_params().count("hedge_ticks") > 0) prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); if(dprorata.order_params().count("fill_limit") > 0) @@ -127,15 +124,15 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) me->child_template = &(serv.make_child_template(child_def)); } - init_hedge_template(serv, prorata.hedge()); - init_hedge_template(serv, prorata.overfill_hedge()); - set_parameters(serv, prorata); + init_hedge_template(serv, prorata.hedge(), false); + init_hedge_template(serv, prorata.overfill_hedge(), true); return {}; } void ProRataAlgo::do_launch(IAlgoServices &algo) { me->algo = &algo; + set_parameters(algo, algo.definition().custom_def().prorata()); LOG(OrderMan, Diag, "Starting ProRataAlgo {}, {} {}@{}. Will cover {} ticks", algo.order_key().value(), securities::side_name(algo.definition().side()), algo.quantity().value(), @@ -150,6 +147,7 @@ void ProRataAlgo::do_launch(IAlgoServices &algo) void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) { me->algo = &algo; + set_parameters(algo, algo.definition().custom_def().prorata()); me->child_key = k; me->hedged_fills = st.total_fills; algo.subscribe_book(SecurityKey(algo.security().id())); @@ -187,15 +185,17 @@ void ProRataAlgo::on_book(SecurityKey, const Book& b) case Status::WORKING: check_overfill(); break; - case Status::DONE: + case Status::DONE_WORKING: check_overfill(); break; + case Status::GETTING_OUT: + break; } } void ProRataAlgo::check_overfill() { - if(me->is_overfilled) + if(me->is_overfilled && me->status != Status::GETTING_OUT) { if(me->enable_stay_in) { @@ -204,8 +204,10 @@ void ProRataAlgo::check_overfill() auto &my_side_psz = me->last_book.side(my_side)[0]; auto &other_side_psz = me->last_book.side(other_side)[0]; - bool market_moved = !securities::is_same(Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); + bool market_moved = !securities::is_equal_or_better(other_side, + Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); + if (market_moved || (on_market && my_side_psz.size() <= me->stay_in_threshold.value()) ) get_out(); } @@ -214,14 +216,59 @@ void ProRataAlgo::check_overfill() } } +void ProRataAlgo::scratch(std::pair kk) +{ + auto other_side = securities::other_side(algo().definition().side()); + Price scratch_price; + Quantity scratch_quan; + SpreaderUtils::traverse_depth_for_quantity(me->last_book, other_side, kk.second, scratch_price, scratch_quan); + + Update update; + update.set_price(scratch_price.value()); + //we still want to try to scratch all of it + update.set_quantity(kk.second.value()); + algo().update_child(kk.first, update); +} + void ProRataAlgo::get_out() { - if(me->enable_scratch_on_overfill) - { - //scratch all my overs TODO - me->enable_scratch_on_overfill = false; + if(me->status != Status::GETTING_OUT) + { + me->status = Status::GETTING_OUT; + if(me->enable_scratch_on_overfill) + { + for(auto& h : me->overfill_hedges) + scratch(h); + } + algo().cancel_child(me->child_key); + check_terminate(); } - algo().cancel_child(me->child_key); +} + +void ProRataAlgo::standard_hedge(Price hedge_price, Quantity hedge_qty) +{ + auto hedge = algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); + me->hedges.push_back(hedge); + LOG(OrderMan, Diag, + "ProRataAlgo {}, launched new hedge in response to child fill. " + "Hedge: {} {}@{}", + algo().order_key().value(), + securities::side_name(algo().definition().side()), + hedge_qty.value(), hedge_price.value()); +} + +void ProRataAlgo::overfill_hedge(Price hedge_price, Quantity hedge_qty) +{ + auto hedge = algo().launch_child(*me->overfill_hedge_template, hedge_price, hedge_qty); + me->overfill_hedges[hedge] = hedge_qty; + LOG(OrderMan, Diag, + "ProRataAlgo {}, launched new OVERFILL hedge in response to child fill. " + "Hedge: {} {}@{}", + algo().order_key().value(), + securities::side_name(algo().definition().side()), + hedge_qty.value(), hedge_price.value()); + me->is_overfilled = true; + check_overfill(); } void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) @@ -230,33 +277,22 @@ void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) { if (st.total_fills.value() > me->hedged_fills.value()) { - auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); - me->hedged_fills = Quantity(hedge_qty.value() + me->hedged_fills.value()); + auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); auto hedge_price = securities::improve_price(algo().definition().side(), st.last_price, me->hedge_price_diff); - if(me->hedged_fills < me->fill_limit) - { - auto hedge = algo().launch_child(*me->hedge_template, hedge_price, hedge_qty); - me->hedges.push_back(hedge); - LOG(OrderMan, Diag, - "ProRataAlgo {}, launched new hedge in response to child fill. " - "Hedge: {} {}@{}", - algo().order_key().value(), - securities::side_name(algo().definition().side()), - hedge_qty.value(), hedge_price.value()); - } + if( me->hedged_fills.value() + hedge_qty.value() <= me->fill_limit.value() ) + standard_hedge(hedge_price, hedge_qty); else { - auto hedge = algo().launch_child(*me->overfill_hedge_template, hedge_price, hedge_qty); - me->overfill_hedges.push_back(hedge); - LOG(OrderMan, Diag, - "ProRataAlgo {}, launched new OVERFILL hedge in response to child fill. " - "Hedge: {} {}@{}", - algo().order_key().value(), - securities::side_name(algo().definition().side()), - hedge_qty.value(), hedge_price.value()); - me->is_overfilled = true; - check_overfill(); + auto over = me->hedged_fills.value() + hedge_qty.value() - me->fill_limit.value(); + if(over < hedge_qty.value()) + { + standard_hedge(hedge_price, Quantity(hedge_qty.value() - over)); + overfill_hedge(hedge_price, Quantity(over)); + } + else + overfill_hedge(hedge_price, hedge_qty); } + me->hedged_fills = Quantity(hedge_qty.value() + me->hedged_fills.value()); } algo().update_fill_state(st.total_fills, st.last_price); switch (st.status) @@ -265,11 +301,11 @@ void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) break; case ChildStatus::COMPLETED: LOG(OrderMan, Info, "ProRataAlgo {}. Child is completed, waiting for hedges ", algo().order_key().value()); - me->status = Status::DONE; + me->status = Status::DONE_WORKING; break; case ChildStatus::CANCELED: LOG(OrderMan, Info, "ProRataAlgo {}. Child was canceled, waiting on hedges", algo().order_key().value()); - me->status = Status::DONE; + me->status = Status::DONE_WORKING; break; } } @@ -283,8 +319,8 @@ void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) case ChildStatus::COMPLETED: if (auto it = std::find(me->hedges.begin(), me->hedges.end(), k); it != me->hedges.end()) me->hedges.erase(it); - if (auto oit = std::find(me->overfill_hedges.begin(), me->overfill_hedges.end(), k); oit != me->overfill_hedges.end()) - me->overfill_hedges.erase(oit); + if(me->overfill_hedges.find(k) != me->overfill_hedges.end()) + me->overfill_hedges.erase(k); break; } } @@ -293,7 +329,7 @@ void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) void ProRataAlgo::do_update(const api::orderman::Update& update) { - if (me->status != Status::DONE) + if (me->status != Status::DONE_WORKING) { LOG(OrderMan, Diag, "ProRataAlgo order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); @@ -317,9 +353,9 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) if(prorata_update.has_child()) *child_update.mutable_custom() = prorata_update.child(); if(prorata_update.has_hedge()) - init_hedge_template(algo(), prorata_update.hedge()); + init_hedge_template(algo(), prorata_update.hedge(), false); if(prorata_update.has_overfill_hedge()) - init_hedge_template(algo(), prorata_update.overfill_hedge()); + init_hedge_template(algo(), prorata_update.overfill_hedge(), true); } algo().update_child(me->child_key, child_update); } @@ -332,14 +368,19 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) } void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, - const api::orderman::CustomOrderDefinition& hedge_cdef) + const api::orderman::CustomOrderDefinition& hedge_cdef, bool overfill) { - auto hedge_def = algo.definition(); + api::orderman::OrderDefinition hedge_def; + hedge_def.CopyFrom(algo.definition()); + hedge_def.set_side(securities::other_side(algo.definition().side())); hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); hedge_def.clear_attach_target_order_key(); *hedge_def.mutable_custom_def() = hedge_cdef; - me->hedge_template = &(algo.make_child_template(hedge_def)); + if(!overfill) + me->hedge_template = &(algo.make_child_template(hedge_def)); + else + me->overfill_hedge_template = &(algo.make_child_template(hedge_def)); } void ProRataAlgo::set_parameters(IBeforeLaunchAlgoServices &serv, const api::orderman::ProRataOrderDefinition& prorata) @@ -359,7 +400,7 @@ void ProRataAlgo::check_terminate() "ProRataAlgo {} check_terminate. State: {} Hedge count: {}", algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, me->hedges.size()); - if (me->status == Status::DONE && me->hedges.empty() && me->overfill_hedges.empty()) + if (me->status == Status::DONE_WORKING && me->hedges.empty() && me->overfill_hedges.empty()) { LOG(OrderMan, Info, "ProRataAlgo {}. Last hedge completed, terminating", algo().order_key().value()); algo().complete(); diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index f4c19960a..07ffc294c 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -27,8 +27,11 @@ private: std::unique_ptr me; IAlgoServices &algo() const; void set_parameters(IBeforeLaunchAlgoServices &, const api::orderman::ProRataOrderDefinition&); - void init_hedge_template(IBeforeLaunchAlgoServices &, const api::orderman::CustomOrderDefinition&); + void init_hedge_template(IBeforeLaunchAlgoServices &, const api::orderman::CustomOrderDefinition&, bool); + void scratch(std::pair kk); void get_out(); + void standard_hedge(Price hedge_price, Quantity hedge_qty); + void overfill_hedge(Price hedge_price, Quantity hedge_qty); void check_overfill(); void check_terminate(); const char *status_name(Status st); diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 86d064979..e63254a9f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -22,7 +22,6 @@ struct ScratchAlgo::PImpl ChildTemplate *child_template = nullptr; Book last_book; Book book_at_inception; - Quantity total_fills = Quantity(0); Price scratch_price=Price(os::nan()); }; @@ -41,11 +40,6 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) auto& mutable_def = const_cast(def); //TODO Terrible hack auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); - std::string keys; - for(auto& pair : dscratch.order_params()) - { - keys += pair.first; - } if(dscratch.order_params().count("scratch_threshold") > 0) *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); @@ -185,7 +179,7 @@ void ScratchAlgo::try_to_scratch() me->scratch_price = scratch_price; child_update.set_price(scratch_price.value()); //we still want to try to scratch all of it - child_update.set_quantity(algo().quantity().value() - me->total_fills.value()); + child_update.set_quantity(algo().quantity().value()); algo().update_child(me->child_key, child_update); me->status = Status::SCRATCHED; } @@ -225,7 +219,6 @@ void ScratchAlgo::update_scratch_threshold() void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) { algo().update_fill_state(st.total_fills, st.last_price); - me->total_fills = st.total_fills; switch (st.status) { case ChildStatus::WORKING: From 0a33fb37b90e1ca72ca38d471bf319714eeb8994 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 27 May 2019 22:53:56 -0400 Subject: [PATCH 016/153] rata looking good --- src/lib/api/services/OrderService.proto | 10 ++++++---- .../orders/algo/algo_orders/ProRataAlgo.cpp | 20 ++++++++++++------- .../orders/algo/algo_orders/ScratchAlgo.cpp | 5 +++++ src/server/sparkd/cmds/Run.cpp | 7 +++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index f90925449..35d385b16 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -201,14 +201,16 @@ message CompositeOrderDefinition { } message ScratchOrderDefinition { spark.QuantityMeasure scratch_threshold = 1; + uint32 chase_ticks = 2; CustomOrderDefinition child = 10; } message ProRataOrderDefinition { uint32 hedge_ticks = 1; - spark.QuantityMeasure fill_limit = 2; - spark.QuantityMeasure stay_in_threshold = 4; - bool enable_scratch_on_overfill = 5; - bool enable_stay_in = 6; + uint32 chase_ticks = 2; + spark.QuantityMeasure fill_limit = 5; + spark.QuantityMeasure stay_in_threshold = 6; + bool enable_scratch_on_overfill = 7; + bool enable_stay_in = 8; CustomOrderDefinition child = 10; CustomOrderDefinition hedge = 20; diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 20d489edc..6e109135b 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -37,10 +37,11 @@ struct ProRataAlgo::PImpl OrderKey child_key = null_order_key(); Price hedge_price_diff; + Price chase_diff; Quantity fill_limit; Quantity stay_in_threshold; bool enable_scratch_on_overfill; - bool enable_stay_in; + bool enable_stay_in; ChildTemplate *child_template = nullptr; ChildTemplate *hedge_template = nullptr; @@ -72,6 +73,10 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dprorata.order_params().count("hedge_ticks") > 0) prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); + prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); + if(dprorata.order_params().count("chase_ticks") > 0) + prorata.set_chase_ticks(dprorata.order_params().at("chase_ticks").as_uint32()); + if(dprorata.order_params().count("fill_limit") > 0) *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); if(dprorata.order_params().count("stay_in_threshold") > 0) @@ -218,14 +223,14 @@ void ProRataAlgo::check_overfill() void ProRataAlgo::scratch(std::pair kk) { - auto other_side = securities::other_side(algo().definition().side()); + auto cross_side = algo().definition().side(); Price scratch_price; Quantity scratch_quan; - SpreaderUtils::traverse_depth_for_quantity(me->last_book, other_side, kk.second, scratch_price, scratch_quan); + SpreaderUtils::traverse_depth_for_quantity(me->last_book, cross_side, kk.second, scratch_price, scratch_quan); + scratch_price = securities::worsen_price(cross_side, scratch_price, me->chase_diff); Update update; update.set_price(scratch_price.value()); - //we still want to try to scratch all of it update.set_quantity(kk.second.value()); algo().update_child(kk.first, update); } @@ -235,12 +240,12 @@ void ProRataAlgo::get_out() if(me->status != Status::GETTING_OUT) { me->status = Status::GETTING_OUT; + algo().cancel_child(me->child_key); if(me->enable_scratch_on_overfill) { for(auto& h : me->overfill_hedges) scratch(h); - } - algo().cancel_child(me->child_key); + } check_terminate(); } } @@ -275,7 +280,7 @@ void ProRataAlgo::on_child_change(OrderKey k, const ChildState& st) { if (k == me->child_key) { - if (st.total_fills.value() > me->hedged_fills.value()) + if (st.total_fills.value() > me->hedged_fills.value() && me->status == Status::WORKING) { auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); auto hedge_price = securities::improve_price(algo().definition().side(), st.last_price, me->hedge_price_diff); @@ -386,6 +391,7 @@ void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, void ProRataAlgo::set_parameters(IBeforeLaunchAlgoServices &serv, const api::orderman::ProRataOrderDefinition& prorata) { me->hedge_price_diff = Price(securities::tick_size(serv.security()).value() * prorata.hedge_ticks()); + me->chase_diff = Price(securities::tick_size(serv.security()).value() * prorata.chase_ticks()); if(prorata.has_fill_limit()) me->fill_limit = SpreaderUtils::_quantity_threshold(prorata.fill_limit(), algo().quantity()); if(prorata.has_stay_in_threshold()) diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index e63254a9f..5cd57bf4a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -17,6 +17,7 @@ struct ScratchAlgo::PImpl { IAlgoServices *algo; Quantity scratch_threshold; + Price chase_diff; Status status = Status::WAITING_FOR_MD; OrderKey child_key = null_order_key(); ChildTemplate *child_template = nullptr; @@ -42,6 +43,8 @@ void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dscratch.order_params().count("scratch_threshold") > 0) *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); + if(dscratch.order_params().count("chase_ticks") > 0) + scratch.set_chase_ticks(dscratch.order_params().at("chase_ticks").as_uint32()); if(dscratch.order_params().count("child") > 0) *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); @@ -214,6 +217,8 @@ void ScratchAlgo::update_scratch_threshold() { me->scratch_threshold = SpreaderUtils::_quantity_threshold( algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); + me->chase_diff = Price(securities::tick_size( + algo().security()).value() * algo().definition().custom_def().scratch().chase_ticks()); } void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 14b2faaf6..299e53901 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -89,6 +89,9 @@ int Run::execute(std::vector const &) { auto param = desc.add_param_desc(); param->set_param_name("scratch_threshold"); param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); + param = desc.add_param_desc(); + param->set_param_name("chase_ticks"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); param = desc.add_param_desc(); param->set_param_name("child"); @@ -103,6 +106,10 @@ int Run::execute(std::vector const &) { auto param = desc.add_param_desc(); param->set_param_name("hedge_ticks"); param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); + param = desc.add_param_desc(); + param->set_param_name("chase_ticks"); + param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); + param = desc.add_param_desc(); param->set_param_name("fill_limit"); param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); From 91989e6bafcede628b0ef3da821c6bff5ef3931b Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 10:19:31 -0400 Subject: [PATCH 017/153] Quantity measure validation --- .../orders/algo/algo_orders/ProRataAlgo.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 6e109135b..b177fd227 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -73,14 +73,25 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dprorata.order_params().count("hedge_ticks") > 0) prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); - prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); + prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); + if(dprorata.order_params().count("chase_ticks") > 0) prorata.set_chase_ticks(dprorata.order_params().at("chase_ticks").as_uint32()); if(dprorata.order_params().count("fill_limit") > 0) *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); + if(!prorata.has_fill_limit() || + prorata.fill_limit().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + prorata.mutable_fill_limit()->set_absolute(1u); + + if(dprorata.order_params().count("stay_in_threshold") > 0) - *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); + *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); + if(!prorata.has_stay_in_threshold() || + prorata.stay_in_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + prorata.mutable_stay_in_threshold()->set_absolute(1u); + + if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); if(dprorata.order_params().count("enable_stay_in") > 0) @@ -131,6 +142,7 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) init_hedge_template(serv, prorata.hedge(), false); init_hedge_template(serv, prorata.overfill_hedge(), true); + return {}; } From e8bc07360db095887ad89f2b77e72780b7310a00 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 13:52:18 -0400 Subject: [PATCH 018/153] got the dynamism and updates working --- src/lib/orderman/orders/algo/DynamicHelpers.h | 40 ++++++++++++ .../orders/algo/algo_orders/ScratchAlgo.cpp | 65 +++++++++++-------- .../orders/algo/algo_orders/ScratchAlgo.h | 7 +- 3 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 src/lib/orderman/orders/algo/DynamicHelpers.h diff --git a/src/lib/orderman/orders/algo/DynamicHelpers.h b/src/lib/orderman/orders/algo/DynamicHelpers.h new file mode 100644 index 000000000..99261d06d --- /dev/null +++ b/src/lib/orderman/orders/algo/DynamicHelpers.h @@ -0,0 +1,40 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" + +namespace bts { +namespace orderman { + +template +bool has_custom(const T& orig) +{ + return orig.has_custom_def(); +} +template<> +bool has_custom(const api::orderman::Update& orig) +{ + return orig.has_custom(); +} +template +const api::orderman::CustomOrderDefinition& custom(const T& orig) +{ + return orig.custom_def(); +} +template<> +const api::orderman::CustomOrderDefinition& custom(const api::orderman::Update& orig) +{ + return orig.custom(); +} +template +api::orderman::CustomOrderDefinition& mutable_custom(T& orig) +{ + return *orig.mutable_custom_def(); +} +template<> +api::orderman::CustomOrderDefinition& mutable_custom(api::orderman::Update& orig) +{ + return *orig.mutable_custom(); +} + + +}} \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 5cd57bf4a..49a4e61b3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -1,5 +1,6 @@ #include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" #include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/algo/DynamicHelpers.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" @@ -16,6 +17,8 @@ enum class ScratchAlgo::Status { WAITING_FOR_MD, WAITING_TO_SCRATCH, SCRATCHED } struct ScratchAlgo::PImpl { IAlgoServices *algo; + OrderDefinition definition; + Quantity scratch_threshold; Price chase_diff; Status status = Status::WAITING_FOR_MD; @@ -29,39 +32,40 @@ struct ScratchAlgo::PImpl ScratchAlgo::ScratchAlgo() : me(new PImpl()) {} ScratchAlgo::~ScratchAlgo() {} IAlgoServices &ScratchAlgo::algo() const { return *me->algo; } +const OrderDefinition& ScratchAlgo::definition() const { return me->definition; } -void ScratchAlgo::check_dynamic(const api::orderman::OrderDefinition& def) +template +T convert_from_dynamic(const T& orig) { - if (def.has_custom_def() && def.custom_def().has_dynamic()) - { - - //copy - api::orderman::DynamicOrderDefinition dscratch; - dscratch.CopyFrom(def.custom_def().dynamic()); - auto& mutable_def = const_cast(def); //TODO Terrible hack - auto& scratch = *mutable_def.mutable_custom_def()->mutable_scratch(); + T newt = orig; + if ( has_custom(orig) && custom(orig).has_dynamic()) + { + const DynamicOrderDefinition& dscratch = custom(orig).dynamic(); + auto& scratch = *mutable_custom(newt).mutable_scratch(); if(dscratch.order_params().count("scratch_threshold") > 0) *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); + if(dscratch.order_params().count("chase_ticks") > 0) scratch.set_chase_ticks(dscratch.order_params().at("chase_ticks").as_uint32()); if(dscratch.order_params().count("child") > 0) *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); else - scratch.mutable_child()->mutable_limit(); - } + scratch.mutable_child()->mutable_limit(); + } + return newt; } OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { - auto& def = serv.definition(); - check_dynamic(def); - bts_assert(def.has_custom_def()); - bts_assert(def.custom_def().has_scratch()); + me->definition = convert_from_dynamic(serv.definition()); + + bts_assert(definition().has_custom_def()); + bts_assert(definition().custom_def().has_scratch()); // Validate the scratch part of the definition - auto &scratch = def.custom_def().scratch(); - if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) + auto &scratch = definition().custom_def().scratch(); + if (!scratch.has_child() && OrderKey(definition().attach_target_order_key()) == null_order_key()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, "Scratch order must have either a child order definition or an order to attach to"}; @@ -70,8 +74,8 @@ OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. - auto child_def = def; - *child_def.mutable_custom_def() = def.custom_def().scratch().child(); + auto child_def = definition(); + *child_def.mutable_custom_def() = definition().custom_def().scratch().child(); me->child_template = &(serv.make_child_template(child_def)); } return {}; @@ -96,9 +100,13 @@ void ScratchAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState } void ScratchAlgo::do_update(api::orderman::Update const &update) -{ +{ LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - if (update.has_custom() && !update.custom().has_scratch()) + + Update converted_update = convert_from_dynamic(update); + me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); + + if (update.has_custom() && !converted_update.custom().has_scratch()) { algo().terminate_error("Invalid update"); return; @@ -111,7 +119,7 @@ void ScratchAlgo::do_update(api::orderman::Update const &update) if (update.has_custom()) { update_scratch_threshold(); - auto &scratch_update = update.custom().scratch(); + auto &scratch_update = converted_update.custom().scratch(); *child_update.mutable_custom() = scratch_update.child(); } algo().update_child(me->child_key, child_update); @@ -135,7 +143,7 @@ void ScratchAlgo::on_book(SecurityKey, Book const &b) { case Status::WAITING_FOR_MD: { - auto other_side = securities::other_side(algo().definition().side()); + auto other_side = securities::other_side(definition().side()); auto &other_side_top = me->last_book.side(other_side)[0]; if (other_side_top.size() > 0) @@ -160,7 +168,7 @@ void ScratchAlgo::on_book(SecurityKey, Book const &b) void ScratchAlgo::try_to_scratch() { bts_assert(me->status == Status::WAITING_TO_SCRATCH); - auto my_side = algo().definition().side(); + auto my_side = definition().side(); auto other_side = securities::other_side(my_side); Quantity my_quan; @@ -215,10 +223,15 @@ Price ScratchAlgo::get_price_to_pass (Price price) const void ScratchAlgo::update_scratch_threshold() { + if (definition().custom_def().scratch().scratch_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + { + algo().terminate_error("Invalid quantity, terminating"); + return; + } me->scratch_threshold = SpreaderUtils::_quantity_threshold( - algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); + definition().custom_def().scratch().scratch_threshold(), algo().quantity()); me->chase_diff = Price(securities::tick_size( - algo().security()).value() * algo().definition().custom_def().scratch().chase_ticks()); + algo().security()).value() * definition().custom_def().scratch().chase_ticks()); } void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h index 76c220a80..11640bdca 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h @@ -25,12 +25,13 @@ private: enum class Status; struct PImpl; std::unique_ptr me; - - IAlgoServices &algo() const ; + + const api::orderman::OrderDefinition& definition() const; + IAlgoServices& algo() const; + void update_scratch_threshold(); void try_to_scratch(); Price get_price_to_pass (Price price) const; - void check_dynamic(const api::orderman::OrderDefinition&); }; }} From 938a572a54f54a571535a764f89fd2f78e7b9e8a Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 14:14:16 -0400 Subject: [PATCH 019/153] rethinking the dynamictype -> native type for sake of gui going to make a new type of DefaultOrderVM that uses reflection instead of converting dynamic -> native in the orderman --- .../orderman/orders/algo/DynamicHelpers.cpp | 25 ++++++++ src/lib/orderman/orders/algo/DynamicHelpers.h | 17 ++---- .../orders/algo/algo_orders/ProRataAlgo.cpp | 60 ++++++++++--------- .../orders/algo/algo_orders/ProRataAlgo.h | 2 +- .../orders/algo/algo_orders/ScratchAlgo.cpp | 3 +- 5 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 src/lib/orderman/orders/algo/DynamicHelpers.cpp diff --git a/src/lib/orderman/orders/algo/DynamicHelpers.cpp b/src/lib/orderman/orders/algo/DynamicHelpers.cpp new file mode 100644 index 000000000..6d347c867 --- /dev/null +++ b/src/lib/orderman/orders/algo/DynamicHelpers.cpp @@ -0,0 +1,25 @@ +#include "DynamicHelpers.h" + +namespace bts { +namespace orderman { + +template<> +bool has_custom(const api::orderman::Update& orig) +{ + return orig.has_custom(); +} + +template<> +const api::orderman::CustomOrderDefinition& custom(const api::orderman::Update& orig) +{ + return orig.custom(); +} + +template<> +api::orderman::CustomOrderDefinition& mutable_custom(api::orderman::Update& orig) +{ + return *orig.mutable_custom(); +} + + +}} \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/DynamicHelpers.h b/src/lib/orderman/orders/algo/DynamicHelpers.h index 99261d06d..53488ba33 100644 --- a/src/lib/orderman/orders/algo/DynamicHelpers.h +++ b/src/lib/orderman/orders/algo/DynamicHelpers.h @@ -11,30 +11,23 @@ bool has_custom(const T& orig) return orig.has_custom_def(); } template<> -bool has_custom(const api::orderman::Update& orig) -{ - return orig.has_custom(); -} +bool has_custom(const api::orderman::Update& orig); + template const api::orderman::CustomOrderDefinition& custom(const T& orig) { return orig.custom_def(); } template<> -const api::orderman::CustomOrderDefinition& custom(const api::orderman::Update& orig) -{ - return orig.custom(); -} +const api::orderman::CustomOrderDefinition& custom(const api::orderman::Update& orig); + template api::orderman::CustomOrderDefinition& mutable_custom(T& orig) { return *orig.mutable_custom_def(); } template<> -api::orderman::CustomOrderDefinition& mutable_custom(api::orderman::Update& orig) -{ - return *orig.mutable_custom(); -} +api::orderman::CustomOrderDefinition& mutable_custom(api::orderman::Update& orig); }} \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index b177fd227..f9a30826a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -3,6 +3,8 @@ #include "lib/domain_model/Security.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/algo/DynamicHelpers.h" + #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" #include "lib/utils/PBUtils.h" @@ -34,6 +36,8 @@ struct ProRataAlgo::PImpl { } IAlgoServices *algo; + OrderDefinition definition; + OrderKey child_key = null_order_key(); Price hedge_price_diff; @@ -60,16 +64,16 @@ struct ProRataAlgo::PImpl ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} ProRataAlgo::~ProRataAlgo() {} IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } +const OrderDefinition& ProRataAlgo::definition() const { return me->definition; } -void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) +template +T convert_from_dynamic(const T& orig) { - if (def.has_custom_def() && def.custom_def().has_dynamic()) - { - //copy - api::orderman::DynamicOrderDefinition dprorata; - dprorata.CopyFrom(def.custom_def().dynamic()); - auto& mutable_def = const_cast(def); //TODO Terrible hack - auto& prorata = *mutable_def.mutable_custom_def()->mutable_prorata(); + T newt = orig; + if(has_custom(orig) && custom(orig).has_dynamic()) + { + const DynamicOrderDefinition& dprorata = custom(orig).dynamic(); + auto& prorata = *mutable_custom(newt).mutable_prorata(); if(dprorata.order_params().count("hedge_ticks") > 0) prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); @@ -83,14 +87,12 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(!prorata.has_fill_limit() || prorata.fill_limit().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) prorata.mutable_fill_limit()->set_absolute(1u); - - + if(dprorata.order_params().count("stay_in_threshold") > 0) *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); if(!prorata.has_stay_in_threshold() || prorata.stay_in_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) prorata.mutable_stay_in_threshold()->set_absolute(1u); - if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); @@ -110,19 +112,20 @@ void ProRataAlgo::check_dynamic(const api::orderman::OrderDefinition& def) if(dprorata.order_params().count("overfill_hedge") > 0) *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); else - prorata.mutable_overfill_hedge()->mutable_limit(); - } + prorata.mutable_overfill_hedge()->mutable_limit(); + } + return newt; } OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { const api::orderman::OrderDefinition& def = serv.definition(); - check_dynamic(def); - bts_assert(def.has_custom_def()); - bts_assert(def.custom_def().has_prorata()); + me->definition = convert_from_dynamic(serv.definition()); + bts_assert(definition().has_custom_def()); + bts_assert(definition().custom_def().has_prorata()); // Validate the prorata part of the definition - auto &prorata = def.custom_def().prorata(); + auto &prorata = definition().custom_def().prorata(); if (!prorata.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, @@ -135,8 +138,8 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. - auto child_def = def; - *child_def.mutable_custom_def() = def.custom_def().prorata().child(); + auto child_def = definition(); + *child_def.mutable_custom_def() = definition().custom_def().prorata().child(); me->child_template = &(serv.make_child_template(child_def)); } @@ -149,12 +152,12 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) void ProRataAlgo::do_launch(IAlgoServices &algo) { me->algo = &algo; - set_parameters(algo, algo.definition().custom_def().prorata()); + set_parameters(algo, definition().custom_def().prorata()); LOG(OrderMan, Diag, "Starting ProRataAlgo {}, {} {}@{}. Will cover {} ticks", algo.order_key().value(), - securities::side_name(algo.definition().side()), algo.quantity().value(), + securities::side_name(definition().side()), algo.quantity().value(), algo.price().value(), - algo.definition().custom_def().prorata().hedge_ticks()); + definition().custom_def().prorata().hedge_ticks()); LOG(OrderMan, Diag, "ProRataAlgo {} launching child", algo.order_key().value()); me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); @@ -164,14 +167,13 @@ void ProRataAlgo::do_launch(IAlgoServices &algo) void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) { me->algo = &algo; - set_parameters(algo, algo.definition().custom_def().prorata()); + set_parameters(algo, definition().custom_def().prorata()); me->child_key = k; me->hedged_fills = st.total_fills; algo.subscribe_book(SecurityKey(algo.security().id())); on_child_change(k, st); } - void ProRataAlgo::on_book(SecurityKey, const Book& b) { me->last_book = b; @@ -351,7 +353,9 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) LOG(OrderMan, Diag, "ProRataAlgo order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - if (update.has_custom() && !update.custom().has_prorata()) + Update converted_update = convert_from_dynamic(update); + me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); + if (update.has_custom() && !converted_update.custom().has_prorata()) { algo().terminate_error("Invalid update"); return; @@ -364,7 +368,7 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) // process custom part of the udpate if any if (update.has_custom()) { - auto &prorata_update = update.custom().prorata(); + auto &prorata_update = converted_update.custom().prorata(); set_parameters(algo(), prorata_update); if(prorata_update.has_child()) @@ -388,9 +392,9 @@ void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, const api::orderman::CustomOrderDefinition& hedge_cdef, bool overfill) { api::orderman::OrderDefinition hedge_def; - hedge_def.CopyFrom(algo.definition()); + hedge_def.CopyFrom(definition()); - hedge_def.set_side(securities::other_side(algo.definition().side())); + hedge_def.set_side(securities::other_side(definition().side())); hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); hedge_def.clear_attach_target_order_key(); *hedge_def.mutable_custom_def() = hedge_cdef; diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index 07ffc294c..05ae354b3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -35,7 +35,7 @@ private: void check_overfill(); void check_terminate(); const char *status_name(Status st); - void check_dynamic(const api::orderman::OrderDefinition&); + const api::orderman::OrderDefinition& definition() const; }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 49a4e61b3..745cc72cb 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -104,8 +104,7 @@ void ScratchAlgo::do_update(api::orderman::Update const &update) LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); Update converted_update = convert_from_dynamic(update); - me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); - + me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); if (update.has_custom() && !converted_update.custom().has_scratch()) { algo().terminate_error("Invalid update"); From 1567ca475d62e529b60c7c731ca9b15960ef8eaf Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 14:29:01 -0400 Subject: [PATCH 020/153] now moving to gui --- .../orders/algo/algo_orders/ProRataAlgo.cpp | 136 +++++++++--------- .../orders/algo/algo_orders/ProRataAlgo.h | 1 - .../orders/algo/algo_orders/ScratchAlgo.cpp | 71 +++++---- .../orders/algo/algo_orders/ScratchAlgo.h | 4 +- 4 files changed, 106 insertions(+), 106 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index f9a30826a..96b7aa52e 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -36,8 +36,6 @@ struct ProRataAlgo::PImpl { } IAlgoServices *algo; - OrderDefinition definition; - OrderKey child_key = null_order_key(); Price hedge_price_diff; @@ -64,68 +62,66 @@ struct ProRataAlgo::PImpl ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} ProRataAlgo::~ProRataAlgo() {} IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } -const OrderDefinition& ProRataAlgo::definition() const { return me->definition; } -template -T convert_from_dynamic(const T& orig) -{ - T newt = orig; - if(has_custom(orig) && custom(orig).has_dynamic()) - { - const DynamicOrderDefinition& dprorata = custom(orig).dynamic(); - auto& prorata = *mutable_custom(newt).mutable_prorata(); +// template +// T convert_from_dynamic(const T& orig) +// { +// T newt = orig; +// if(has_custom(orig) && custom(orig).has_dynamic()) +// { +// const DynamicOrderDefinition& dprorata = custom(orig).dynamic(); +// auto& prorata = *mutable_custom(newt).mutable_prorata(); - if(dprorata.order_params().count("hedge_ticks") > 0) - prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); - prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); +// if(dprorata.order_params().count("hedge_ticks") > 0) +// prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); +// prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); - if(dprorata.order_params().count("chase_ticks") > 0) - prorata.set_chase_ticks(dprorata.order_params().at("chase_ticks").as_uint32()); +// if(dprorata.order_params().count("chase_ticks") > 0) +// prorata.set_chase_ticks(dprorata.order_params().at("chase_ticks").as_uint32()); - if(dprorata.order_params().count("fill_limit") > 0) - *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); - if(!prorata.has_fill_limit() || - prorata.fill_limit().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) - prorata.mutable_fill_limit()->set_absolute(1u); +// if(dprorata.order_params().count("fill_limit") > 0) +// *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); +// if(!prorata.has_fill_limit() || +// prorata.fill_limit().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) +// prorata.mutable_fill_limit()->set_absolute(1u); - if(dprorata.order_params().count("stay_in_threshold") > 0) - *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); - if(!prorata.has_stay_in_threshold() || - prorata.stay_in_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) - prorata.mutable_stay_in_threshold()->set_absolute(1u); +// if(dprorata.order_params().count("stay_in_threshold") > 0) +// *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); +// if(!prorata.has_stay_in_threshold() || +// prorata.stay_in_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) +// prorata.mutable_stay_in_threshold()->set_absolute(1u); - if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) - prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); - if(dprorata.order_params().count("enable_stay_in") > 0) - prorata.set_enable_stay_in(dprorata.order_params().at("enable_stay_in").as_bool()); +// if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) +// prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); +// if(dprorata.order_params().count("enable_stay_in") > 0) +// prorata.set_enable_stay_in(dprorata.order_params().at("enable_stay_in").as_bool()); - if(dprorata.order_params().count("child") > 0) - *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); - else - prorata.mutable_child()->mutable_limit(); +// if(dprorata.order_params().count("child") > 0) +// *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); +// else +// prorata.mutable_child()->mutable_limit(); - if(dprorata.order_params().count("hedge") > 0) - *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); - else - prorata.mutable_hedge()->mutable_limit(); +// if(dprorata.order_params().count("hedge") > 0) +// *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); +// else +// prorata.mutable_hedge()->mutable_limit(); - if(dprorata.order_params().count("overfill_hedge") > 0) - *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); - else - prorata.mutable_overfill_hedge()->mutable_limit(); - } - return newt; -} +// if(dprorata.order_params().count("overfill_hedge") > 0) +// *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); +// else +// prorata.mutable_overfill_hedge()->mutable_limit(); +// } +// return newt; +// } OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { const api::orderman::OrderDefinition& def = serv.definition(); - me->definition = convert_from_dynamic(serv.definition()); - bts_assert(definition().has_custom_def()); - bts_assert(definition().custom_def().has_prorata()); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_prorata()); // Validate the prorata part of the definition - auto &prorata = definition().custom_def().prorata(); + auto &prorata = def.custom_def().prorata(); if (!prorata.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, @@ -138,8 +134,8 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. - auto child_def = definition(); - *child_def.mutable_custom_def() = definition().custom_def().prorata().child(); + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().prorata().child(); me->child_template = &(serv.make_child_template(child_def)); } @@ -152,12 +148,12 @@ OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) void ProRataAlgo::do_launch(IAlgoServices &algo) { me->algo = &algo; - set_parameters(algo, definition().custom_def().prorata()); + set_parameters(algo, algo.definition().custom_def().prorata()); LOG(OrderMan, Diag, "Starting ProRataAlgo {}, {} {}@{}. Will cover {} ticks", algo.order_key().value(), - securities::side_name(definition().side()), algo.quantity().value(), + securities::side_name(algo.definition().side()), algo.quantity().value(), algo.price().value(), - definition().custom_def().prorata().hedge_ticks()); + algo.definition().custom_def().prorata().hedge_ticks()); LOG(OrderMan, Diag, "ProRataAlgo {} launching child", algo.order_key().value()); me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); @@ -167,7 +163,7 @@ void ProRataAlgo::do_launch(IAlgoServices &algo) void ProRataAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) { me->algo = &algo; - set_parameters(algo, definition().custom_def().prorata()); + set_parameters(algo, algo.definition().custom_def().prorata()); me->child_key = k; me->hedged_fills = st.total_fills; algo.subscribe_book(SecurityKey(algo.security().id())); @@ -353,9 +349,7 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) LOG(OrderMan, Diag, "ProRataAlgo order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - Update converted_update = convert_from_dynamic(update); - me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); - if (update.has_custom() && !converted_update.custom().has_prorata()) + if (update.has_custom() && !update.custom().has_prorata()) { algo().terminate_error("Invalid update"); return; @@ -368,7 +362,7 @@ void ProRataAlgo::do_update(const api::orderman::Update& update) // process custom part of the udpate if any if (update.has_custom()) { - auto &prorata_update = converted_update.custom().prorata(); + auto &prorata_update = update.custom().prorata(); set_parameters(algo(), prorata_update); if(prorata_update.has_child()) @@ -392,9 +386,9 @@ void orderman::ProRataAlgo::init_hedge_template(IBeforeLaunchAlgoServices &algo, const api::orderman::CustomOrderDefinition& hedge_cdef, bool overfill) { api::orderman::OrderDefinition hedge_def; - hedge_def.CopyFrom(definition()); + hedge_def.CopyFrom(algo.definition()); - hedge_def.set_side(securities::other_side(definition().side())); + hedge_def.set_side(securities::other_side(algo.definition().side())); hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); hedge_def.clear_attach_target_order_key(); *hedge_def.mutable_custom_def() = hedge_cdef; @@ -408,10 +402,22 @@ void ProRataAlgo::set_parameters(IBeforeLaunchAlgoServices &serv, const api::ord { me->hedge_price_diff = Price(securities::tick_size(serv.security()).value() * prorata.hedge_ticks()); me->chase_diff = Price(securities::tick_size(serv.security()).value() * prorata.chase_ticks()); - if(prorata.has_fill_limit()) - me->fill_limit = SpreaderUtils::_quantity_threshold(prorata.fill_limit(), algo().quantity()); - if(prorata.has_stay_in_threshold()) - me->stay_in_threshold = SpreaderUtils::_quantity_threshold(prorata.stay_in_threshold(), algo().quantity()); + + if (!prorata.has_fill_limit() || prorata.fill_limit().quantity_case() == + bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + { + algo().terminate_error("Invalid fill limit quantity, terminating"); + return; + } + me->fill_limit = SpreaderUtils::_quantity_threshold(prorata.fill_limit(), algo().quantity()); + + if (!prorata.has_stay_in_threshold() || prorata.stay_in_threshold().quantity_case() == + bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + { + algo().terminate_error("Invalid stay_in_threshold quantity, terminating"); + return; + } + me->stay_in_threshold = SpreaderUtils::_quantity_threshold(prorata.stay_in_threshold(), algo().quantity()); me->enable_scratch_on_overfill = prorata.enable_scratch_on_overfill(); me->enable_stay_in = prorata.enable_stay_in(); } diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h index 05ae354b3..502cbf520 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.h @@ -35,7 +35,6 @@ private: void check_overfill(); void check_terminate(); const char *status_name(Status st); - const api::orderman::OrderDefinition& definition() const; }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 745cc72cb..2bb3b56ad 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -32,40 +32,38 @@ struct ScratchAlgo::PImpl ScratchAlgo::ScratchAlgo() : me(new PImpl()) {} ScratchAlgo::~ScratchAlgo() {} IAlgoServices &ScratchAlgo::algo() const { return *me->algo; } -const OrderDefinition& ScratchAlgo::definition() const { return me->definition; } -template -T convert_from_dynamic(const T& orig) -{ - T newt = orig; - if ( has_custom(orig) && custom(orig).has_dynamic()) - { - const DynamicOrderDefinition& dscratch = custom(orig).dynamic(); - auto& scratch = *mutable_custom(newt).mutable_scratch(); +// template +// T convert_from_dynamic(const T& orig) +// { +// T newt = orig; +// if ( has_custom(orig) && custom(orig).has_dynamic()) +// { +// const DynamicOrderDefinition& dscratch = custom(orig).dynamic(); +// auto& scratch = *mutable_custom(newt).mutable_scratch(); - if(dscratch.order_params().count("scratch_threshold") > 0) - *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); +// if(dscratch.order_params().count("scratch_threshold") > 0) +// *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); - if(dscratch.order_params().count("chase_ticks") > 0) - scratch.set_chase_ticks(dscratch.order_params().at("chase_ticks").as_uint32()); +// if(dscratch.order_params().count("chase_ticks") > 0) +// scratch.set_chase_ticks(dscratch.order_params().at("chase_ticks").as_uint32()); - if(dscratch.order_params().count("child") > 0) - *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); - else - scratch.mutable_child()->mutable_limit(); - } - return newt; -} +// if(dscratch.order_params().count("child") > 0) +// *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); +// else +// scratch.mutable_child()->mutable_limit(); +// } +// return newt; +// } OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { - me->definition = convert_from_dynamic(serv.definition()); - - bts_assert(definition().has_custom_def()); - bts_assert(definition().custom_def().has_scratch()); + const auto& def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_scratch()); // Validate the scratch part of the definition - auto &scratch = definition().custom_def().scratch(); - if (!scratch.has_child() && OrderKey(definition().attach_target_order_key()) == null_order_key()) + auto &scratch = def.custom_def().scratch(); + if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, "Scratch order must have either a child order definition or an order to attach to"}; @@ -74,8 +72,8 @@ OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. - auto child_def = definition(); - *child_def.mutable_custom_def() = definition().custom_def().scratch().child(); + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().scratch().child(); me->child_template = &(serv.make_child_template(child_def)); } return {}; @@ -103,9 +101,7 @@ void ScratchAlgo::do_update(api::orderman::Update const &update) { LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - Update converted_update = convert_from_dynamic(update); - me->definition.mutable_custom_def()->CopyFrom(custom(converted_update)); - if (update.has_custom() && !converted_update.custom().has_scratch()) + if (update.has_custom() && !update.custom().has_scratch()) { algo().terminate_error("Invalid update"); return; @@ -118,7 +114,7 @@ void ScratchAlgo::do_update(api::orderman::Update const &update) if (update.has_custom()) { update_scratch_threshold(); - auto &scratch_update = converted_update.custom().scratch(); + auto &scratch_update = update.custom().scratch(); *child_update.mutable_custom() = scratch_update.child(); } algo().update_child(me->child_key, child_update); @@ -142,7 +138,7 @@ void ScratchAlgo::on_book(SecurityKey, Book const &b) { case Status::WAITING_FOR_MD: { - auto other_side = securities::other_side(definition().side()); + auto other_side = securities::other_side(algo().definition().side()); auto &other_side_top = me->last_book.side(other_side)[0]; if (other_side_top.size() > 0) @@ -167,7 +163,7 @@ void ScratchAlgo::on_book(SecurityKey, Book const &b) void ScratchAlgo::try_to_scratch() { bts_assert(me->status == Status::WAITING_TO_SCRATCH); - auto my_side = definition().side(); + auto my_side = algo().definition().side(); auto other_side = securities::other_side(my_side); Quantity my_quan; @@ -222,15 +218,16 @@ Price ScratchAlgo::get_price_to_pass (Price price) const void ScratchAlgo::update_scratch_threshold() { - if (definition().custom_def().scratch().scratch_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + if (algo().definition().custom_def().scratch().scratch_threshold().quantity_case() == + bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) { algo().terminate_error("Invalid quantity, terminating"); return; } me->scratch_threshold = SpreaderUtils::_quantity_threshold( - definition().custom_def().scratch().scratch_threshold(), algo().quantity()); + algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); me->chase_diff = Price(securities::tick_size( - algo().security()).value() * definition().custom_def().scratch().chase_ticks()); + algo().security()).value() * algo().definition().custom_def().scratch().chase_ticks()); } void ScratchAlgo::on_child_change(OrderKey, ChildState const &st) diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h index 11640bdca..ca6fada14 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.h @@ -24,9 +24,7 @@ public: private: enum class Status; struct PImpl; - std::unique_ptr me; - - const api::orderman::OrderDefinition& definition() const; + std::unique_ptr me; IAlgoServices& algo() const; void update_scratch_threshold(); From b1e19294e6f4de47401076fe351a2d40153818cc Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 17:50:35 -0400 Subject: [PATCH 021/153] making some headway on generic order ui --- src/client/spark/Orders/OrderDataHelper.cs | 12 ++ .../Orders/OrderTypeVMs/GenericOrderHelper.cs | 52 +++++ .../OrderTypeVMs/GenericOrderParamVM.cs | 178 ++++++++++++++++++ .../GenericOrderTemplateSelector.cs | 63 +++++++ .../Orders/OrderTypeVMs/GenericOrderVM.cs | 152 +++++++++++++++ .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 3 +- .../Order/AlgoContentTemplateSelector.xaml | 170 +++++++++++++++++ .../Order/AlgoContentTemplateSelector.xaml.cs | 2 + src/lib/api/services/OrderService.proto | 8 + 9 files changed, 639 insertions(+), 1 deletion(-) create mode 100644 src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs create mode 100644 src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs create mode 100644 src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs create mode 100644 src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index e31fface7..c3c9a8c96 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -213,6 +213,8 @@ namespace mbts.spark.utilities { public OffsetOrderDefinition offset_order() => OrderDataHelper.Defaults.OffsetOrderDefinition(); public TriggerOrderDefinition trigger_order() => OrderDataHelper.Defaults.TriggerOrderDefinition(); public DynamicOrderDefinition dynamic_order() => OrderDataHelper.Defaults.DynamicOrderDefinition(); + public GenericOrderDefinition generic_order(CustomOrderDefinition.CustomOneofCase order_type) => + OrderDataHelper.Defaults.GenericOrderDefinition(order_type); public SpreaderOrderDefinition spreader_order() => OrderDataHelper.Defaults.SpreaderOrderDefinition(); public SpreaderOrderDefinition spreader_order(SecurityImmutable security, ExecutionStrategyImmutable strategy) => new SpreaderOrderDefinition { @@ -245,6 +247,9 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = spreader_order(); break; case CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = dynamic_order(); break; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: rv.QueueHolder = queue_holder_order(); break; + default: + rv.Generic = generic_order(order_type); + break; } return rv; } @@ -270,6 +275,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; + case CustomOrderDefinition.CustomOneofCase.Generic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; case CustomOrderDefinition.CustomOneofCase.None: goto default; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: return new OrderMetadata { KeyBinding = "Q", Color = BTSColors.purple.rgb_color() }; default: return _default_order_metadata; @@ -431,6 +437,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; + case CustomOrderDefinition.CustomOneofCase.Generic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; case CustomOrderDefinition.CustomOneofCase.None: default: return _default_order_metadata; } @@ -993,6 +1000,9 @@ namespace mbts.spark.utilities { case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Trigger: rv.Trigger = TriggerOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = SpreaderOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = DynamicOrderDefinition(); break; + default: + rv.Generic = GenericOrderDefinition(order_type); + break; } return rv; } @@ -1016,7 +1026,9 @@ namespace mbts.spark.utilities { public static OffsetOrderDefinition OffsetOrderDefinition() => new OffsetOrderDefinition { Child = CustomOrderDefinition(), }; public static TriggerOrderDefinition TriggerOrderDefinition() => new TriggerOrderDefinition { Child = CustomOrderDefinition(), TriggerCondition = new TriggerCondition(), TriggerAction = new TriggerAction(), }; public static DynamicOrderDefinition DynamicOrderDefinition() => new DynamicOrderDefinition(); + public static GenericOrderDefinition GenericOrderDefinition(CustomOrderDefinition.CustomOneofCase order_type) => new GenericOrderDefinition { knownType = order_type.ToString() } ; public static SpreaderOrderDefinition SpreaderOrderDefinition() => new SpreaderOrderDefinition { ExecutionStrategy = new ExecutionStrategy() }; } } + } diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs new file mode 100644 index 000000000..e6a261364 --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs @@ -0,0 +1,52 @@ + +using bts.api; +using bts.api.orderman; +using bts.utils; +using Google.Protobuf; +using Google.Protobuf.Reflection; +using mbts.ui.utility; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.Linq; +using System.Reactive.Linq; + +namespace mbts.spark +{ + [Export] + class GenericOrderHelper + { + [ImportingConstructor] + private GenericOrderHelper(OrderService order_service) + { + } + + internal static string get_enumerated_value(IMessage message, FieldDescriptor fd) + { + var ev = fd.Accessor.GetValue(message); + return ev.ToString(); + } + internal static void set_enumerated_value(IMessage message, FieldDescriptor fd, string value) + { + if (message == null) + { + return; + } + var ev = fd.EnumType.Values.First(x => x.Name == value)?.Number; + if(ev.HasValue) + fd.Accessor.SetValue(message, ev.Value); + } + + internal static bts.spark.QuantityMeasureImmutable? get_quantity_value(IMessage message, FieldDescriptor fd) + { + return (bts.spark.QuantityMeasureImmutable)fd.Accessor.GetValue(message); + } + internal static void set_quantity_value(IMessage message, FieldDescriptor fd, bts.spark.QuantityMeasureImmutable? value) + { + if (message == null) + return; + fd.Accessor.SetValue(message, value?.CloneToMutable()); + } + } +} diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs new file mode 100644 index 000000000..606c83641 --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs @@ -0,0 +1,178 @@ +using bts.api.orderman; +using Google.Protobuf.Reflection; +using mbts.ui.shell; +using bts.spark; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Reactive.Linq; +using Google.Protobuf.Reflection; + +namespace mbts.spark +{ + interface IGenericOrderParamVM + { + FieldDescriptor descriptor { get; } + Google.Protobuf.IMessage message { get; set; } + } + + interface IGenericOrderParamVM : IGenericOrderParamVM + { + T value { get; set; } + IObservable subscribe_value(); + } + + abstract class GenericOrderParamVM : DisposableViewModelBase, IGenericOrderParamVM + { + Google.Protobuf.IMessage _message; + protected GenericOrderParamVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) + { + _message = message; + this.descriptor = descriptor; + } + + public FieldDescriptor descriptor { get; set; } + public Google.Protobuf.IMessage message + { + get => _message; + set + { + _message = value; + on_param_changed(); + } + } + + protected abstract void on_param_changed(); + } + + class GenericOrderParamVM : GenericOrderParamVM, IGenericOrderParamVM + { + readonly Func _convert; + readonly Func _convert_back; + TView _value; + + public GenericOrderParamVM(Google.Protobuf.IMessage message, FieldDescriptor field_descriptor, Func convert, Func convert_back) : + base(message, field_descriptor) + { + _convert = convert; + _convert_back = convert_back; + } + + public TView value + { + get => _value; + set + { + this.descriptor.Accessor.SetValue(this.message, _convert_back(_value = value)); + RaisePropertyChanged(); + } + } + + public IObservable subscribe_value() => + Observable.Create(o => + { + PropertyChanged += on_prop_changed; + return Disposable.Create(() => PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) => + o.OnNext(value); + }); + + protected override void on_param_changed() + { + _value = _convert(ConvertUtils.try_convert(this.descriptor.Accessor.GetValue(this.message))); + RaisePropertyChanged(nameof(value)); + } + } + + class GenericOrderEnumeratedVM : GenericOrderParamVM, IGenericOrderParamVM + { + public GenericOrderEnumeratedVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) + : base(message, descriptor) + { + elements = new ObservableCollection(_get_elements(descriptor)); + } + + public ObservableCollection elements { get; } + + public string selected_element + { + get => GenericOrderHelper.get_enumerated_value(message, descriptor); + set + { + var orig = selected_element; + GenericOrderHelper.set_enumerated_value(message, descriptor, value); + if (selected_element != orig) + RaisePropertyChanged(); + } + } + string IGenericOrderParamVM.value { get => selected_element; set => selected_element = value; } + + IObservable IGenericOrderParamVM.subscribe_value() => + Observable.Create(o => + { + PropertyChanged += on_prop_changed; + return Disposable.Create(() => PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) => + o.OnNext(selected_element); + }); + + protected override void on_param_changed() + { + _sync_elements(); + RaisePropertyChanged(nameof(selected_element)); + } + + static IEnumerable _get_elements(FieldDescriptor descriptor) + { + if(descriptor.FieldType == Google.Protobuf.Reflection.FieldType.Enum) + { + return descriptor.EnumType.Values.Select(x => x.Name); + } + else + return Enumerable.Empty(); + } + + void _sync_elements() + { + elements.sync_with(_get_elements(descriptor)); + } + } + + + class GenericOrderQuantityVM : GenericOrderParamVM, IGenericOrderParamVM + { + public GenericOrderQuantityVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) + : base(message, descriptor) { } + + QuantityMeasureImmutable? IGenericOrderParamVM.value { get => quantity; set => quantity= value; } + public QuantityMeasureImmutable? quantity + { + get => GenericOrderHelper.get_quantity_value(message, descriptor); + set + { + var orig = quantity; + GenericOrderHelper.set_quantity_value(message, descriptor, value); + if (quantity != orig) + RaisePropertyChanged(); + } + } + + public IObservable subscribe_value() + { + return Observable.Create(o => + { + PropertyChanged += on_prop_changed; + return Disposable.Create(() => PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) => + o.OnNext(quantity); + }); + } + + protected override void on_param_changed() + { + RaisePropertyChanged(nameof(quantity)); + } + } +} diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs new file mode 100644 index 000000000..8d29d0271 --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs @@ -0,0 +1,63 @@ + +using bts.api.orderman; +using mbts.spark; +using mbts.ui.converters; +using mbts.ui.utilities; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; + +namespace mbts.ui.spark +{ + class GenericOrderTemplateSelector : DataTemplateSelector + { + public DataTemplate BoolTemplate { get; set; } + public DataTemplate DoubleTemplate { get; set; } + public DataTemplate IntTemplate { get; set; } + public DataTemplate UIntTemplate { get; set; } + public DataTemplate StringTemplate { get; set; } + public DataTemplate OrderDefTemplate { get; set; } + public DataTemplate QuantityMeasureTemplate { get; set; } + + public DataTemplate EnumTemplate { get; set; } + + public override DataTemplate SelectTemplate(object item, DependencyObject container) + { + var vm = item as GenericOrderParamVM; + if (vm != null) + { + switch (vm.descriptor.FieldType) + { + case Google.Protobuf.Reflection.FieldType.Bool: + return BoolTemplate; + case Google.Protobuf.Reflection.FieldType.Float: + case Google.Protobuf.Reflection.FieldType.Double: + return DoubleTemplate; + case Google.Protobuf.Reflection.FieldType.Int32: + case Google.Protobuf.Reflection.FieldType.Int64: + return IntTemplate; + case Google.Protobuf.Reflection.FieldType.UInt32: + case Google.Protobuf.Reflection.FieldType.UInt64: + return UIntTemplate; + case Google.Protobuf.Reflection.FieldType.String: + return StringTemplate; + case Google.Protobuf.Reflection.FieldType.Enum: + return EnumTemplate; + case Google.Protobuf.Reflection.FieldType.Message: + { + if(vm.descriptor.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName) + return QuantityMeasureTemplate; + else if (vm.descriptor.MessageType.FullName == typeof(bts.api.orderman.CustomOrderDefinition).FullName) + return OrderDefTemplate; + } + break; + default: return null; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs new file mode 100644 index 000000000..6c5bbf74f --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs @@ -0,0 +1,152 @@ + +using bts; +using bts.api.orderman; +using bts.collections; +using mbts.collections; +using mbts.spark.utilities; +using mbts.ui.utility; +using mbts.utils; +using bts.spark; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Reactive.Linq; + +using AlgoType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; + +namespace mbts.spark +{ + static partial class OrderTypeVMFactory + { + class GenericOrderVM : OrderTypeVMBase + { + readonly ObservableKeyedCollection _parameters = new ObservableKeyedCollection(vm => vm.descriptor.ParamName); + readonly Property _algo_type_string; + readonly AlgoType _algo_type; + + public GenericOrderVM(AlgoKey key, AlgoType algo_type, string display_name, AlgoEditingPermissions editing_permissions) + : base(key, display_name, editing_permissions) + { + _algo_type = algo_type; + _algo_type_string = add_property(nameof(this.algo_type), GenericOrderDefinition.KnownTypeFieldNumber, + algo_type.ToString(), permissions: AlgoParamPermissions.None); + _parameters = new ObservableKeyedCollection(vm => vm.descriptor.Name); + _resources.AddRange( + _algo_type_string.Subscribe(_ => + { + _sync_generic_params(); + sync_selectors(); + }), + ObservableAutoWrapper.connect_item_hooks(_parameters, p => + { + p.PropertyChanged += on_param_prop_changed; + return Disposable.Create(() => + { + p.PropertyChanged -= on_param_prop_changed; + remove_property(p.descriptor.Name); + p.Dispose(); + }); + }, true)); + void on_param_prop_changed(object sender, PropertyChangedEventArgs e) => raise_definition_changed(); + } + + public string algo_type => _algo_type_string.Value; + public override string abbreviation => + model == null ? display_name : model.factory.abbreviate_algo(new OneOf(_algo_type)); + public override AlgoDisplayGroup group => AlgoDisplayGroup.API; + public ObservableCollection parameters => _parameters; + + protected CustomOrderDefinition construct() + { + string propname = _algo_type.ToString(); + var pi = typeof(CustomOrderDefinition).GetProperty(propname, + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); + ExceptionAssert.Argument.NotNull(pi, "construction of " + propname); + var rt = pi.PropertyType; + var rtt = Activator.CreateInstance(rt); + + System.Reflection.typ + } + + protected override void on_model_internal() + { + _sync_generic_params(); + } + + + void _sync_generic_params() + { + if (model != null) + { + var desc = model.dynamic_helper.get_descriptor(dyn_type); + Google.Protobuf.Reflection.MessageDescriptor desc = + _parameters.SyncWithDictionary(desc.ParamDesc.Where(_is_value_param_desc).ToDictionary(p => p.ParamName), _mk_param_vm); + } + } + + + protected override IEnumerable get_selector_datum() + { + var data = OrderDataHelper.get_child_algo_data( + new CustomOrderDefinition { Dynamic = model?.dynamic_helper?.create_def(dyn_type) ?? new DynamicOrderDefinition() + }); + return data; + } + + static bool _is_value_param_desc(DynamicOrderParamDescriptor desc) + { + return desc.ParamType.DescriptorCase == DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated || + (desc.ParamType.DescriptorCase == DynamicOrderTypeDescriptor.DescriptorOneofCase.Primitive && desc.ParamType.Primitive != DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef); + } + + + DynamicOrderParamVM _mk_param_vm(DynamicOrderParamDescriptor descriptor) { + ExceptionAssert.Argument.NotNull(descriptor, nameof(descriptor)); + switch (descriptor.ParamType.DescriptorCase) { + case DynamicOrderTypeDescriptor.DescriptorOneofCase.Primitive: + switch (descriptor.ParamType.Primitive) { + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveBool: return mk(DynamicOrderParam.AsBoolFieldNumber, v => v, v => v.GetValueOrDefault()); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveDouble: return mk(DynamicOrderParam.AsDoubleFieldNumber, v => v, v => v, double.NaN); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault()); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return mk(DynamicOrderParam.AsStringFieldNumber, v => v, v => v); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: + { + var qm = new DynamicOrderQuantityVM(descriptor); + return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasure { Ratio = 1.0, Absolute = 1 }); + } + default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.Primitive, nameof(descriptor)); + } + case DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated: + var pvm = new DynamicOrderEnumeratedVM(descriptor); + attached(pvm, DynamicOrderHelper.get_enumerated_value, DynamicOrderHelper.set_enumerated_value); + return pvm; + default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.DescriptorCase, nameof(descriptor)); + } + + DynamicOrderParamVM mk(int field_number, Func convert, Func convert_back, TView default_value = default) { + var fdesc = DynamicOrderParam.Descriptor.FindFieldByNumber(field_number); + var pvm = new DynamicOrderParamVM(descriptor, fdesc, convert, convert_back); + return attached>(pvm, get_from_param, assign_to_param, default_value); + + TView get_from_param(DynamicOrderParam param) => convert(ConvertUtils.try_convert(fdesc.Accessor.GetValue(param))); + void assign_to_param(DynamicOrderParam param, TView value) => fdesc.Accessor.SetValue(param, convert_back(value)); + } + + TParam attached(TParam pvm, Func selector, Action assign_to_param, T default_value = default) where TParam : DynamicOrderParamVM, IDynamicOrderParamVM { + add_property( + pvm.descriptor.ParamName, + mdef => mdef.OrderParams.TryGetValue(pvm.descriptor.ParamName, out DynamicOrderParam p) ? selector(p) : default_value, + (mdef, v) => assign_to_param(mdef.OrderParams.GetOrAdd(pvm.descriptor.ParamName, pn => new DynamicOrderParam()), v), + () => pvm.value, + v => pvm.value = v, + pvm.subscribe_value(), + default_value); + return pvm; + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index 693dd9e25..d760f0737 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -56,6 +56,7 @@ namespace mbts.spark { public static IAlgoVM iceberg(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new IcebergOrderVM(key, display_name, editing_permissions); public static IAlgoVM spreader(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new SpreaderOrderVM(key, display_name, editing_permissions); public static IAlgoVM dynamic(AlgoKey key, string display_name, string dynamic_type, AlgoEditingPermissions editing_permissions) => new DynamicOrderVM(key, dynamic_type, display_name, editing_permissions); + public static IAlgoVM generic(AlgoKey key, string display_name, AlgoType algo_type, AlgoEditingPermissions editing_permissions) => new GenericOrderVM(key, algo_type, display_name, editing_permissions); public static IAlgoVM create(AlgoData def_data, AlgoEditingPermissions editing_permissions = default_editing_permissions, bool is_expanded = false) { if (def_data.value.CustomCase == AlgoType.Composite) { @@ -87,7 +88,7 @@ namespace mbts.spark { case AlgoType.Spreader: vm = spreader(key, name, editing_permissions); break; case AlgoType.Dynamic: vm = dynamic(key, name, name, editing_permissions); break; case AlgoType.None: vm = new DefaultAlgoVM(); break; - default: throw new NotImplementedException(); + default: vm = generic(key, name, algo_type, editing_permissions); break; } vm.is_expanded = is_expanded && vm.can_expand; return vm; diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index dc159595c..f5dc590c9 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1138,5 +1138,175 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs index 9e1110cd8..1c8cfaf22 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs @@ -44,6 +44,7 @@ namespace mbts.ui.spark { public DataTemplate SpreaderTemplate { get; set; } public DataTemplate DynamicOrderTemplate { get; set; } public DataTemplate DefaultOrderTemplate { get; set; } + public DataTemplate GenericOrderTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item is IDynamicAlgoVMImpl) { @@ -65,6 +66,7 @@ namespace mbts.ui.spark { case CustomOrderDefinition.CustomOneofCase.Iceberg: return IcebergOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Spreader: return SpreaderTemplate; case CustomOrderDefinition.CustomOneofCase.Dynamic: return DynamicOrderTemplate; + default: return GenericOrderTemplate; } } return null; diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 35d385b16..6f1d48fe7 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -103,6 +103,7 @@ message CustomOrderDefinition ScratchOrderDefinition scratch = 28; ProRataOrderDefinition prorata = 29; + GenericOrderDefinition generic = 900; DynamicOrderDefinition dynamic = 1000; } } @@ -220,6 +221,11 @@ message DynamicOrderDefinition { string dynamic_order_type = 1; map order_params = 2; } +//Like dynamic, this order type generates a UI, but is used a wrapper for a typed native algo above +message GenericOrderDefinition { + string known_type = 1; + CustomOrderDefinition definition = 2; +} // End custom order definitions message OrderRecord { @@ -271,6 +277,7 @@ message OrderState { ScratchOrderState scratch = 27; ProRataOrderState prorata = 28; + GenericOrderState generic = 250; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } } @@ -348,6 +355,7 @@ message PricerOrderState {} message ScratchOrderState {} message ProRataOrderState {} message DynamicOrderState {} +message GenericOrderState {} message QueueHolderState {} enum OrderStatus { From 01956e92b6b95127b6c334cbcfad40666c889ff1 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 21:25:39 -0400 Subject: [PATCH 022/153] added facility for simple guis of well known order type protos to be generated; worth it. --- src/client/spark/Orders/OrderDataHelper.cs | 28 ++- .../OrderTypeVMs/DynamicOrderParamVM.cs | 2 +- .../Orders/OrderTypeVMs/DynamicOrderVM.cs | 2 +- .../Orders/OrderTypeVMs/GenericOrderHelper.cs | 6 +- .../OrderTypeVMs/GenericOrderParamVM.cs | 39 ++-- .../GenericOrderTemplateSelector.cs | 58 +++--- .../Orders/OrderTypeVMs/GenericOrderVM.cs | 180 ++++++++++-------- .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 7 +- .../Collections/ObservableKeyedCollection.cs | 16 ++ src/lib/api/services/OrderService.proto | 8 - src/server/sparkd/cmds/Run.cpp | 58 ------ 11 files changed, 194 insertions(+), 210 deletions(-) diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index c3c9a8c96..af4fd93ec 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -213,8 +213,6 @@ namespace mbts.spark.utilities { public OffsetOrderDefinition offset_order() => OrderDataHelper.Defaults.OffsetOrderDefinition(); public TriggerOrderDefinition trigger_order() => OrderDataHelper.Defaults.TriggerOrderDefinition(); public DynamicOrderDefinition dynamic_order() => OrderDataHelper.Defaults.DynamicOrderDefinition(); - public GenericOrderDefinition generic_order(CustomOrderDefinition.CustomOneofCase order_type) => - OrderDataHelper.Defaults.GenericOrderDefinition(order_type); public SpreaderOrderDefinition spreader_order() => OrderDataHelper.Defaults.SpreaderOrderDefinition(); public SpreaderOrderDefinition spreader_order(SecurityImmutable security, ExecutionStrategyImmutable strategy) => new SpreaderOrderDefinition { @@ -248,7 +246,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = dynamic_order(); break; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: rv.QueueHolder = queue_holder_order(); break; default: - rv.Generic = generic_order(order_type); + OrderTypeVMFactory.GenericOrderFactory.setup_generic_custom_order(rv, order_type); break; } return rv; @@ -275,7 +273,6 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; - case CustomOrderDefinition.CustomOneofCase.Generic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; case CustomOrderDefinition.CustomOneofCase.None: goto default; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: return new OrderMetadata { KeyBinding = "Q", Color = BTSColors.purple.rgb_color() }; default: return _default_order_metadata; @@ -437,7 +434,6 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; - case CustomOrderDefinition.CustomOneofCase.Generic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; case CustomOrderDefinition.CustomOneofCase.None: default: return _default_order_metadata; } @@ -593,9 +589,6 @@ namespace mbts.spark.utilities { new AlgoStructureKey(defi.CustomCase))); public static readonly CustomOrderDefinition.CustomOneofCase[] - // The following standard types are not yet ready for production use: - // CustomOrderDefinition.CustomOneofCase.Tick, - // CustomOrderDefinition.CustomOneofCase.Dynamic, exchange_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Market, CustomOrderDefinition.CustomOneofCase.Limit, @@ -605,24 +598,28 @@ namespace mbts.spark.utilities { CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Scratch, + CustomOrderDefinition.CustomOneofCase.Prorata, }, - // The following spreader types are not yet ready for production use: - // CustomOrderDefinition.CustomOneofCase.Dynamic, spreader_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Spreader, CustomOrderDefinition.CustomOneofCase.Iceberg, CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, - CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Cover, + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Scratch, + CustomOrderDefinition.CustomOneofCase.Prorata, }, attachable_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Iceberg, CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Scratch, + CustomOrderDefinition.CustomOneofCase.Prorata, }; public static readonly CustomOrderDefinition.CustomOneofCase[] all_order_types = exchange_order_types.Union(spreader_order_types).ToArray(); @@ -1001,7 +998,7 @@ namespace mbts.spark.utilities { case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = SpreaderOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = DynamicOrderDefinition(); break; default: - rv.Generic = GenericOrderDefinition(order_type); + OrderTypeVMFactory.GenericOrderFactory.setup_generic_custom_order(rv, order_type); break; } return rv; @@ -1026,7 +1023,6 @@ namespace mbts.spark.utilities { public static OffsetOrderDefinition OffsetOrderDefinition() => new OffsetOrderDefinition { Child = CustomOrderDefinition(), }; public static TriggerOrderDefinition TriggerOrderDefinition() => new TriggerOrderDefinition { Child = CustomOrderDefinition(), TriggerCondition = new TriggerCondition(), TriggerAction = new TriggerAction(), }; public static DynamicOrderDefinition DynamicOrderDefinition() => new DynamicOrderDefinition(); - public static GenericOrderDefinition GenericOrderDefinition(CustomOrderDefinition.CustomOneofCase order_type) => new GenericOrderDefinition { knownType = order_type.ToString() } ; public static SpreaderOrderDefinition SpreaderOrderDefinition() => new SpreaderOrderDefinition { ExecutionStrategy = new ExecutionStrategy() }; } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs index d80ae2413..ade3260a4 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs @@ -26,7 +26,7 @@ namespace mbts.spark { protected DynamicOrderParamVM(DynamicOrderParamDescriptor descriptor) : this(descriptor, DynamicOrderHelper.init_param(descriptor)) { } - protected DynamicOrderParamVM(DynamicOrderParamDescriptor descriptor, DynamicOrderParam param) { + private DynamicOrderParamVM(DynamicOrderParamDescriptor descriptor, DynamicOrderParam param) { this.descriptor = descriptor; _param = param; } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs index 76af9879d..37726c245 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs @@ -74,7 +74,7 @@ namespace mbts.spark { switch (descriptor.ParamType.Primitive) { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveBool: return mk(DynamicOrderParam.AsBoolFieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveDouble: return mk(DynamicOrderParam.AsDoubleFieldNumber, v => v, v => v, double.NaN); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return mk(DynamicOrderParam.AsStringFieldNumber, v => v, v => v); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs index e6a261364..948b8a487 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderHelper.cs @@ -40,7 +40,11 @@ namespace mbts.spark internal static bts.spark.QuantityMeasureImmutable? get_quantity_value(IMessage message, FieldDescriptor fd) { - return (bts.spark.QuantityMeasureImmutable)fd.Accessor.GetValue(message); + var qm = (bts.spark.QuantityMeasure)fd.Accessor.GetValue(message); + if(qm != null) + return new bts.spark.QuantityMeasureImmutable(qm); + else + return null; } internal static void set_quantity_value(IMessage message, FieldDescriptor fd, bts.spark.QuantityMeasureImmutable? value) { diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs index 606c83641..a0ed8fbb8 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderParamVM.cs @@ -8,13 +8,13 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reactive.Linq; -using Google.Protobuf.Reflection; +using Google.Protobuf; namespace mbts.spark { interface IGenericOrderParamVM { - FieldDescriptor descriptor { get; } + FieldDescriptor descriptor { get; } Google.Protobuf.IMessage message { get; set; } } @@ -24,12 +24,14 @@ namespace mbts.spark IObservable subscribe_value(); } - abstract class GenericOrderParamVM : DisposableViewModelBase, IGenericOrderParamVM + abstract class GenericOrderParamVM : DisposableViewModelBase, IGenericOrderParamVM + where TMutable : class, IMessage, new() { - Google.Protobuf.IMessage _message; - protected GenericOrderParamVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) - { - _message = message; + TMutable _message; + + protected GenericOrderParamVM(FieldDescriptor descriptor) + { + _message = new TMutable(); this.descriptor = descriptor; } @@ -39,7 +41,7 @@ namespace mbts.spark get => _message; set { - _message = value; + _message = (TMutable)value; on_param_changed(); } } @@ -47,14 +49,15 @@ namespace mbts.spark protected abstract void on_param_changed(); } - class GenericOrderParamVM : GenericOrderParamVM, IGenericOrderParamVM + class GenericOrderParamVM : GenericOrderParamVM, IGenericOrderParamVM + where TMutable : class, IMessage, new() { readonly Func _convert; readonly Func _convert_back; TView _value; - public GenericOrderParamVM(Google.Protobuf.IMessage message, FieldDescriptor field_descriptor, Func convert, Func convert_back) : - base(message, field_descriptor) + public GenericOrderParamVM(FieldDescriptor field_descriptor, Func convert, Func convert_back) : + base(field_descriptor) { _convert = convert; _convert_back = convert_back; @@ -86,10 +89,11 @@ namespace mbts.spark } } - class GenericOrderEnumeratedVM : GenericOrderParamVM, IGenericOrderParamVM + class GenericOrderEnumeratedVM : GenericOrderParamVM, IGenericOrderParamVM + where TMutable : class, IMessage, new() { - public GenericOrderEnumeratedVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) - : base(message, descriptor) + public GenericOrderEnumeratedVM(FieldDescriptor descriptor) + : base(descriptor) { elements = new ObservableCollection(_get_elements(descriptor)); } @@ -141,10 +145,11 @@ namespace mbts.spark } - class GenericOrderQuantityVM : GenericOrderParamVM, IGenericOrderParamVM + class GenericOrderQuantityVM : GenericOrderParamVM, IGenericOrderParamVM + where TMutable : class, IMessage, new() { - public GenericOrderQuantityVM(Google.Protobuf.IMessage message, FieldDescriptor descriptor) - : base(message, descriptor) { } + public GenericOrderQuantityVM(FieldDescriptor descriptor) + : base(descriptor) { } QuantityMeasureImmutable? IGenericOrderParamVM.value { get => quantity; set => quantity= value; } public QuantityMeasureImmutable? quantity diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs index 8d29d0271..4eaafb297 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderTemplateSelector.cs @@ -12,7 +12,7 @@ using System.Windows.Data; namespace mbts.ui.spark { - class GenericOrderTemplateSelector : DataTemplateSelector + class GenericOrderParamTemplateSelector : DataTemplateSelector { public DataTemplate BoolTemplate { get; set; } public DataTemplate DoubleTemplate { get; set; } @@ -26,36 +26,40 @@ namespace mbts.ui.spark public override DataTemplate SelectTemplate(object item, DependencyObject container) { - var vm = item as GenericOrderParamVM; + var vm = item as IGenericOrderParamVM; if (vm != null) + return select_template(vm.descriptor); + return null; + } + + public DataTemplate select_template(Google.Protobuf.Reflection.FieldDescriptor descriptor) + { + switch (descriptor.FieldType) { - switch (vm.descriptor.FieldType) + case Google.Protobuf.Reflection.FieldType.Bool: + return BoolTemplate; + case Google.Protobuf.Reflection.FieldType.Float: + case Google.Protobuf.Reflection.FieldType.Double: + return DoubleTemplate; + case Google.Protobuf.Reflection.FieldType.Int32: + case Google.Protobuf.Reflection.FieldType.Int64: + return IntTemplate; + case Google.Protobuf.Reflection.FieldType.UInt32: + case Google.Protobuf.Reflection.FieldType.UInt64: + return UIntTemplate; + case Google.Protobuf.Reflection.FieldType.String: + return StringTemplate; + case Google.Protobuf.Reflection.FieldType.Enum: + return EnumTemplate; + case Google.Protobuf.Reflection.FieldType.Message: { - case Google.Protobuf.Reflection.FieldType.Bool: - return BoolTemplate; - case Google.Protobuf.Reflection.FieldType.Float: - case Google.Protobuf.Reflection.FieldType.Double: - return DoubleTemplate; - case Google.Protobuf.Reflection.FieldType.Int32: - case Google.Protobuf.Reflection.FieldType.Int64: - return IntTemplate; - case Google.Protobuf.Reflection.FieldType.UInt32: - case Google.Protobuf.Reflection.FieldType.UInt64: - return UIntTemplate; - case Google.Protobuf.Reflection.FieldType.String: - return StringTemplate; - case Google.Protobuf.Reflection.FieldType.Enum: - return EnumTemplate; - case Google.Protobuf.Reflection.FieldType.Message: - { - if(vm.descriptor.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName) - return QuantityMeasureTemplate; - else if (vm.descriptor.MessageType.FullName == typeof(bts.api.orderman.CustomOrderDefinition).FullName) - return OrderDefTemplate; - } - break; - default: return null; + if(descriptor.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName) + return QuantityMeasureTemplate; + else if (descriptor.MessageType.FullName == typeof(bts.api.orderman.CustomOrderDefinition).FullName) + return OrderDefTemplate; } + break; + default: return null; } return null; } diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs index 6c5bbf74f..3a42bbdfe 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs @@ -14,31 +14,57 @@ using System.ComponentModel; using System.Linq; using System.Reactive.Linq; + +using Google.Protobuf; +using Google.Protobuf.Reflection; +using bts.api; using AlgoType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; namespace mbts.spark { static partial class OrderTypeVMFactory { - class GenericOrderVM : OrderTypeVMBase + internal class GenericOrderFactory + { + static public IAlgoVM create_vm(AlgoKey key, AlgoType algo_type, string display_name, AlgoEditingPermissions editing_permissions) + { + var prop = typeof(CustomOrderDefinition).GetProperty(algo_type.ToString(), + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); + var rt = prop.PropertyType; + + var propi = typeof(CustomOrderDefinitionImmutable).GetProperty(algo_type.ToString(), + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); + var rti = propi.PropertyType; + + var vmt = typeof(GenericOrderVM<,>); + Type[] typeargs = { rt, rti }; + var makevm = vmt.MakeGenericType(typeargs); + object vm = Activator.CreateInstance(makevm, key, algo_type, display_name, editing_permissions); + return (IAlgoVM)vm; + } + static public void setup_generic_custom_order(CustomOrderDefinition custom, CustomOrderDefinition.CustomOneofCase type) + { + var prop = typeof(CustomOrderDefinition).GetProperty(type.ToString(), + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); + var rt = prop.PropertyType; + var rtt = Activator.CreateInstance(rt); + prop.SetValue(custom, rtt); + } + } + + class GenericOrderVM : OrderTypeVMBase + where TMutable : class, IMessage, new() where TImmutable : struct, IImmutableMessage { - readonly ObservableKeyedCollection _parameters = new ObservableKeyedCollection(vm => vm.descriptor.ParamName); - readonly Property _algo_type_string; + readonly ObservableKeyedCollection, string> _parameters = + new ObservableKeyedCollection, string>(vm => vm.descriptor.Name); readonly AlgoType _algo_type; public GenericOrderVM(AlgoKey key, AlgoType algo_type, string display_name, AlgoEditingPermissions editing_permissions) : base(key, display_name, editing_permissions) { _algo_type = algo_type; - _algo_type_string = add_property(nameof(this.algo_type), GenericOrderDefinition.KnownTypeFieldNumber, - algo_type.ToString(), permissions: AlgoParamPermissions.None); - _parameters = new ObservableKeyedCollection(vm => vm.descriptor.Name); + _parameters = new ObservableKeyedCollection, string>(vm => vm.descriptor.Name); _resources.AddRange( - _algo_type_string.Subscribe(_ => - { - _sync_generic_params(); - sync_selectors(); - }), ObservableAutoWrapper.connect_item_hooks(_parameters, p => { p.PropertyChanged += on_param_prop_changed; @@ -50,103 +76,97 @@ namespace mbts.spark }); }, true)); void on_param_prop_changed(object sender, PropertyChangedEventArgs e) => raise_definition_changed(); + _sync_generic_params(); + sync_selectors(); } - public string algo_type => _algo_type_string.Value; + public string algo_type => _algo_type.ToString(); public override string abbreviation => model == null ? display_name : model.factory.abbreviate_algo(new OneOf(_algo_type)); public override AlgoDisplayGroup group => AlgoDisplayGroup.API; - public ObservableCollection parameters => _parameters; - - protected CustomOrderDefinition construct() - { - string propname = _algo_type.ToString(); - var pi = typeof(CustomOrderDefinition).GetProperty(propname, - System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); - ExceptionAssert.Argument.NotNull(pi, "construction of " + propname); - var rt = pi.PropertyType; - var rtt = Activator.CreateInstance(rt); - - System.Reflection.typ - } + public ObservableCollection> parameters => _parameters; protected override void on_model_internal() { _sync_generic_params(); } - void _sync_generic_params() { if (model != null) - { - var desc = model.dynamic_helper.get_descriptor(dyn_type); - Google.Protobuf.Reflection.MessageDescriptor desc = - _parameters.SyncWithDictionary(desc.ParamDesc.Where(_is_value_param_desc).ToDictionary(p => p.ParamName), _mk_param_vm); + { + Google.Protobuf.Reflection.MessageDescriptor desc = new TMutable().Descriptor; + _parameters.SyncWithDictionaryNotNull( + desc.Fields.InDeclarationOrder().Where(_is_value_param_desc).ToDictionary(p => p.Name), _mk_param_vm); } } + static bool _is_value_param_desc(FieldDescriptor desc) + { + return (desc.FieldType != Google.Protobuf.Reflection.FieldType.Message || + _is_quantity_measure_desc(desc)); + } + static bool _is_quantity_measure_desc(FieldDescriptor desc) + { + return desc.FieldType == Google.Protobuf.Reflection.FieldType.Message && + desc.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName; + } protected override IEnumerable get_selector_datum() { - var data = OrderDataHelper.get_child_algo_data( - new CustomOrderDefinition { Dynamic = model?.dynamic_helper?.create_def(dyn_type) ?? new DynamicOrderDefinition() - }); - return data; + return OrderDataHelper.get_child_algo_data(_algo_type); } - static bool _is_value_param_desc(DynamicOrderParamDescriptor desc) + GenericOrderParamVM _mk_param_vm(FieldDescriptor descriptor) { - return desc.ParamType.DescriptorCase == DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated || - (desc.ParamType.DescriptorCase == DynamicOrderTypeDescriptor.DescriptorOneofCase.Primitive && desc.ParamType.Primitive != DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef); - } - - - DynamicOrderParamVM _mk_param_vm(DynamicOrderParamDescriptor descriptor) { ExceptionAssert.Argument.NotNull(descriptor, nameof(descriptor)); - switch (descriptor.ParamType.DescriptorCase) { - case DynamicOrderTypeDescriptor.DescriptorOneofCase.Primitive: - switch (descriptor.ParamType.Primitive) { - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveBool: return mk(DynamicOrderParam.AsBoolFieldNumber, v => v, v => v.GetValueOrDefault()); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveDouble: return mk(DynamicOrderParam.AsDoubleFieldNumber, v => v, v => v, double.NaN); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault()); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return mk(DynamicOrderParam.AsStringFieldNumber, v => v, v => v); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: - { - var qm = new DynamicOrderQuantityVM(descriptor); - return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasure { Ratio = 1.0, Absolute = 1 }); - } - default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.Primitive, nameof(descriptor)); - } - case DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated: - var pvm = new DynamicOrderEnumeratedVM(descriptor); - attached(pvm, DynamicOrderHelper.get_enumerated_value, DynamicOrderHelper.set_enumerated_value); - return pvm; - default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.DescriptorCase, nameof(descriptor)); + + if (descriptor.FieldType == FieldType.Message && + descriptor.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName) + { + var qm = new GenericOrderQuantityVM(descriptor); + return attached(qm, GenericOrderHelper.get_quantity_value, GenericOrderHelper.set_quantity_value, new QuantityMeasure { Ratio = 1.0, Absolute = 1 }); } - - DynamicOrderParamVM mk(int field_number, Func convert, Func convert_back, TView default_value = default) { - var fdesc = DynamicOrderParam.Descriptor.FindFieldByNumber(field_number); - var pvm = new DynamicOrderParamVM(descriptor, fdesc, convert, convert_back); - return attached>(pvm, get_from_param, assign_to_param, default_value); - - TView get_from_param(DynamicOrderParam param) => convert(ConvertUtils.try_convert(fdesc.Accessor.GetValue(param))); - void assign_to_param(DynamicOrderParam param, TView value) => fdesc.Accessor.SetValue(param, convert_back(value)); - } - - TParam attached(TParam pvm, Func selector, Action assign_to_param, T default_value = default) where TParam : DynamicOrderParamVM, IDynamicOrderParamVM { - add_property( - pvm.descriptor.ParamName, - mdef => mdef.OrderParams.TryGetValue(pvm.descriptor.ParamName, out DynamicOrderParam p) ? selector(p) : default_value, - (mdef, v) => assign_to_param(mdef.OrderParams.GetOrAdd(pvm.descriptor.ParamName, pn => new DynamicOrderParam()), v), - () => pvm.value, - v => pvm.value = v, - pvm.subscribe_value(), - default_value); + else if(descriptor.FieldType == FieldType.Enum) + { + var pvm = new GenericOrderEnumeratedVM(descriptor); + attached(pvm, GenericOrderHelper.get_enumerated_value, GenericOrderHelper.set_enumerated_value); return pvm; } + else if (descriptor.FieldType == FieldType.Bool) + return mk(descriptor, v => v, v => v.GetValueOrDefault()); + else if (descriptor.FieldType == FieldType.Double || descriptor.FieldType == FieldType.Float) + return mk(descriptor, v => v, v => v, double.NaN); + else if (descriptor.FieldType == FieldType.Int32) + return mk(descriptor, v => v, v => v.GetValueOrDefault()); + else if (descriptor.FieldType == FieldType.UInt32) + return mk(descriptor, v => v, v => v.GetValueOrDefault()); + else if (descriptor.FieldType == FieldType.String) + return mk(descriptor, v => v, v => v); + else + return null; } + GenericOrderParamVM mk(FieldDescriptor fdesc, Func convert, Func convert_back, TView default_value = default) + { + var pvm = new GenericOrderParamVM(fdesc, convert, convert_back); + return attached>(pvm, get_from_param, assign_to_param, default_value); + + TView get_from_param(IMessage param, FieldDescriptor fd) => convert(ConvertUtils.try_convert(fdesc.Accessor.GetValue(param))); + void assign_to_param(IMessage param, FieldDescriptor fd, TView value) => fdesc.Accessor.SetValue(param, convert_back(value)); + } + + TParam attached(TParam pvm, Func selector, Action assign_to_param, T default_value = default) where TParam : GenericOrderParamVM, IGenericOrderParamVM + { + add_property( + pvm.descriptor.Name, + mdef => mdef != null ? selector(mdef, pvm.descriptor) : default_value, + (mdef, v) => assign_to_param(mdef, pvm.descriptor, v), + () => pvm.value, + v => pvm.value = v, + pvm.subscribe_value(), + default_value); + return pvm; + } } } } \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index d760f0737..ed4ba86db 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -56,7 +56,12 @@ namespace mbts.spark { public static IAlgoVM iceberg(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new IcebergOrderVM(key, display_name, editing_permissions); public static IAlgoVM spreader(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new SpreaderOrderVM(key, display_name, editing_permissions); public static IAlgoVM dynamic(AlgoKey key, string display_name, string dynamic_type, AlgoEditingPermissions editing_permissions) => new DynamicOrderVM(key, dynamic_type, display_name, editing_permissions); - public static IAlgoVM generic(AlgoKey key, string display_name, AlgoType algo_type, AlgoEditingPermissions editing_permissions) => new GenericOrderVM(key, algo_type, display_name, editing_permissions); + + public static IAlgoVM generic(AlgoKey key, string display_name, + AlgoType algo_type, AlgoEditingPermissions editing_permissions) + { + return GenericOrderFactory.create_vm(key, algo_type, display_name, editing_permissions); + } public static IAlgoVM create(AlgoData def_data, AlgoEditingPermissions editing_permissions = default_editing_permissions, bool is_expanded = false) { if (def_data.value.CustomCase == AlgoType.Composite) { diff --git a/src/client/spark/core/Collections/ObservableKeyedCollection.cs b/src/client/spark/core/Collections/ObservableKeyedCollection.cs index 578c51acb..e25dc53a6 100644 --- a/src/client/spark/core/Collections/ObservableKeyedCollection.cs +++ b/src/client/spark/core/Collections/ObservableKeyedCollection.cs @@ -128,6 +128,22 @@ namespace mbts.collections { } } + public void SyncWithDictionaryNotNull(IReadOnlyDictionary dictionary, Func factory) + { + foreach (var k in _map.Keys.Except(dictionary.Keys).ToList()) + Remove(k); + foreach (var kv in dictionary) + { + if (!ContainsKey(kv.Key)) + { + var v = factory(kv.Value); + if(v != null) + Add(v); + } + } + } + + public void SyncWithDictionary(IReadOnlyDictionary dictionary, Func factory) { foreach (var k in _map.Keys.Except(dictionary.Keys).ToList()) Remove(k); diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 6f1d48fe7..35d385b16 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -103,7 +103,6 @@ message CustomOrderDefinition ScratchOrderDefinition scratch = 28; ProRataOrderDefinition prorata = 29; - GenericOrderDefinition generic = 900; DynamicOrderDefinition dynamic = 1000; } } @@ -221,11 +220,6 @@ message DynamicOrderDefinition { string dynamic_order_type = 1; map order_params = 2; } -//Like dynamic, this order type generates a UI, but is used a wrapper for a typed native algo above -message GenericOrderDefinition { - string known_type = 1; - CustomOrderDefinition definition = 2; -} // End custom order definitions message OrderRecord { @@ -277,7 +271,6 @@ message OrderState { ScratchOrderState scratch = 27; ProRataOrderState prorata = 28; - GenericOrderState generic = 250; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } } @@ -355,7 +348,6 @@ message PricerOrderState {} message ScratchOrderState {} message ProRataOrderState {} message DynamicOrderState {} -message GenericOrderState {} message QueueHolderState {} enum OrderStatus { diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 299e53901..6460a060b 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -81,64 +81,6 @@ int Run::execute(std::vector const &) { plugin::init_plugins(plugin_services); // finished initializing order plugins - //TODO these will go away once Haris approves algo - using namespace api::orderman; - { - DynamicOrderDescriptor desc; - desc.set_dynamic_order_type("Scratch"); - auto param = desc.add_param_desc(); - param->set_param_name("scratch_threshold"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); - param = desc.add_param_desc(); - param->set_param_name("chase_ticks"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); - - param = desc.add_param_desc(); - param->set_param_name("child"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); - plugin_services.register_dynamic_algo_order( - desc, []() { return std::make_unique(); }); - } - - { - DynamicOrderDescriptor desc; - desc.set_dynamic_order_type("ProRata"); - auto param = desc.add_param_desc(); - param->set_param_name("hedge_ticks"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); - param = desc.add_param_desc(); - param->set_param_name("chase_ticks"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); - - param = desc.add_param_desc(); - param->set_param_name("fill_limit"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); - - param = desc.add_param_desc(); - param->set_param_name("stay_in_threshold"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_QUANTITY); - param = desc.add_param_desc(); - param->set_param_name("enable_scratch_on_overfill"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_BOOL); - param = desc.add_param_desc(); - param->set_param_name("enable_stay_in"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_BOOL); - - param = desc.add_param_desc(); - param->set_param_name("child"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); - param = desc.add_param_desc(); - param->set_param_name("hedge"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); - param = desc.add_param_desc(); - param->set_param_name("overfill_hedge"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); - - - plugin_services.register_dynamic_algo_order( - desc, []() { return std::make_unique(); }); - } - orderman::OrderServiceServer order_service_server( sc, mdclient, dyn_order_registry, stats, perf_log); support::ErrorReportServer error_report_service(sc); From 90178908ba524dca535aedbde04953b4636869d0 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 28 May 2019 22:02:21 -0400 Subject: [PATCH 023/153] looking good; need John to help me figure out the hedge lablels --- .../spark/Orders/OrderTypeVMs/GenericOrderVM.cs | 9 ++++----- .../Order/AlgoContentTemplateSelector.xaml | 14 +++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs index 3a42bbdfe..dd5117dfd 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs @@ -48,6 +48,10 @@ namespace mbts.spark System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); var rt = prop.PropertyType; var rtt = Activator.CreateInstance(rt); + + var ci = rt.GetProperties().Where(x => x.PropertyType == typeof(CustomOrderDefinition)); + foreach(var cid in ci) + cid.SetValue(rtt, new CustomOrderDefinition { Limit = new LimitOrderDefinition() }); prop.SetValue(custom, rtt); } } @@ -112,11 +116,6 @@ namespace mbts.spark desc.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName; } - protected override IEnumerable get_selector_datum() - { - return OrderDataHelper.get_child_algo_data(_algo_type); - } - GenericOrderParamVM _mk_param_vm(FieldDescriptor descriptor) { ExceptionAssert.Argument.NotNull(descriptor, nameof(descriptor)); diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index f5dc590c9..34ece4c80 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1167,7 +1167,7 @@ - + - + - + - + - + - + + Text="{Binding descriptor.Name, Converter={x:Static ut:DisplayNameHelper.SnakeToTitleCaseConverter}}"/> From 717c90867aebeab89d3962bd0554e0cff87626ea Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Wed, 29 May 2019 15:08:14 -0400 Subject: [PATCH 024/153] fixed multiple child case, thanks Eric --- .../Trading/AlgoStudio/VM/AlgoDefinitionVM.cs | 3 ++ src/client/spark/Orders/OrderDataHelper.cs | 6 ++- .../Orders/OrderTypeVMs/GenericOrderVM.cs | 6 +++ .../spark/Orders/OrderTypeVMs/IOrderTypeVM.cs | 1 + .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 2 + .../Order/AlgoContentTemplateSelector.xaml | 2 +- .../orders/algo/algo_orders/ProRataAlgo.cpp | 50 ------------------- .../orders/algo/algo_orders/ScratchAlgo.cpp | 23 --------- 8 files changed, 18 insertions(+), 75 deletions(-) diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs index 7ec794043..55f88e7c2 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs @@ -51,6 +51,9 @@ namespace mbts.spark { readonly Property _selected_format; readonly EnumMaskInt32 _permissions_mask = new EnumMaskInt32(); + mbts.ui.spark.AlgoContentTemplateSelector.OrderKind IAlgoSelector.order_kind => + new mbts.ui.spark.AlgoContentTemplateSelector.OrderKind(string.Empty, string.Empty); + int? _favorite_index; Brush _badge_fill; Snapshot? _committed; diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index af4fd93ec..8b5c25c0a 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -26,7 +26,11 @@ namespace mbts.spark.utilities { public readonly Func get_child; public readonly Action assign_child; - public ChildAlgoData(string name, Func get_child_from_impl, Action assign_child_to_impl, Func get_child, Action assign_child) { + public ChildAlgoData(string name, Func get_child_from_impl, + Action assign_child_to_impl, + Func get_child, + Action assign_child) + { this.name = name; this.get_child = get_child; this.assign_child = assign_child; diff --git a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs index dd5117dfd..b8a128520 100644 --- a/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/GenericOrderVM.cs @@ -116,6 +116,12 @@ namespace mbts.spark desc.MessageType.FullName == typeof(bts.spark.QuantityMeasure).FullName; } + protected override IEnumerable get_selector_datum() + { + var v = OrderDataHelper.get_child_algo_data(_algo_type); + return v; + } + GenericOrderParamVM _mk_param_vm(FieldDescriptor descriptor) { ExceptionAssert.Argument.NotNull(descriptor, nameof(descriptor)); diff --git a/src/client/spark/Orders/OrderTypeVMs/IOrderTypeVM.cs b/src/client/spark/Orders/OrderTypeVMs/IOrderTypeVM.cs index 8fd3dfcf6..a8fb64efe 100644 --- a/src/client/spark/Orders/OrderTypeVMs/IOrderTypeVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/IOrderTypeVM.cs @@ -92,6 +92,7 @@ namespace mbts.spark { IReadOnlyObservableCollection algos_view { get; } IAlgoVM selected_algo { get; set; } void load_algo_data(IReadOnlyDictionary algo_data, AlgoKey? default_algo); + mbts.ui.spark.AlgoContentTemplateSelector.OrderKind order_kind {get;} } static class OrderTypeVMExtensions { diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index ed4ba86db..7ce192ead 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -954,6 +954,8 @@ namespace mbts.spark { public string id { get; } public string display_name { get; } + public mbts.ui.spark .AlgoContentTemplateSelector.OrderKind order_kind => + new mbts.ui.spark.AlgoContentTemplateSelector.OrderKind(display_name, string.Empty); public bool is_enabled { get; set; } = true; public bool initialized { get; private set; } diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 34ece4c80..56a66b77b 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1299,7 +1299,7 @@ SelectedItem="{Binding selected_algo}" CanEditStructure="{Binding DataContext.editing_permissions, Converter={StaticResource CanEditStructureConverter}, RelativeSource={RelativeSource AncestorType=ItemsControl}}" ContentTemplateSelector="{DynamicResource AlgoTemplateSelector}" - OrderKind="{x:Static usp:AlgoContentTemplateSelector.ChildKind}" + OrderKind="{Binding order_kind}" OrderKindTemplate="{StaticResource OrderKindTemplate}" /> diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 96b7aa52e..00a39730c 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -63,56 +63,6 @@ ProRataAlgo::ProRataAlgo() : me(new PImpl()) {} ProRataAlgo::~ProRataAlgo() {} IAlgoServices &ProRataAlgo::algo() const { return *me->algo; } -// template -// T convert_from_dynamic(const T& orig) -// { -// T newt = orig; -// if(has_custom(orig) && custom(orig).has_dynamic()) -// { -// const DynamicOrderDefinition& dprorata = custom(orig).dynamic(); -// auto& prorata = *mutable_custom(newt).mutable_prorata(); - -// if(dprorata.order_params().count("hedge_ticks") > 0) -// prorata.set_hedge_ticks(dprorata.order_params().at("hedge_ticks").as_uint32()); -// prorata.set_hedge_ticks(std::max(1u, prorata.hedge_ticks())); - -// if(dprorata.order_params().count("chase_ticks") > 0) -// prorata.set_chase_ticks(dprorata.order_params().at("chase_ticks").as_uint32()); - -// if(dprorata.order_params().count("fill_limit") > 0) -// *prorata.mutable_fill_limit() = dprorata.order_params().at("fill_limit").as_quantity(); -// if(!prorata.has_fill_limit() || -// prorata.fill_limit().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) -// prorata.mutable_fill_limit()->set_absolute(1u); - -// if(dprorata.order_params().count("stay_in_threshold") > 0) -// *prorata.mutable_stay_in_threshold() = dprorata.order_params().at("stay_in_threshold").as_quantity(); -// if(!prorata.has_stay_in_threshold() || -// prorata.stay_in_threshold().quantity_case() == bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) -// prorata.mutable_stay_in_threshold()->set_absolute(1u); - -// if(dprorata.order_params().count("enable_scratch_on_overfill") > 0) -// prorata.set_enable_scratch_on_overfill(dprorata.order_params().at("enable_scratch_on_overfill").as_bool()); -// if(dprorata.order_params().count("enable_stay_in") > 0) -// prorata.set_enable_stay_in(dprorata.order_params().at("enable_stay_in").as_bool()); - -// if(dprorata.order_params().count("child") > 0) -// *prorata.mutable_child() = dprorata.order_params().at("child").as_order_def(); -// else -// prorata.mutable_child()->mutable_limit(); - -// if(dprorata.order_params().count("hedge") > 0) -// *prorata.mutable_hedge() = dprorata.order_params().at("hedge").as_order_def(); -// else -// prorata.mutable_hedge()->mutable_limit(); - -// if(dprorata.order_params().count("overfill_hedge") > 0) -// *prorata.mutable_overfill_hedge() = dprorata.order_params().at("overfill_hedge").as_order_def(); -// else -// prorata.mutable_overfill_hedge()->mutable_limit(); -// } -// return newt; -// } OrderOpResult ProRataAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 2bb3b56ad..9d9af6b98 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -33,29 +33,6 @@ ScratchAlgo::ScratchAlgo() : me(new PImpl()) {} ScratchAlgo::~ScratchAlgo() {} IAlgoServices &ScratchAlgo::algo() const { return *me->algo; } -// template -// T convert_from_dynamic(const T& orig) -// { -// T newt = orig; -// if ( has_custom(orig) && custom(orig).has_dynamic()) -// { -// const DynamicOrderDefinition& dscratch = custom(orig).dynamic(); -// auto& scratch = *mutable_custom(newt).mutable_scratch(); - -// if(dscratch.order_params().count("scratch_threshold") > 0) -// *scratch.mutable_scratch_threshold() = dscratch.order_params().at("scratch_threshold").as_quantity(); - -// if(dscratch.order_params().count("chase_ticks") > 0) -// scratch.set_chase_ticks(dscratch.order_params().at("chase_ticks").as_uint32()); - -// if(dscratch.order_params().count("child") > 0) -// *scratch.mutable_child() = dscratch.order_params().at("child").as_order_def(); -// else -// scratch.mutable_child()->mutable_limit(); -// } -// return newt; -// } - OrderOpResult ScratchAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { const auto& def = serv.definition(); From 3d11aba8ef9ee12c1539d462d437c2d24ef5b3f3 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Wed, 29 May 2019 15:35:47 -0400 Subject: [PATCH 025/153] fix build from merge --- src/lib/CMakeDeps_win.cmake | 2 +- src/lib/api/services/OrderService.proto | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/CMakeDeps_win.cmake b/src/lib/CMakeDeps_win.cmake index c332cbc98..39c84c8bb 100644 --- a/src/lib/CMakeDeps_win.cmake +++ b/src/lib/CMakeDeps_win.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index e45314190..235b49171 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -101,8 +101,8 @@ message CustomOrderDefinition OCOOrderDefinition oco = 25; PricerOrderDefinition pricer = 26; CompositeOrderDefinition composite = 27; - ScratchOrderDefinition scratch = 28; - ProRataOrderDefinition prorata = 29; + ScratchOrderDefinition scratch = 31; + ProRataOrderDefinition prorata = 32; DynamicOrderDefinition dynamic = 1000; } @@ -272,8 +272,8 @@ message OrderState { QueueHolderState queue_holder = 30; OCOOrderState oco = 25; PricerOrderState pricer = 26; - ScratchOrderState scratch = 27; - ProRataOrderState prorata = 28; + ScratchOrderState scratch = 31; + ProRataOrderState prorata = 32; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } From d8559276d04c12aff67715e1b729bc0b4cec18a9 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Wed, 29 May 2019 16:55:11 -0400 Subject: [PATCH 026/153] test case for scratchalgo --- .../orders/algo/algo_orders/ScratchAlgo.cpp | 14 +++--- testing/manual/algos/scratch_controlled.txt | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 testing/manual/algos/scratch_controlled.txt diff --git a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp index 9d9af6b98..d2645d7a1 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ScratchAlgo.cpp @@ -146,16 +146,16 @@ void ScratchAlgo::try_to_scratch() Quantity my_quan; auto &my_side_psz = me->last_book.side(my_side)[0]; auto &other_side_psz = me->last_book.side(other_side)[0]; - bool market_moved = !securities::is_equal_or_better(other_side, - Price(other_side_psz.price()), Price(me->book_at_inception.side(other_side)[0].price())); + auto other_side_price = Price(other_side_psz.price()); + auto inception_other_side_price = Price(me->book_at_inception.side(other_side)[0].price()); + + bool market_moved = !securities::is_equal_or_better(other_side, other_side_price, inception_other_side_price); bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); if (market_moved || (on_market && my_side_psz.size() <= me->scratch_threshold.value()) ) { - Price scratch_price; - Quantity scratch_quan; - SpreaderUtils::traverse_depth_for_quantity(me->last_book, other_side, algo().quantity(), scratch_price, scratch_quan); - LOG(OrderMan, Diag, "ScratchOrder order {} scratching: {}@{} {}", - algo().order_key().value(), algo().quantity().value(), scratch_price, scratch_quan); + Price scratch_price = securities::worsen_price(other_side, inception_other_side_price, me->chase_diff); + LOG(OrderMan, Diag, "ScratchOrder order {} scratching: {}@{}", + algo().order_key().value(), algo().quantity().value(), scratch_price); // Time to scratch Update child_update; diff --git a/testing/manual/algos/scratch_controlled.txt b/testing/manual/algos/scratch_controlled.txt new file mode 100644 index 000000000..0d8740cb4 --- /dev/null +++ b/testing/manual/algos/scratch_controlled.txt @@ -0,0 +1,48 @@ +Test Author: Kevin +If it breaks: Kevin + +When the scratch order is placed, it takes a snapshot of the opposite inside market; if that level gets +worse, it will cross the market chase_ticks through. + +It also continually monitors the size on the same side of the market if the quantity at that (same side) level falls below the "Scratch Threshold" AND the order's price is equal to the best bid or offer. It will cross by chase_ticks. + +0. This needs sim market data **not** to be merged with replay (or +cert), so we can create our own markets. Use the following key to set +market data up: + +ExchangeSim { + value { + merge_with_md: false + } +} + +1. Bring up a ladder, lets say in SIM:ESU9 (does not matter which security) + +2. Create the ask side of the market by sending 50 lot sell limit orders at 2833.25 through 2832.25 +(if there are no markets use the M View and the ticket to enter, if you already had left over +orders cancel them) + +2. Create the bid side of the market by sending 50 lot buy limit orders at 2830.50 through 2831.50. +You should have no orders at 2831.75 and 2832. + +Basics: +3. Enter a 20 lot buy scratch order at 2831.50 with the following parameters +Scratch Threshold = Absolute:21 +Max Chase Ticks = 2 +Child = Limit +Verify that the Lean order along with the Child order shows up + +4) Now cancel the 50 lot sell at 2832.25, you should see the 20 lot scratch pay 2832.50 on 20 + +5) Repeat 3, now cancel the 50 lot buy at 2831.50, you should again see scratch pay 2832.50 on 20. +Put the 50 lot buy back in. + +6) Repeat 3, put an offer in at 2832.25, cancel it; nothing should happen. Cancel the Scratch Algo + +7) This time enter the algo with Max Chase = 0, cancel the 50 lot offer at 2832.75 and 2833. Nothing should happen. Now cancel the 2832.5 offer, you should see the algo pay up to 2832.5, but not fill. + +8) Cancel the last offer at 2833.25. Nothing should happen. + +9) Repeat all of this on the sell side + + From 5b818bf276ecd64629ec5955cee81795e16e842e Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Wed, 29 May 2019 17:43:05 -0400 Subject: [PATCH 027/153] test for prorata order --- .../orders/algo/algo_orders/ProRataAlgo.cpp | 1 + testing/manual/algos/pro_rata_controlled.txt | 87 +++++++++++++++++++ testing/manual/algos/scratch_controlled.txt | 9 +- 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 testing/manual/algos/pro_rata_controlled.txt diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index 00a39730c..bebd90879 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -187,6 +187,7 @@ void ProRataAlgo::scratch(std::pair kk) Price scratch_price; Quantity scratch_quan; SpreaderUtils::traverse_depth_for_quantity(me->last_book, cross_side, kk.second, scratch_price, scratch_quan); + //Quantity scratch_quan = Price(me->book_at_inception.side(other_side)[0].price()) scratch_price = securities::worsen_price(cross_side, scratch_price, me->chase_diff); Update update; diff --git a/testing/manual/algos/pro_rata_controlled.txt b/testing/manual/algos/pro_rata_controlled.txt new file mode 100644 index 000000000..b9ea169e6 --- /dev/null +++ b/testing/manual/algos/pro_rata_controlled.txt @@ -0,0 +1,87 @@ +Test Author: Kevin +If it breaks: Kevin + +When the prorata order is placed, it acts as a slightly more complex hedging algorithm than Cover. +It will: + - Classify each fill as a normal fill or an 'overfill' according to "Fill Limit" parameters + - Standard fills are hedged with the "Hedge" child order + - Over fills are hedge with the "Overfill Hedge" child order + +The prorata order works the child until the Fill Limit is breached. +Once the fill limit is breached, the algo will begin the 'get out process' +Unless: + - If "Enable Stay In" is checked, AND + a) The opposite inside market (at inception) doesn't move against us AND + b) The same side quantity remains ABOVE the "Stay in Threshold" +If "Enable Stay In" is not checked, 'get out process' begins right away + +"Get Out Process" means + 1) Cancel the child order + 2) Scratch the overfill hedges if and only if the "Enable Scratch on Overfill" is checked + -Scratches are done "Chase Ticks" through the original opposite market + + +0. This needs sim market data **not** to be merged with replay (or +cert), so we can create our own markets. Use the following key to set +market data up: + +ExchangeSim { + value { + merge_with_md: false + } +} + +1. Bring up a ladder, lets say in SIM:ESU9 (does not matter which security) + +2. Create the ask side of the market by sending 50 lot sell limit orders at 2833.25 through 2832.25 +(if there are no markets use the M View and the ticket to enter, if you already had left over +orders cancel them) + +2. Create the bid side of the market by sending 50 lot buy limit orders at 2830.75 through 2831.50. +You should have no orders at 2831.50, 2831.75 and 2832. + +Basics: +3. Enter a 20 lot buy prorata order at 2831.50 with the following parameters + + Hedge Ticks = 1 + Chase Ticks = 1 + Fill Limit = Absolute:5 + Stay in Threshold = Absolute:21 + Enable Scratch on Overfill = true + Enable Stay In = true + +Verify that the Prorata order along with the Child order shows up. + +Next put a 50 lot buy limit order on top of your prorata at 2831.50 + + +4) Using 1 lot limit sells, fill 5 of your prorata order. Verify that you see covers going in 1 tick better + +5) Cancel the 50 lot limit at 2831.50. Nothing should happen. Put the 50 lot back. + +6) Fill one more 1 lot. + +7) Again cancel the 50 lot limit at 2831.50. You should see: + - The remainder of the 20 lot should cancel + - 1 lot of your hedges should scratch to sell at 2831.25 + - The parent order should stay in + - You should still be working 5 sales for hedges + +8) Fill the 5 sells, it should cancel the parent. + +9) Repeat 3-8, but this time set Enable Scratch on Overfill = false, Enable Stay In = true + - You should see the same results in 7, except the 1 lot scratch won't happen so you'll still have 6 working hedges + +9) Repeat 3-6, but this time set Enable Scratch on Overfill = false, Enable Stay In = false + - After filling your last lot in 1 you should see your working order cancel, with 6 hedges working + - The ladder may deceive you here, check the OB + - Cancel your hedges, this should cancel the whole chain + +9) Repeat 3-6, but this time set Enable Scratch on Overfill = true, Enable Stay In = false + - After filling your last lot in 1 you should see your working order cancel, one hedge scratch (at 2831.50), and 5 hedges working + - The ladder may deceive you here, check the OB + - Cancel your hedges, this should cancel the whole chain + +10) Repeat all of this on the sell side + + diff --git a/testing/manual/algos/scratch_controlled.txt b/testing/manual/algos/scratch_controlled.txt index 0d8740cb4..e711374a2 100644 --- a/testing/manual/algos/scratch_controlled.txt +++ b/testing/manual/algos/scratch_controlled.txt @@ -27,10 +27,11 @@ You should have no orders at 2831.75 and 2832. Basics: 3. Enter a 20 lot buy scratch order at 2831.50 with the following parameters -Scratch Threshold = Absolute:21 -Max Chase Ticks = 2 -Child = Limit -Verify that the Lean order along with the Child order shows up + Scratch Threshold = Absolute:21 + Max Chase Ticks = 2 + Child = Limit + + Verify that the Scratch order along with the Child order shows up 4) Now cancel the 50 lot sell at 2832.25, you should see the 20 lot scratch pay 2832.50 on 20 From a6ad70fc05f8a4060c23cb493e7649aac14d66c7 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 1 Jul 2019 15:46:03 -0400 Subject: [PATCH 028/153] SP-28 Algo unit test utils --- src/lib/md/relay/RelayRecorder.cpp | 51 ++- src/lib/md/relay/RelayRecorder.h | 5 +- .../orders/algo/IBeforeLaunchAlgoServices.cpp | 7 + .../orders/algo/IBeforeLaunchAlgoServices.h | 3 + .../algo/algo_orders/tests/AlgoTestDriver.cpp | 374 ++++++++++++++++++ .../algo/algo_orders/tests/AlgoTestDriver.h | 140 +++++++ src/lib/utils/ValueWrapper.h | 1 + 7 files changed, 570 insertions(+), 11 deletions(-) create mode 100644 src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h diff --git a/src/lib/md/relay/RelayRecorder.cpp b/src/lib/md/relay/RelayRecorder.cpp index d2a094b52..a15652138 100644 --- a/src/lib/md/relay/RelayRecorder.cpp +++ b/src/lib/md/relay/RelayRecorder.cpp @@ -11,6 +11,7 @@ #include "lib/md/protos/co2.pb.h" #include "lib/md/SecurityIDRecorder.h" #include "lib/config/protos/system.pb.h" +#include "lib/config/protos/MarketData.pb.h" #include "lib/utils/bts_chrono.h" #include @@ -173,7 +174,7 @@ void replay_file(const std::string& filename, IRelayWriter& writer, int32_t nagg } while (loop); } -void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) { +bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) { // split up url parts size_t http_len = BTS_ARRAYSIZE("http://") - 1; auto after_host = url.find('/', http_len); @@ -184,21 +185,53 @@ void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) filename = os::sparkdata_path("data/replay/") + filename; utils::download_file_if_newer(host, path, filename); if (boost::filesystem::exists(filename)) { - replay_file(filename, writer, naggle_ms, true); + replay_file(filename, writer, naggle_ms, loop); + return true; + } + return false; +} + +void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) { + if (!replay_url(url, writer, naggle_ms, loop)) { + const auto fallback = config::get().fallback(); + if (boost::filesystem::exists(fallback)) { + replay_file(fallback, writer, naggle_ms, loop); + } } } -void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) { - // loops forever - replay_url(url, writer, naggle_ms); - // If it returns it means it failed to download or play cached - const auto fallback = config::get().fallback(); - if (boost::filesystem::exists(fallback)) { - replay_file(fallback, writer, naggle_ms); +struct SecurityExtractorDone : public std::bad_exception {}; +struct SecurityExtractor : public IRelayWriter { + size_t _count; + std::function _cb; + std::set _keys; + SecurityExtractor(std::function& cb, size_t count) + : _count(count), _cb(cb), _keys() { } + ~SecurityExtractor() override {}; + void write(const co2::MDReply& data, bool*) { + for (auto const &ch : data.channels()) { + for (auto const &cr : ch.updates()) { + if (cr.has_security() && _keys.find(cr.security().id()) == _keys.end()) { + _keys.insert(cr.security().id()); + _cb(cr.security()); + } + } + } + if (_keys.size() >= _count) { + throw SecurityExtractorDone(); + } } +}; +void get_securities_from_replay(std::function cb) { + SecurityExtractor extractor(cb, 100); + auto url = config::get().url(); + try { + replay_url_with_fallback(url, extractor, 0, false); + } catch(const SecurityExtractorDone&) { } } + shared_ptr mkrelayproducer(bool shared); /*this is the public one*/ diff --git a/src/lib/md/relay/RelayRecorder.h b/src/lib/md/relay/RelayRecorder.h index d70dd4757..da6286340 100644 --- a/src/lib/md/relay/RelayRecorder.h +++ b/src/lib/md/relay/RelayRecorder.h @@ -29,9 +29,10 @@ void print_recording(const std::string&); void unprint_recording(const std::string& src, const std::string& dest); void trace_recording(const std::string& file, security_key key); void replay_recording(const std::string&, int32_t delay_ms, const std::string fallback=""); -void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms); -void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms); +bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true); +void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true); void replay_file(const std::string& filename, IRelayWriter& writer, int32_t naggle_ms, bool loop=false); +void get_securities_from_replay(std::function); }}//namespace diff --git a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp index 87f753e91..426290ef4 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp @@ -1 +1,8 @@ #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" +namespace bts { +namespace orderman { + +TimeStamp IBeforeLaunchAlgoServices::get_time() const { return Timer::now(); } + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h index 733f60d7c..3756971db 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h @@ -3,6 +3,7 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/domain_model/SecurityKey.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/utils/bts_chrono.h" namespace bts { namespace orderman { @@ -42,6 +43,8 @@ class IBeforeLaunchAlgoServices { virtual ChildTemplate & make_child_template(api::orderman::OrderDefinition const &child_def, size_t reserve = 1) = 0; + + virtual TimeStamp get_time() const; }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp new file mode 100644 index 000000000..2791ac953 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -0,0 +1,374 @@ +#include "AlgoTestDriver.h" +#include "lib/md/relay/RelayRecorder.h" +#include "catch/catch.hpp" +#include "lib/api/services/OrderService.pb.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/logging/logging.h" + +#include +#include + +namespace bts { +namespace orderman { +namespace test { +using namespace api::orderman; + +constexpr bool has_flag(ATDFlags prop, ATDFlags flag) { + return ((uint32_t)prop & (uint32_t)flag) == (uint32_t)flag; +} + +const std::map& get_some_securities() { + static std::map results; + if (results.empty()) { + md::get_securities_from_replay([&](const api::Security& s) { + results.insert(std::make_pair(SecurityKey(s.id()), s)); + }); + } + return results; +} + + +struct RecursionCounter { + size_t depth = 0; + virtual void flush() = 0; +}; +struct RecursionGuard { + RecursionGuard(RecursionCounter& c) : counter(c) { + counter.depth += 1; + REQUIRE(counter.depth < 100); + } + ~RecursionGuard() { + counter.depth -= 1; + if (counter.depth == 0) { counter.flush(); } + } +private: + RecursionCounter& counter; +}; + +struct AlgoTestDriverBase::Impl : public RecursionCounter { + IAlgoOrder& algo; + api::orderman::OrderDefinition definition; + Price price; + Quantity size; + ATDFlags flags; + TimeStamp now; + api::Security security; + OrderKey order_key, parent_key; + + AlgoState state; + std::vector orders; + + std::map books; + std::map book_subs; + bool sub_timer = false; + + // Callbacks + std::vector> on_child_launching; + std::vector> on_child_changing; + std::vector> on_child_changed; + + Impl(IAlgoOrder& o) : algo(o) {} + + OrderKey next_order() { + _next_order.value() += 1; + return _next_order; + } + RecursionGuard recusion_guard() { return {*this}; } + + void flush() override; + bool flushing = false; + // child changes must be asyncronous + std::vector> deferred; + void defer_child_change(OrderKey k, ChildState cs) { deferred.push_back(std::make_tuple(k,cs)); } + +private: + OrderKey _next_order = OrderKey(99); +}; + +void AlgoTestDriverBase::Impl::flush() { + if(flushing) return; + flushing = true; + for (const auto& update : deferred) { + algo.on_child_change(std::get(update), std::get(update)); + } + deferred.clear(); + flushing = false; +} + +AlgoTestDriverBase::AlgoTestDriverBase( + IAlgoOrder& algo, + const api::orderman::OrderDefinition& def, + Price price, Quantity size, + ATDFlags flags) + : me(new Impl(algo)) { + + me->definition = def; + me->price = price; + me->size = size; + me->flags = flags; + me->now = Timer::now(); + me->state = AlgoState::CREATED; + + auto securities = get_some_securities(); + REQUIRE(securities.size() > 0); + me->security = securities.begin()->second; + + me->order_key = me->next_order(); + if (has_flag(flags, ATDFlags::HAS_PARENT)) { + me->parent_key = me->next_order(); + } +} + +AlgoTestDriverBase::~AlgoTestDriverBase() {} + +OrderOpResult AlgoTestDriverBase::before_launch() { + REQUIRE(me->state == AlgoState::CREATED); + me->state = AlgoState::READY; + return me->algo.do_before_launch(*this); +} + +void AlgoTestDriverBase::launch() { + REQUIRE(me->state == AlgoState::READY); + me->algo.do_launch(*this); + me->state = AlgoState::RUNNING; +} + +void AlgoTestDriverBase::launch_attached(OrderKey key, ChildState state) { + REQUIRE(me->state == AlgoState::READY); + me->algo.do_launch_attached(*this, key, state); + me->state = AlgoState::RUNNING; +} +void AlgoTestDriverBase::do_update(const api::orderman::Update& update) { + auto guard = me->recusion_guard(); + REQUIRE(me->state == AlgoState::RUNNING); + me->algo.do_update(update); +} + +void AlgoTestDriverBase::update_book(const Book& book) { + auto key = book.security_key(); + me->books[key] = book; + auto iter = me->book_subs.find(key); + if (iter != me->book_subs.end()) { + me->algo.on_book(key, book); + } +} +void AlgoTestDriverBase::advance_time(Duration d) { + me->now += d; + if (me->sub_timer) { + me->algo.on_timer(); + } +} + +const std::vector& AlgoTestDriverBase::child_orders() const { + return me->orders; +} + +AlgoState AlgoTestDriverBase::state() const { + return me->state; +} + +void AlgoTestDriverBase::fill(OrderKey k, Price price, Quantity qty) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == k) { + REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value()); + for (const auto& cb : me->on_child_changing) cb(child); + auto old = child; + + auto total_price = child.cstate.avg_price.value() * child.cstate.total_fills.value(); + total_price += price.value() * qty.value(); + + child.cstate.last_fills = qty; + child.cstate.total_fills.value() += qty.value(); + child.cstate.last_price = price; + child.cstate.avg_price = Price { total_price / child.cstate.total_fills.value() }; + + if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) { + child.cstate.status = ChildStatus::COMPLETED; + } + + for (const auto& cb : me->on_child_changed) cb(old, child); + // No defer because this is called from outside + if (child.subscribed) me->algo.on_child_change(k, child.cstate); + break; + } + } +} + +const ChildOrder* largest_order(const std::vector& orders) { + uint32_t sz = 0; + const ChildOrder* result = nullptr; + for (const auto& order : orders) { + auto available = + order.cstate.quantity.value() - order.cstate.total_fills.value(); + if (order.cstate.status == ChildStatus::WORKING && + available > sz) { + sz = available; + result = ℴ + } + } + return result; +} +void AlgoTestDriverBase::fill(Price p, Quantity q) { + auto to_fill = q.value(); + while(to_fill > 0) { + auto order = largest_order(this->child_orders()); + REQUIRE(order); + auto available = (order->cstate.quantity.value() - order->cstate.total_fills.value()); + if (available >= to_fill) { + this->fill(order->key, p, Quantity {to_fill}); + return; + } + else { + this->fill(order->key, p, Quantity {available}); + to_fill -= available; + } + } +} + +void AlgoTestDriverBase::on_child_launching(std::function&& cb) { + me->on_child_launching.push_back(cb); +} +void AlgoTestDriverBase::on_child_changing(std::function&& cb) { + me->on_child_changing.push_back(cb); +} +void AlgoTestDriverBase::on_child_changed(std::function&& cb) { + me->on_child_changed.push_back(cb); +} + +// +// Utility + +ChildOrder mkchild(OrderKey key, Price price, Quantity qty, bool sub) { + ChildOrder result = {}; + result.key = key; + result.cstate.price = price; + result.cstate.quantity = qty; + result.subscribed = sub; + return result; +} + +// +// IBeforeLaunch + +const api::orderman::OrderDefinition& AlgoTestDriverBase::definition() const { return me->definition; } +const api::Security& AlgoTestDriverBase::security() const { return me->security; } +OrderKey AlgoTestDriverBase::order_key() const { return me->order_key; } +OrderKey AlgoTestDriverBase::parent_order_key() const { return me->parent_key; } +const api::Security* AlgoTestDriverBase::lookup_security(SecurityKey key) const { + auto s = get_some_securities(); + auto iter = s.find(key); + if (iter != s.end()) { + return &iter->second; + } + return nullptr; +} +bool AlgoTestDriverBase::is_attaching() const { return has_flag(me->flags, ATDFlags::IS_ATTACHING); } +ChildTemplate& AlgoTestDriverBase::make_child_template(const api::orderman::OrderDefinition&, size_t) { + ChildTemplate* t = nullptr; + return *t; +} +TimeStamp AlgoTestDriverBase::get_time() const { return me->now; } + +// +// IAlgoServices + +Price AlgoTestDriverBase::price() const { return me->price; } +Quantity AlgoTestDriverBase::quantity() const { return me->size; } +Quantity AlgoTestDriverBase::total_fills() const { return Quantity(0); } + +void AlgoTestDriverBase::subscribe_book(SecurityKey key){ + me->book_subs[key] = true; + auto iter = me->books.find(key); + if (iter != me->books.end()) { + me->algo.on_book(key, iter->second); + } +} +void AlgoTestDriverBase::unsubscribe_book(SecurityKey key) { + me->book_subs.erase(key); +} +void AlgoTestDriverBase::subscribe_child_change(OrderKey k) { + for (auto& child : me->orders) { + if (child.key == k) { + child.subscribed = true; + } + } +} +void AlgoTestDriverBase::unsubscribe_child_change(OrderKey k) { + for (auto& child : me->orders) { + if (child.key == k) { + child.subscribed = false; + } + } +} +void AlgoTestDriverBase::subscribe_timer() { me->sub_timer = true; } +void AlgoTestDriverBase::unsubscribe_timer() { me->sub_timer = false; } + +OrderKey AlgoTestDriverBase::launch_child(ChildTemplate&, Price price, Quantity qty, bool sub) { + OrderKey key = me->next_order(); + me->orders.push_back( + mkchild(key, price, qty, sub)); + for(const auto& cb : me->on_child_launching) cb(key); + return key; +} +OrderKey AlgoTestDriverBase::launch_child(api::orderman::OrderDefinition const & od, Price price, Quantity qty, bool sub) { + return launch_child(make_child_template(od), price, qty, sub); +} +void AlgoTestDriverBase::update_child(OrderKey key, api::orderman::Update const & update) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == key) { + for (const auto& cb : me->on_child_changing) cb(child); + auto old = child; + if (update.price() != 0) { + child.cstate.price = Price(update.price()); + } + if (update.quantity() != 0) { + child.cstate.quantity = Quantity(update.quantity()); + } + if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) { + child.cstate.status = ChildStatus::COMPLETED; + } + for (const auto& cb : me->on_child_changed) cb(old, child); + + if (child.subscribed) { + me->defer_child_change(key, child.cstate); + } + } + } +} +void AlgoTestDriverBase::cancel_child(OrderKey key) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == key) { + REQUIRE(child.cstate.status != ChildStatus::CANCELED); + for (const auto& cb : me->on_child_changing) cb(child); + auto before = child; + child.cstate.status = ChildStatus::CANCELED; + for (const auto& cb : me->on_child_changed) cb(before, child); + if(child.subscribed) { + me->defer_child_change(key, child.cstate); + } + break; + } + } +} +void AlgoTestDriverBase::detach_child(OrderKey key) { + std::remove_if(me->orders.begin(), me->orders.end(), [key](const auto& order) { return order.key == key; }); +} +void AlgoTestDriverBase::detach_all_children() { + me->orders.clear(); +} +void AlgoTestDriverBase::cancel(std::string const &) { + me->state = AlgoState::CANCELED; +} +void AlgoTestDriverBase::terminate_error(std::string const &) { + me->state = AlgoState::TERMINATED; +} +void AlgoTestDriverBase::complete() { + me->state = AlgoState::COMPLETED; +} +void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } +void AlgoTestDriverBase::update_fill_state(Quantity, Price) { } + +}}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h new file mode 100644 index 000000000..b475e8941 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -0,0 +1,140 @@ +#pragma once +#include "lib/orderman/orders/algo/IAlgoOrder.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/md/IntPrice.h" +#include +#include +#include + +namespace bts { +namespace orderman { +namespace test { + +const std::map& get_some_securities(); + +enum class ATDFlags { + NONE = 0, + IS_ATTACHING = 1, + HAS_PARENT = 2, +}; + +enum class AlgoState { + CREATED, + // before_lauh() -> + READY, + // launch() -> + RUNNING, + CANCELED, + TERMINATED, + COMPLETED +}; + +struct ChildOrder { + OrderKey key; + bool subscribed; + ChildState cstate; + uint32_t active_qty() const { + return cstate.status == ChildStatus::WORKING ? + cstate.quantity.value() - cstate.total_fills.value() : 0; + } +}; + +class AlgoTestDriverBase : private IAlgoServices { + struct Impl; + std::unique_ptr me; + + public: + AlgoTestDriverBase( + IAlgoOrder& algo, + const api::orderman::OrderDefinition& def, + Price price, Quantity size, + ATDFlags flags=ATDFlags::NONE); + ~AlgoTestDriverBase(); + + // + // Expose some things from IAlgoServices + const api::Security& security() const override; + Price price() const override; + Quantity quantity() const override; + Quantity total_fills() const override; + + // + // Change state + + OrderOpResult before_launch(); + void launch(); + void launch_attached(OrderKey, ChildState); + void do_update(const api::orderman::Update&); + + void update_book(const Book&); + void advance_time(Duration); + + // + // Inspection methods + const std::vector& child_orders() const; + AlgoState state() const; + + // Fills for this specific order + void fill(OrderKey, Price, Quantity); + // Fill this quantity, start with the largest child order + void fill(Price, Quantity); + + void on_child_launching(std::function&&); + void on_child_changing(std::function&&); + void on_child_changed(std::function&&); + + + +private: + + // + // IBeforeLaunchAlgoServices + const api::orderman::OrderDefinition& definition() const override; + // const api::Security& security() const override; + OrderKey order_key() const override; + OrderKey parent_order_key() const override; + const api::Security* lookup_security(SecurityKey) const override; + bool is_attaching() const override; + ChildTemplate& make_child_template(const api::orderman::OrderDefinition& child_def, size_t reserve = 1) override; + TimeStamp get_time() const override; + + // + // IAlgoServices + void subscribe_book(SecurityKey)override; + void unsubscribe_book(SecurityKey) override; + void subscribe_child_change(OrderKey) override; + void unsubscribe_child_change(OrderKey) override; + void subscribe_timer() override; + void unsubscribe_timer() override; + + OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) override; + OrderKey launch_child(api::orderman::OrderDefinition const &, Price, + Quantity, bool) override; + void update_child(OrderKey , api::orderman::Update const &) override; + void cancel_child(OrderKey) override; + void detach_child(OrderKey) override; + void detach_all_children() override; + + void cancel(std::string const &) override; + void terminate_error(std::string const &) override; + void complete() override; + void set_ladder_visibility(bool) override; + void update_fill_state(Quantity , Price) override; + +}; + +template +struct AlgoTestDriver : public AlgoTestDriverBase +{ + // Avoids mutual ownership problem + Algo _algo; + AlgoTestDriver( + const api::orderman::OrderDefinition& def, + const md::IntPrice& price, Quantity size, + ATDFlags flags=ATDFlags::NONE) + : AlgoTestDriverBase(_algo, def, Price {price.price()}, size, flags), _algo() + { } + const Algo& algo() const { return _algo; } +}; + +}}}//namespace \ No newline at end of file diff --git a/src/lib/utils/ValueWrapper.h b/src/lib/utils/ValueWrapper.h index 6c8b8692b..5df5e2ace 100644 --- a/src/lib/utils/ValueWrapper.h +++ b/src/lib/utils/ValueWrapper.h @@ -24,6 +24,7 @@ public: bool operator==(ValueWrapper const&v) const {return _v == v._v;} bool operator!=(ValueWrapper const&v) const {return _v != v._v;} bool operator<(ValueWrapper const&v) const {return _v < v._v;} + bool operator>(ValueWrapper const&v) const {return _v > v._v;} ValueT const& value() const {return _v;} ValueT & value() {return _v;} private: From 226fcc279bc9ddbdbc76607692c32f40720ffcb3 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 1 Jul 2019 15:46:20 -0400 Subject: [PATCH 029/153] Utils for manipulating dynamic algos --- .../orderman_utils/orderman_utils.cpp | 63 +++++++++++++++++++ .../orderman/orderman_utils/orderman_utils.h | 61 ++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/src/lib/orderman/orderman_utils/orderman_utils.cpp b/src/lib/orderman/orderman_utils/orderman_utils.cpp index 8099a0b5a..cd26ab515 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.cpp +++ b/src/lib/orderman/orderman_utils/orderman_utils.cpp @@ -49,5 +49,68 @@ bool should_forward_attachment( } } +api::orderman::DynamicOrderParam dynamic_parameter(int32_t v) { + api::orderman::DynamicOrderParam res; + res.set_as_int32(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(uint32_t v) { + api::orderman::DynamicOrderParam res; + res.set_as_uint32(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(double v) { + api::orderman::DynamicOrderParam res; + res.set_as_double(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(std::string v) { + api::orderman::DynamicOrderParam res; + res.set_as_string(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(bool v) { + api::orderman::DynamicOrderParam res; + res.set_as_bool(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_order_def()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_array()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_struct()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_enumerated()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_array(const std::vector& v) { + api::orderman::DynamicOrderArray a; + for (const auto& item : v) { + a.add_element()->CopyFrom(item); + } + return dynamic_parameter(a); +} +api::orderman::DynamicOrderParam dynamic_map( + const std::vector>& v) { + DynamicOrderStruct res; + auto& map = *res.mutable_field(); + for (const auto& tup : v) { + map[std::get(tup)] = std::get<1>(tup); + } + return dynamic_parameter(res); +} + + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orderman_utils/orderman_utils.h b/src/lib/orderman/orderman_utils/orderman_utils.h index 50b97ce43..435c5bbb1 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.h +++ b/src/lib/orderman/orderman_utils/orderman_utils.h @@ -1,4 +1,6 @@ #include "lib/api/services/OrderService.pb.h" +#include +#include namespace bts { namespace orderman { @@ -30,5 +32,64 @@ inline bool is_parent_order(const bts::api::orderman::OrderRecord &ord) { bool should_forward_attachment(const api::orderman::CustomOrderDefinition &def); bool can_consume_attachment(api::orderman::CustomOrderDefinition::CustomCase) ; + +api::orderman::DynamicOrderParam dynamic_parameter(int32_t); +api::orderman::DynamicOrderParam dynamic_parameter(uint32_t); +api::orderman::DynamicOrderParam dynamic_parameter(double); +api::orderman::DynamicOrderParam dynamic_parameter(std::string); +api::orderman::DynamicOrderParam dynamic_parameter(bool); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated&); +api::orderman::DynamicOrderParam dynamic_array(const std::vector&); +api::orderman::DynamicOrderParam dynamic_map( const std::vector>&); + +template +std::optional get_if(const api::orderman::DynamicOrderParam&) { return nullptr; } + +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsInt32 ? std::optional{p.as_int32()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsUint32 ? std::optional{p.as_uint32()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsDouble ? std::optional{p.as_double()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsBool ? std::optional{p.as_bool()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsOrderDef ? std::optional{p.as_order_def()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsArray ? std::optional{p.as_array()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsStruct ? std::optional{p.as_struct()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsEnumerated ? std::optional{p.as_enumerated()} : std::nullopt; +} + + +template +std::optional get_if(const google::protobuf::Map& map, const std::string& key) { + auto c = map.find(key); + if (c != map.end()) { + return get_if(c->second); + } + return std::nullopt; +} + } // namespace orderman } // namespace bts From 0dcdbd540ecf65dbcca807b968de7205a8149f58 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 1 Jul 2019 15:47:56 -0400 Subject: [PATCH 030/153] SP-87 Splitter algo --- .gitignore | 1 + src/lib/api/services/OrderService.proto | 11 +- src/lib/md/IntPrice.h | 6 +- src/lib/md/PriceSize.h | 2 +- .../orderman/events/OrderStateUpdateEvent.h | 3 + .../orderman/order_factory/OrderFactory.cpp | 5 + .../orders/algo/algo_orders/SplitterAlgo.cpp | 219 ++++++++ .../orders/algo/algo_orders/SplitterAlgo.h | 32 ++ .../algo/algo_orders/tests/splitter_tests.cpp | 526 ++++++++++++++++++ 9 files changed, 800 insertions(+), 5 deletions(-) create mode 100644 src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h create mode 100644 src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp diff --git a/.gitignore b/.gitignore index 7c976336d..98475e215 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ trace_data/ /src/lib/orderman/perf_analysis/last_pretty_log.csv /src/lib/orderman/perf_analysis/log.csv /doc/user/public/* +.ionide diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 1908ff7bf..f0cb044fb 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -101,6 +101,7 @@ message CustomOrderDefinition OCOOrderDefinition oco = 25; PricerOrderDefinition pricer = 26; CompositeOrderDefinition composite = 27; + SplitterOrderDefinition splitter = 29; DynamicOrderDefinition dynamic = 1000; } @@ -204,6 +205,12 @@ message CompositeOrderDefinition { uint32 algo_template_key = 1; CustomOrderDefinition definition = 2; } +message SplitterOrderDefinition { + uint32 max_order_qty = 1; + uint32 exposed_qty = 2; + // uint32 target_qty = 3; + CustomOrderDefinition child = 10; +} message DynamicOrderDefinition { string dynamic_order_type = 1; map order_params = 2; @@ -253,9 +260,10 @@ message OrderState { TriggerOrderState trigger = 21; CoverOrderState cover = 22; OffsetOrderState offset = 24; - QueueHolderState queue_holder = 30; OCOOrderState oco = 25; PricerOrderState pricer = 26; + QueueHolderState queue_holder = 30; + SplitterOrderState splitter = 31; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } @@ -334,6 +342,7 @@ message OCOOrderState {} message PricerOrderState {} message DynamicOrderState {} message QueueHolderState {} +message SplitterOrderState {} enum OrderStatus { diff --git a/src/lib/md/IntPrice.h b/src/lib/md/IntPrice.h index dfca4e73f..22822745b 100644 --- a/src/lib/md/IntPrice.h +++ b/src/lib/md/IntPrice.h @@ -34,9 +34,9 @@ class IntPrice { #endif public: - IntPrice() : _mantissa(0), _exponent(127) {/*nan*/} - IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {} - IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {} + constexpr IntPrice() : _mantissa(0), _exponent(127) {/*nan*/} + constexpr IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {} + constexpr IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {} bool operator==(const IntPrice& other) const { if(_exponent == other._exponent) { diff --git a/src/lib/md/PriceSize.h b/src/lib/md/PriceSize.h index e10a90365..3a72de6b1 100644 --- a/src/lib/md/PriceSize.h +++ b/src/lib/md/PriceSize.h @@ -77,7 +77,7 @@ public: extern PriceSize NO_MARKET; static_assert(sizeof(PriceSize) == 16, "PriceSize should be small"); -template +template PriceSize PriceSize_from_double(double price, Size size) { if (size == 0 || os::is_nan(price)) return NO_MARKET; return { IntPrice_from_double(price), size }; diff --git a/src/lib/orderman/events/OrderStateUpdateEvent.h b/src/lib/orderman/events/OrderStateUpdateEvent.h index b639c4549..9aeb47d81 100644 --- a/src/lib/orderman/events/OrderStateUpdateEvent.h +++ b/src/lib/orderman/events/OrderStateUpdateEvent.h @@ -153,6 +153,9 @@ class OrderStateUpdateEvent : public mercury::Event { case api::orderman::OrderState::kDynamic: result.mutable_dynamic(); break; + case api::orderman::OrderState::kSplitter: + result.mutable_splitter(); + break; case api::orderman::OrderState::CUSTOM_NOT_SET: break; } diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index 461ea669c..2f80825fe 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -19,6 +19,7 @@ #include "lib/orderman/orders/algo/algo_orders/TopAlgo.h" #include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h" #include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/SplitterAlgo.h" #include "lib/orderman/orders/book_algos/OCOOrder.h" #include "lib/orderman/orders/book_algos/OffsetOrder.h" #include "lib/orderman/orders/book_algos/PricerOrder.h" @@ -208,6 +209,10 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, case CustomOrderDefinition::kComposite: order_ptr.reset(new orderman::CompositeOrder()); break; + case CustomOrderDefinition::kSplitter: + order_ptr.reset( + new orderman::AlgoWrapperOrder(std::make_unique())); + break; case CustomOrderDefinition::CUSTOM_NOT_SET: bts_assert(false); break; diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp new file mode 100644 index 000000000..f7a2e6f2e --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -0,0 +1,219 @@ +#include "SplitterAlgo.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +// #include "google/btree/btree_map.h" + +#include +#include + +namespace bts { +namespace orderman { +namespace splitter { + +using namespace api::orderman; + +struct ChildOrderState { + OrderKey key; + ChildState state; + uint32_t remaining() const { return state.quantity.value() - state.total_fills.value(); } + uint32_t min_qty() const { return state.total_fills.value(); } +}; + +struct SplitterAlgo::Impl { + IAlgoServices* algo = nullptr; + ChildTemplate* child_template = nullptr; + + uint32_t clip = 0; + uint32_t exposed_qty = 0; + uint32_t target_qty = 0; + + bool is_terminated = false; + + std::vector orders; +}; + +SplitterAlgo::SplitterAlgo() : me(new Impl()) { } +SplitterAlgo::~SplitterAlgo() {} + +OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { + // Validate and store parameters + + auto& def = ibl.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_splitter()); + auto& splitter = def.custom_def().splitter(); + + me->clip = splitter.max_order_qty(); + me->exposed_qty = splitter.exposed_qty(); + if (me->clip <= 0 || me->exposed_qty <= 0) { + return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, + "max order quantity and exposed quantity must be greater than 0"}; + } + + // Create child template + auto child_def = def; + *child_def.mutable_custom_def() = splitter.child(); + me->child_template = &ibl.make_child_template(child_def); + + me->orders.reserve(me->exposed_qty / me->clip + 1); + + return {}; +} + +void SplitterAlgo::do_launch(IAlgoServices& ias) { + me->algo = &ias; + me->target_qty = ias.quantity().value(); + // launches initial child orders + sync_orders(); +} +void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) { + //todo +} +void SplitterAlgo::do_update(const api::orderman::Update& update) { + // Update # child orders + if (update.has_custom() && update.custom().has_splitter()) { + auto& splitter = update.custom().splitter(); + if (splitter.max_order_qty() > 0) { + me->clip = splitter.max_order_qty(); + } + me->exposed_qty = splitter.exposed_qty(); + } + me->target_qty = update.quantity(); + + sync_orders(); +} +void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { + for (auto& child : me->orders) { + if (child.key == key) { + child.state = state; + break; + } + } + + sync_orders(); +} + +struct MinMax { + uint32_t min, max; +}; + +// TEST case: +// total = 300, clip = 100 +// constraints: 50,50,50,50 +void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector& constraints, std::vector* results) { + bts_assert(results); + results->clear(); + + uint32_t irreducible = 0; + for (const auto& mm : constraints) irreducible += mm.min; + + auto citer = constraints.begin(); + while(total > 0) { + auto current = std::min(total, clip); + // fit to constraint + if (citer != constraints.end()) { + irreducible -= citer->min; + if (current > citer->max) current = citer->max; + if (current < citer->min) current = citer->min; + + // I can't be higher than (total - irreducible) + if (current > (total - irreducible)) current = (total-irreducible); + citer++; + } + results->push_back(Quantity{current}); + total -= current; + } +} + +void get_constraints(const std::vector& orders, std::vector* constraints) { + bts_assert(constraints); + for (const auto& order : orders) { + //Note: to maintain queue position set max to current quantity. This will cause more orders to be added to the end if size increases + constraints->push_back(MinMax { + order.state.total_fills.value(), + order.state.status == ChildStatus::WORKING ? + std::numeric_limits::max() : order.state.total_fills.value() + }); + } +} + +bool SplitterAlgo::is_complete() const { + uint32_t fills = 0; + for (const auto& o : me->orders) { fills += o.state.total_fills.value(); } + return fills >= me->target_qty; +} + +void SplitterAlgo::terminate() const { + algo().complete(); + //?? Do we need to clean up children? + for (const auto& o : me->orders) { + if (o.state.status == ChildStatus::WORKING) { + algo().cancel_child(o.key); + } + } + me->is_terminated = true; +} + +void SplitterAlgo::sync_orders() { + if (me->is_terminated) return; + if (is_complete()) { + terminate(); + return; + } + + std::sort(me->orders.begin(), me->orders.end(), [](auto& a, auto b) { return b.min_qty() < a.min_qty(); }); + std::vector constraints; + get_constraints(me->orders, &constraints); + + std::vector desired; + compute_desired_sizes(me->exposed_qty, me->clip, constraints, &desired); + + auto oiter = me->orders.begin(); + // iterate to the current number of orders, don't consider orders that we're adding + auto order_count = me->orders.size(); + auto price = algo().price(); + for (const auto& qty : desired) { + if (order_count == 0) { + // Ran out of existing orders, time to add more + auto key = algo().launch_child(*me->child_template, price, qty); + // Assume the order begins successfully. Hopefully we get back some sort of NACK otherwise + me->orders.push_back({ key, {} }); + me->orders.back().state.price = price; + me->orders.back().state.quantity = Quantity { qty }; + me->orders.back().state.status = ChildStatus::WORKING; + } + else { + // Check the current order's quantity and modify it to match desired qty + if (oiter->state.status == ChildStatus::WORKING) { + if (oiter->state.price != price || + oiter->state.quantity != qty) { + Update update; + update.set_quantity(qty.value()); + update.set_price(price.value()); + algo().update_child(oiter->key, update); + } + } + oiter++; + order_count -= 1; + } + } + // cancel any orders past desired + if (desired.size() < me->orders.size()) { + for (size_t x = desired.size(); x < me->orders.size(); ++x) { + auto& order= me->orders[x]; + if (order.state.status == ChildStatus::WORKING) { + order.state.status = ChildStatus::CANCELED; + algo().cancel_child(order.key); + } + } + } + +} + +IAlgoServices& SplitterAlgo::algo() const { bts_assert(me->algo); return *me->algo; } + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h new file mode 100644 index 000000000..ff2d86598 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -0,0 +1,32 @@ +#pragma once +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { +namespace splitter { + +class SplitterAlgo : public IAlgoOrder +{ + struct Impl; + std::unique_ptr me; + +public: + SplitterAlgo(); + ~SplitterAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_child_change(OrderKey, ChildState const &) override; + +private: + IAlgoServices& algo() const; + + void sync_orders(); + bool is_complete() const; + void terminate() const; +}; + + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp new file mode 100644 index 000000000..adb06283b --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -0,0 +1,526 @@ + +#include "catch/catch.hpp" +#include "../SplitterAlgo.h" +#include "AlgoTestDriver.h" +#include "lib/orderman/orderman_utils/Book.h" +#include +#include +#include + +using namespace std; +using namespace bts::orderman::splitter; +using namespace bts::api::orderman; + +namespace bts { +namespace orderman { +namespace test { + +/* + +- max/exposed, assert correct # of orders on the market +- completes on target_qty reached +- completes on target_qty exceeded +- send fills, check order state +- send fills, then change target_qty + + */ + +constexpr auto some_price = md::IntPrice { 128000, -3 }; + +using ExpectedCount = utils::ValueWrapper; +struct SimpleCase { + ExpectedCount expected_orders; + Quantity target; + SplitterOrderDefinition sod; +}; + +SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, uint32_t target) { + SimpleCase result; + result.expected_orders = expected; + result.target = Quantity {target}; + result.sod.set_max_order_qty(max_size); + result.sod.set_exposed_qty(exposed); + return result; +} + + +TEST_CASE("initial orders", "[algo][splitter]") { + std::vector cases; + cases.push_back(mkSimple(ExpectedCount {10},10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); + + for (const auto& tcase : cases) { + auto expected = tcase.expected_orders.value(); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = tcase.sod; + AlgoTestDriver state(def, some_price, tcase.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.child_orders().size() == expected); + REQUIRE(state.state() == AlgoState::RUNNING); + } +} + +TEST_CASE("completes on target", "[algo][splitter]") { + std::vector cases; + cases.push_back(mkSimple(ExpectedCount {3}, 30, 90, 50)); + cases.push_back(mkSimple(ExpectedCount {10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount {4}, 13, 50, 150)); + cases.push_back(mkSimple(ExpectedCount {12}, 13, 150, 50)); + + for (const auto& tcase : cases) { + auto expected = tcase.expected_orders.value(); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = tcase.sod; + AlgoTestDriver state(def, some_price, tcase.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.child_orders().size() == expected); + REQUIRE(state.state() == AlgoState::RUNNING); + + SECTION("Fill completely") { + auto orders = state.child_orders(); + // Fill the order + size_t filled = 0; + size_t iterations = 0; + while(state.state() == AlgoState::RUNNING) { + // loop to allow more orders to be added + ++iterations; + REQUIRE(iterations < 999); + for (const auto& order : orders) { + state.fill(order.key, Price (some_price.price()), order.cstate.quantity); + filled += order.cstate.quantity.value(); + if (filled >= tcase.target.value()) { + REQUIRE(state.state() == AlgoState::COMPLETED); + break; + } + else { + REQUIRE(state.state() == AlgoState::RUNNING); + } + } + } + } + SECTION("fill one by one") { + const auto& orders = state.child_orders(); + // Fill 1 per iteration. + // Algo should add more orders if not enough are visible initially + size_t filled = 0; + size_t iterations = 0; + while(state.state() == AlgoState::RUNNING) { + ++iterations; + REQUIRE(iterations < 999);//not making progress + for (const auto& order : orders) { + if (order.cstate.status == ChildStatus::WORKING) { + auto price = Price {some_price.price()}; + state.fill(order.key, price, Quantity {1}); + filled += 1; + // Fills should never cause new orders to get added + REQUIRE(state.child_orders().size() == expected); + if (filled >= tcase.target.value()) { + REQUIRE(state.state() == AlgoState::COMPLETED); + break; + } + else { + REQUIRE(state.state() == AlgoState::RUNNING); + } + } + } + } + } + } +} + +TEST_CASE("updates", "[algo][splitter]") { + // Start in state 0, update to state 1 + std::vector, SimpleCase>> cases; + + // reduce max order size, increase # orders + cases.push_back({ + mkSimple(ExpectedCount {10} /*order count*/, 10 /*max*/, 100 /*exposed*/, 50 /*target*/), + {}, + mkSimple(ExpectedCount {20}, 5, 100, 50) + }); + /// reduce max order size, keep # orders constant + cases.push_back({ + mkSimple(ExpectedCount {10}, 10, 100, 50), + {}, + mkSimple(ExpectedCount {10}, 5, 50, 50) + }); + cases.push_back({ + mkSimple(ExpectedCount {10}, 10, 100, 50), + {5,5}, + mkSimple(ExpectedCount {8}, 5, 50, 40) + }); + cases.push_back({ + mkSimple(ExpectedCount {10}, 10, 100, 50), + {5,5}, + mkSimple(ExpectedCount {18}, 5, 100, 50) + }); + + + for (const auto& tcase : cases) { + auto case_init = std::get<0>(tcase); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = case_init.sod; + AlgoTestDriver state(def, some_price, case_init.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == case_init.expected_orders.value()); + for (const auto& o : state.child_orders()) { + if (o.cstate.status == ChildStatus::WORKING) { + REQUIRE(o.cstate.quantity.value() <= case_init.sod.exposed_qty()); + } + } + + for(auto q : std::get<1>(tcase)) { + state.fill({}, Quantity {q}); + } + + Update update; + auto case_next = std::get<2>(tcase); + update.set_price(some_price.price()); + update.set_quantity(case_next.target.value()); + *def.mutable_custom_def()->mutable_splitter() = case_next.sod; + *update.mutable_custom() = def.custom_def(); + + state.do_update(update); + REQUIRE(state.state() == AlgoState::RUNNING); + size_t working = 0; + for (const auto& c : state.child_orders()) if (c.cstate.status == ChildStatus::WORKING) working += 1; + REQUIRE(working == case_next.expected_orders.value()); + for (const auto& o : state.child_orders()) { + if (o.cstate.status == ChildStatus::WORKING) { + REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty()); + } + } + } +} +TEST_CASE("Reduction with fills", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/50, 100/50, 100/50, 100/50 + // Change expose to 300 + // naive: 100, 100, 100 but we *can't* reduce the last to 0 + // expected: 100, 100, 50, 50 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + // Add fills + for (const auto& order : state.child_orders()) { + state.fill(order.key, {}, Quantity {50}); + } + + // Reduce exposed quantity + Update update; + update.set_quantity(300); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(300); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + REQUIRE(total == 300); + + // need this to be 100,100,50,50 not 100,100,100 + std::set actual, expected {100,100,50,50}; + for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + REQUIRE(actual == expected); +} + +TEST_CASE("Reduction with fills 2", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/35, 100/45, 100/55, 100/65 + // Change expose to 300 + // naive: 100, 100, 100 but we *can't* reduce the last to 0 + // expected: 100, 100, 65, 35 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + // Add fills + state.fill({}, Quantity {35}); + state.fill({}, Quantity {45}); + state.fill({}, Quantity {55}); + state.fill({}, Quantity {65}); + + // Reduce exposed quantity + Update update; + update.set_quantity(300); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(300); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + REQUIRE(total == 300); + + // need this to be 100,100,50,50 not 100,100,100 + std::set actual, expected {100,100,65,35}; + for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + REQUIRE(actual == expected); +} + +TEST_CASE("Reduction with fills 3", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/35, 100/45, 100/55, 100/65 + // Change expose to 201 + // naive: 100, 100, 1 but we *can't* reduce the last to 0 + // expected: 66, 55, 45, 35 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + // Add fills + state.fill({}, Quantity {35}); + state.fill({}, Quantity {45}); + state.fill({}, Quantity {55}); + state.fill({}, Quantity {65}); + + // Reduce exposed quantity + Update update; + update.set_quantity(201); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(201); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + REQUIRE(total == 201); + + std::set actual, expected {66,55,45,35}; + for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + REQUIRE(actual == expected); +} + +TEST_CASE("reduce to completion", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/100, 100/100, 100/0, 100/00 + // Change expose to 200 + // expected: algo transitions to copmleted state canceling 2 orders + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + // Add fills + state.fill({}, Quantity{100}); + state.fill({}, Quantity{100}); + + // Reduce exposed quantity + Update update; + update.set_quantity(200); + def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(200); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::COMPLETED); + size_t total = 0; + for (const auto& order : state.child_orders()) { + if (order.cstate.status == ChildStatus::WORKING) { + total += order.cstate.quantity.value(); + } + else { + total += order.cstate.total_fills.value(); + } + } + REQUIRE(total == 200); +} + +TEST_CASE("reduce to completion 2", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/100, 100/100, 100/0, 100/00 + // Change expose to 200 + // expected: algo transitions to copmleted state canceling 2 orders + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + // Add fills + state.fill({}, Quantity{100}); + state.fill({}, Quantity{50}); + state.fill({}, Quantity{50}); + + // Reduce exposed quantity + Update update; + update.set_quantity(200); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::COMPLETED); +} + +TEST_CASE("Async Fills", "[algo][splitter]") { + // OK, NOW it gets tricky because we're going to test the asyncronous stuff + + // Start with 4 orders size 20 outstanding (80 total exposed) + // Change exposure to 64 so one of the four will be reduced to 5 + // Before the change is confirmed fill the one that is reducing by 7 + // which would constitute an overfill + // Algo must reduce a second order to compensate + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(20); + splitter->set_exposed_qty(80); + AlgoTestDriver state(def, some_price, Quantity {80}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 20); } + + // Set up some instant fills + bool applied_fills = false; + state.on_child_changing([&](const auto&) { + if (applied_fills) return; + applied_fills = true; + // 7 fills on the last order which is has a size reduction in flight + state.fill(state.child_orders().rbegin()->key, {}, Quantity {7}); + }); + + Update update; + update.set_price(state.price().value()); + update.set_quantity(state.quantity().value()); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(65); + // This should trigger 1 order to be reduced to size 5 from 20 + // in the meantime we'll trigger fills of size 7 + // This results in an overfill for the 5 lot order + // And the + state.do_update(update); + + // expected result: + // 7 fills, exposed_qty 65 = 58 active + uint32_t active = 0; + for (const auto& o : state.child_orders()) active += o.active_qty(); + REQUIRE(active == 58); +} + +TEST_CASE("Increase then decrease", "[algo][splitter]") { + // Start with 3 orders of size 100 + // Increase the size to 400 so we launch a 4th order + // Then instantly send another update to 350 + + // NOTE: this test is not very good due to limitations in the test driver + // I'm leaving it in because it passes but if it starts failing in the future + // it's probably OK to disable it + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto* splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(300); + AlgoTestDriver state(def, some_price, Quantity {300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 3); + for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + + Update update; + update.set_price(state.price().value()); + update.set_quantity(state.quantity().value()); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(400); + + // Set up some instant fills + bool applied_second_update = false; + state.on_child_launching([&,update](const auto&) mutable { + if (!applied_second_update) { + applied_second_update = true; + update.mutable_custom()->mutable_splitter()->set_exposed_qty(350); + state.do_update(update); + } + }); + + state.do_update(update); + + // expected result: + // 350 exposed, NOT 450 + uint32_t quantity = 0; + for (const auto& o : state.child_orders()) quantity += o.active_qty(); + REQUIRE(quantity == 350); +} + + +}}}//namespace \ No newline at end of file From b8a9116ea6bedd4cc19b9d79b8c5b9ab7df86d97 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 1 Jul 2019 16:08:28 -0400 Subject: [PATCH 031/153] SP-30 Dorman part 1 --- .../orders/algo/algo_orders/DormanAlgo.cpp | 382 ++++++++++++++++++ .../orders/algo/algo_orders/DormanAlgo.h | 82 ++++ .../algo/algo_orders/tests/dorman_tests.cpp | 353 ++++++++++++++++ 3 files changed, 817 insertions(+) create mode 100644 src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h create mode 100644 src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp new file mode 100644 index 000000000..b1f31c717 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -0,0 +1,382 @@ +#include "DormanAlgo.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/utils/PBUtils.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" + + +namespace bts { +namespace orderman { +namespace dorman { + +using namespace api::orderman; + + +// Class which will send out multiple orders but act like one order +struct ChildOrderState { + Quantity size; + Quantity filled; +}; + +class MultiOrder { + IAlgoServices* _algo = nullptr; + uint32_t _clip = 0; + ChildTemplate* _template = nullptr; + + Quantity _qty = {}; + std::map _orders; + + public: + MultiOrder() {} + ~MultiOrder() { + if (_algo) { + for (const auto& pair : _orders) { + _algo->cancel_child(pair.first); + } + } + } + void do_before_launch(IBeforeLaunchAlgoServices& algo, uint32_t clip) { + _clip = clip == 0 ? std::numeric_limits::max() : clip; + auto def = algo.definition(); + + CustomOrderDefinition default_order; + default_order.mutable_limit(); + + *def.mutable_custom_def() = + get_if(def.custom_def().dynamic().order_params(), "Child") + .value_or(default_order); + _template = &algo.make_child_template(def); + } + + void set_algo_services(IAlgoServices& a) { _algo = &a; } + + void update(Price p, Quantity q) { + bts_assert(_algo != nullptr); + // This class does not handle delayed size changes + if (q < _qty) { + // cancel some orders + auto excess = _qty.value() - q.value(); + while(excess > 0) { + // cancel some orders + auto iter = _orders.begin(); + auto lsize = iter->second.size.value() - iter->second.filled.value(); + if (lsize > excess) { + // Reduce order size + Update update; + update.set_quantity(lsize - excess); + _algo->update_child(iter->first, update); + iter->second.size.value() -= excess; + excess = 0; + } + else { + excess -= lsize; + _algo->cancel_child(iter->first); + _orders.erase(iter); + } + } + _qty = q; + } + else if (q > _qty) { + // add more orders + auto shortfall = q.value() - _qty.value(); + while(shortfall > 0) { + auto order_size = Quantity { std::min(shortfall, _clip) }; + auto child_id = _algo->launch_child(*_template, p, order_size, true); + _orders.insert(std::make_pair(child_id, ChildOrderState { order_size, Quantity {0} })); + shortfall -= order_size.value(); + } + _qty = q; + } + } + + Quantity quantity() const { return _qty; } +}; + + +// +// Algo State +// + +// +// Algo Impl +// +struct FutureQty { + Quantity size; + TimeStamp when; +}; + +struct DormanAlgo::Impl { + IAlgoServices *algo = nullptr; + AlgoParams params; + api::Side side; + + std::optional next_size; + MultiOrder child; +}; + +DormanAlgo::DormanAlgo() : + me(new Impl()) +{ } +DormanAlgo::~DormanAlgo() { } + +// Not latency sensitive. Do expensive initialization here +OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& iblas) { + const auto& def = iblas.definition(); + // Validation + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_dynamic()); + auto params = to_dorman_params(def.custom_def().dynamic()); + if (auto r = std::get_if(¶ms)) { + return *r; + } + me->params = std::get(params); + me->side = def.side(); + if (me->side != api::Side::SIDE_BUY && me->side != api::Side::SIDE_SELL) { + return OrderOpResult { ORDER_ERROR_INVALID_ORDER_DEFINITON, "Invalid side" }; + } + me->child.do_before_launch(iblas, me->params.clip_limit); + + return {/*ok*/}; +} + +void DormanAlgo::do_launch(IAlgoServices& algo) { + me->algo = &algo; + me->child.set_algo_services(algo); + + algo.subscribe_book(SecurityKey(algo.security().id())); + algo.subscribe_timer(); +} + +void DormanAlgo::do_launch_attached(IAlgoServices& algo, OrderKey k, ChildState state) { + me->algo = &algo; + (void)k; + (void)state; +} +void DormanAlgo::do_update(const api::orderman::Update& update) { + LOG(OrderMan, Diag, "Dorman order {} receiving update: {}", + algo().order_key().value(), utils::pb::to_string(update)); + (void)update; +} + +void DormanAlgo::on_child_change(OrderKey k, const ChildState& st) { + (void)k; + (void)st; +} +void DormanAlgo::on_book(SecurityKey key, const Book& book) { + if (key.value() != me->algo->security().id()) { return; } + auto price = me->algo->price(); + auto tick = me->algo->security().contract_spec().low_tick(); + + // Determine available size on the market + auto& side = book.side(me->side); + uint32_t available = 0; + for (const auto& level : side) { + if (securities::test_price_equality(level.price(), price.value(), tick)) { + available = level.size(); + break; + } + } + + // Check the step_table + auto target = Quantity {0}; + if (available > 0) { + for (const auto& entry : me->params.steps.step_table) { + target = Quantity { std::get<1>(entry) }; + if (std::get<0>(entry) > available) { + break; + } + } + } + target = Quantity { std::min(target.value(), me->params.steps.maximum) }; + + // First on_book + if (me->child.quantity() == Quantity {0}) { + me->child.update(price, target); + } + // See if we need to adjust + else { + if (target < me->child.quantity()) { + // If target < size reduce immidiately + me->child.update(price, target); + me->next_size = std::nullopt; + } + else if (target > me->child.quantity()) { + // if target > size schedule increase + if (me->next_size) { + // already planning to increase + me->next_size.value().size = target; + } + else { + // no scheduled increase + me->next_size = { FutureQty { target, me->algo->get_time() + std::chrono::seconds(me->params.steps.trail_seconds) } }; + } + } + } +} +void DormanAlgo::on_timer() { + // Check for pending size changes + if (me->next_size && + me->algo->get_time() >= me->next_size.value().when) { + auto size = me->next_size.value().size; + me->child.update(me->algo->price(), size); + me->next_size = std::nullopt; + } +} + +IAlgoServices& DormanAlgo::algo() const { return *me->algo; } + +// +// Visibility for testing +// + +Quantity DormanAlgo::desired_size() const { return me->child.quantity(); } + +// +// +// Parameter parsing and validation +// +// + +Result bad_definition(const char* msg) { return {OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, msg}}; } + +Result to_dorman_params(const api::orderman::DynamicOrderDefinition& def) { + const auto& params = def.order_params(); + AlgoParams res; + + // + // Step Table + auto step_table = get_if(params, "Step Table"); + if (!step_table) return bad_definition("Missing 'Step Table'"); + for (const auto& element : step_table.value().element()) { + auto row = get_if(element); + if (!row) return bad_definition("Invalid row type in step table."); + auto mk = get_if(row.value().field(), "Market Quantity"); + if (!mk) return bad_definition("Step table row missing 'Market Quantity'"); + auto ok = get_if(row.value().field(), "Order Quantity"); + if (!ok) return bad_definition("Step table row missing 'Order Quantity'"); + res.steps.step_table.push_back({mk.value(), ok.value()}); + } + res.steps.maximum = get_if(params, "Max Size").value_or(-1); + res.steps.trail_seconds = get_if(params, "Trail Seconds").value_or(0); + + // + // Clip limit + res.clip_limit = get_if(params, "Clip Size").value_or(0); + + // + // scratch + auto enable_scratch = get_if(params, "Enable Scratch"); + if (enable_scratch && enable_scratch.value()) { + auto overfill = get_if(params, "Max Overfill"); + if (!overfill) return bad_definition("Missing 'Max Overfill'"); + res.scratch = { overfill.value() }; + } + + // + // profit orders + auto enable_profit_orders = get_if(params, "Profit Orders"); + if (enable_profit_orders && enable_profit_orders.value()) { + auto ticks = get_if(params, "Ticks Away"); + if (!ticks) return bad_definition("Missing 'Ticks Away'"); + res.profit_orders = { ticks.value() }; + } + + // + // trade accumulation + auto trade_acc = get_if(params, "Trade Accumulation"); + if (trade_acc) { + auto ta = trade_acc.value(); + TradeAccumulationParams tap; + + auto contracts = get_if(ta.field(), "Contracts"); + if (!contracts) return bad_definition("Missing 'Trade Accumulation # Contracts'"); + tap.contracts = contracts.value(); + + auto interval = get_if(ta.field(), "Interval (seconds)"); + if (!interval) return bad_definition("Missing 'Trade Accumulation Interval'"); + tap.seconds = interval.value(); + + auto sleep = get_if(ta.field(), "Sleep (seconds)"); + if (!sleep) return bad_definition("Missing 'Trade Accumulation Sleep'"); + tap.sleep_seconds = sleep.value(); + + res.trade_accumulation = {tap}; + } + + return {res}; +} + +api::orderman::DynamicOrderDefinition to_dynamic_order_params(const AlgoParams& input) { + api::orderman::DynamicOrderDefinition result; + result.set_dynamic_order_type("Dorman"); + auto& map = *result.mutable_order_params(); + // step table + + // array of maps + // rows are "de-normalized" maps with column headers as keys + // + // example: + // map["Step Table"] = + // dynamic_array({ + // dynamic_map({ + // {"Market Quantity", dynamic_parameter(500)}, + // {"Order Quantity", dynamic_parameter(250)} + // }), + // dynamic_map({ + // {"Market Quantity", dynamic_parameter(1000)}, + // {"Order Quantity", dynamic_parameter(400)} + // }), + // dynamic_map({ + // {"Market Quantity", dynamic_parameter(1000)}, + // {"Order Quantity", dynamic_parameter(1000)} + // }) + // }); + std::vector table; + std::transform( + input.steps.step_table.begin(), + input.steps.step_table.end(), + std::back_inserter(table), + [](const auto& item) { + return dynamic_map({ + { "Market Quantity", dynamic_parameter(std::get<0>(item)) }, + { "Order Quantity", dynamic_parameter(std::get<1>(item)) } + }); + }); + map["Step Table"] = dynamic_array(table); + map["Max Size"] = dynamic_parameter(input.steps.maximum); + map["Trail Seconds"] = dynamic_parameter(input.steps.trail_seconds); + map["Clip Size"] = dynamic_parameter(input.clip_limit); + + // scratch + map["Enable Scratch"] = dynamic_parameter((bool)(input.scratch)); + if (input.scratch) { + map["Max Overfill"] = dynamic_parameter(input.scratch.value().max_overfill); + } + // + // profit orders + map["Profit Orders"] = dynamic_parameter((bool)input.profit_orders); + if (input.profit_orders) { + map["Ticks Away"] = dynamic_parameter(input.profit_orders.value().ticks_away); + } + + // + // trade accumulator + if (input.trade_accumulation) { + auto& tap = input.trade_accumulation.value(); + map["Trade Accumulation"] = + dynamic_map({ + { "Contracts", dynamic_parameter(tap.contracts) }, + { "Interval (seconds)", dynamic_parameter(tap.seconds) }, + { "Sleep (seconds)", dynamic_parameter(tap.sleep_seconds) } + }); + } + + return result; +} + + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h new file mode 100644 index 000000000..9d0628b76 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h @@ -0,0 +1,82 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" +#include +#include +#include +#include + +namespace bts { +namespace orderman { +namespace dorman { + +// +// Algo parameters +// + +// This defines the market quantity vs the order quantity +struct StepParams { + std::vector> step_table; + // This is the cap on the maximum quantity, + // even though the step table can allow for higher quantities (depending on the + // clip size this could be divided across multiple exchange orders) + uint32_t maximum; + // If the market increases don't follow until it has stayed high for trail_seconds; + uint32_t trail_seconds; +}; + +// If the number of accumulated fills (net of profit orders) touches or goes beyond +// this, the fills will be scratched by entering opposite orders at the fill price +struct ScratchParams { + uint32_t max_overfill; +}; +// If this is enabled, then as fills accumulate, profit GTC orders are +// immediately sent N ticks away on the opposite side. +struct ProfitOrderParams { + uint32_t ticks_away; +}; +// If N contracts trade in T seconds, then the algo is paused (ie child exchange order +// is/are cancelled) for X seconds. Once the condition clears order is reentered. +struct TradeAccumulationParams { + uint32_t contracts; + uint32_t seconds; + uint32_t sleep_seconds; +}; +struct AlgoParams { + StepParams steps; + uint32_t clip_limit; + std::optional scratch; + std::optional profit_orders; + std::optional trade_accumulation; +}; + + +class DormanAlgo : public IAlgoOrder { + struct Impl; + std::unique_ptr me; + IAlgoServices& algo() const; + +public: + DormanAlgo(); + ~DormanAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_child_change(OrderKey, ChildState const &) override; + void on_book(SecurityKey, const Book&) override; + void on_timer() override; + + Quantity desired_size() const; + +}; + +template +using Result = std::variant; + +Result to_dorman_params(const api::orderman::DynamicOrderDefinition&); +// std::optional to_dorman_params(const api::orderman::DynamicOrderDefinition&); +api::orderman::DynamicOrderDefinition to_dynamic_order_params(const AlgoParams&); + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp new file mode 100644 index 000000000..55a8ad308 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -0,0 +1,353 @@ +#include "catch/catch.hpp" +#include "../DormanAlgo.h" +#include "AlgoTestDriver.h" +#include "lib/orderman/orderman_utils/Book.h" +#include +#include + +using namespace std; +using namespace bts::orderman::dorman; + +namespace bts { +namespace orderman { +namespace test { + +/* STATE + +struct OrderState { + Price desired_price; + Quantity desired_size; +} + +*/ + +/* TEST ASPECTS + +order_qty vs market quantity +order_qty reduce when market changes +order_qty increase (w/ delay) when market changes +order_qty maximum + +overfill [under/over] max with auto-scratch [on/off] +Profit orders + - test that orders are sent + - test that away order are at the right price + +Trade Accumulation + - accumulator goes [on/off] + - Dorman goes to sleep when it goes 'on' and wakes up when it goes 'off' + +Market Moves + - Available size increases + - Available size decreases + +clip size pseudo-order + - change clip size + - immidiate effect? + - don't have to cancel if clip size goes down? + - change order size [up/down] + - suspend/resume + - cancels all + + + */ + +void check_round_trip(const AlgoParams& input) { + auto d1 = to_dynamic_order_params(input); + auto ap1 = to_dorman_params(d1); + if (std::holds_alternative(ap1)) { + std::cout << std::get(ap1).error_msg; + } + REQUIRE(std::holds_alternative(ap1)); + auto d2 = to_dynamic_order_params(std::get(ap1)); + + REQUIRE(d2.DebugString() == d1.DebugString()); +} + +TEST_CASE("dorman_round_trip_params", "[algo][dorman]") { + + SECTION("a") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} }, + 1024, 5 + }; + check_round_trip(input); + } + + SECTION("b") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} + }, + 1024, 5 + }; + input.scratch = { 500 }; + input.profit_orders = { 42 }; + input.clip_limit = 220; + check_round_trip(input); + } + + SECTION("c") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} + }, + 1024, 5 + }; + input.profit_orders = { 42 }; + input.trade_accumulation = { + 100, 200, 300 + }; + check_round_trip(input); + } + +} + +struct ExpectedSize { + uint32_t input; + uint32_t expected; +}; +constexpr auto some_price = md::IntPrice { 128000, -3 }; + +size_t count_child_size(const std::vector& child_orders) { + return std::accumulate( + child_orders.begin(), + child_orders.end(), 0, + [](uint32_t acc, const auto& child) { + if (child.cstate.status != ChildStatus::CANCELED) { + // count working+completed + return acc + child.cstate.quantity.value(); + } + else return acc; + }); +} + + +TEST_CASE("dorman_order_size", "[algo][dorman]") { + // Repeate the test for a variety of clip sizes + for (auto clip : { 100, 20, 900, 1 }) { + + // create a table of various sizes + // send market state to the algo + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 400}, + {2000, 600}, + {3500, 750}, + {10000, 999} + }; + input.steps.maximum = 800; + input.steps.trail_seconds = 5; + input.clip_limit = clip; + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + + + SECTION("initial size vs market") { + std::vector test_sizes { + { 10, 250 }, + { 200, 250 }, + { 250, 250 }, + { 500, 400 }, + { 999, 400 }, + { 2500, 750 }, + { 9999, 800 } + }; + + for (auto& s : test_sizes) + { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + + // order size is driven by the available market size + book.bid()[0] = { some_price, s.input }; + state.update_book(book); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().desired_size().value() == s.expected); + REQUIRE( + s.expected == count_child_size(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); + } + } + } + SECTION("Reduce qty on market shrink (immidiate)") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().desired_size() == Quantity(400)); + + // Check that orders were created + REQUIRE( + 400 == count_child_size(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); + } + + // Reduce market + book.bid()[0] = { some_price, 405 }; + state.update_book(book); + REQUIRE(state.algo().desired_size() == Quantity(250)); + REQUIRE( + 250 == count_child_size(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); + } + } + + SECTION("Increase qty w/ delay when market grows") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().desired_size() == Quantity(400)); + + // Check that orders were created + REQUIRE( + 400 == count_child_size(state.child_orders())); + + // INCREASE market + book.bid()[0] = { some_price, 1001 }; + state.update_book(book); + // NOTE: that the orders do not follow the market instantly + // so this is still 400 + REQUIRE( + 400 == count_child_size(state.child_orders())); + + // Still 400 after the timer ticks but not enough time has passed + state.advance_time(std::chrono::seconds()); + REQUIRE( + 400 == count_child_size(state.child_orders())); + + state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); + REQUIRE( + 600 == count_child_size(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); + } + + } + SECTION("Respect maximum") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 10505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + + REQUIRE(state.algo().desired_size() == Quantity(input.steps.maximum)); + + // Check that orders were created + REQUIRE( + input.steps.maximum == count_child_size(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); + } + } + } +} + +TEST_CASE("Dorman fills", "[algo][dorman]") { + + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + SECTION("complete when filled") { + // Send out some orders + // Fill them + // Expect order state to become finished + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 501 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().desired_size() == Quantity(450)); + + for (const auto& order : state.child_orders()) { + state.fill(order.key, price, order.cstate.quantity); + } + + REQUIRE(state.state() == AlgoState::COMPLETED); + } + + // Test cases for transitions w/ fills + // + + // target = 500 + // send 5 orders of size 100 + // market move, new target = 450 + // reduce order #5 to 50 + // Before we confirm the move + // - fill #4 for 80 + // :: algo needs to shed another 30 Quantity + + // ^ same but fill #2 for 80 + + + /* + NOTES: + vector = f(fills, target, clip) + => feed into diff + */ +} + +TEST_CASE("Dorman overfill", "[algo][dorman]") { + SECTION("auto-scratch OFF") { + } + SECTION("auto-scratch ON") { + } +} + +TEST_CASE("Dorman profit-orders", "[algo][dorman]") { + SECTION("profit-orders OFF") { + // Profit orders are not sent + } + SECTION("profit-orders ON") { + // profit orders are sent at the right price + } +} + +TEST_CASE("Dorman trade accumulation", "[algo][dorman]") { + SECTION("exceed limit") { + // Profit orders are not sent + } + SECTION("return to normal") { + // profit orders are sent at the right price + } +} + + +}}}//namespace + From 1104f4db1d5293c04bd83ece42a7178fe8f970eb Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 2 Jul 2019 09:52:24 -0400 Subject: [PATCH 032/153] SP-87 Reject attach in before_launch --- src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index f7a2e6f2e..786e2ad2d 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -61,6 +61,11 @@ OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { me->orders.reserve(me->exposed_qty / me->clip + 1); + if (ibl.is_attaching()) { + return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED, + "Splitter does not support attach."}; + } + return {}; } @@ -71,7 +76,8 @@ void SplitterAlgo::do_launch(IAlgoServices& ias) { sync_orders(); } void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) { - //todo + // rejected in before_launch + bts_assert(false); } void SplitterAlgo::do_update(const api::orderman::Update& update) { // Update # child orders From ac90a2131483454575f12654ecf928dc8fe0c782 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 2 Jul 2019 11:43:36 -0400 Subject: [PATCH 033/153] Clang build fixes. --- src/lib/CMakeDeps.cmake | 2 +- src/lib/md/relay/RelayRecorder.cpp | 2 +- .../orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/CMakeDeps.cmake b/src/lib/CMakeDeps.cmake index bef6d7f85..cad2d5f8e 100644 --- a/src/lib/CMakeDeps.cmake +++ b/src/lib/CMakeDeps.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;orderman/limiter/tests/limit_tests.cpp;orderman/order_factory/DynamicOrderRegistry.cpp;orderman/order_factory/OrderFactory.cpp;orderman/order_router/OrderRouter.cpp;orderman/orderman_utils/fix_order_codec.cpp;orderman/orderman_utils/orderman_utils.cpp;orderman/orders/CompositeOrder.cpp;orderman/orders/DynamicOrder.cpp;orderman/orders/NativeOrder.cpp;orderman/orders/Order.cpp;orderman/orders/TimeBoxOrder.cpp;orderman/orders/TriggerOrder.cpp;orderman/orders/algo/AlgoWrapperOrder.cpp;orderman/orders/algo/IAlgoOrder.cpp;orderman/orders/algo/IAlgoServices.cpp;orderman/orders/algo/IBeforeLaunchAlgoServices.cpp;orderman/orders/algo/algo_orders/CoverAlgo.cpp;orderman/orders/algo/algo_orders/IcebergAlgo.cpp;orderman/orders/algo/algo_orders/JoinAlgo.cpp;orderman/orders/algo/algo_orders/LeanAlgo.cpp;orderman/orders/algo/algo_orders/TopAlgo.cpp;orderman/orders/book_algos/BookAlgo.cpp;orderman/orders/book_algos/BookOrderBase.cpp;orderman/orders/book_algos/OCOOrder.cpp;orderman/orders/book_algos/OffsetOrder.cpp;orderman/orders/book_algos/PricerOrder.cpp;orderman/orders/queue_holder/QueueHolderOrder.cpp;orderman/orders/queue_holder/state_workers/QHOEndStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOStartStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForCancelAllStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp;orderman/orders/spreader_orders/SpreaderOrder.cpp;orderman/orders/spreader_orders/spreader/LegFillState.cpp;orderman/orders/spreader_orders/spreader/LegPropertyData.cpp;orderman/orders/spreader_orders/spreader/QuoteLegPriceQuantityState.cpp;orderman/orders/spreader_orders/spreader/SingleFillHedger.cpp;orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp;orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp;orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.cpp;orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp;orderman/orders/spreader_orders/spreader/SpreaderUtils.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderServices.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.cpp;orderman/orders/spreader_orders/spreader/hedger/HedgerManager.cpp;orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSet.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSetHistory.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForIOCWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForInitialDataStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/orders/spreader_orders/tests/spreader_tests.cpp;orderman/server/OrderServiceServer.cpp;orderman/tests/fat_wrapper_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/persistence_utils.cpp;persistence/tests/PersistenceServiceTests.cpp;plugin/SparkServices.cpp;plugin/plugin.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;tracing/PerfLog.cpp;tracing/bts_trace.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;orderman/limiter/tests/limit_tests.cpp;orderman/order_factory/DynamicOrderRegistry.cpp;orderman/order_factory/OrderFactory.cpp;orderman/order_router/OrderRouter.cpp;orderman/orderman_utils/fix_order_codec.cpp;orderman/orderman_utils/orderman_utils.cpp;orderman/orders/CompositeOrder.cpp;orderman/orders/DynamicOrder.cpp;orderman/orders/NativeOrder.cpp;orderman/orders/Order.cpp;orderman/orders/TimeBoxOrder.cpp;orderman/orders/TriggerOrder.cpp;orderman/orders/algo/AlgoWrapperOrder.cpp;orderman/orders/algo/IAlgoOrder.cpp;orderman/orders/algo/IAlgoServices.cpp;orderman/orders/algo/IBeforeLaunchAlgoServices.cpp;orderman/orders/algo/algo_orders/CoverAlgo.cpp;orderman/orders/algo/algo_orders/IcebergAlgo.cpp;orderman/orders/algo/algo_orders/JoinAlgo.cpp;orderman/orders/algo/algo_orders/LeanAlgo.cpp;orderman/orders/algo/algo_orders/SplitterAlgo.cpp;orderman/orders/algo/algo_orders/TopAlgo.cpp;orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp;orderman/orders/algo/algo_orders/tests/splitter_tests.cpp;orderman/orders/book_algos/BookAlgo.cpp;orderman/orders/book_algos/BookOrderBase.cpp;orderman/orders/book_algos/OCOOrder.cpp;orderman/orders/book_algos/OffsetOrder.cpp;orderman/orders/book_algos/PricerOrder.cpp;orderman/orders/queue_holder/QueueHolderOrder.cpp;orderman/orders/queue_holder/state_workers/QHOEndStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOStartStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForCancelAllStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp;orderman/orders/spreader_orders/SpreaderOrder.cpp;orderman/orders/spreader_orders/spreader/LegFillState.cpp;orderman/orders/spreader_orders/spreader/LegPropertyData.cpp;orderman/orders/spreader_orders/spreader/QuoteLegPriceQuantityState.cpp;orderman/orders/spreader_orders/spreader/SingleFillHedger.cpp;orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp;orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp;orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.cpp;orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp;orderman/orders/spreader_orders/spreader/SpreaderUtils.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderServices.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.cpp;orderman/orders/spreader_orders/spreader/hedger/HedgerManager.cpp;orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSet.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSetHistory.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForIOCWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForInitialDataStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/orders/spreader_orders/tests/spreader_tests.cpp;orderman/server/OrderServiceServer.cpp;orderman/tests/fat_wrapper_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/persistence_utils.cpp;persistence/tests/PersistenceServiceTests.cpp;plugin/SparkServices.cpp;plugin/plugin.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;tracing/PerfLog.cpp;tracing/bts_trace.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/md/relay/RelayRecorder.cpp b/src/lib/md/relay/RelayRecorder.cpp index a15652138..f443c0926 100644 --- a/src/lib/md/relay/RelayRecorder.cpp +++ b/src/lib/md/relay/RelayRecorder.cpp @@ -208,7 +208,7 @@ struct SecurityExtractor : public IRelayWriter { SecurityExtractor(std::function& cb, size_t count) : _count(count), _cb(cb), _keys() { } ~SecurityExtractor() override {}; - void write(const co2::MDReply& data, bool*) { + void write(const co2::MDReply& data, bool*) override { for (auto const &ch : data.channels()) { for (auto const &cr : ch.updates()) { if (cr.has_security() && _keys.find(cr.security().id()) == _keys.end()) { diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index 2791ac953..c2997c288 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -31,6 +31,7 @@ const std::map& get_some_securities() { struct RecursionCounter { size_t depth = 0; virtual void flush() = 0; + virtual ~RecursionCounter() {} }; struct RecursionGuard { RecursionGuard(RecursionCounter& c) : counter(c) { @@ -371,4 +372,4 @@ void AlgoTestDriverBase::complete() { void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } void AlgoTestDriverBase::update_fill_state(Quantity, Price) { } -}}}//namespace \ No newline at end of file +}}}//namespace From 8248bd7db8c242b51518f7015e2ac9b1fb3d86a9 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 2 Jul 2019 13:26:11 -0400 Subject: [PATCH 034/153] Add descriptive comment to splitteralgo --- .../orders/algo/algo_orders/SplitterAlgo.h | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h index ff2d86598..ca293856c 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -5,6 +5,28 @@ namespace bts { namespace orderman { namespace splitter { +/* + SplitterAlgo + + Parameters: + max_order_qty + -- Clip size. No single order can exceed this quantity. + exposed_qty + -- The total quantity we want visible on the exchange. + -- This will typically be higher than the quantity which helps capture fills in pro-rata algorithms + price, quantity + -- Common to all orders. + -- Splitter is filled when it reaches (or exceeds) quantity not exposed_qty + + SplitterAlgo acts as a single order to its parent but actually divides the total exposure + by the clip size and working several orders at once. + - It completes when the quantity is matched or exceeded. + - It tries to maximize the size of the orders and it may sacrifice queue position to do that. + - It reacts to fills and parameter changes and adjusts it's children appropriately. + - See splitter_tests.cpp for examples + - Future work: By constraining maximum size we can force this algorithm to maintain queue position. + +*/ class SplitterAlgo : public IAlgoOrder { struct Impl; From cdabf3b0233dd53e9d0698b604acf099c1ab9151 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 2 Jul 2019 13:34:30 -0400 Subject: [PATCH 035/153] SP-87 - Report fills to parent --- .../orderman/orders/algo/algo_orders/SplitterAlgo.cpp | 9 ++++++++- .../orders/algo/algo_orders/tests/AlgoTestDriver.cpp | 9 ++++++--- .../orders/algo/algo_orders/tests/splitter_tests.cpp | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index 786e2ad2d..761604888 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -5,7 +5,6 @@ #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orderman_utils/orderman_utils.h" #include "lib/orderman/orders/algo/IAlgoServices.h" -// #include "google/btree/btree_map.h" #include #include @@ -32,6 +31,8 @@ struct SplitterAlgo::Impl { uint32_t target_qty = 0; bool is_terminated = false; + Quantity total_fills = {}; + Price last_fill_price = {}; std::vector orders; }; @@ -95,6 +96,12 @@ void SplitterAlgo::do_update(const api::orderman::Update& update) { void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { for (auto& child : me->orders) { if (child.key == key) { + auto delta = state.total_fills.value() - child.state.total_fills.value(); + if (delta != 0) { + me->total_fills = Quantity { me->total_fills.value() + delta }; + me->last_fill_price = state.last_price; + algo().update_fill_state(me->total_fills, me->last_fill_price); + } child.state = state; break; } diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index c2997c288..bee71af2f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -55,6 +55,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter { TimeStamp now; api::Security security; OrderKey order_key, parent_key; + std::tuple fill_state; AlgoState state; std::vector orders; @@ -276,7 +277,9 @@ TimeStamp AlgoTestDriverBase::get_time() const { return me->now; } Price AlgoTestDriverBase::price() const { return me->price; } Quantity AlgoTestDriverBase::quantity() const { return me->size; } -Quantity AlgoTestDriverBase::total_fills() const { return Quantity(0); } +Quantity AlgoTestDriverBase::total_fills() const { + return std::get(me->fill_state); +} void AlgoTestDriverBase::subscribe_book(SecurityKey key){ me->book_subs[key] = true; @@ -370,6 +373,6 @@ void AlgoTestDriverBase::complete() { me->state = AlgoState::COMPLETED; } void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } -void AlgoTestDriverBase::update_fill_state(Quantity, Price) { } +void AlgoTestDriverBase::update_fill_state(Quantity q, Price p) { me->fill_state = { q, p }; } -}}}//namespace +}}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp index adb06283b..46cce7292 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -99,6 +99,7 @@ TEST_CASE("completes on target", "[algo][splitter]") { for (const auto& order : orders) { state.fill(order.key, Price (some_price.price()), order.cstate.quantity); filled += order.cstate.quantity.value(); + REQUIRE(filled == state.total_fills().value()); if (filled >= tcase.target.value()) { REQUIRE(state.state() == AlgoState::COMPLETED); break; @@ -123,6 +124,7 @@ TEST_CASE("completes on target", "[algo][splitter]") { auto price = Price {some_price.price()}; state.fill(order.key, price, Quantity {1}); filled += 1; + REQUIRE(filled == state.total_fills().value()); // Fills should never cause new orders to get added REQUIRE(state.child_orders().size() == expected); if (filled >= tcase.target.value()) { @@ -184,10 +186,14 @@ TEST_CASE("updates", "[algo][splitter]") { } } + uint32_t fills = 0; for(auto q : std::get<1>(tcase)) { state.fill({}, Quantity {q}); + fills += q; } + REQUIRE(state.total_fills().value() == fills); + Update update; auto case_next = std::get<2>(tcase); update.set_price(some_price.price()); @@ -251,6 +257,8 @@ TEST_CASE("Reduction with fills", "[algo][splitter]") { std::set actual, expected {100,100,50,50}; for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } REQUIRE(actual == expected); + + REQUIRE(state.total_fills().value() == 200); } TEST_CASE("Reduction with fills 2", "[algo][splitter]") { From 4e6e97d90b9d74ab136ed75a229da73f81ff8b39 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 3 Jul 2019 13:40:41 -0400 Subject: [PATCH 036/153] Added unit test for splitter compute_desired_sizes. --- .../orders/algo/algo_orders/SplitterAlgo.cpp | 4 - .../orders/algo/algo_orders/SplitterAlgo.h | 4 + .../algo/algo_orders/tests/splitter_tests.cpp | 306 +++++++++++------- 3 files changed, 190 insertions(+), 124 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index 786e2ad2d..f9d79c2db 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -103,10 +103,6 @@ void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { sync_orders(); } -struct MinMax { - uint32_t min, max; -}; - // TEST case: // total = 300, clip = 100 // constraints: 50,50,50,50 diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h index ff2d86598..927bd87f3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -28,5 +28,9 @@ private: void terminate() const; }; +// exposed for unit testing purposes. +struct MinMax {uint32_t min, max;}; +void compute_desired_sizes(uint32_t , uint32_t , + const std::vector& , std::vector* ); }}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp index adb06283b..5139d971a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -1,11 +1,13 @@ -#include "catch/catch.hpp" #include "../SplitterAlgo.h" #include "AlgoTestDriver.h" +#include "catch/catch.hpp" #include "lib/orderman/orderman_utils/Book.h" +#include #include -#include #include +#include +#include "lib/utils/bts_chrono.h" using namespace std; using namespace bts::orderman::splitter; @@ -25,33 +27,75 @@ namespace test { */ -constexpr auto some_price = md::IntPrice { 128000, -3 }; +constexpr auto some_price = md::IntPrice{128000, -3}; -using ExpectedCount = utils::ValueWrapper; +using ExpectedCount = utils::ValueWrapper; struct SimpleCase { ExpectedCount expected_orders; Quantity target; SplitterOrderDefinition sod; }; -SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, uint32_t target) { +SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, + uint32_t target) { SimpleCase result; result.expected_orders = expected; - result.target = Quantity {target}; + result.target = Quantity{target}; result.sod.set_max_order_qty(max_size); result.sod.set_exposed_qty(exposed); return result; } +TEST_CASE("compute_desired_sizes fuzzing", "[algo][splitter]") { + srand((unsigned)time(0)); + + auto start = Timer::now(); + + while (sec_diff(Timer::now(),start) < 5) { + uint32_t total = rand() % 100 + 1; + uint32_t clip = rand() % 4 + 1; + std::vector constraints(rand() % 10); + std::vector result; + + uint32_t all_fills = 0; + for (auto &mm : constraints) { + uint32_t order_working = rand() % 2; + uint32_t fills = rand() % 2 * clip; + all_fills += fills; + mm = {fills, order_working ? fills + rand() % clip + 1 : fills}; + } + if (all_fills < total) { + compute_desired_sizes(total, clip, constraints, &result); + uint32_t exposed_qty = 0; + for (auto q : result) + exposed_qty += q.value(); + + bool clip_size_honored = true; + for (auto q : result) + clip_size_honored = clip_size_honored && (q.value() <= clip); + + if (exposed_qty != total || !clip_size_honored) { + std::cout << "total: " << total << "\n" + << "clip: " << clip << "\n{ "; + + for (auto mm : constraints) + std::cout << "{" << mm.min << "," << mm.max << "},"; + std::cout << " }\n"; + } + REQUIRE(clip_size_honored); + REQUIRE(exposed_qty == total); + } + } +} TEST_CASE("initial orders", "[algo][splitter]") { std::vector cases; - cases.push_back(mkSimple(ExpectedCount {10},10, 100, 50)); - cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto expected = tcase.expected_orders.value(); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -67,15 +111,15 @@ TEST_CASE("initial orders", "[algo][splitter]") { TEST_CASE("completes on target", "[algo][splitter]") { std::vector cases; - cases.push_back(mkSimple(ExpectedCount {3}, 30, 90, 50)); - cases.push_back(mkSimple(ExpectedCount {10}, 10, 100, 50)); - cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); - cases.push_back(mkSimple(ExpectedCount {4}, 13, 50, 150)); - cases.push_back(mkSimple(ExpectedCount {12}, 13, 150, 50)); + cases.push_back(mkSimple(ExpectedCount{3}, 30, 90, 50)); + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{4}, 13, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{12}, 13, 150, 50)); - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto expected = tcase.expected_orders.value(); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -88,48 +132,47 @@ TEST_CASE("completes on target", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); SECTION("Fill completely") { - auto orders = state.child_orders(); + auto orders = state.child_orders(); // Fill the order size_t filled = 0; size_t iterations = 0; - while(state.state() == AlgoState::RUNNING) { + while (state.state() == AlgoState::RUNNING) { // loop to allow more orders to be added ++iterations; REQUIRE(iterations < 999); - for (const auto& order : orders) { - state.fill(order.key, Price (some_price.price()), order.cstate.quantity); + for (const auto &order : orders) { + state.fill(order.key, Price(some_price.price()), + order.cstate.quantity); filled += order.cstate.quantity.value(); if (filled >= tcase.target.value()) { REQUIRE(state.state() == AlgoState::COMPLETED); break; - } - else { + } else { REQUIRE(state.state() == AlgoState::RUNNING); } } } } SECTION("fill one by one") { - const auto& orders = state.child_orders(); + const auto &orders = state.child_orders(); // Fill 1 per iteration. // Algo should add more orders if not enough are visible initially size_t filled = 0; size_t iterations = 0; - while(state.state() == AlgoState::RUNNING) { + while (state.state() == AlgoState::RUNNING) { ++iterations; - REQUIRE(iterations < 999);//not making progress - for (const auto& order : orders) { + REQUIRE(iterations < 999); // not making progress + for (const auto &order : orders) { if (order.cstate.status == ChildStatus::WORKING) { - auto price = Price {some_price.price()}; - state.fill(order.key, price, Quantity {1}); + auto price = Price{some_price.price()}; + state.fill(order.key, price, Quantity{1}); filled += 1; // Fills should never cause new orders to get added REQUIRE(state.child_orders().size() == expected); if (filled >= tcase.target.value()) { REQUIRE(state.state() == AlgoState::COMPLETED); break; - } - else { + } else { REQUIRE(state.state() == AlgoState::RUNNING); } } @@ -144,30 +187,22 @@ TEST_CASE("updates", "[algo][splitter]") { std::vector, SimpleCase>> cases; // reduce max order size, increase # orders - cases.push_back({ - mkSimple(ExpectedCount {10} /*order count*/, 10 /*max*/, 100 /*exposed*/, 50 /*target*/), - {}, - mkSimple(ExpectedCount {20}, 5, 100, 50) - }); + cases.push_back({mkSimple(ExpectedCount{10} /*order count*/, 10 /*max*/, + 100 /*exposed*/, 50 /*target*/), + {}, + mkSimple(ExpectedCount{20}, 5, 100, 50)}); /// reduce max order size, keep # orders constant - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {}, - mkSimple(ExpectedCount {10}, 5, 50, 50) - }); - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {5,5}, - mkSimple(ExpectedCount {8}, 5, 50, 40) - }); - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {5,5}, - mkSimple(ExpectedCount {18}, 5, 100, 50) - }); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {}, + mkSimple(ExpectedCount{10}, 5, 50, 50)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{8}, 5, 50, 40)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{18}, 5, 100, 50)}); - - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto case_init = std::get<0>(tcase); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -178,14 +213,14 @@ TEST_CASE("updates", "[algo][splitter]") { state.launch(); REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == case_init.expected_orders.value()); - for (const auto& o : state.child_orders()) { + for (const auto &o : state.child_orders()) { if (o.cstate.status == ChildStatus::WORKING) { REQUIRE(o.cstate.quantity.value() <= case_init.sod.exposed_qty()); } } - for(auto q : std::get<1>(tcase)) { - state.fill({}, Quantity {q}); + for (auto q : std::get<1>(tcase)) { + state.fill({}, Quantity{q}); } Update update; @@ -198,9 +233,11 @@ TEST_CASE("updates", "[algo][splitter]") { state.do_update(update); REQUIRE(state.state() == AlgoState::RUNNING); size_t working = 0; - for (const auto& c : state.child_orders()) if (c.cstate.status == ChildStatus::WORKING) working += 1; + for (const auto &c : state.child_orders()) + if (c.cstate.status == ChildStatus::WORKING) + working += 1; REQUIRE(working == case_next.expected_orders.value()); - for (const auto& o : state.child_orders()) { + for (const auto &o : state.child_orders()) { if (o.cstate.status == ChildStatus::WORKING) { REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty()); } @@ -218,21 +255,23 @@ TEST_CASE("Reduction with fills", "[algo][splitter]") { // expected: 100, 100, 50, 50 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - for (const auto& order : state.child_orders()) { - state.fill(order.key, {}, Quantity {50}); + for (const auto &order : state.child_orders()) { + state.fill(order.key, {}, Quantity{50}); } // Reduce exposed quantity @@ -244,12 +283,16 @@ TEST_CASE("Reduction with fills", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 300); - + // need this to be 100,100,50,50 not 100,100,100 - std::set actual, expected {100,100,50,50}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + std::set actual, expected{100, 100, 50, 50}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -264,23 +307,25 @@ TEST_CASE("Reduction with fills 2", "[algo][splitter]") { // expected: 100, 100, 65, 35 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - state.fill({}, Quantity {35}); - state.fill({}, Quantity {45}); - state.fill({}, Quantity {55}); - state.fill({}, Quantity {65}); + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); // Reduce exposed quantity Update update; @@ -291,12 +336,16 @@ TEST_CASE("Reduction with fills 2", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 300); - + // need this to be 100,100,50,50 not 100,100,100 - std::set actual, expected {100,100,65,35}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + std::set actual, expected{100, 100, 65, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -311,23 +360,25 @@ TEST_CASE("Reduction with fills 3", "[algo][splitter]") { // expected: 66, 55, 45, 35 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - state.fill({}, Quantity {35}); - state.fill({}, Quantity {45}); - state.fill({}, Quantity {55}); - state.fill({}, Quantity {65}); + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); // Reduce exposed quantity Update update; @@ -338,11 +389,15 @@ TEST_CASE("Reduction with fills 3", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 201); - - std::set actual, expected {66,55,45,35}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + + std::set actual, expected{66, 55, 45, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -356,17 +411,19 @@ TEST_CASE("reduce to completion", "[algo][splitter]") { // expected: algo transitions to copmleted state canceling 2 orders api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills state.fill({}, Quantity{100}); @@ -380,11 +437,10 @@ TEST_CASE("reduce to completion", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::COMPLETED); size_t total = 0; - for (const auto& order : state.child_orders()) { + for (const auto &order : state.child_orders()) { if (order.cstate.status == ChildStatus::WORKING) { total += order.cstate.quantity.value(); - } - else { + } else { total += order.cstate.total_fills.value(); } } @@ -401,17 +457,19 @@ TEST_CASE("reduce to completion 2", "[algo][splitter]") { // expected: algo transitions to copmleted state canceling 2 orders api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills state.fill({}, Quantity{100}); @@ -437,25 +495,28 @@ TEST_CASE("Async Fills", "[algo][splitter]") { api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(20); splitter->set_exposed_qty(80); - AlgoTestDriver state(def, some_price, Quantity {80}); - + AlgoTestDriver state(def, some_price, Quantity{80}); + REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 20); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 20); + } // Set up some instant fills bool applied_fills = false; - state.on_child_changing([&](const auto&) { - if (applied_fills) return; + state.on_child_changing([&](const auto &) { + if (applied_fills) + return; applied_fills = true; // 7 fills on the last order which is has a size reduction in flight - state.fill(state.child_orders().rbegin()->key, {}, Quantity {7}); + state.fill(state.child_orders().rbegin()->key, {}, Quantity{7}); }); Update update; @@ -465,13 +526,14 @@ TEST_CASE("Async Fills", "[algo][splitter]") { // This should trigger 1 order to be reduced to size 5 from 20 // in the meantime we'll trigger fills of size 7 // This results in an overfill for the 5 lot order - // And the + // And the state.do_update(update); - // expected result: + // expected result: // 7 fills, exposed_qty 65 = 58 active uint32_t active = 0; - for (const auto& o : state.child_orders()) active += o.active_qty(); + for (const auto &o : state.child_orders()) + active += o.active_qty(); REQUIRE(active == 58); } @@ -480,23 +542,25 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { // Increase the size to 400 so we launch a 4th order // Then instantly send another update to 350 - // NOTE: this test is not very good due to limitations in the test driver + // NOTE: this test is not very good due to limitations in the test driver // I'm leaving it in because it passes but if it starts failing in the future // it's probably OK to disable it api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(300); - AlgoTestDriver state(def, some_price, Quantity {300}); - + AlgoTestDriver state(def, some_price, Quantity{300}); + REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 3); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } Update update; update.set_price(state.price().value()); @@ -505,7 +569,7 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { // Set up some instant fills bool applied_second_update = false; - state.on_child_launching([&,update](const auto&) mutable { + state.on_child_launching([&, update](const auto &) mutable { if (!applied_second_update) { applied_second_update = true; update.mutable_custom()->mutable_splitter()->set_exposed_qty(350); @@ -515,12 +579,14 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { state.do_update(update); - // expected result: + // expected result: // 350 exposed, NOT 450 uint32_t quantity = 0; - for (const auto& o : state.child_orders()) quantity += o.active_qty(); + for (const auto &o : state.child_orders()) + quantity += o.active_qty(); REQUIRE(quantity == 350); } - -}}}//namespace \ No newline at end of file +} // namespace test +} // namespace orderman +} // namespace bts From 08c27625009f7735b75d82a1e88263a1af25d0da Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 3 Jul 2019 13:41:07 -0400 Subject: [PATCH 037/153] SP-87 PR feedback 1/2 --- .../orders/algo/algo_orders/SplitterAlgo.cpp | 30 +++++++++++++++---- .../orders/algo/algo_orders/SplitterAlgo.h | 2 ++ .../algo/algo_orders/tests/splitter_tests.cpp | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index 8e3a54b85..9afc50d9f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -18,7 +18,6 @@ using namespace api::orderman; struct ChildOrderState { OrderKey key; ChildState state; - uint32_t remaining() const { return state.quantity.value() - state.total_fills.value(); } uint32_t min_qty() const { return state.total_fills.value(); } }; @@ -55,10 +54,12 @@ OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { "max order quantity and exposed quantity must be greater than 0"}; } - // Create child template - auto child_def = def; - *child_def.mutable_custom_def() = splitter.child(); - me->child_template = &ibl.make_child_template(child_def); + if (me->exposed_qty < me->target_qty) { + return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Exposed quantity must be greater than or equal to target quantity"}; + } + + update_child_template(ibl); me->orders.reserve(me->exposed_qty / me->clip + 1); @@ -88,9 +89,16 @@ void SplitterAlgo::do_update(const api::orderman::Update& update) { me->clip = splitter.max_order_qty(); } me->exposed_qty = splitter.exposed_qty(); + update_child_template(algo()); } me->target_qty = update.quantity(); + if (me->exposed_qty < me->target_qty) { + algo().cancel("Exposed quantity must be greater than or equal to target quantity"); + return; + } + + //todo: forward update.custom() to sync_orders sync_orders(); } void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { @@ -120,6 +128,8 @@ void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector overfills ? + auto citer = constraints.begin(); while(total > 0) { auto current = std::min(total, clip); @@ -166,6 +176,16 @@ void SplitterAlgo::terminate() const { } me->is_terminated = true; } + +void SplitterAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl) { + // Create child template + auto child_def = ibl.definition(); + bts_assert(child_def.has_custom_def()); + bts_assert(child_def.custom_def().has_splitter()); + auto& splitter = child_def.custom_def().splitter(); + *child_def.mutable_custom_def() = splitter.child(); + me->child_template = &ibl.make_child_template(child_def); +} void SplitterAlgo::sync_orders() { if (me->is_terminated) return; diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h index 04258dafd..07eb15e41 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -48,6 +48,8 @@ private: void sync_orders(); bool is_complete() const; void terminate() const; + + void update_child_template(IBeforeLaunchAlgoServices&); }; // exposed for unit testing purposes. diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp index 923c39fcb..03f52ef09 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -529,7 +529,7 @@ TEST_CASE("Async Fills", "[algo][splitter]") { Update update; update.set_price(state.price().value()); - update.set_quantity(state.quantity().value()); + update.set_quantity(65); update.mutable_custom()->mutable_splitter()->set_exposed_qty(65); // This should trigger 1 order to be reduced to size 5 from 20 // in the meantime we'll trigger fills of size 7 From 7f2988ec57471346036ffdd327eca1fa58867920 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 3 Jul 2019 14:07:43 -0400 Subject: [PATCH 038/153] SP-87 PR feedback 2/2 --- .../orders/algo/algo_orders/SplitterAlgo.cpp | 19 +++++++++++++------ .../orders/algo/algo_orders/SplitterAlgo.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index 9afc50d9f..78d257102 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -82,14 +82,18 @@ void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) { bts_assert(false); } void SplitterAlgo::do_update(const api::orderman::Update& update) { - // Update # child orders + const CustomOrderDefinition* new_child_def = nullptr; if (update.has_custom() && update.custom().has_splitter()) { auto& splitter = update.custom().splitter(); if (splitter.max_order_qty() > 0) { me->clip = splitter.max_order_qty(); } me->exposed_qty = splitter.exposed_qty(); - update_child_template(algo()); + + if (splitter.has_child()) { + update_child_template(algo()); + new_child_def = &splitter.child(); + } } me->target_qty = update.quantity(); @@ -98,8 +102,7 @@ void SplitterAlgo::do_update(const api::orderman::Update& update) { return; } - //todo: forward update.custom() to sync_orders - sync_orders(); + sync_orders(new_child_def); } void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { for (auto& child : me->orders) { @@ -187,7 +190,7 @@ void SplitterAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl) { me->child_template = &ibl.make_child_template(child_def); } -void SplitterAlgo::sync_orders() { +void SplitterAlgo::sync_orders(const api::orderman::CustomOrderDefinition* new_child_def) { if (me->is_terminated) return; if (is_complete()) { terminate(); @@ -219,10 +222,14 @@ void SplitterAlgo::sync_orders() { // Check the current order's quantity and modify it to match desired qty if (oiter->state.status == ChildStatus::WORKING) { if (oiter->state.price != price || - oiter->state.quantity != qty) { + oiter->state.quantity != qty || + new_child_def != nullptr) { Update update; update.set_quantity(qty.value()); update.set_price(price.value()); + if (new_child_def != nullptr) { + *update.mutable_custom() = *new_child_def; + } algo().update_child(oiter->key, update); } } diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h index 07eb15e41..92850aa7d 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -45,7 +45,7 @@ public: private: IAlgoServices& algo() const; - void sync_orders(); + void sync_orders(const api::orderman::CustomOrderDefinition* new_child_def = nullptr); bool is_complete() const; void terminate() const; From 2561cb396047130e62ae05af32f306c7e8aebe63 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 4 Jul 2019 19:59:21 -0400 Subject: [PATCH 039/153] Better comments --- .../orderman/orders/spreader_orders/SpreaderOrder.h | 11 ++++------- .../spreader/SpreaderBookkeepingData.h | 9 ++++++--- .../orders/spreader_orders/spreader/SpreaderUtils.h | 12 ++++++++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index b582320a1..35445df2a 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -62,14 +62,11 @@ private: const bool _active; // this is the main data bool _cancel_hedges_on_cancel; - // if this is true, then in the case of nlegs - // (>2) re-comp the hedge prices on a fill + // if this is true, then re-comp the hedge prices on a fill + // (for > 2 legs) const bool _n_legs_recomp_hedges_on_fill; - // this tells that in the n leg case the last - // call to compute cross hedges on a quoting fill - // was successful or not (TODO:need to put this, - // _n_legs_recomp_hedges_on_fill and SBKD::_nlegs_crossed_hedges - // in its own class) + // this tells if the last call to compute + // cross hedges on a quoting fill was successful or not bool _n_legs_last_recomp_hedges_on_fill_succ; // helps with forced calls, and all the artificiality of these // calls are trapped here diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h index 54b7b595d..4eef7008d 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h @@ -36,9 +36,12 @@ private: Price _input_price; Price _effective_price; Quantity _quantity; - // this is used to pass in the calcuated hedge price/qty - // (preallocating so we dont have to allocate - // memory when calculating and calculating values) + // this is used as an out variable to the function + // that calculates the hedges based on crossing the + // hedge markets. + // this could have been a return variable from the + // function, but this is allocated once and resused to + // avoid allocations in the critical path std::vector> _nlegs_crossed_hedges; private: size_t _calc_n_quoting_legs(const std::vector& legs); diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index bd3149ee6..0bf08c6e4 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -794,6 +794,16 @@ public: quote_fill_quantity, hedge_leg); } + // Given a quoting leg fill (ie leg id, fill price and + // fill qty), this function calculates the hedge prices, + // computed by crossing into the hedge market to get the + // required fill size. + // Function returns false if + // i. one of the hedging markets can not support the hedging + // size requirement + // ii. the computed spread price = + // q_leg_ratio*q_fill_price + sum(hedge_leg_ratio*hedge_cross_price) + // is worse than the spreader price static bool compute_nlegs_crossed_hedges( api::Side spr_side, Price spr_price, @@ -805,8 +815,6 @@ public: std::vector>& out_drivers){ // spr_cross_price = q_leg_ratio*q_fill_price + // sum(hedge_leg_ratio*hedge_cross_price) - // where hedge_cross_price is the hedge price found by - // crossing the hedge market to aquire the hedge qty double spr_cross_price = q_fill_price.value()*q_leg.price_multiplier(); for (size_t i=0;i Date: Fri, 5 Jul 2019 11:16:24 -0400 Subject: [PATCH 040/153] Reorganized the data for NLeg Hedge Recomp better --- .../orders/spreader_orders/SpreaderOrder.cpp | 28 +++++++-------- .../orders/spreader_orders/SpreaderOrder.h | 8 ----- .../spreader/NLegsHedgeRecompData.cpp | 22 ++++++++++++ .../spreader/NLegsHedgeRecompData.h | 36 +++++++++++++++++++ .../spreader/SpreaderBookkeepingData.cpp | 5 ++- .../spreader/SpreaderBookkeepingData.h | 15 +++----- 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.cpp create mode 100644 src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 02f4a3975..54c019ed7 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -6,9 +6,12 @@ #include "lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h" +#include "lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h" #include "lib/orderman/events/SettlementUpdateEvent.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/helper_macros.h" +#include "lib/config/config.h" +#include "lib/config/protos/spreader.pb.h" #include "lib/os/os.h" namespace bts { @@ -21,9 +24,6 @@ _cancelled (false), _report_dirty(false), _active(true), _cancel_hedges_on_cancel(false), -_n_legs_recomp_hedges_on_fill( - config::get().n_legs_recomp_hedges_on_fill()), -_n_legs_last_recomp_hedges_on_fill_succ(false), _cw_helper(), _cancel_timeout (5000),// TODO:get it from config _check_termination_period (250), // should also be a setting/config like above @@ -380,14 +380,14 @@ void SpreaderOrder::_send_hedge(size_t hedge_leg_index, void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, Price filled_price, double filled_quantity){ - _n_legs_last_recomp_hedges_on_fill_succ = false; - if(_n_legs_recomp_hedges_on_fill && - _bookkeeping_data().n_legs() > 2){ - SpreaderBookkeepingData& bk_data = _bookkeeping_data(); + SpreaderBookkeepingData& bk_data = _bookkeeping_data(); + NLegsHedgeRecompData& recomp_data = bk_data.hedge_recomp_data(); + recomp_data.set_last_call_succ(false); + if(recomp_data.activated){ const LegPropertyData& q_leg_data = bk_data.get_leg_data(filled_leg_index).leg_property_data(); const ISpreaderManager& spreader = *this; - _n_legs_last_recomp_hedges_on_fill_succ = + bool is_succ = SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(), bk_data.effective_price(), bk_data.n_legs(), @@ -395,7 +395,8 @@ void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index, filled_price, filled_quantity, spreader, - bk_data.nlegs_crossed_hedges()); + recomp_data.hedges()); + recomp_data.set_last_call_succ(is_succ); } } void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, @@ -416,11 +417,10 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, hedged_leg_data.leg_property_data(), computed_hedge_price, hedge_quantity); - else{ - if(_n_legs_recomp_hedges_on_fill && - _n_legs_last_recomp_hedges_on_fill_succ){ - auto& crossed_hedge = - _bookkeeping_data().nlegs_crossed_hedges()[hedge_leg_index]; + else{ + NLegsHedgeRecompData& recomp_data = _bookkeeping_data().hedge_recomp_data(); + if(recomp_data.last_call_succ()){ + auto& crossed_hedge = recomp_data.hedges()[hedge_leg_index]; computed_hedge_price = crossed_hedge.first; hedge_quantity = crossed_hedge.second; } diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index 35445df2a..e223d09f1 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -18,8 +18,6 @@ #include "lib/orderman/events/OrderStateUpdateEvent.h" #include "lib/orderman/events/BookUpdateEvent.h" #include "lib/mercury/events/TimerEvent.h" -#include "lib/config/config.h" -#include "lib/config/protos/spreader.pb.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderCacheWarmingState.h" namespace bts { @@ -62,12 +60,6 @@ private: const bool _active; // this is the main data bool _cancel_hedges_on_cancel; - // if this is true, then re-comp the hedge prices on a fill - // (for > 2 legs) - const bool _n_legs_recomp_hedges_on_fill; - // this tells if the last call to compute - // cross hedges on a quoting fill was successful or not - bool _n_legs_last_recomp_hedges_on_fill_succ; // helps with forced calls, and all the artificiality of these // calls are trapped here CacheWarmingState _cw_helper; diff --git a/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.cpp b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.cpp new file mode 100644 index 000000000..fe8819940 --- /dev/null +++ b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.cpp @@ -0,0 +1,22 @@ +#include "NLegsHedgeRecompData.h" +#include "lib/config/config.h" +#include "lib/config/protos/spreader.pb.h" + +namespace bts{ +namespace orderman { + +NLegsHedgeRecompData::NLegsHedgeRecompData(size_t n_legs) +: +_last_call_succ(false), +_hedges(), +activated(n_legs > 2 && + config::get().n_legs_recomp_hedges_on_fill()) +{ + if(activated){ + _hedges.reserve(n_legs); + for(size_t i=0; i(os::nan(),0.0)); + } +} + +}} \ No newline at end of file diff --git a/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h new file mode 100644 index 000000000..6aab74357 --- /dev/null +++ b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h @@ -0,0 +1,36 @@ +#pragma once +#include "lib/orderman/orderman_utils/orderman_common.h" +namespace bts{ +namespace orderman { + +// This is the data related to recomputing the hedges +// for >2 legs on a fill. +// When this is activated, then on a fill, the hedge markets +// are analyzed to reduce the chances of getting legged +class NLegsHedgeRecompData{ +private: + bool _last_call_succ; + // this is used as an out variable to the function + // that calculates the hedges based on crossing the + // hedge markets. + // this could have been a return variable from the + // function, but this is allocated once and reused to + // avoid allocations in the critical path + std::vector> _hedges; +public: + // activated if > 2 legs and configured + const bool activated; +public: + NLegsHedgeRecompData (size_t n_legs); + bool last_call_succ() const{ + return _last_call_succ; + } + std::vector>& hedges(){ + return _hedges; + } + void set_last_call_succ (bool val){ + _last_call_succ = val; + } +}; + +}} \ No newline at end of file diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp index 8ad85b95b..a440a9ea1 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp @@ -21,12 +21,12 @@ _n_legs(legs.size()), _n_quoting_legs(_calc_n_quoting_legs(legs)), _side(order_def.side()), _order_def(order_def), -_reported_state(){ +_reported_state(), +_hedge_recomp_data(legs.size()){ LOG(OrderMan, Info, "SpreaderBookkeepingData.cons started " "key: {} n_legs: {}", spreader_order_key.value(), _n_legs); _leg_data.reserve(_n_legs); - _nlegs_crossed_hedges.reserve(_n_legs); size_t i=0; for (const auto& leg_property_data : legs){ _leg_data.emplace_back(spreader_pao_services, @@ -36,7 +36,6 @@ _reported_state(){ quoter_ord_defs[i], hedger_ord_defs[i], leg_property_data); - _nlegs_crossed_hedges.emplace_back(std::pair(os::nan(),0.0)); bts_assert (_sec_key_to_index.find(leg_property_data.security_key()) == _sec_key_to_index.end()); _sec_key_to_index.insert(std::pair( diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h index 4eef7008d..c2408f318 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h @@ -10,9 +10,10 @@ #include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h" #include "lib/orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.h" #include "lib/orderman/orders/spreader_orders/spreader/hedger/HedgerManager.h" +#include "NLegsHedgeRecompData.h" namespace bts{ -namespace orderman { +namespace orderman { class SingleFillHedger; // encapsulates, organizes and provides easy access // and lookups for data needed by the spreader. @@ -36,13 +37,7 @@ private: Price _input_price; Price _effective_price; Quantity _quantity; - // this is used as an out variable to the function - // that calculates the hedges based on crossing the - // hedge markets. - // this could have been a return variable from the - // function, but this is allocated once and resused to - // avoid allocations in the critical path - std::vector> _nlegs_crossed_hedges; + NLegsHedgeRecompData _hedge_recomp_data; private: size_t _calc_n_quoting_legs(const std::vector& legs); public: @@ -74,8 +69,8 @@ public: Price input_price () const {return _input_price;} Price effective_price () const {return _effective_price;} Quantity quantity () const {return _quantity;} - std::vector>& nlegs_crossed_hedges(){ - return _nlegs_crossed_hedges; + NLegsHedgeRecompData& hedge_recomp_data(){ + return _hedge_recomp_data; } // gets leg data from leg's sec key SpreaderLegData& get_leg_data (SecurityKey security_key){ From 7e1463683f1642cfbe3307b917025c00ca98e2b7 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 5 Jul 2019 11:55:02 -0400 Subject: [PATCH 041/153] SP-30 base Dorman algo atop SplitterAlgo --- .vscode/launch.json | 2 +- .../orders/algo/algo_orders/DormanAlgo.cpp | 230 +++++++++--------- .../orders/algo/algo_orders/DormanAlgo.h | 11 +- .../algo/algo_orders/tests/AlgoTestDriver.cpp | 15 +- .../algo/algo_orders/tests/AlgoTestDriver.h | 1 + .../algo/algo_orders/tests/dorman_tests.cpp | 224 ++++++++--------- 6 files changed, 247 insertions(+), 236 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index fac8a7668..cda1e31c2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -249,7 +249,7 @@ "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ "test", - "SingleWriterConcurrentVector" + "[dorman]" ], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/server/", diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp index b1f31c717..ef5692f45 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -15,87 +15,8 @@ namespace dorman { using namespace api::orderman; - -// Class which will send out multiple orders but act like one order -struct ChildOrderState { - Quantity size; - Quantity filled; -}; - -class MultiOrder { - IAlgoServices* _algo = nullptr; - uint32_t _clip = 0; - ChildTemplate* _template = nullptr; - - Quantity _qty = {}; - std::map _orders; - - public: - MultiOrder() {} - ~MultiOrder() { - if (_algo) { - for (const auto& pair : _orders) { - _algo->cancel_child(pair.first); - } - } - } - void do_before_launch(IBeforeLaunchAlgoServices& algo, uint32_t clip) { - _clip = clip == 0 ? std::numeric_limits::max() : clip; - auto def = algo.definition(); - - CustomOrderDefinition default_order; - default_order.mutable_limit(); - - *def.mutable_custom_def() = - get_if(def.custom_def().dynamic().order_params(), "Child") - .value_or(default_order); - _template = &algo.make_child_template(def); - } - - void set_algo_services(IAlgoServices& a) { _algo = &a; } - - void update(Price p, Quantity q) { - bts_assert(_algo != nullptr); - // This class does not handle delayed size changes - if (q < _qty) { - // cancel some orders - auto excess = _qty.value() - q.value(); - while(excess > 0) { - // cancel some orders - auto iter = _orders.begin(); - auto lsize = iter->second.size.value() - iter->second.filled.value(); - if (lsize > excess) { - // Reduce order size - Update update; - update.set_quantity(lsize - excess); - _algo->update_child(iter->first, update); - iter->second.size.value() -= excess; - excess = 0; - } - else { - excess -= lsize; - _algo->cancel_child(iter->first); - _orders.erase(iter); - } - } - _qty = q; - } - else if (q > _qty) { - // add more orders - auto shortfall = q.value() - _qty.value(); - while(shortfall > 0) { - auto order_size = Quantity { std::min(shortfall, _clip) }; - auto child_id = _algo->launch_child(*_template, p, order_size, true); - _orders.insert(std::make_pair(child_id, ChildOrderState { order_size, Quantity {0} })); - shortfall -= order_size.value(); - } - _qty = q; - } - } - - Quantity quantity() const { return _qty; } -}; - +#define REQUIRE(condition, message) \ + if (!(condition)) { algo().cancel(message); return; } // // Algo State @@ -113,9 +34,14 @@ struct DormanAlgo::Impl { IAlgoServices *algo = nullptr; AlgoParams params; api::Side side; - + Quantity exposed_qty = {}; std::optional next_size; - MultiOrder child; + + OrderDefinition splitter_definition; + OrderKey child_id; + ChildState child_state; + + uint32_t fills ={}; }; DormanAlgo::DormanAlgo() : @@ -123,49 +49,90 @@ DormanAlgo::DormanAlgo() : { } DormanAlgo::~DormanAlgo() { } -// Not latency sensitive. Do expensive initialization here -OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& iblas) { - const auto& def = iblas.definition(); +OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { + const auto& def = ibl.definition(); // Validation bts_assert(def.has_custom_def()); bts_assert(def.custom_def().has_dynamic()); + auto params = to_dorman_params(def.custom_def().dynamic()); - if (auto r = std::get_if(¶ms)) { - return *r; + if (auto error = std::get_if(¶ms)) { + return *error; } me->params = std::get(params); me->side = def.side(); if (me->side != api::Side::SIDE_BUY && me->side != api::Side::SIDE_SELL) { return OrderOpResult { ORDER_ERROR_INVALID_ORDER_DEFINITON, "Invalid side" }; } - me->child.do_before_launch(iblas, me->params.clip_limit); + + CustomOrderDefinition default_order; + default_order.mutable_limit(); + auto child_def = + get_if(def.custom_def().dynamic().order_params(), "Child") + .value_or(default_order); + + update_child_template(ibl, child_def); + + if (ibl.is_attaching()) { + return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED, + "Splitter does not support attach."}; + } return {/*ok*/}; } + void DormanAlgo::do_launch(IAlgoServices& algo) { me->algo = &algo; - me->child.set_algo_services(algo); - algo.subscribe_book(SecurityKey(algo.security().id())); algo.subscribe_timer(); } -void DormanAlgo::do_launch_attached(IAlgoServices& algo, OrderKey k, ChildState state) { - me->algo = &algo; - (void)k; - (void)state; +void DormanAlgo::do_launch_attached(IAlgoServices&, OrderKey, ChildState) { + bts_assert(false); } void DormanAlgo::do_update(const api::orderman::Update& update) { LOG(OrderMan, Diag, "Dorman order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - (void)update; + + if (me->fills >= update.quantity()) { + algo().complete(); + } + + if (update.has_custom()) { + REQUIRE(update.custom().has_dynamic(), "Update cannot change order type"); + auto params = to_dorman_params(update.custom().dynamic()); + + if (auto error = std::get_if(¶ms)) { + algo().cancel(error->error_msg); + } + + me->params = std::get(params); + + // Check for child + auto child = get_if(update.custom().dynamic().order_params(), "Child"); + if (child) { + update_child_template(algo(), child.value()); + } + } } -void DormanAlgo::on_child_change(OrderKey k, const ChildState& st) { - (void)k; - (void)st; +void DormanAlgo::on_child_change(OrderKey k, const ChildState& state) { + REQUIRE(k == me->child_id, "Unexpected update from illigitimate child"); + REQUIRE(state.total_fills.value() >= me->child_state.total_fills.value(), "Unexpected fill decrease."); + + auto delta = state.total_fills.value() - me->child_state.total_fills.value(); + if (delta != 0) { + me->fills += delta; + algo().update_fill_state(Quantity {me->fills}, state.last_price); + } + me->child_state = state; + + if (me->fills >= algo().quantity().value()) { + algo().complete(); + } } + void DormanAlgo::on_book(SecurityKey key, const Book& book) { if (key.value() != me->algo->security().id()) { return; } auto price = me->algo->price(); @@ -194,24 +161,24 @@ void DormanAlgo::on_book(SecurityKey key, const Book& book) { target = Quantity { std::min(target.value(), me->params.steps.maximum) }; // First on_book - if (me->child.quantity() == Quantity {0}) { - me->child.update(price, target); + if (!child_active()) { + update_child(target); } // See if we need to adjust else { - if (target < me->child.quantity()) { - // If target < size reduce immidiately - me->child.update(price, target); + // If target < size reduce immidiately + if (target < me->exposed_qty) { + update_child(target); me->next_size = std::nullopt; } - else if (target > me->child.quantity()) { - // if target > size schedule increase + // if target > size schedule increase + else if (target > me->exposed_qty) { if (me->next_size) { - // already planning to increase + // already planning to increase, update the target me->next_size.value().size = target; } else { - // no scheduled increase + // no existing scheduled increase, create one now me->next_size = { FutureQty { target, me->algo->get_time() + std::chrono::seconds(me->params.steps.trail_seconds) } }; } } @@ -222,7 +189,7 @@ void DormanAlgo::on_timer() { if (me->next_size && me->algo->get_time() >= me->next_size.value().when) { auto size = me->next_size.value().size; - me->child.update(me->algo->price(), size); + update_child(size); me->next_size = std::nullopt; } } @@ -230,10 +197,49 @@ void DormanAlgo::on_timer() { IAlgoServices& DormanAlgo::algo() const { return *me->algo; } // -// Visibility for testing -// +// private -Quantity DormanAlgo::desired_size() const { return me->child.quantity(); } +void DormanAlgo::update_child(Quantity exposed) { + auto& splitter = *me->splitter_definition.mutable_custom_def()->mutable_splitter(); + splitter.set_max_order_qty(me->params.clip_limit); + splitter.set_exposed_qty(exposed.value()); + me->exposed_qty = exposed; + if (!child_active()) { + // launch child + me->child_id = algo().launch_child(me->splitter_definition, algo().price(), algo().quantity()); + } + else { + Update update; + update.set_price(algo().price().value()); + update.set_quantity(algo().quantity().value()); + *update.mutable_custom()->mutable_splitter() = splitter; + algo().update_child(me->child_id, update); + } +} + +void DormanAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl, const CustomOrderDefinition& child) { + auto child_def = ibl.definition(); + // Insert a spliter between this and the configured child definition. + api::orderman::SplitterOrderDefinition& splitter_def = *child_def.mutable_custom_def()->mutable_splitter(); + *splitter_def.mutable_child() = child; + me->splitter_definition = child_def; +} + +Quantity DormanAlgo::exposed_qty() const { return me->exposed_qty; } + +bool DormanAlgo::child_active() const { return me->child_id.value() != 0; } + +void DormanAlgo::pause_until(TimeStamp) { + // cancel child + algo().cancel_child(me->child_id); + + // retain me->child_id for updates + + // set paused flag + // set paused_time flag + // ignore child_update if paused + // check paused_time in on_timer +} // // diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h index 9d0628b76..01f997068 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h @@ -68,7 +68,16 @@ public: void on_book(SecurityKey, const Book&) override; void on_timer() override; - Quantity desired_size() const; + Quantity exposed_qty() const; + +private: + // + // Child management + void update_child_template(IBeforeLaunchAlgoServices&, const api::orderman::CustomOrderDefinition&); + void launch_child(); + void update_child(Quantity exposed); + bool child_active() const; + void pause_until(TimeStamp); }; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index bee71af2f..14a9439ad 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -315,8 +315,13 @@ OrderKey AlgoTestDriverBase::launch_child(ChildTemplate&, Price price, Quantity for(const auto& cb : me->on_child_launching) cb(key); return key; } -OrderKey AlgoTestDriverBase::launch_child(api::orderman::OrderDefinition const & od, Price price, Quantity qty, bool sub) { - return launch_child(make_child_template(od), price, qty, sub); +OrderKey AlgoTestDriverBase::launch_child(api::orderman::OrderDefinition const & def, Price price, Quantity qty, bool sub) { + OrderKey key = me->next_order(); + me->orders.push_back( + mkchild(key, price, qty, sub)); + me->orders.back().def = {def}; + for(const auto& cb : me->on_child_launching) cb(key); + return key; } void AlgoTestDriverBase::update_child(OrderKey key, api::orderman::Update const & update) { auto guard = me->recusion_guard(); @@ -333,6 +338,12 @@ void AlgoTestDriverBase::update_child(OrderKey key, api::orderman::Update const if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) { child.cstate.status = ChildStatus::COMPLETED; } + if (update.has_custom()) { + if (!child.def) { + child.def = {api::orderman::OrderDefinition()}; + } + *child.def.value().mutable_custom_def() = update.custom(); + } for (const auto& cb : me->on_child_changed) cb(old, child); if (child.subscribed) { diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index b475e8941..c39f7f2db 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -33,6 +33,7 @@ struct ChildOrder { OrderKey key; bool subscribed; ChildState cstate; + std::optional def; uint32_t active_qty() const { return cstate.status == ChildStatus::WORKING ? cstate.quantity.value() - cstate.total_fills.value() : 0; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp index 55a8ad308..e3d5c6f83 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -16,7 +16,7 @@ namespace test { struct OrderState { Price desired_price; - Quantity desired_size; + Quantity exposed_qty; } */ @@ -113,14 +113,17 @@ struct ExpectedSize { }; constexpr auto some_price = md::IntPrice { 128000, -3 }; -size_t count_child_size(const std::vector& child_orders) { +size_t child_exposed(const std::vector& child_orders) { return std::accumulate( child_orders.begin(), child_orders.end(), 0, [](uint32_t acc, const auto& child) { if (child.cstate.status != ChildStatus::CANCELED) { - // count working+completed - return acc + child.cstate.quantity.value(); + REQUIRE(child.def); + return + acc + + child.def.value().custom_def().splitter().exposed_qty() + + child.cstate.total_fills.value(); } else return acc; }); @@ -128,140 +131,121 @@ size_t count_child_size(const std::vector& child_orders) { TEST_CASE("dorman_order_size", "[algo][dorman]") { - // Repeate the test for a variety of clip sizes - for (auto clip : { 100, 20, 900, 1 }) { + // create a table of various sizes + // send market state to the algo + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 400}, + {2000, 600}, + {3500, 750}, + {10000, 999} + }; + input.steps.maximum = 800; + input.steps.trail_seconds = 5; + input.clip_limit = 100; - // create a table of various sizes - // send market state to the algo - AlgoParams input; - input.steps.step_table = { - {500, 250}, - {1000, 400}, - {2000, 600}, - {3500, 750}, - {10000, 999} + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + + SECTION("initial size vs market") { + std::vector test_sizes { + { 10, 250 }, + { 200, 250 }, + { 250, 250 }, + { 500, 400 }, + { 999, 400 }, + { 2500, 750 }, + { 9999, 800 } }; - input.steps.maximum = 800; - input.steps.trail_seconds = 5; - input.clip_limit = clip; - api::orderman::OrderDefinition def; - def.set_side(api::SIDE_BUY); - def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); - - - SECTION("initial size vs market") { - std::vector test_sizes { - { 10, 250 }, - { 200, 250 }, - { 250, 250 }, - { 500, 400 }, - { 999, 400 }, - { 2500, 750 }, - { 9999, 800 } - }; - - for (auto& s : test_sizes) - { - AlgoTestDriver state(def, some_price, Quantity {10}); - auto secid = SecurityKey(state.security().id()); - Book book(secid); - - // order size is driven by the available market size - book.bid()[0] = { some_price, s.input }; - state.update_book(book); - - REQUIRE(state.before_launch().ok()); - state.launch(); - REQUIRE(state.algo().desired_size().value() == s.expected); - REQUIRE( - s.expected == count_child_size(state.child_orders())); - for(const auto& ch : state.child_orders()) { - REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); - } - } - } - SECTION("Reduce qty on market shrink (immidiate)") { + for (auto& s : test_sizes) + { AlgoTestDriver state(def, some_price, Quantity {10}); auto secid = SecurityKey(state.security().id()); Book book(secid); - book.bid()[0] = { some_price, 505 }; + + // order size is driven by the available market size + book.bid()[0] = { some_price, s.input }; state.update_book(book); + REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().desired_size() == Quantity(400)); + REQUIRE(state.algo().exposed_qty().value() == s.expected); + REQUIRE(s.expected == child_exposed(state.child_orders())); + } + } + SECTION("Reduce qty on market shrink (immidiate)") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(400)); - // Check that orders were created - REQUIRE( - 400 == count_child_size(state.child_orders())); - for(const auto& ch : state.child_orders()) { - REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); - } - - // Reduce market - book.bid()[0] = { some_price, 405 }; - state.update_book(book); - REQUIRE(state.algo().desired_size() == Quantity(250)); - REQUIRE( - 250 == count_child_size(state.child_orders())); - for(const auto& ch : state.child_orders()) { - REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); - } + // Check that orders were created + REQUIRE(400 == child_exposed(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity == state.quantity()); } - SECTION("Increase qty w/ delay when market grows") { - AlgoTestDriver state(def, some_price, Quantity {10}); - auto secid = SecurityKey(state.security().id()); - Book book(secid); - book.bid()[0] = { some_price, 505 }; - state.update_book(book); - REQUIRE(state.before_launch().ok()); - state.launch(); - REQUIRE(state.algo().desired_size() == Quantity(400)); + // Reduce market + book.bid()[0] = { some_price, 405 }; + state.update_book(book); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); + REQUIRE( + 250 == child_exposed(state.child_orders())); + } - // Check that orders were created - REQUIRE( - 400 == count_child_size(state.child_orders())); + SECTION("Increase qty w/ delay when market grows") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(400)); - // INCREASE market - book.bid()[0] = { some_price, 1001 }; - state.update_book(book); - // NOTE: that the orders do not follow the market instantly - // so this is still 400 - REQUIRE( - 400 == count_child_size(state.child_orders())); + // Check that orders were created + REQUIRE( + 400 == child_exposed(state.child_orders())); - // Still 400 after the timer ticks but not enough time has passed - state.advance_time(std::chrono::seconds()); - REQUIRE( - 400 == count_child_size(state.child_orders())); + // INCREASE market + book.bid()[0] = { some_price, 1001 }; + state.update_book(book); + // NOTE: that the orders do not follow the market instantly + // so this is still 400 + REQUIRE( + 400 == child_exposed(state.child_orders())); - state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); - REQUIRE( - 600 == count_child_size(state.child_orders())); - for(const auto& ch : state.child_orders()) { - REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); - } + // Still 400 after the timer ticks but not enough time has passed + state.advance_time(std::chrono::seconds()); + REQUIRE( + 400 == child_exposed(state.child_orders())); - } - SECTION("Respect maximum") { - AlgoTestDriver state(def, some_price, Quantity {10}); - auto secid = SecurityKey(state.security().id()); - Book book(secid); - book.bid()[0] = { some_price, 10505 }; - state.update_book(book); - REQUIRE(state.before_launch().ok()); - state.launch(); + state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); + REQUIRE( + 600 == child_exposed(state.child_orders())); - REQUIRE(state.algo().desired_size() == Quantity(input.steps.maximum)); + } + SECTION("Respect maximum") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 10505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); - // Check that orders were created - REQUIRE( - input.steps.maximum == count_child_size(state.child_orders())); - for(const auto& ch : state.child_orders()) { - REQUIRE(ch.cstate.quantity.value() <= input.clip_limit); - } - } + REQUIRE(state.algo().exposed_qty() == Quantity(input.steps.maximum)); + + // Check that orders were created + REQUIRE( + input.steps.maximum == child_exposed(state.child_orders())); } } @@ -293,7 +277,7 @@ TEST_CASE("Dorman fills", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().desired_size() == Quantity(450)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); for (const auto& order : state.child_orders()) { state.fill(order.key, price, order.cstate.quantity); From 729acc4ad596d4f1d004225739750a2065a86d7f Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Sun, 7 Jul 2019 20:52:42 -0400 Subject: [PATCH 042/153] deleted default constructors and assignment operators on NLegsHedgeRecompData plus tightened a condition for application of recompute redhedging --- src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp | 2 +- .../orders/spreader_orders/spreader/NLegsHedgeRecompData.h | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 54c019ed7..32d552e99 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -419,7 +419,7 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, hedge_quantity); else{ NLegsHedgeRecompData& recomp_data = _bookkeeping_data().hedge_recomp_data(); - if(recomp_data.last_call_succ()){ + if(recomp_data.activated && recomp_data.last_call_succ()){ auto& crossed_hedge = recomp_data.hedges()[hedge_leg_index]; computed_hedge_price = crossed_hedge.first; hedge_quantity = crossed_hedge.second; diff --git a/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h index 6aab74357..e299fed82 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/NLegsHedgeRecompData.h @@ -21,7 +21,12 @@ public: // activated if > 2 legs and configured const bool activated; public: - NLegsHedgeRecompData (size_t n_legs); + NLegsHedgeRecompData(size_t n_legs); + NLegsHedgeRecompData() = delete; + NLegsHedgeRecompData(const NLegsHedgeRecompData&) = delete; + NLegsHedgeRecompData(NLegsHedgeRecompData&&) = delete; + NLegsHedgeRecompData& operator=(const NLegsHedgeRecompData&) = delete; + NLegsHedgeRecompData& operator=(NLegsHedgeRecompData&&) = delete; bool last_call_succ() const{ return _last_call_succ; } From 4ecbfe5f5b2a18f978ea1cd4bb69898815324e47 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 5 Jul 2019 17:08:42 -0400 Subject: [PATCH 043/153] SP-31 Dorman auto-scratch --- .../orders/algo/algo_orders/DormanAlgo.cpp | 34 ++++- .../orders/algo/algo_orders/DormanAlgo.h | 2 + .../algo/algo_orders/tests/AlgoTestDriver.cpp | 21 ++- .../algo/algo_orders/tests/AlgoTestDriver.h | 6 +- .../algo/algo_orders/tests/dorman_tests.cpp | 131 +++++++++++++----- 5 files changed, 148 insertions(+), 46 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp index ef5692f45..ce8d33ce4 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -75,7 +75,7 @@ OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { if (ibl.is_attaching()) { return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED, - "Splitter does not support attach."}; + "This algo does not support attach."}; } return {/*ok*/}; @@ -95,10 +95,6 @@ void DormanAlgo::do_update(const api::orderman::Update& update) { LOG(OrderMan, Diag, "Dorman order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - if (me->fills >= update.quantity()) { - algo().complete(); - } - if (update.has_custom()) { REQUIRE(update.custom().has_dynamic(), "Update cannot change order type"); auto params = to_dorman_params(update.custom().dynamic()); @@ -115,10 +111,14 @@ void DormanAlgo::do_update(const api::orderman::Update& update) { update_child_template(algo(), child.value()); } } + + if (me->fills >= update.quantity()) { + algo().complete(); + } } -void DormanAlgo::on_child_change(OrderKey k, const ChildState& state) { - REQUIRE(k == me->child_id, "Unexpected update from illigitimate child"); +void DormanAlgo::on_child_change(OrderKey key, const ChildState& state) { + REQUIRE(key == me->child_id, "Unexpected update from illigitimate child"); REQUIRE(state.total_fills.value() >= me->child_state.total_fills.value(), "Unexpected fill decrease."); auto delta = state.total_fills.value() - me->child_state.total_fills.value(); @@ -128,6 +128,11 @@ void DormanAlgo::on_child_change(OrderKey k, const ChildState& state) { } me->child_state = state; + if (me->params.scratch && + me->fills >= me->params.scratch.value().max_overfill) { + do_scratch(); + } + if (me->fills >= algo().quantity().value()) { algo().complete(); } @@ -241,6 +246,21 @@ void DormanAlgo::pause_until(TimeStamp) { // check paused_time in on_timer } +void DormanAlgo::do_scratch() { + // When the # of fills exceeds the max overfill quantity + // We enter fills opposite fills at the fill price and terminate + + OrderDefinition def = me->splitter_definition; + def.set_side(securities::other_side(me->side)); + def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(me->fills); + + auto price = me->child_state.last_price; + auto scratch_id = algo().launch_child(def, price, Quantity { me->fills }, false); + + algo().detach_child(scratch_id); + algo().complete(); +} + // // // Parameter parsing and validation diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h index 01f997068..6f262d8cb 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h @@ -79,6 +79,8 @@ private: bool child_active() const; void pause_until(TimeStamp); + void do_scratch(); + }; template diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index 14a9439ad..abd9f1cf3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -59,6 +59,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter { AlgoState state; std::vector orders; + std::vector detached_orders; std::map books; std::map book_subs; @@ -68,6 +69,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter { std::vector> on_child_launching; std::vector> on_child_changing; std::vector> on_child_changed; + bool allow_overfill = false; Impl(IAlgoOrder& o) : algo(o) {} @@ -165,6 +167,10 @@ const std::vector& AlgoTestDriverBase::child_orders() const { return me->orders; } +const std::vector& AlgoTestDriverBase::detached_orders() const { + return me->detached_orders; +} + AlgoState AlgoTestDriverBase::state() const { return me->state; } @@ -173,7 +179,7 @@ void AlgoTestDriverBase::fill(OrderKey k, Price price, Quantity qty) { auto guard = me->recusion_guard(); for (auto& child : me->orders) { if (child.key == k) { - REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value()); + if (!me->allow_overfill) REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value()); for (const auto& cb : me->on_child_changing) cb(child); auto old = child; @@ -237,6 +243,9 @@ void AlgoTestDriverBase::on_child_changing(std::function&& cb) { me->on_child_changed.push_back(cb); } + +bool AlgoTestDriverBase::allow_overfill() const { return me->allow_overfill; } +void AlgoTestDriverBase::allow_overfill(bool value) { me->allow_overfill = value; } // // Utility @@ -369,9 +378,17 @@ void AlgoTestDriverBase::cancel_child(OrderKey key) { } } void AlgoTestDriverBase::detach_child(OrderKey key) { - std::remove_if(me->orders.begin(), me->orders.end(), [key](const auto& order) { return order.key == key; }); + for (auto iter = me->orders.begin(); iter != me->orders.end(); ++iter) { + auto order = *iter; + if (order.key == key) { + me->detached_orders.push_back(order); + me->orders.erase(iter); + break; + } + } } void AlgoTestDriverBase::detach_all_children() { + for (const auto& order : me->orders) me->detached_orders.push_back(order); me->orders.clear(); } void AlgoTestDriverBase::cancel(std::string const &) { diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index c39f7f2db..50ac02acd 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -73,6 +73,7 @@ class AlgoTestDriverBase : private IAlgoServices { // // Inspection methods const std::vector& child_orders() const; + const std::vector& detached_orders() const; AlgoState state() const; // Fills for this specific order @@ -84,7 +85,10 @@ class AlgoTestDriverBase : private IAlgoServices { void on_child_changing(std::function&&); void on_child_changed(std::function&&); - + // + // Tweak internal REQUIRE + bool allow_overfill() const; + void allow_overfill(bool=true); private: diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp index e3d5c6f83..b38121daf 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -7,6 +7,7 @@ using namespace std; using namespace bts::orderman::dorman; +using namespace bts::api::orderman; namespace bts { namespace orderman { @@ -113,22 +114,31 @@ struct ExpectedSize { }; constexpr auto some_price = md::IntPrice { 128000, -3 }; -size_t child_exposed(const std::vector& child_orders) { + +template +size_t count_x(const std::vector& child_orders, CB fn) { return std::accumulate( child_orders.begin(), child_orders.end(), 0, - [](uint32_t acc, const auto& child) { - if (child.cstate.status != ChildStatus::CANCELED) { - REQUIRE(child.def); - return - acc + - child.def.value().custom_def().splitter().exposed_qty() - + child.cstate.total_fills.value(); - } - else return acc; + [&](uint32_t acc, const auto& child) { + return acc + fn(child); }); } +size_t count_exposed(const std::vector& child_orders) { + return count_x(child_orders, [](const ChildOrder& child) { + REQUIRE(child.def); + if (child.cstate.status == ChildStatus::CANCELED) return 0u; + else return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value(); + }); +} + +size_t count_fills(const std::vector& child_orders) { + return count_x(child_orders, [](const ChildOrder& child) { + return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value(); + }); +} + TEST_CASE("dorman_order_size", "[algo][dorman]") { // create a table of various sizes @@ -173,7 +183,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { REQUIRE(state.before_launch().ok()); state.launch(); REQUIRE(state.algo().exposed_qty().value() == s.expected); - REQUIRE(s.expected == child_exposed(state.child_orders())); + REQUIRE(s.expected == count_exposed(state.child_orders())); } } SECTION("Reduce qty on market shrink (immidiate)") { @@ -187,7 +197,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { REQUIRE(state.algo().exposed_qty() == Quantity(400)); // Check that orders were created - REQUIRE(400 == child_exposed(state.child_orders())); + REQUIRE(400 == count_exposed(state.child_orders())); for(const auto& ch : state.child_orders()) { REQUIRE(ch.cstate.quantity == state.quantity()); } @@ -197,7 +207,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { state.update_book(book); REQUIRE(state.algo().exposed_qty() == Quantity(250)); REQUIRE( - 250 == child_exposed(state.child_orders())); + 250 == count_exposed(state.child_orders())); } SECTION("Increase qty w/ delay when market grows") { @@ -212,7 +222,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { // Check that orders were created REQUIRE( - 400 == child_exposed(state.child_orders())); + 400 == count_exposed(state.child_orders())); // INCREASE market book.bid()[0] = { some_price, 1001 }; @@ -220,16 +230,16 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { // NOTE: that the orders do not follow the market instantly // so this is still 400 REQUIRE( - 400 == child_exposed(state.child_orders())); + 400 == count_exposed(state.child_orders())); // Still 400 after the timer ticks but not enough time has passed state.advance_time(std::chrono::seconds()); REQUIRE( - 400 == child_exposed(state.child_orders())); + 400 == count_exposed(state.child_orders())); state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); REQUIRE( - 600 == child_exposed(state.child_orders())); + 600 == count_exposed(state.child_orders())); } SECTION("Respect maximum") { @@ -245,12 +255,11 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { // Check that orders were created REQUIRE( - input.steps.maximum == child_exposed(state.child_orders())); + input.steps.maximum == count_exposed(state.child_orders())); } } TEST_CASE("Dorman fills", "[algo][dorman]") { - AlgoParams input; input.steps.step_table = { {500, 250}, @@ -289,29 +298,79 @@ TEST_CASE("Dorman fills", "[algo][dorman]") { // Test cases for transitions w/ fills // - // target = 500 - // send 5 orders of size 100 - // market move, new target = 450 - // reduce order #5 to 50 - // Before we confirm the move - // - fill #4 for 80 - // :: algo needs to shed another 30 Quantity + SECTION("Complete by update") { + // target = 500, exposed = 650 + // Fill 400 + // Change target to 350 + // expect completion + AlgoTestDriver state(def, some_price, Quantity {500}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {500}); - // ^ same but fill #2 for 80 - - - /* - NOTES: - vector = f(fills, target, clip) - => feed into diff - */ + // Fill 400 + state.fill(price, Quantity { 400 }); + Update update; + update.set_quantity(350); + state.do_update(update); + REQUIRE(state.state() == AlgoState::COMPLETED); + } } TEST_CASE("Dorman overfill", "[algo][dorman]") { - SECTION("auto-scratch OFF") { - } + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + SECTION("auto-scratch ON") { + input.scratch = {{ 500 }}; } + SECTION("auto-scratch OFF") { + // both sections will run + input.scratch = std::nullopt; + } + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {100}); + state.allow_overfill(); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {100}); + REQUIRE(state.child_orders().size() == 1); + + state.fill(state.child_orders().begin()->key, price, Quantity {500}); + REQUIRE(state.state() == AlgoState::COMPLETED); + + if (input.scratch) { + // check that we scratched + REQUIRE(state.detached_orders().size() > 0); + } + else { + // check that we *did not* scratch + REQUIRE(state.detached_orders().size() == 0); + } + } TEST_CASE("Dorman profit-orders", "[algo][dorman]") { From fb08533488f5df0114854a08e8001340ba410d2e Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 8 Jul 2019 13:51:54 -0400 Subject: [PATCH 044/153] SP-32 profit orders --- .../orders/algo/algo_orders/DormanAlgo.cpp | 42 ++++++- .../orders/algo/algo_orders/DormanAlgo.h | 3 + .../algo/algo_orders/tests/AlgoTestDriver.cpp | 12 +- .../algo/algo_orders/tests/AlgoTestDriver.h | 1 + .../algo/algo_orders/tests/dorman_tests.cpp | 115 +++++++++++++++++- 5 files changed, 166 insertions(+), 7 deletions(-) diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp index ce8d33ce4..eec8210f0 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -42,6 +42,8 @@ struct DormanAlgo::Impl { ChildState child_state; uint32_t fills ={}; + + std::vector profit_orders; }; DormanAlgo::DormanAlgo() : @@ -113,7 +115,7 @@ void DormanAlgo::do_update(const api::orderman::Update& update) { } if (me->fills >= update.quantity()) { - algo().complete(); + complete(); } } @@ -125,6 +127,7 @@ void DormanAlgo::on_child_change(OrderKey key, const ChildState& state) { if (delta != 0) { me->fills += delta; algo().update_fill_state(Quantity {me->fills}, state.last_price); + send_profit_order(state.last_price, Quantity {delta}); } me->child_state = state; @@ -134,7 +137,7 @@ void DormanAlgo::on_child_change(OrderKey key, const ChildState& state) { } if (me->fills >= algo().quantity().value()) { - algo().complete(); + complete(); } } @@ -199,6 +202,14 @@ void DormanAlgo::on_timer() { } } +IAlgoOrder::OnCancelResponse DormanAlgo::on_cancel() { + // detach any remaining profit orders so they keep running + for(const auto& key : me->profit_orders) { + algo().detach_child(key); + } + return OnCancelResponse::HONOR; +} + IAlgoServices& DormanAlgo::algo() const { return *me->algo; } // @@ -258,6 +269,33 @@ void DormanAlgo::do_scratch() { auto scratch_id = algo().launch_child(def, price, Quantity { me->fills }, false); algo().detach_child(scratch_id); + complete(); +} + +void DormanAlgo::send_profit_order(Price p, Quantity q) { + if (me->params.profit_orders) { + auto side = securities::other_side(me->side); + Price offset = + Price { algo().security().contract_spec().low_tick() * me->params.profit_orders.value().ticks_away }; + auto price = + securities::worsen_price(side, p, offset); + + OrderDefinition def = me->splitter_definition; + def.set_time_in_force(TimeInForce::TIME_IN_FORCE_GTC); + def.set_side(side); + def.mutable_custom_def()->limit(); + me->profit_orders.push_back( + algo().launch_child(def, price, q, false)); + } +} + +void DormanAlgo::complete() { + // Allow profit orders to keep running + if (me->params.profit_orders) { + for(const auto& key : me->profit_orders) { + algo().detach_child(key); + } + } algo().complete(); } diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h index 6f262d8cb..4be257975 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h @@ -67,6 +67,7 @@ public: void on_child_change(OrderKey, ChildState const &) override; void on_book(SecurityKey, const Book&) override; void on_timer() override; + IAlgoOrder::OnCancelResponse on_cancel() override; Quantity exposed_qty() const; @@ -80,6 +81,8 @@ private: void pause_until(TimeStamp); void do_scratch(); + void send_profit_order(Price, Quantity); + void complete(); }; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index abd9f1cf3..12861b54e 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -162,6 +162,16 @@ void AlgoTestDriverBase::advance_time(Duration d) { me->algo.on_timer(); } } +IAlgoOrder::OnCancelResponse AlgoTestDriverBase::cancel() { + auto r = me->algo.on_cancel(); + if (r == IAlgoOrder::OnCancelResponse::HONOR) { + me->state = AlgoState::CANCELED; + for (auto& o : me->orders) { + o.cstate.status = ChildStatus::CANCELED; + } + } + return r; +} const std::vector& AlgoTestDriverBase::child_orders() const { return me->orders; @@ -392,7 +402,7 @@ void AlgoTestDriverBase::detach_all_children() { me->orders.clear(); } void AlgoTestDriverBase::cancel(std::string const &) { - me->state = AlgoState::CANCELED; + cancel(); } void AlgoTestDriverBase::terminate_error(std::string const &) { me->state = AlgoState::TERMINATED; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index 50ac02acd..1d8acc5d6 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -69,6 +69,7 @@ class AlgoTestDriverBase : private IAlgoServices { void update_book(const Book&); void advance_time(Duration); + IAlgoOrder::OnCancelResponse cancel(); // // Inspection methods diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp index b38121daf..5613de3d1 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -2,6 +2,7 @@ #include "../DormanAlgo.h" #include "AlgoTestDriver.h" #include "lib/orderman/orderman_utils/Book.h" +#include "lib/domain_model/Security.h" #include #include @@ -374,14 +375,120 @@ TEST_CASE("Dorman overfill", "[algo][dorman]") { } TEST_CASE("Dorman profit-orders", "[algo][dorman]") { - SECTION("profit-orders OFF") { - // Profit orders are not sent + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + + bool test_cancel = true; + + SECTION("Profit Orders ON - Cancel") { + // Send profit orders 3 ticks away + input.profit_orders = {{ 3 }}; } - SECTION("profit-orders ON") { - // profit orders are sent at the right price + SECTION("Profit Orders ON - Complete") { + // Send profit orders 3 ticks away + input.profit_orders = {{ 3 }}; + test_cancel = false; + } + SECTION("Profit Orders OFF") { + input.profit_orders = std::nullopt; + } + SECTION("Profit Orders OFF - complete") { + input.profit_orders = std::nullopt; + test_cancel = false; + } + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {100}); + auto secid = SecurityKey(state.security().id()); + auto tick = state.security().contract_spec().low_tick(); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {100}); + REQUIRE(state.child_orders().size() == 1); + + state.fill(state.child_orders().begin()->key, price, Quantity {20}); + + if (input.profit_orders) { + // We're testing the bid side so we expect 3 ticks up + auto expected_price = Price { + price.value() + (tick * input.profit_orders.value().ticks_away) }; + REQUIRE(state.child_orders().size() > 1); + REQUIRE(state.detached_orders().empty()); + size_t profit_order_count = 0; + for (const auto& order : state.child_orders()) { + if (order.def.value().side() == api::SIDE_SELL) { + profit_order_count += 1; + REQUIRE(order.cstate.quantity.value() == 20); + REQUIRE(securities::is_same(order.cstate.price, expected_price)); + } + } + REQUIRE(profit_order_count == 1); + } + else { + REQUIRE(state.child_orders().size() == 1); + REQUIRE(state.detached_orders().empty()); + } + + state.fill(state.child_orders().begin()->key, price, Quantity {42}); + + if (input.profit_orders) { + auto expected_price = Price { + price.value() + (tick * input.profit_orders.value().ticks_away) }; + REQUIRE(state.child_orders().size() > 1); + REQUIRE(state.detached_orders().empty()); + bool got_20 = false; + bool got_42 = false; + for (const auto& order : state.child_orders()) { + if (order.def.value().side() == api::SIDE_SELL) { + if (order.cstate.quantity.value() == 20) got_20 = true; + if (order.cstate.quantity.value() == 42) got_42 = true; + REQUIRE(securities::is_same(order.cstate.price, expected_price)); + } + } + REQUIRE(got_42); + REQUIRE(got_20); + } + else { + REQUIRE(state.child_orders().size() == 1); + REQUIRE(state.detached_orders().empty()); + } + + if (test_cancel) { + // Profit orders should detach on cancel + state.cancel(); + REQUIRE(state.state() == AlgoState::CANCELED); + if (input.profit_orders) { + REQUIRE(state.detached_orders().size() > 0); + } + } + else { + state.fill(state.child_orders().begin()->key, price, Quantity {38}); + REQUIRE(state.state() == AlgoState::COMPLETED); + if (input.profit_orders) { + REQUIRE(state.detached_orders().size() > 0); + } } } +// TEST_CASE("Dorman detaches profit orders when completed by scratch", "[algo][dorman][!hide]") { +// } + TEST_CASE("Dorman trade accumulation", "[algo][dorman]") { SECTION("exceed limit") { // Profit orders are not sent From dc922a0299e12974e86d263382297f0cf2dc71a8 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Mon, 8 Jul 2019 14:01:24 -0400 Subject: [PATCH 045/153] Fixed the test that was failing when Spreader:n_legs_recomp_hedges_on_fill: true --- .../orders/spreader_orders/tests/spreader_tests.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index 5ffa28931..e10981ea0 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -3034,6 +3034,7 @@ TEST_CASE ("spreader_perf_2legs", "[spreader]") } TEST_CASE ("spreader_perf_4legs", "[spreader]") { + bool recomp = config::get().n_legs_recomp_hedges_on_fill(); bts::orderman::OrderKey order_key(0); Price spreader_price = Price (40); Quantity spreader_quantity = Quantity (250); @@ -3216,7 +3217,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") 0.005)); REQUIRE(securities::test_price_equality( bookkeeping_data.get_leg_data (2).hedger_manager().test_only_get_hedger_at (0).order_state().out_price().value(), - 19.995, + recomp ? 19.990 : 19.995, 0.005)); // back to leg1's hedger, this is what will // be mainly tested here on @@ -3225,7 +3226,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); REQUIRE(securities::test_price_equality( hedger_1_1.order_state().out_price().value(), - 20.015, + recomp ? 20.005 : 20.015, 0.005)); REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), @@ -3260,7 +3261,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") REQUIRE(hedger_1_1.order_state().out_quantity().value() == 1); REQUIRE(securities::test_price_equality( hedger_1_1.order_state().out_price().value(), - 20.015, + recomp ? 20.005 : 20.015, 0.005)); REQUIRE(hedger_1_1.state()==HedgerState::WAITING_FOR_FIRST_ACK); spreader.test_only_on_child_order_update (hedger_1_1.order_state().order_key(), @@ -3297,7 +3298,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") REQUIRE(h.order_state().out_quantity().value()==2); REQUIRE(securities::test_price_equality( h.order_state().out_price().value(), - 20.015, + recomp ? 20.005 : 20.015, 0.005)); } // send order acks ////////// @@ -3312,7 +3313,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") REQUIRE(h.order_state().out_quantity().value()==2); REQUIRE(securities::test_price_equality( h.order_state().out_price().value(), - 20.015, + recomp ? 20.005 : 20.015, 0.005)); } REQUIRE(hm_1.n_active_hedgers()==4); @@ -3329,7 +3330,7 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") REQUIRE(h.order_state().out_quantity().value()==2); REQUIRE(securities::test_price_equality( h.order_state().out_price().value(), - 20.015, + recomp ? 20.005 : 20.015, 0.005)); } REQUIRE(hm_1.n_active_hedgers()==4); From 72a6689cd4010e0714f3eca392350206c873a9a8 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 8 Jul 2019 14:41:07 -0400 Subject: [PATCH 046/153] SP-117 Expose ticker to IAlgoOrder --- src/lib/orderman/events/TickerUpdateEvent.h | 1 + .../orderman/orders/algo/AlgoWrapperOrder.cpp | 16 ++++++++++++++++ src/lib/orderman/orders/algo/AlgoWrapperOrder.h | 2 ++ src/lib/orderman/orders/algo/IAlgoOrder.h | 4 ++++ src/lib/orderman/orders/algo/IAlgoServices.h | 4 ++++ .../algo/algo_orders/tests/AlgoTestDriver.cpp | 16 ++++++++++++++++ .../algo/algo_orders/tests/AlgoTestDriver.h | 4 ++++ src/server/sparkd/cmds/QuickTest.cpp | 2 ++ 8 files changed, 49 insertions(+) diff --git a/src/lib/orderman/events/TickerUpdateEvent.h b/src/lib/orderman/events/TickerUpdateEvent.h index d34522d48..c8082d169 100644 --- a/src/lib/orderman/events/TickerUpdateEvent.h +++ b/src/lib/orderman/events/TickerUpdateEvent.h @@ -6,6 +6,7 @@ #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/utils/bts_chrono.h" #include "lib/md/protos/co2.pb.h" +#include "boost/container/small_vector.hpp" namespace bts { namespace orderman { diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index c3a483969..2520fdc62 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -49,6 +49,7 @@ struct AlgoWrapperOrder::PImpl { OrderState const &state() const { return _state; } Map book_subs; + Map ticker_subs; Map children; bool has_timer_sub = false; @@ -364,6 +365,21 @@ void AlgoWrapperOrder::unsubscribe_book(SecurityKey k) { me->services->unsubscribe_from_book(it->second); } +void AlgoWrapperOrder::subscribe_ticker(SecurityKey k) { + if (!is_working()) return; + bts_assert(me->ticker_subs.find(k) == me->ticker_subs.end()); + + me->ticker_subs[k] = me->services->subscribe_to_ticker( + k, [this,k](const auto& ev) { me->algo->on_ticker(k, ev.trades); }); +} + +void AlgoWrapperOrder::unsubscribe_ticker(SecurityKey k) { + if (!is_working()) return; + bts_assert(me->ticker_subs.find(k) != me->ticker_subs.end()); + if (auto it = me->ticker_subs.find(k); it != me->ticker_subs.end()) + me->services->unsubscribe_from_ticker(it->second); +} + void AlgoWrapperOrder::subscribe_child_change(OrderKey k) { if (!is_working()) return; diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h index f11a84a23..472d9c2c0 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h @@ -46,6 +46,8 @@ class AlgoWrapperOrder : public Order, void unsubscribe_child_change(OrderKey) override; void subscribe_timer() override; void unsubscribe_timer() override; + void subscribe_ticker(SecurityKey) override; + void unsubscribe_ticker(SecurityKey) override; OrderKey launch_child(ChildTemplate &, Price, Quantity, bool subscribe = true) override; OrderKey launch_child(api::orderman::OrderDefinition const &, Price, diff --git a/src/lib/orderman/orders/algo/IAlgoOrder.h b/src/lib/orderman/orders/algo/IAlgoOrder.h index 3b882b49d..6fc25a893 100644 --- a/src/lib/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib/orderman/orders/algo/IAlgoOrder.h @@ -1,6 +1,7 @@ #pragma once #include "lib/orderman/orders/Order.h" +#include "lib/orderman/events/TickerUpdateEvent.h" #include namespace bts { @@ -36,6 +37,9 @@ class IAlgoOrder { // The book for the specified security has changed virtual void on_book(SecurityKey, Book const &) {} + // Trades stream for the specified security + virtual void on_ticker(SecurityKey, const boost::container::small_vector&) {}; + // A period timer has run out virtual void on_timer() {} diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index 4d9cd2f2f..5fb5acd28 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -38,6 +38,10 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { virtual void subscribe_timer() = 0; virtual void unsubscribe_timer() = 0; + // Subscribe to trade ticker + virtual void subscribe_ticker(SecurityKey) = 0; + virtual void unsubscribe_ticker(SecurityKey) = 0; + // -------------------------------------------------------------- // Child management // -------------------------------------------------------------- diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index 12861b54e..4343d9a68 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -63,6 +63,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter { std::map books; std::map book_subs; + std::map ticker_subs; bool sub_timer = false; // Callbacks @@ -156,6 +157,12 @@ void AlgoTestDriverBase::update_book(const Book& book) { me->algo.on_book(key, book); } } +void AlgoTestDriverBase::update_ticker(SecurityKey key, boost::container::small_vector& data) { + auto iter = me->ticker_subs.find(key); + if (iter != me->ticker_subs.end() && iter->second) { + me->algo.on_ticker(key, data); + } +} void AlgoTestDriverBase::advance_time(Duration d) { me->now += d; if (me->sub_timer) { @@ -310,6 +317,14 @@ void AlgoTestDriverBase::subscribe_book(SecurityKey key){ void AlgoTestDriverBase::unsubscribe_book(SecurityKey key) { me->book_subs.erase(key); } + +void AlgoTestDriverBase::subscribe_ticker(SecurityKey key) { + me->ticker_subs[key] = true; +} +void AlgoTestDriverBase::unsubscribe_ticker(SecurityKey key) { + me->ticker_subs.erase(key); +} + void AlgoTestDriverBase::subscribe_child_change(OrderKey k) { for (auto& child : me->orders) { if (child.key == k) { @@ -327,6 +342,7 @@ void AlgoTestDriverBase::unsubscribe_child_change(OrderKey k) { void AlgoTestDriverBase::subscribe_timer() { me->sub_timer = true; } void AlgoTestDriverBase::unsubscribe_timer() { me->sub_timer = false; } + OrderKey AlgoTestDriverBase::launch_child(ChildTemplate&, Price price, Quantity qty, bool sub) { OrderKey key = me->next_order(); me->orders.push_back( diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index 1d8acc5d6..ffa0060e6 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -1,6 +1,7 @@ #pragma once #include "lib/orderman/orders/algo/IAlgoOrder.h" #include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/events/TickerUpdateEvent.h" #include "lib/md/IntPrice.h" #include #include @@ -68,6 +69,7 @@ class AlgoTestDriverBase : private IAlgoServices { void do_update(const api::orderman::Update&); void update_book(const Book&); + void update_ticker(SecurityKey, boost::container::small_vector&); void advance_time(Duration); IAlgoOrder::OnCancelResponse cancel(); @@ -112,6 +114,8 @@ private: void unsubscribe_child_change(OrderKey) override; void subscribe_timer() override; void unsubscribe_timer() override; + void subscribe_ticker(SecurityKey) override; + void unsubscribe_ticker(SecurityKey) override; OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) override; OrderKey launch_child(api::orderman::OrderDefinition const &, Price, diff --git a/src/server/sparkd/cmds/QuickTest.cpp b/src/server/sparkd/cmds/QuickTest.cpp index f6f86be5a..37f087650 100644 --- a/src/server/sparkd/cmds/QuickTest.cpp +++ b/src/server/sparkd/cmds/QuickTest.cpp @@ -33,6 +33,8 @@ struct Proxy : public IAlgoServices { void unsubscribe_child_change(OrderKey) {} void subscribe_timer() {} void unsubscribe_timer() {} + void subscribe_ticker(SecurityKey) override {} + void unsubscribe_ticker(SecurityKey) override {} OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) { return {}; } OrderKey launch_child(api::orderman::OrderDefinition const &, Price, Quantity, bool) { From 74a91a7d2e2212ef7f31c4f681ba1e2ee7a21ac7 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 8 Jul 2019 14:49:44 -0400 Subject: [PATCH 047/153] SP-117 typedef TradeVector type --- src/lib/orderman/events/TickerUpdateEvent.h | 3 ++- src/lib/orderman/orders/algo/IAlgoOrder.h | 2 +- .../orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp | 2 +- .../orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/orderman/events/TickerUpdateEvent.h b/src/lib/orderman/events/TickerUpdateEvent.h index c8082d169..029d0fc04 100644 --- a/src/lib/orderman/events/TickerUpdateEvent.h +++ b/src/lib/orderman/events/TickerUpdateEvent.h @@ -28,9 +28,10 @@ class TickerUpdateEvent : public mercury::Event { co2::AggressorSide aggressor_side; }; - boost::container::small_vector trades; + boost::container::small_vector trades; TickerUpdateEvent(SecurityKey key) : _key(key), trades() {} }; +typedef boost::container::small_vector TradeVector; } } diff --git a/src/lib/orderman/orders/algo/IAlgoOrder.h b/src/lib/orderman/orders/algo/IAlgoOrder.h index 6fc25a893..15cf711d5 100644 --- a/src/lib/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib/orderman/orders/algo/IAlgoOrder.h @@ -38,7 +38,7 @@ class IAlgoOrder { virtual void on_book(SecurityKey, Book const &) {} // Trades stream for the specified security - virtual void on_ticker(SecurityKey, const boost::container::small_vector&) {}; + virtual void on_ticker(SecurityKey, const TradeVector&) {}; // A period timer has run out virtual void on_timer() {} diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index 4343d9a68..777553d29 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -157,7 +157,7 @@ void AlgoTestDriverBase::update_book(const Book& book) { me->algo.on_book(key, book); } } -void AlgoTestDriverBase::update_ticker(SecurityKey key, boost::container::small_vector& data) { +void AlgoTestDriverBase::update_ticker(SecurityKey key, TradeVector& data) { auto iter = me->ticker_subs.find(key); if (iter != me->ticker_subs.end() && iter->second) { me->algo.on_ticker(key, data); diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index ffa0060e6..917c36bdd 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -69,7 +69,7 @@ class AlgoTestDriverBase : private IAlgoServices { void do_update(const api::orderman::Update&); void update_book(const Book&); - void update_ticker(SecurityKey, boost::container::small_vector&); + void update_ticker(SecurityKey, TradeVector&); void advance_time(Duration); IAlgoOrder::OnCancelResponse cancel(); From 6b799bf93b1a1920a844c3aa98fe7ced018ee941 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 8 Jul 2019 15:37:35 -0500 Subject: [PATCH 048/153] [non-functional] normalize spacing/tabs in LoginView.xaml --- .../spark/view/Shell/Login/LoginView.xaml | 293 ++++++++++-------- 1 file changed, 157 insertions(+), 136 deletions(-) diff --git a/src/client/spark/view/Shell/Login/LoginView.xaml b/src/client/spark/view/Shell/Login/LoginView.xaml index 61ab1e961..e11a66b2f 100644 --- a/src/client/spark/view/Shell/Login/LoginView.xaml +++ b/src/client/spark/view/Shell/Login/LoginView.xaml @@ -1,143 +1,164 @@ - - - - - - - - + + + + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - @@ -580,4 +444,4 @@ - \ No newline at end of file + diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs index 95214f635..f14a1798f 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs @@ -1,13 +1,10 @@ using mbts.spark; using mbts.ui.converters; -using System; using System.Windows.Controls; using System.Windows.Data; -using System.Windows.Input; namespace mbts.ui.spark { partial class AlgoStudioView : UserControl { - public const string BadgesInTreeViewVisibilityKey = nameof(BadgesInTreeViewVisibilityKey); public static readonly IValueConverter EditPaneVisualStateConverter = ValueConverter.create(obj => obj == null ? VisualStates.Inactive : VisualStates.Active); @@ -18,7 +15,7 @@ namespace mbts.ui.spark { DataContextChanged += (s, e) => set_controller(); set_controller(); // local methods - void set_controller() => Disposable.replace(ref _controller, AlgoStudioViewControllerFactory.create(DataContext, _tree_view, name_text_box, custom_badge_color_picker_button, custom_badge_color_picker_popup)); + void set_controller() => Disposable.replace(ref _controller, AlgoStudioViewControllerFactory.create(DataContext, name_text_box, custom_badge_color_picker_button, custom_badge_color_picker_popup)); } public new AlgoStudioVM DataContext { @@ -28,15 +25,6 @@ namespace mbts.ui.spark { public void add_definition() => _controller.add_definition(); public void add_definition_clone(AlgoDefinitionVM src_definition) => _controller.add_definition_clone(src_definition); - public void add_template(AlgoDefinitionVM definition) => _controller.add_template(definition); - public void rename_definition(AlgoDefinitionVM definition) => _controller.rename_definition(definition); - public void rename_template(AlgoDefinitionTemplateVM template) => _controller.rename_template(template); - public void select_definition(AlgoDefinitionVM definition) => _controller.select_definition(definition); - public void select_template(AlgoDefinitionTemplateVM template) => _controller.select_template(template); - public IDisposable preserve_selected_definition() => _controller.preserve_selected_definition(); - - void _end_edit_executed(object s, ExecutedRoutedEventArgs e) => - _controller.end_edit(e); public static class VisualStateNames { public static class EditPane { @@ -75,14 +63,6 @@ namespace mbts.ui.spark { Inactive = $"{GroupName}{VisualStates.Inactive}"; } - public static class TreeViewBadge { - public const string GroupName = nameof(TreeViewBadge); - - public static readonly string - Active = $"{GroupName}{VisualStates.Active}", - Inactive = $"{GroupName}{VisualStates.Inactive}"; - } - public static class FavoriteToggleButton { public const string GroupName = nameof(FavoriteToggleButton); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs index dbdbd1f7b..6fa462c60 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs @@ -1,8 +1,6 @@ using mbts.spark; using mbts.ui.controls; using System; -using System.Collections.Generic; -using System.Linq; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; @@ -11,21 +9,14 @@ namespace mbts.ui.spark { interface IAlgoStudioViewController : IDisposable { void add_definition(); void add_definition_clone(AlgoDefinitionVM src_definition); - void add_template(AlgoDefinitionVM definition); - void rename_definition(AlgoDefinitionVM definition); - void rename_template(AlgoDefinitionTemplateVM template); - void end_edit(ExecutedRoutedEventArgs e); - void select_definition(AlgoDefinitionVM definition); - void select_template(AlgoDefinitionTemplateVM template); - IDisposable preserve_selected_definition(); } class AlgoStudioViewControllerFactory { static readonly IAlgoStudioViewController _empty = new EmptyController(); - public static IAlgoStudioViewController create(AlgoStudioVM vm, TreeView tree_view, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) => - vm == null || tree_view == null || name_text_box == null || color_picker_button == null || color_picker_popup == null ? - _empty : new Controller(vm, tree_view, name_text_box, color_picker_button, color_picker_popup); + public static IAlgoStudioViewController create(AlgoStudioVM vm, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) => + vm == null || name_text_box == null || color_picker_button == null || color_picker_popup == null ? + _empty : new Controller(vm, name_text_box, color_picker_button, color_picker_popup); /// /// This wrapper class is returned by when the vm or tree_view is null. @@ -34,25 +25,16 @@ namespace mbts.ui.spark { class EmptyController : IAlgoStudioViewController { public void add_definition() { } public void add_definition_clone(AlgoDefinitionVM src_definition) { } - public void add_template(AlgoDefinitionVM definition) { } - public void rename_definition(AlgoDefinitionVM definition) { } - public void rename_template(AlgoDefinitionTemplateVM template) { } - public void end_edit(ExecutedRoutedEventArgs e) { } - public void select_definition(AlgoDefinitionVM definition) { } - public void select_template(AlgoDefinitionTemplateVM template) { } - public IDisposable preserve_selected_definition() => Disposable.empty; public void Dispose() { } } class Controller : IAlgoStudioViewController { readonly AlgoStudioVM _vm; - readonly TreeView _tree_view; readonly TextBox _name_text_box; IDisposable _dispose; - public Controller(AlgoStudioVM vm, TreeView tree_view, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) { + public Controller(AlgoStudioVM vm, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) { _vm = vm; - _tree_view = tree_view; _name_text_box = name_text_box; _dispose = PopupViewController.attach(color_picker_popup, color_picker_button); } @@ -67,67 +49,11 @@ namespace mbts.ui.spark { _begin_edit(); } - public void add_template(AlgoDefinitionVM def) { - var new_tmp = def.template_factory(); - def.templates.Add(new_tmp); - _begin_edit(); - } - - public void end_edit(ExecutedRoutedEventArgs e) { - if (e.OriginalSource is EditableTextBlock etb) { - etb.end_edit(); - _get_container(etb.DataContext)?.SetCurrentValue(TreeViewItem.IsSelectedProperty, true); - } - } - - public void rename_definition(AlgoDefinitionVM definition) { - if (_tree_view.ItemContainerGenerator.ContainerFromItem(definition) is TreeViewItem tvi) { - var etb = tvi.descendants_of_type().FirstOrDefault(); - etb.SetCurrentValue(EditableTextBlock.IsEditingProperty, true); - } - } - - public void rename_template(AlgoDefinitionTemplateVM template) { - if (_tree_view.ItemContainerGenerator.ContainerFromItem(template.owner) is TreeViewItem tvi) { - if (tvi.ItemContainerGenerator.ContainerFromItem(template) is TreeViewItem ttvi) { - var etb = ttvi.descendants_of_type().FirstOrDefault(); - etb.SetCurrentValue(EditableTextBlock.IsEditingProperty, true); - } - } - } - - public void select_definition(AlgoDefinitionVM definition) => - _tree_view.ItemContainerGenerator.ContainerFromItem(definition)?.SetCurrentValue(TreeViewItem.IsSelectedProperty, true); - - public void select_template(AlgoDefinitionTemplateVM template) { - if (_tree_view.ItemContainerGenerator.ContainerFromItem(template.owner) is TreeViewItem def_view) { - def_view.SetCurrentValue(TreeViewItem.IsSelectedProperty, true); - def_view.ItemContainerGenerator.ContainerFromItem(template)?.SetCurrentValue(TreeViewItem.IsSelectedProperty, true); - } - } - - public IDisposable preserve_selected_definition() => - Disposable.Create(() => { - var name = _vm.selected_definition.name; - if (_vm.definitions.TryGetFirst(d => d.name == name, out AlgoDefinitionVM def)) - _tree_view.ItemContainerGenerator.ContainerFromItem(def)?.SetCurrentValue(TreeViewItem.IsSelectedProperty, true); - }); - public void Dispose() => Disposable.dispose(ref _dispose); void _begin_edit() => WPFHelper.push_frame_until(() => Keyboard.Focus(_name_text_box) == _name_text_box); - - TreeViewItem _get_container(object obj) => - obj is AlgoDefinitionTemplateVM t ? _get_container(t) : - obj is AlgoDefinitionVM d ? _get_container(d) : null; - - TreeViewItem _get_container(AlgoDefinitionVM d) => - _tree_view.ItemContainerGenerator.ContainerFromItem(d) as TreeViewItem; - - TreeViewItem _get_container(AlgoDefinitionTemplateVM t) => - _get_container(t.owner) is TreeViewItem dview ? dview.ItemContainerGenerator.ContainerFromItem(t) as TreeViewItem : null; } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs index 24164b38f..d0e80f526 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs @@ -62,35 +62,18 @@ namespace mbts.spark { _vm = new AlgoStudioVM(_model); _vm.load_from(_state); _view = new AlgoStudioView { DataContext = _vm }; - _view.Resources[AlgoStudioView.BadgesInTreeViewVisibilityKey] = _ui_configuration.show_badges_in_tree_view ? Visibility.Visible : Visibility.Collapsed; _resources = new CompositeDisposable(); - // observable sources - var content_src = _vm.observe_selected().Select(_ => _vm.content_source); // add definition _commands.Add(new SparkCommandCommand("Add Algo", Icons.AddBoxes, new DelegateCommand(_view.add_definition))); // add definition clone var add_definition_clone_spark_cmd = new ObservantSparkCommandCommand(Icons.DocumentCopy, d => $"Clone '{d?.name}'", d => d != null, _view.add_definition_clone); _commands.Add(add_definition_clone_spark_cmd); - // add template - var add_template_spark_cmd = new ObservantSparkCommandCommand(Icons.AddFile, d => $"Add '{d?.name}' Template", d => d != null, _view.add_template) { - show_in_toolbar = (_ui_configuration.add_template_button & bts.config.AlgoStudioOptions.AddTemplateButton.MainToolBar) == bts.config.AlgoStudioOptions.AddTemplateButton.MainToolBar, - show_in_context_menu = (_ui_configuration.add_template_button & bts.config.AlgoStudioOptions.AddTemplateButton.DefinitionContextMenu) == bts.config.AlgoStudioOptions.AddTemplateButton.DefinitionContextMenu - }; - _commands.Add(add_template_spark_cmd); - _resources.Add(_vm.subscribe_definition().OnDispatcher().Subscribe(add_definition_clone_spark_cmd, add_template_spark_cmd)); + _resources.Add(_vm.subscribe_definition().OnDispatcher().Subscribe(add_definition_clone_spark_cmd)); // save if (_ui_configuration.can_save_specific) { - var content_is_dirty_src = _vm.observe_selected().SelectManyInSeries(t => { - if (t.template != null) { - return t.template.observe_is_dirty(); - } else if (t.definition != null) { - return t.definition.subscribe_is_definition_dirty(); - } else return Observable.Return(false); - }); - _commands.Add(new ReactiveSparkCommand(content_src.OnDispatcher().Select(c => $"Save '{c?.name}'"), Icons.DiskFile, () => { - if (_vm.selected_template is AlgoDefinitionTemplateVM t) - _vm.save_template(t).ignore(); - else if (_vm.selected_definition is AlgoDefinitionVM d) + var content_is_dirty_src = _vm.subscribe_definition().SelectManyInSeries(def => def?.subscribe_is_definition_dirty() ?? Observable.Return(false)); + _commands.Add(new ReactiveSparkCommand(_vm.subscribe_definition().OnDispatcher().Select(c => $"Save '{c?.name}'"), Icons.DiskFile, () => { + if (_vm.selected_definition is AlgoDefinitionVM d) _vm.save_definition(d).ignore(); }, content_is_dirty_src)); } @@ -101,48 +84,39 @@ namespace mbts.spark { // undo/redo if (_ui_configuration.can_undo_redo) { // undo - var undo_cmd = new ObservantCommand(() => _vm.content_source?.try_undo()); + var undo_cmd = new ObservantCommand(() => _vm.selected_definition?.try_undo()); var undo_spark_cmd = new SparkCommandCommand("Undo", Icons.UndoCircular, undo_cmd); - var can_undo_source = _vm.observe_selected().SelectManyInSeries(tup => { - IAlgoStudioContentVM content = (IAlgoStudioContentVM)tup.template ?? tup.definition; - return content != null ? + var can_undo_source = _vm.subscribe_definition().SelectManyInSeries(def => + def != null ? Observable.Create(o => { - content.can_undo_changed += notify; + def.can_undo_changed += notify; notify(); - return Disposable.Create(() => content.can_undo_changed -= notify); - void notify() => o.OnNext(content.can_undo); + return Disposable.Create(() => def.can_undo_changed -= notify); + void notify() => o.OnNext(def.can_undo); }) : - Observable.Return(false); - }); + Observable.Return(false)); _commands.Add(undo_spark_cmd); _resources.Add(can_undo_source.Subscribe(undo_cmd)); // redo - var redo_cmd = new ObservantCommand(() => _vm.content_source?.try_redo()); + var redo_cmd = new ObservantCommand(() => _vm.selected_definition?.try_redo()); var redo_spark_cmd = new SparkCommandCommand("Redo", Icons.RedoCircular, redo_cmd); - var can_redo_source = _vm.observe_selected().SelectManyInSeries(tup => { - IAlgoStudioContentVM content = (IAlgoStudioContentVM)tup.template ?? tup.definition; - return content != null ? + var can_redo_source = _vm.subscribe_definition().SelectManyInSeries(def => + def != null ? Observable.Create(o => { - content.can_redo_changed += notify; + def.can_redo_changed += notify; notify(); - return Disposable.Create(() => content.can_redo_changed -= notify); - void notify() => o.OnNext(content.can_redo); + return Disposable.Create(() => def.can_redo_changed -= notify); + void notify() => o.OnNext(def.can_redo); }) : - Observable.Return(false); - }); + Observable.Return(false)); _commands.Add(redo_spark_cmd); _resources.Add(can_redo_source.Subscribe(redo_cmd)); } // remove var remove_spark_cmd = new ObservantSparkCommandCommand(Icons.Trash, v => $"Remove '{v?.name}'", v => v != null, value => { - if (value is AlgoDefinitionTemplateVM t) { - _vm.remove_template(t).ignore(); - } else if (value is AlgoDefinitionVM d) { + if (value is AlgoDefinitionVM d) { string prompt; MessageBoxImage icon; - if (_model.algo_model.templates_enabled() && d.templates.Count > 0) { - prompt = $"Deleting this definition ({d.name}) will also delete its templates ({d.templates.Count}).\n\nDo you wan to Continue?"; - icon = MessageBoxImage.Warning; - } else if (d.key.mode == AlgoKey.Mode.Persisted) { + if (d.key.mode == AlgoKey.Mode.Persisted) { prompt = $"Are you sure you want to delete '{d.name}?'"; icon = MessageBoxImage.Question; } else { @@ -162,7 +136,7 @@ namespace mbts.spark { } }); _commands.Add(remove_spark_cmd); - _resources.Add(content_src.OnDispatcher().Subscribe(remove_spark_cmd)); + _resources.Add(_vm.subscribe_definition().OnDispatcher().Subscribe(remove_spark_cmd)); } void _dispose_core() { diff --git a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs deleted file mode 100644 index b036f0671..000000000 --- a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs +++ /dev/null @@ -1,319 +0,0 @@ -using bts; -using bts.api.core; -using bts.api.orderman; -using bts.persist.algo; -using bts.spark; -using mbts.utils; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reactive.Disposables; -using System.Reactive.Linq; -using System.Threading.Tasks; - -namespace mbts.spark { - class AlgoStudioDefinitionTemplateModel : IAlgoStudioModel, IDisposable { - readonly AlgoService _service; - readonly AdminModel _admin_model; - readonly ErrorReporter _error_reporter; - readonly ObservableDemuxer _metadata_demuxer = new ObservableDemuxer(); - readonly TableUpdateDemuxer _definition_templates_demuxer; - IReadOnlyDictionary _algos = DictionaryHelper.empty(); - IReadOnlyDictionary _algo_templates = DictionaryHelper.empty(); - IDisposable _dispose; - - public AlgoStudioDefinitionTemplateModel(AlgoService service, IAlgoDefinitionMetadataService meta, AdminModel admin_model, ErrorReporter error_reporter, bts.config.AlgoStudioOptions ui_configuration) { - _service = service; - metadata_service = meta; - _admin_model = admin_model; - _error_reporter = error_reporter; - this.ui_configuration = ui_configuration; - _definition_templates_demuxer = new TableUpdateDemuxer(t => t.key, t => t.definition_key); - definitions = _service.subscribei_algos() - .Select(u => new Update( - u.all.Select(_Utils.definition_data).ToList(), - u.added.Select(_Utils.definition_data).ToList(), - u.changes.Select(ch => new Change(_Utils.definition_data(ch.Old), _Utils.definition_data(ch.New))).ToList(), - u.removed.Select(d => _Utils.definition_data(d)).ToList(), - def => def.key)) - .Table() - .Cache(); - templates = _service.subscribei_algo_templates() - .Select(u => new Update( - u.all.Select(_Utils.template_data).ToList(), - u.added.Select(_Utils.template_data).ToList(), - u.changes.Select(ch => new Change(_Utils.template_data(ch.Old), _Utils.template_data(ch.New))).ToList(), - u.removed.Select(_Utils.template_data).ToList(), - def => def.key)) - .Table() - .Cache(); - _dispose = new CompositeDisposable( - definitions.OnDispatcher().Subscribe(t => _algos = t.state), - templates.OnDispatcher().Subscribe(t => _algo_templates = t.state), - templates.Subscribe(_definition_templates_demuxer), - metadata_service.subscribe_metadata().Subscribe(_metadata_demuxer)); - } - - public IObservable> definitions { get; } - public IObservable> templates { get; } - public IAlgoDefinitionMetadataService metadata_service { get; } - public bts.config.AlgoStudioOptions ui_configuration { get; } - - public IObservable> subscribe_definition_templates(AlgoKey definition) => - _definition_templates_demuxer.subscribe(definition); - - public async Task save_definition(AlgoData definition_data) { - if (definition_data.key.is_persisted) { - // TODO: implement update op when backend ready - } else { - // create op - return await _create_definition(new CompositeAlgoDefinitionImmutable(_Utils.to_definition(definition_data))); - } - return null; - } - public async Task save_template(AlgoTemplateData template_data) { - if (template_data.key.is_persisted) { - // update op - return await _update_template(_Utils.to_recordi(template_data)); - } else { - // create op - return await _create_template(_Utils.to_templatei(template_data)); - } - } - - /// - /// The api is not used here (there are some unresolved dependency issues with definitions on the backend). - /// The definition is marked as hidden in the AlgoMetadata stored on the server. - /// - /// For implementation details, see: , - /// - public async Task remove_definition(AlgoKey key) { - if (_algos.TryGetValue(key, out AlgoData curr)) { - if (_definition_templates_demuxer.try_get_values(key, out IReadOnlyDictionary tmps)) { - foreach (var t in tmps.Values) - await remove_template(t.key); - } - await metadata_service.hide_definition(key); - return true; - } - return false; - } - - public async Task remove_template(AlgoKey template_key) { - var req = new AlgoServiceRemoveRequest(); - _Utils.set_key(ref req, template_key); - var reply = await _safe_execute(() => _service.remove_template_async(req), ActionMetadata.remove_template); - return reply.success && reply.value.Success; - } - - public void Dispose() => - Disposable.dispose(ref _dispose); - - async Task _create_definition(CompositeAlgoDefinitionImmutable definition) => - await _create_definition(definition, _admin_model.user.id); - - async Task _create_template(AlgoTemplateImmutable template) { - var (value, success) = await _error_reporter.safe_execute(() => _service.create_template_async( - new TemplateCreateRequest { UserId = _admin_model.user.id, NewTemplate = template.CloneToMutable() }), - r => r.Success, r => r.ErrMsg, ActionMetadata.CreateTemplate); - if (success) - return _Utils.template_data(value.Record); - return null; - } - - async Task _update_template(AlgoTemplateRecordImmutable template) { - var (value, success) = await _error_reporter.safe_execute(() => _service.update_template_async( - new TemplateUpdateRequest { Key = template.Key, UpdatedTemplate = template.AlgoTemplate.CloneToMutable() }), - r => r.Success, r => r.ErrMsg.ToString(), ActionMetadata.UpdateTemplate); - if (success) - return _Utils.template_data(template); - return null; - } - - async Task _create_definition(CompositeAlgoDefinitionImmutable definition, uint user_id) { - (var result, var success) = await _error_reporter.safe_execute(() => _service.create_composite_algo_async( - new CompositeAlgoCreateRequest { - NewAlgo = definition.CloneToMutable(), - UserId = user_id - }), r => r.Success, r => r.ErrMsg, "create definition"); - return success ? _Utils.definition_data(new CompositeAlgoRecordImmutable(result.Record)) : (AlgoData?)null; - } - - async Task> _safe_execute(Func>> execute, IActionMetadata am) { - try { - var result = await execute(); - if (!result.success) { - _report_error(am.present_progressive_desc, result.error_message); - } else if (!am.get_success(result.value)) { - _report_error(am.present_progressive_desc, am.get_error_msg(result.value)); - } - return result; - } catch (Exception ex) { - _report_error(am.present_progressive_desc, ex.ToString()); - } - return new Result(bts.api.ApiError.UnexpectedResponse); - } - - void _report_error(string present_progressive_desc, string msg) => - _error_reporter.report_error($"Error {present_progressive_desc}: {msg}", AlgoStudioWindow.NAME, System.Windows.MessageBoxImage.Warning); - - void _report_no_user_id(string desc) => - _error_reporter.report_error($"{desc} failed: did not find current user id"); - - // used for error handling/reporting - interface IActionMetadata { - string desc { get; } - string present_progressive_desc { get; } - } - - interface IActionMetadata : IActionMetadata { - bool get_success(TReply reply); - string get_error_msg(TReply reply); - } - - static class ActionMetadata { - public const string - CreateDefinition = "create definition", - UpdateDefinition = "update definition", - CreateTemplate = "create template", - UpdateTemplate = "update template"; - public static readonly IActionMetadata update_template = new Impl(_update, _template, r => r.Success, r => r.ErrMsg.ToString()); - public static readonly IActionMetadata remove_template = new Impl(_delete, _template, r => r.Success, r => r.ErrMsg.ToString()); - public static readonly IActionMetadata create_template = new Impl(_create, _template, r => r.Success, r => r.ErrMsg); - public static readonly IActionMetadata create_definition = new Impl(_create, _definition, r => r.Success, r => r.ErrMsg); - - static readonly DescTuple _create = new DescTuple("create", "creating"), _update = new DescTuple("update", "updating"), _delete = new DescTuple("delete", "deleting"); - static readonly string _definition = "definition", _template = "template"; - - class Impl : IActionMetadata { - readonly DescTuple _desc; - readonly string _subject; - readonly Func _get_success; - readonly Func _get_error_msg; - - public Impl(DescTuple desc, string subject, Func get_success, Func get_error_msg) { - _desc = desc; - _subject = subject; - _get_success = get_success; - _get_error_msg = get_error_msg; - } - public string desc => _desc.desc; - public string present_progressive_desc => _desc.present_progressive_desc; - public bool get_success(T reply) => _get_success(reply); - public string get_error_msg(T reply) => _get_error_msg(reply); - } - - struct DescTuple { - public readonly string desc, present_progressive_desc; - public DescTuple(string desc, string present_progressive_desc) { - this.desc = desc; - this.present_progressive_desc = present_progressive_desc; - } - } - } - - /// - /// The AlgoService protobuf types have been extracted out of the viewmodels (, , , ). - /// The viewmodels instead use these types to represent the data: is used in place of , and is used in place of . - /// This thin abstraction isolates the server-side from the client-side, so if one day the server side types change, we won't need to change any client-side code except the methods below which handle the conversion from server type to corresponding client type. - /// There are a couple other methods in here as well (this is an extensions class afterall), but [John]I am going out of town soon (1/6/18) and did not get a chance to organize these methods beyond containing them in . - /// - static class _Utils { - // AlgoTemplateImmutable custom_order_definition(i) - public static CustomOrderDefinitionImmutable custom_order_definitioni(AlgoTemplateImmutable templatei) { - switch (templatei.AlgoParamsCase) { - case AlgoTemplate.AlgoParamsOneofCase.CompositeAlgoParams: return templatei.CompositeAlgoParams.Algo; - case AlgoTemplate.AlgoParamsOneofCase.SystemAlgoParams: return templatei.SystemAlgoParams; - default: throw ExceptionBuilder.Argument.unexpected_case(templatei.AlgoParamsCase, nameof(templatei)); - } - } - - public static AlgoData definition_data(AlgoTemplateImmutable templatei) { - switch (templatei.AlgoParamsCase) { - case AlgoTemplate.AlgoParamsOneofCase.CompositeAlgoParams: return definition_data(templatei.CompositeAlgoParams); - case AlgoTemplate.AlgoParamsOneofCase.SystemAlgoParams: - return new AlgoData( - templatei.SystemAlgoParams, - templatei.SystemAlgoParams.CustomCase.display_name(), - get_key(templatei.SystemAlgoParams)); - default: throw ExceptionBuilder.Argument.unexpected_case(templatei.AlgoParamsCase, nameof(templatei)); - } - } - - public static AlgoData definition_data(CompositeAlgoRecordImmutable recordi) => new AlgoData(recordi.AlgoDefinition.Algo, recordi.AlgoDefinition.Name, get_key(recordi)); - - public static AlgoData definition_data(CompositeAlgoDefinitionImmutable definitioni) => - new AlgoData(definitioni.Algo, definitioni.Name, get_key(definitioni)); - - public static AlgoData? definition_data(Result result) { - if (result.success && result.value.Success) { - var r = result.value.Record; - return new AlgoData(new CustomOrderDefinitionImmutable(r.AlgoDefinition.Algo), r.AlgoDefinition.Name, new AlgoKey(new AlgoPersistedKey(r.Key))); - } else return null; - } - - // AlgoTemplateRecord(Immutable) template data - public static AlgoTemplateData template_data(AlgoTemplateRecord record) => template_data(new AlgoTemplateRecordImmutable(record)); - public static AlgoTemplateData template_data(AlgoTemplateRecordImmutable recordi) => new AlgoTemplateData(custom_order_definitioni(recordi.AlgoTemplate), recordi.AlgoTemplate.Name, get_key(recordi), definition_data(recordi.AlgoTemplate)); - - public static CompositeAlgoDefinition to_definition(AlgoData definition_data) => - new CompositeAlgoDefinition { - Name = definition_data.name, - Algo = definition_data.value.CloneToMutable() - }; - - // AlgoTemplateData to_record(i) - public static AlgoTemplateRecord to_record(AlgoTemplateData template) { - var record = new AlgoTemplateRecord { - AlgoTemplate = new AlgoTemplate { - Name = template.name, - CompositeAlgoParams = new CompositeAlgoDefinition { - Name = template.definition.name, - Algo = template.value.CloneToMutable() - } - } - }; - set_key(ref record, template.key); - return record; - } - public static AlgoTemplateRecordImmutable to_recordi(AlgoTemplateData template) => new AlgoTemplateRecordImmutable(to_record(template)); - - // AlgoTemplateData to_template(i) - public static AlgoTemplate to_template(AlgoTemplateData template) => - new AlgoTemplate { - Name = template.name, - CompositeAlgoParams = new CompositeAlgoDefinition { - Name = template.definition.name, - Algo = template.value.CloneToMutable() - } - }; - public static AlgoTemplateImmutable to_templatei(AlgoTemplateData template) => new AlgoTemplateImmutable(to_template(template)); - - public static AlgoKey get_key(CompositeAlgoRecordImmutable recordi) => new AlgoKey(new AlgoPersistedKey(recordi.Key)); - public static AlgoKey get_key(AlgoTemplateRecordImmutable recordi) => new AlgoKey(new AlgoPersistedKey(recordi.Key)); - public static AlgoKey get_key(CompositeAlgoDefinitionImmutable definitioni) => new AlgoKey(new AlgoPersistedKey(definitioni.Name)); - public static AlgoKey get_key(CustomOrderDefinitionImmutable custom_order_definitioni) => new AlgoKey(custom_order_definitioni.CustomCase); - - public static void set_key(ref AlgoTemplateRecord record, AlgoKey key) { - switch (key.mode) { - case AlgoKey.Mode.Persisted: - switch (key.persisted.mode) { - case AlgoPersistedKey.Mode.String: - record.Key = key.persisted.id_string; break; - } - break; - } - } - public static void set_key(ref AlgoServiceRemoveRequest request, AlgoKey key) { - switch (key.mode) { - case AlgoKey.Mode.Persisted: - switch (key.persisted.mode) { - case AlgoPersistedKey.Mode.String: - request.Key = key.persisted.id_string; break; - } - break; - } - } - } - } -} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs index 57518ae9f..2d859112d 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs @@ -6,10 +6,12 @@ using bts.spark; using mbts.utils; using System; using System.Collections.Generic; +using System.ComponentModel.Composition; using System.Reactive.Linq; using System.Threading.Tasks; namespace mbts.spark { + [Export(typeof(IAlgoStudioModel))] class AlgoStudioFlatDefinitionModel : IAlgoStudioModel, IDisposable { readonly AlgoDefinitionService _service; readonly AdminModel _admin_model; @@ -18,12 +20,12 @@ namespace mbts.spark { IReadOnlyDictionary _algo_templates = DictionaryHelper.empty(); IDisposable _dispose; - public AlgoStudioFlatDefinitionModel(AlgoDefinitionService service, IAlgoDefinitionMetadataService metadata_service, AdminModel admin_model, ErrorReporter error_reporter, bts.config.AlgoStudioOptions ui_configuration) { + [ImportingConstructor] + public AlgoStudioFlatDefinitionModel(AlgoDefinitionService service, IAlgoDefinitionMetadataService metadata_service, AdminModel admin_model, ErrorReporter error_reporter) { _service = service; this.metadata_service = metadata_service; _admin_model = admin_model; _error_reporter = error_reporter; - this.ui_configuration = ui_configuration; definitions = service.subscribei_algo_definitions() .Filter(r => r.Metadata.IsActive && r.Metadata.CreatorUserId == _admin_model.user.id) @@ -35,7 +37,7 @@ namespace mbts.spark { public IObservable> definitions { get; } public IObservable> templates => Observable.Empty>(); public IAlgoDefinitionMetadataService metadata_service { get; } - public bts.config.AlgoStudioOptions ui_configuration { get; } + public bts.config.AlgoStudioOptions ui_configuration { get; } = config.Configuration.get(); public IObservable> subscribe_definition_templates(AlgoKey definition_key) => Observable.Return(TableUpdate.mk_empty(t => t.key)); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/Model/IAlgoStudioModel.cs b/src/client/spark/Modules/Trading/AlgoStudio/Model/IAlgoStudioModel.cs index 0d2b85cbe..acbcdffb5 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/Model/IAlgoStudioModel.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/Model/IAlgoStudioModel.cs @@ -1,10 +1,7 @@ using bts; -using bts.api; using bts.api.core; -using bts.spark; using System; using System.Collections.Generic; -using System.ComponentModel.Composition; using System.Threading.Tasks; namespace mbts.spark { @@ -20,34 +17,13 @@ namespace mbts.spark { bts.config.AlgoStudioOptions ui_configuration { get; } } - class AlgoStudioModelExporter { - [ImportingConstructor] - AlgoStudioModelExporter(Spark.AlgoServiceWrapper service_wrapper, IAlgoDefinitionMetadataService meta, AdminModel admin_model, ErrorReporter error_reporter) { - var ui_configuration = config.Configuration.get(); - switch (service_wrapper.mode) { - case Spark.AlgoServiceWrapper.Mode.Flat: model = new AlgoStudioFlatDefinitionModel(service_wrapper.flat, meta, admin_model, error_reporter, ui_configuration); break; - case Spark.AlgoServiceWrapper.Mode.Hierarchical: model = new AlgoStudioDefinitionTemplateModel(service_wrapper.hierarhical, meta, admin_model, error_reporter, ui_configuration); break; - } - } - - [Export] - public IAlgoStudioModel model { get; } - } - static class AlgoStudioModelUtils { - public static bool templates_enabled(this IAlgoStudioModel model) => - get_templates_enabled(model.ui_configuration); - - public static bool get_templates_enabled(bts.config.AlgoStudioOptions ui_configuration) => - ui_configuration.add_template_button != bts.config.AlgoStudioOptions.AddTemplateButton.None; - public static IReadOnlyDictionary get_dynamic_resources(bts.config.AlgoStudioOptions ui_configuration) => new Dictionary { { AlgoStudioWindow.ShowAlgoToolBarRenameDefinitionKey, ui_configuration.can_modify_definition_name && ui_configuration.show_algo_toolbar_rename }, { AlgoStudioWindow.ShowAlgoToolBarSaveDefinitionKey, ui_configuration.can_modify_definition_values && ui_configuration.show_algo_toolbar_save }, { AlgoStudioWindow.ShowAlgoToolBarRevertDefinitionKey, ui_configuration.show_algo_toolbar_revert }, - { AlgoStudioWindow.ShowAlgoToolBarUndoRedoDefinitionKey, ui_configuration.show_algo_toolbar_undo_redo }, - { AlgoStudioWindow.TemplatesEnabledKey, get_templates_enabled(ui_configuration) } + { AlgoStudioWindow.ShowAlgoToolBarUndoRedoDefinitionKey, ui_configuration.show_algo_toolbar_undo_redo } }; } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionTemplateVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionTemplateVM.cs deleted file mode 100644 index 27cfd6f91..000000000 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionTemplateVM.cs +++ /dev/null @@ -1,243 +0,0 @@ -using bts; -using bts.api.orderman; -using bts.utils; -using mbts.spark.utilities; -using mbts.ui; -using mbts.ui.shell; -using mbts.ui.utility; -using mbts.utils; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Reactive.Linq; -using System.Windows.Input; -using VisualStateNames = mbts.ui.spark.AlgoStudioView.VisualStateNames; - -namespace mbts.spark { - class AlgoDefinitionTemplateVM : DisposableViewModelBase, IAlgoStudioContentVM, IEquatable { - /// is referenced by an x:Static extension in OrderDetailsPopupControl.xaml . - /// In most cases, works just like , but for nested types, such as . - /// However, when the former is used to denote the DataType of a DataTemplate, a strange XamlReader exception is thrown at runtime. - /// After spending ~15 minutes investigating what was happening, [John]I decided it was more trouble than it was worth and opted for this workaround. - public static readonly Type CustomType = typeof(CustomTemplatePlaceholderVM); - readonly OrdersModel _model; - readonly VisualStateModel _edit_pane_vs_model = new VisualStateModel(), _tree_view_vs_model = new VisualStateModel(); - /// Note: the difference between and is that the former implements push/pop functionality so the user may undo/redo (like ctrl+z/ctrl+y in text editors), - /// while the latter is a lazy delegate-based container for the most recent value (). - readonly EditStack _edit_stack = new EditStack(); - readonly VersionContainer _version; - readonly DeferrableActionWrapper _refresh_on_local_change; - readonly Property _algo_root; - readonly DelegateProperty _can_undo, _can_redo, _is_dirty; - readonly Property _is_default, _is_favorite, _is_selected; - readonly Property _name, _abbreviation; - // maintains the state of each algo-root, so the user may change the definition root back and forth without any loss of state amongst the templates - Snapshot _committed; - - public AlgoDefinitionTemplateVM(AlgoDefinitionVM owner, OrdersModel model, AlgoData data, AlgoEditingPermissions editing_permissions = AlgoEditingPermissions.AllowEditValues) { - // init core data - this.owner = owner; - _model = model; - key = data.key; - _version = new VersionContainer(_snapshot); - // maintain states for different algo roots, so they aren't lost when the user changes the root algo type - var algo_map = new Dictionary { - { data.key, algo_vm_factory(data.value) } - }; - // init backing props - _name = backing(nameof(this.name), name); - _abbreviation = backing(nameof(abbreviation), string.Empty); - _is_default = backing(nameof(is_default), false); - _is_favorite = backing(nameof(is_favorite), false); - _is_selected = backing(nameof(is_selected), false); - _algo_root = backing(nameof(algo_root), get_algo_root(owner.algo_root)); - _committed = _version.value; - _can_redo = backing(nameof(can_redo), () => _edit_stack.get_can_redo(_version.value)); - _can_undo = backing(nameof(can_undo), () => _edit_stack.get_can_undo(_version.value)); - _is_dirty = backing(nameof(is_dirty), () => !_committed.Equals(_version.value)); - _can_redo.Changed += on_can_redo_changed; - _can_undo.Changed += on_can_undo_changed; - set_as_default_cmd = new DelegateCommand(() => { - if (!is_default && key.mode == AlgoKey.Mode.Persisted) - _model.algo_model.metadata_service.set_as_default(owner.key, key).ignore(); - }); - // init visual states - // TODO: implement these features in IAlgoMetadataService; until then, set the appropriate visual states. - _edit_pane_vs_model.set_state(VisualStateNames.Abbreviation.GroupName, VisualStateNames.Abbreviation.NotSupportedForTemplates); - _tree_view_vs_model.set_state(VisualStateNames.FavoriteToggleButton.GroupName, VisualStateNames.FavoriteToggleButton.Inactive); - switch (key.mode) { - case AlgoKey.Mode.Persisted: - _edit_stack.push(_committed); - _edit_pane_vs_model.set_state(VisualStateNames.Name.GroupName, VisualStateNames.Name.Active); - break; - case AlgoKey.Mode.Unpersisted: _edit_pane_vs_model.set_state(VisualStateNames.Name.GroupName, VisualStateNames.Name.Active); break; - case AlgoKey.Mode.Factory: _edit_pane_vs_model.set_state(VisualStateNames.Name.GroupName, VisualStateNames.Name.Inactive); break; - } - // set hooks/subscriptions - _refresh_on_local_change = new DeferrableActionWrapper(on_local_changed); - _dispose = new System.Reactive.Disposables.CompositeDisposable( - as_demuxer().subscribe_many(_refresh_on_local_change.execute, nameof(name), nameof(abbreviation), nameof(is_default), nameof(is_favorite)), - owner.observe_algo_root().Subscribe(r => _algo_root.set(get_algo_root(r))), - _algo_root.observe_current(true).SelectManyInSeries(r => r?.subscribe_definition()).Subscribe(_ => _refresh_on_local_change.execute()), - Disposable.wrap_collection(algo_map.Values)) { - () => { - _can_redo.Changed -= on_can_redo_changed; - _can_undo.Changed -= on_can_undo_changed; - } - }; - // local methods - IAlgoVM algo_vm_factory(CustomOrderDefinitionImmutable def) => OrderTypeVMFactory.create(new AlgoData(def, def.CustomCase.display_name(), OrderDataHelper.get_shallow_factory_key(def)), editing_permissions); - IAlgoVM get_algo_root(IAlgoVM owner_root) { - var key = new AlgoKey(owner_root.order_type); - if (algo_map.TryGetValue(key, out IAlgoVM vm)) { - vm.load_structure(owner_root.definition); - } else { - vm = algo_map.GetOrAdd(key, ot => algo_vm_factory(owner_root.definition)); - } - return vm; - } - void on_can_undo_changed(bool oldval, bool newval) => can_undo_changed?.Invoke(); - void on_can_redo_changed(bool oldval, bool newval) => can_redo_changed?.Invoke(); - void on_local_changed() { - _version.increment_version(); - _edit_stack.push_distinct(_version.value); - _is_dirty.refresh(); - _refresh_undo_redo(); - } - } - - public event Action can_redo_changed, can_undo_changed; - public event PropertyChange algo_root_changed { add => _algo_root.Changed += value; remove => _algo_root.Changed -= value; } - - public AlgoDefinitionVM owner { get; } - public AlgoKey owner_key => owner.key; - public AlgoKey key { get; } - public string name { get => _name.Value; set => _name.set(value); } - public string abbreviation { get => _abbreviation.Value; set => _abbreviation.set(value); } - public bool is_dirty => _is_dirty.Value; - public bool can_redo => _can_redo.Value; - public bool can_undo => _can_undo.Value; - public bool is_default => _is_default.Value; - public bool is_favorite { get => _is_favorite.Value; set => _is_favorite.set(value); } - public IAlgoVM algo_root => _algo_root.Value; - public virtual bool is_custom_placeholder => false; - public bool is_selected { get => _is_selected.Value; set => _is_selected.set(value); } - public bool is_expanded => false; - public bool is_persisted => key.mode == AlgoKey.Mode.Persisted; - public ObservableCollection edit_pane_visual_states => _edit_pane_vs_model.states; - public ObservableCollection tree_view_visual_states => _tree_view_vs_model.states; - public ICommand set_as_default_cmd { get; } - - public IObservable observe_is_dirty() => _is_dirty.observe_current(true); - public void undo() => _restore(_edit_stack.try_undo); - public void redo() => _restore(_edit_stack.try_redo); - - public void on_meta(bool is_default, bool is_favorite) { - _is_default.set(is_default); - _is_favorite.set(is_favorite); - } - - public AlgoData create_def_data() => - new AlgoData(algo_root.definition, name, key); - - public AlgoTemplateData create() => - create(owner.create_algo_def()); - - public AlgoTemplateData create(AlgoData record) { - if (!OrderDataHelper.EqualsHelpers.structure_equals(algo_root.definition, record.value)) - throw new InvalidOperationException("AlgoTemplate's algo structure does not match that of its parent."); - return new AlgoTemplateData(algo_root.definition, name, key, record); - } - - public AlgoDefinitionTemplateVM to_persisted(AlgoDefinitionVM persisted_owner, AlgoData template) => - new AlgoDefinitionTemplateVM(persisted_owner, _model, template); - - public void load_committed() => _load_from(_committed.name, _committed.definition, true); - - public void load_from(AlgoTemplateData value) { - if (key.mode != AlgoKey.Mode.Persisted) - throw new InvalidOperationException("AlgoStructure has not been saved yet."); - else if (value.key != key) - throw new InvalidOperationException("Value id does not match this id."); - _load_from(value.name, value.value, true); - } - - public bool Equals(AlgoDefinitionTemplateVM other) => other != null && key == other.key; - public override bool Equals(object obj) => Equals(obj as AlgoDefinitionTemplateVM); - public override int GetHashCode() => key.GetHashCode(); - - void _load_from(string new_name, CustomOrderDefinitionImmutable new_def, bool update_committed) { - using (_refresh_on_local_change.defer_execution()) { - name = new_name; - algo_root.load_from(new_def); - } - if (update_committed) { - _committed = _version.value; - _is_dirty.refresh(); - } - } - - void _refresh_undo_redo() { - _can_redo.refresh(); - _can_undo.refresh(); - } - - void _restore(TryGetValue try_get_snapshot) { - while (try_get_snapshot(out Snapshot next_state)) { - if (!_version.value.Equals(next_state)) { - _load_from(next_state.name, next_state.definition, false); - break; - } - } - } - - Snapshot _snapshot() => - new Snapshot(name, abbreviation, algo_root?.definition ?? new CustomOrderDefinitionImmutable(new CustomOrderDefinition()), is_default, is_favorite); - - struct Snapshot : IEquatable { - public readonly string name, abbreviation; - public readonly CustomOrderDefinitionImmutable definition; - public readonly bool is_default, is_favorite; - - public Snapshot(string name, string abbreviation, CustomOrderDefinitionImmutable definition, bool is_default, bool is_favorite) { - this.name = name; - this.abbreviation = abbreviation; - this.definition = definition; - this.is_default = is_default; - this.is_favorite = is_favorite; - } - - public bool Equals(Snapshot other) => - name == other.name && - abbreviation == other.abbreviation && - is_default == other.is_default && - is_favorite == other.is_favorite && - OrderDataHelper.EqualsHelpers.equals(definition, other.definition); - - public override bool Equals(object obj) => - EqualityComparer.equals_obj(this, obj); - - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - EqualityComparer.safe_aggregate_hash(name), - EqualityComparer.safe_aggregate_hash(abbreviation), - is_default.GetHashCode(), - is_favorite.GetHashCode(), - OrderDataHelper.EqualsHelpers.custom_order_def_compareri.GetHashCode(definition)); - - public static bool operator ==(Snapshot a, Snapshot b) => a.Equals(b); - public static bool operator !=(Snapshot a, Snapshot b) => !a.Equals(b); - } - - public class CustomTemplatePlaceholderVM : AlgoDefinitionTemplateVM { - const string _custom_name = "(Custom)"; - - public CustomTemplatePlaceholderVM(AlgoDefinitionVM owner, OrdersModel model, CustomOrderDefinitionImmutable definition) - : base(owner, model, new AlgoData(definition, _custom_name, new AlgoKey(Guid.NewGuid())), AlgoEditingPermissions.AllowEditStructure | AlgoEditingPermissions.AllowEditValues) { - _edit_stack.reset(_snapshot()); - } - - public override bool is_custom_placeholder => true; - } - } -} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs index 7ec794043..40f7d4ef9 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs @@ -6,7 +6,6 @@ using bts.utils; using mbts.collections; using mbts.spark.utilities; using mbts.ui; -using mbts.ui.controls; using mbts.ui.controls.color_picker; using mbts.ui.shell; using mbts.ui.utility; @@ -18,7 +17,6 @@ using System.Collections.Specialized; using System.Linq; using System.Reactive.Disposables; using System.Reactive.Linq; -using System.Windows.Data; using System.Windows.Media; using VisualStateNames = mbts.ui.spark.AlgoStudioView.VisualStateNames; @@ -36,18 +34,14 @@ namespace mbts.spark { public static readonly CustomOrderDefinitionImmutable new_algo_def = OrderDataHelper.Defaults.CustomOrderDefinitionImmutable(CustomOrderDefinition.CustomOneofCase.Iceberg); readonly OrdersModel _model; readonly VisualStateModel _edit_pane_vs_model = new VisualStateModel(), _tree_view_vs_model = new VisualStateModel(); - readonly ObservableKeyedCollection _templates = new ObservableKeyedCollection(t => t.key); readonly EditStack _edit_stack = new EditStack(); readonly VersionContainer _version; readonly Property _is_expanded, _is_selected; readonly Property _algo_root, _visible_algo_root; - readonly Property _selected_template; readonly DelegateProperty _is_favorite, _is_dirty, _can_undo, _can_redo; readonly Property _name, _abbreviation; readonly ObservableKeyedCollection _algo_roots = new ObservableKeyedCollection(vm => vm.id); - readonly ObservableAppendCollection _templates_with_custom; readonly DeferrableActionWrapper _refresh_on_local_changes; - readonly ReactiveThresholdMonitor _any_templates_dirty_monitor = new ReactiveThresholdMonitor(t => t.observe_is_dirty()); readonly Property _selected_format; readonly EnumMaskInt32 _permissions_mask = new EnumMaskInt32(); @@ -55,15 +49,9 @@ namespace mbts.spark { Brush _badge_fill; Snapshot? _committed; AlgoDefinitionMetadata _metadata; - TableUpdateBuffer _template_update_buffer; - bool _connected; - public static AlgoDefinitionVM create(OrdersModel model, AlgoData data, bool connect = false, AlgoFormats init_format = AlgoFormats.Exchange, AlgoEditingPermissions editing_permissions = (AlgoEditingPermissions)int.MaxValue) { - var rv = new AlgoDefinitionVM(model, data, init_format, editing_permissions); - if (connect) - rv.connect(); - return rv; - } + public static AlgoDefinitionVM create(OrdersModel model, AlgoData data, AlgoFormats init_format = AlgoFormats.Exchange, AlgoEditingPermissions editing_permissions = (AlgoEditingPermissions)int.MaxValue) => + new AlgoDefinitionVM(model, data, init_format, editing_permissions); public static AlgoDefinitionVM create_placeholder(OrdersModel model, CustomOrderDefinitionImmutable def, string name, AlgoFormats init_format) => new AlgoDefinitionVM(model, new AlgoData(def, name, new AlgoKey(Guid.NewGuid())), init_format, (AlgoEditingPermissions)int.MaxValue); @@ -78,20 +66,8 @@ namespace mbts.spark { editing_permissions_mask ^= (AlgoEditingPermissions.AllowEditValues | AlgoEditingPermissions.AllowSaveValues); if (!model.algo_model.ui_configuration.can_modify_definition_structure) editing_permissions_mask ^= (AlgoEditingPermissions.AllowEditStructure | AlgoEditingPermissions.AllowSaveStructure); - else if (model.algo_model.templates_enabled()) { - resources.Add(_templates.Subscribe((s, e) => { - if (_templates.Count == 0) { - editing_permissions_mask |= AlgoEditingPermissions.AllowEditStructure | AlgoEditingPermissions.AllowSaveStructure; - } else { - editing_permissions_mask ^= (AlgoEditingPermissions.AllowEditStructure | AlgoEditingPermissions.AllowSaveStructure); - } - })); - } key = def_data.key; algos_view = ObservableAutoWrapper.create_sorted_wrapper(_algo_roots, AlgoCollectionHelper.algo_vm_comparer); - _template_update_buffer = new TableUpdateBuffer(t => t.key); - _templates_with_custom = new ObservableAppendCollection(templates); - templates_view = (ListCollectionView)CollectionViewSource.GetDefaultView(_templates_with_custom); custom_badge_color_palettes.AddRange(ColorPalette.all_presets(model.factory).Select(p => new ColorPaletteVM(p))); _version = new VersionContainer(_snapshot); var conf = _model.algo_model.ui_configuration; @@ -99,7 +75,6 @@ namespace mbts.spark { _name = backing(nameof(name), def_data.name); _abbreviation = backing(nameof(abbreviation), string.Empty); _selected_format = backing(nameof(selected_format), init_format); - _selected_template = backing(nameof(selected_template)); _is_favorite = backing(nameof(is_favorite), () => _favorite_index.HasValue); _is_expanded = backing(nameof(is_expanded), true); _is_selected = backing(nameof(is_selected), true); @@ -117,13 +92,7 @@ namespace mbts.spark { async void init_async() { // visible_algo_root is selected_template.algo_root if user can add templates, otherwise algo_root // - this behavior is configurable in 'AlgoStudioOptions' in UI.proto (01/28/2018) - if (_model.algo_model.templates_enabled()) { - resources.Add(_selected_template.observe_current(true).Subscribe(t => _visible_algo_root.set(t?.algo_root))); - } else { - resources.Add(_algo_root.observe_current(true).Subscribe(_visible_algo_root.set)); - } - // init tree view item visual state - _tree_view_vs_model.set_state(VisualStateNames.TreeViewBadge.GroupName, conf.show_badges_in_tree_view ? VisualStateNames.TreeViewBadge.Active : VisualStateNames.TreeViewBadge.Inactive); + resources.Add(_algo_root.observe_current(true).Subscribe(_visible_algo_root.set)); // set hooks/subscriptions using (_refresh_on_local_changes.supress_execution()) { resources.AddRange( @@ -142,7 +111,6 @@ namespace mbts.spark { }), vm.subscribe_definition().Subscribe(_ => _refresh_on_local_changes.execute()))), ObservableAutoWrapper.connect_item_hooks(algo_roots, a => a as IDisposable ?? Disposable.empty, true), - ObservableAutoWrapper.connect_item_hooks(templates, t => new CompositeDisposable(_any_templates_dirty_monitor.join(t), t), true), Disposable.Create(() => { _can_redo.Changed -= on_can_redo_changed; _can_undo.Changed -= on_can_undo_changed; @@ -150,7 +118,6 @@ namespace mbts.spark { switch (key.mode) { case AlgoKey.Mode.Persisted: _metadata = new AlgoDefinitionMetadata(key); - resources.Add(_template_update_buffer.OnDispatcher().Subscribe(_on_templates)); resources.Add(model.algo_model.metadata_service.subscribe_metadata(key).OnDispatcher().Subscribe(_on_metadata)); _edit_pane_vs_model.set_state(VisualStateNames.Name.GroupName, conf.can_modify_definition_name ? VisualStateNames.Name.Active : VisualStateNames.Name.CannotRenameDefinition); break; @@ -206,7 +173,6 @@ namespace mbts.spark { public IReadOnlyObservableCollection algos_view { get; } public IAlgoVM algo_root { get => _algo_root.Value; set => _algo_root.set(value); } public IAlgoVM visible_algo_root => _visible_algo_root.Value; - public AlgoDefinitionTemplateVM selected_template { get => _selected_template.Value; set => _selected_template.set(value); } public int? favorite_index { get => _favorite_index; set { @@ -238,15 +204,6 @@ namespace mbts.spark { public EnabledValueVM custom_badge_color { get; } = new EnabledValueVM(); public ObservableCollection custom_badge_color_palettes { get; } = new ObservableCollection(); public Brush badge_fill => _badge_fill; - public ObservableCollection templates => _templates; - /// has the same elements as , plus the custom-placeholder-item, which is always at index 0. - /// The custom-placeholder supports legacy behavior in the ladder/ticket algo popup by allowing the user to manipulate the algo-structure (e.g. change an iceberg-limit order to iceberg-lean-cover-offset etc.). - /// However, the custom-placeholder item cannot be used to create/save algo-templates. - /// Algo-templates are just a set of values used to populate a concrete algo-structure; the 'structures' can be created in the AlgoStudio (). - /// If we were to strictly follow this paradigm, the user would no longer be able to freely change the algo-structure in the ladder/ticket popup (they would be forced to open up a new , and we know how much they hate more clicks!). - /// Some users might consider this a regression. The custom-placeholder-item addresses that concern. - public ObservableAppendCollection templates_with_custom => _templates_with_custom; - public ListCollectionView templates_view { get; } // format public AlgoFormats selected_format { get => _selected_format.Value; set => _selected_format.set(value); } public ObservableCollection formats { get; } = new ObservableCollection { AlgoFormats.Exchange, AlgoFormats.Spreader, AlgoFormats.Exchange | AlgoFormats.Spreader }; @@ -321,24 +278,7 @@ namespace mbts.spark { } } - // idempotent - public void prepend_custom_template() { - if (_templates_with_custom.appended_front.Count == 0) - _templates_with_custom.insert_front(new AlgoDefinitionTemplateVM.CustomTemplatePlaceholderVM(this, _model, algo_root.definition)); - } - - public AlgoDefinitionTemplateVM template_factory() => - new AlgoDefinitionTemplateVM(this, _model, algo_root.create_def_data().with(name: new_template_name)) { - is_selected = true - }; - public IObservable subscribe_is_definition_dirty() => _is_dirty.observe_current(true); - public IObservable subscribe_any_templates_dirty() => _any_templates_dirty_monitor; - public IObservable subscribe_is_definition_or_any_templates_dirty() => - Observable.Merge( - subscribe_is_definition_dirty().Select(v => v || _any_templates_dirty_monitor.is_above_threshold), - subscribe_any_templates_dirty().Select(v => v || is_dirty)) - .DistinctUntilChanged(); public void undo() => _restore(_edit_stack.try_undo); public void redo() => _restore(_edit_stack.try_redo); @@ -347,17 +287,6 @@ namespace mbts.spark { _assign_from_data(_committed.Value); _on_committed_changed(); } - /// - /// Subscribes to the templates source for this definition if it has been persisted (subscription requires a persistant id); does nothing if this definition has not been persisted, or if it is already subscribed (idempotent). - /// - public void connect() { - if (!_connected) { - _connected = true; - concat_disposable(_model.algo_model.subscribe_definition_templates(key).OnDispatcher().Subscribe(_template_update_buffer.OnNext)); - } - } - - public IDisposable buffer_updates() => _template_update_buffer.buffer_updates(); public void load_def(CustomOrderDefinitionImmutable def) { var key = OrderDataHelper.get_shallow_factory_key(def); @@ -464,12 +393,6 @@ namespace mbts.spark { using (_refresh_on_local_changes.supress_execution()) { _abbreviation.set(metadata.abbreviation); custom_badge_color.set_nullable(metadata.badge_color); - foreach (var t in templates) { - if (t.key.mode == AlgoKey.Mode.Persisted) - t.on_meta(t.key == metadata.default_template, metadata.favorite_templates.Contains(t.key)); - else - t.on_meta(false, false); - } _version.increment_version(); if (_edit_stack.size == 1 && _edit_stack.pos == 1) { // this is a hack to account for the metadata being part of the committed state, @@ -482,22 +405,6 @@ namespace mbts.spark { } } - void _on_templates(TableUpdate table) { - foreach (var rec in table.removed) - _templates.Remove(rec.key); - foreach (var rec in table.changed) - _templates[rec.key].load_from(rec); - foreach (var rec in table.added) { - if (_templates.TryGetValue(rec.key, out AlgoDefinitionTemplateVM tmp)) { - tmp.load_from(rec); - } else { - tmp = new AlgoDefinitionTemplateVM(this, _model, new AlgoData(rec.value, rec.name, rec.key)); - tmp.on_meta(tmp.key == _metadata.default_template, _metadata.favorite_templates.Contains(tmp.key)); - _templates.Add(tmp); - } - } - } - void _refresh_undo_redo() { _can_undo.refresh(); _can_redo.refresh(); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs index 3f08d209a..d27b6b0e8 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs @@ -1,6 +1,5 @@ using bts; using bts.api.orderman; -using bts.collections; using bts.utils; using mbts.collections; using mbts.ui; @@ -14,6 +13,7 @@ using System.Linq; using System.Reactive.Disposables; using System.Reactive.Linq; using System.Threading.Tasks; +using System.Windows; using System.Windows.Input; using StartupData = bts.shell.AlgoStudioWindowFactory.StartupData; @@ -37,24 +37,22 @@ namespace mbts.spark { }); } - class AlgoStudioVM : DisposableViewModelBase, IHandleSelectedItemChanged { + class AlgoStudioVM : DisposableViewModelBase { static readonly AlgoFormats _studio_formats = AlgoFormats.Exchange; readonly OrdersModel _model; readonly Property _selected_definition; - readonly Property _selected_template; readonly Property _editable_content; readonly DelegateProperty _save_cmd_tooltip, _revert_cmd_tooltip; readonly ObservableKeyedCollection _definitions = new ObservableKeyedCollection(d => d.key); readonly ListCollectionView _definitions_view; readonly TableUpdateBuffer _definitions_update_buffer; - readonly ReactiveThresholdMonitor _any_dirty_monitor = new ReactiveThresholdMonitor(d => d.subscribe_is_definition_or_any_templates_dirty()); + readonly ReactiveThresholdMonitor _any_dirty_monitor = new ReactiveThresholdMonitor(d => d.subscribe_is_definition_dirty()); IReadOnlyDictionary _persisted_meta = new Dictionary(); public AlgoStudioVM(OrdersModel model) { _model = model; _selected_definition = backing(nameof(selected_definition)); - _selected_template = backing(nameof(selected_template)); - _editable_content = backing(nameof(editable_content), observe_selected().SelectManyInSeries(tup => tup.template?.observe_algo_root() ?? tup.definition?.observe_algo_root()).OnDispatcher()); + _editable_content = backing(nameof(editable_content), subscribe_definition().SelectManyInSeries(def => def?.observe_algo_root()).OnDispatcher()); _definitions_update_buffer = new TableUpdateBuffer(def => def.key); _definitions_view = new ListCollectionView(definitions) { CustomSort = AlgoCollectionHelper.algo_definition_vm_comparer.to_non_generic() @@ -71,13 +69,18 @@ namespace mbts.spark { // _dispose = new CompositeDisposable( _model.algo_model.definitions.Subscribe(_definitions_update_buffer), - _definitions_update_buffer.OnDispatcher().Subscribe(u => AlgoCollectionHelper.update_persisted_definitions(_definitions, u, d => AlgoDefinitionVM.create(model, d, true, _studio_formats))), + _definitions_update_buffer.OnDispatcher().Subscribe(u => { + var orig_selected_def = selected_definition; + AlgoCollectionHelper.update_persisted_definitions(_definitions, u, d => AlgoDefinitionVM.create(model, d, _studio_formats)); + if (selected_definition == null) + selected_definition = definitions.FirstOrDefault(); + }), _model.algo_model.metadata_service.subscribe_metadata().OnDispatcher().Subscribe(meta => { _persisted_meta = meta; definitions_view.Refresh(); }), - ObservableAutoWrapper.connect(observe_selected().Select(t => t.template?.edit_pane_visual_states ?? t.definition?.edit_pane_visual_states ?? new ObservableCollection()), content_visual_states), - observe_selected().SelectManyInSeries(t => t.template?.observe_is_dirty() ?? t.definition?.subscribe_is_definition_or_any_templates_dirty() ?? Observable.Return(false)).OnDispatcher() + ObservableAutoWrapper.connect(subscribe_definition().Select(def => def?.edit_pane_visual_states ?? new ObservableCollection()), content_visual_states), + subscribe_definition().SelectManyInSeries(def => def?.subscribe_is_definition_dirty() ?? Observable.Return(false)).OnDispatcher() .Subscribe(new CompositeObserver(save_cmd, revert_cmd) { val => { _save_cmd_tooltip.refresh(); _revert_cmd_tooltip.refresh(); @@ -85,47 +88,30 @@ namespace mbts.spark { ObservableAutoWrapper.connect_item_hooks(definitions, d => new CompositeDisposable(_any_dirty_monitor.join(d), d), true)); // local methods string get_revert_tooltip() { - if (content_source is AlgoDefinitionTemplateVM tmp) { - switch (tmp.key.mode) { - case AlgoKey.Mode.Persisted: return tmp.is_dirty ? $"Revert changes to '{tmp.name}'" : "No changes to revert"; - } - } else if (content_source is AlgoDefinitionVM def) { - switch (def.key.mode) { - case AlgoKey.Mode.Persisted: return def.is_dirty || def.templates.Any(t => t.is_dirty) ? $"Revert changes to '{def.name}' templates" : "No changes to revert"; - } - } - return null; + var def = selected_definition; + return def != null && def.key.mode == AlgoKey.Mode.Persisted ? + (def.is_dirty ? $"Revert changes to '{def.name}' templates" : "No changes to revert") : + null; } async Task save_async(IAlgoStudioContentVM content) { - if (content is AlgoDefinitionTemplateVM tmp) { - await save_template(tmp); - } else if (content is AlgoDefinitionVM def) { + if (content is AlgoDefinitionVM def) await save_definition(def); - } } string get_save_tooltip() { - if (content_source is AlgoDefinitionTemplateVM tmp) { - switch (tmp.key.mode) { - case AlgoKey.Mode.Unpersisted: return tmp.is_dirty ? $"Save '{tmp.name}'" : $"Cannot Save '{tmp.name}'"; - case AlgoKey.Mode.Persisted: return tmp.is_dirty ? $"Save changes to '{tmp.name}'" : "No changes to save"; - } - } else if (content_source is AlgoDefinitionVM def) { + var def = selected_definition; + if (def != null) { switch (def.key.mode) { - case AlgoKey.Mode.Unpersisted: return def.is_dirty || def.templates.Any(t => t.is_dirty) ? $"Save '{def.name}'" : $"Cannot Save '{def.name}'"; - case AlgoKey.Mode.Persisted: return def.is_dirty || def.templates.Any(t => t.is_dirty) ? $"Save changes to '{def.name}' templates" : "No changes to save"; + case AlgoKey.Mode.Unpersisted: return def.is_dirty ? $"Save '{def.name}'" : $"Cannot Save '{def.name}'"; + case AlgoKey.Mode.Persisted: return def.is_dirty ? $"Save changes to '{def.name}' templates" : "No changes to save"; } } return null; } } - event Action<(AlgoDefinitionVM definition, AlgoDefinitionTemplateVM template)> _selected_changed; - public ObservableCollection definitions => _definitions; public ObservableCollection content_visual_states { get; } = new ObservableCollection(); public AlgoDefinitionVM selected_definition { get => _selected_definition.Value; set => _selected_definition.set(value); } - public AlgoDefinitionTemplateVM selected_template { get => _selected_template.Value; set => _selected_template.set(value); } - public IAlgoStudioContentVM content_source => (IAlgoStudioContentVM)selected_template ?? selected_definition; public IAlgoVM editable_content => _editable_content.Value; public ICollectionView definitions_view => _definitions_view; public ICommand save_cmd { get; } @@ -150,12 +136,6 @@ namespace mbts.spark { public StartupData create() => new StartupData(); public void load_from(StartupData state) { } public IObservable subscribe_definition() => _selected_definition.observe_current(true); - public IObservable subscribe_template() => _selected_template.observe_current(true); - public IObservable<(AlgoDefinitionVM definition, AlgoDefinitionTemplateVM template)> observe_selected() => - Observable.Create<(AlgoDefinitionVM definition, AlgoDefinitionTemplateVM template)>(o => { - _selected_changed += o.OnNext; - return Disposable.Create(() => _selected_changed -= o.OnNext); - }); public IObservable subscribe_dirty() => _any_dirty_monitor; public async Task remove_definition(AlgoDefinitionVM definition) { @@ -163,87 +143,43 @@ namespace mbts.spark { await _model.algo_model.remove_definition(definition.key); } else { // This is a new definition, which hasn't been saved/persisted yet. No need to call the server. - definition.templates.Clear(); definitions.Remove(definition); } } - public async Task remove_template(AlgoDefinitionTemplateVM template) { - if (template.key.mode == AlgoKey.Mode.Persisted) { - await _model.algo_model.remove_template(template.key); - } else { - // This is a new template, which hasn't been saved/persisted yet. No need to call the server. - template.owner.templates.Remove(template); - } - } + public Task save_definition(AlgoDefinitionVM definition) => + _save(new[] { definition }); - public async Task save_definition(AlgoDefinitionVM definition) { - using (_save_op()) { - var newly_persisted_keys = await AlgoPersistenceHelper.persist_definition(_model, _definitions, definition); - AlgoKey pkey; - switch (definition.key.mode) { - case AlgoKey.Mode.Unpersisted: - if (!newly_persisted_keys.TryGetValue(definition.key, out pkey)) - return; - break; - case AlgoKey.Mode.Persisted: - pkey = definition.key; - break; - default: return; - } - await _model.algo_model.metadata_service.update_metadata(pkey, _ => definition.create_metadata().with(id: pkey)); - } - } + public Task save_all() => + _save(definitions); - public async Task save_template(AlgoDefinitionTemplateVM template) { - using (_save_op()) - await AlgoPersistenceHelper.persist_template(_model, _definitions, template); - } - - public async Task save_all() => - await _save(definitions.WrapSelector(def => (def, def.templates.AsList()), t => t.def)); - - async Task _save(IList<(AlgoDefinitionVM definition, IList templates)> definition_templates) { - using (_save_op()) - await AlgoPersistenceHelper.persist(_model, definition_templates); - } - - IDisposable _save_op() { - int? def_index = selected_definition == null ? (int?)null : _definitions.IndexOf(selected_definition); - int? tmp_index = selected_template == null ? (int?)null : selected_definition.templates.IndexOf(selected_template); - return new CompositeDisposable(_definitions_update_buffer.buffer_updates()) { - () => { - if (def_index.HasValue) { - var def = _definitions[def_index.Value]; - if (tmp_index.HasValue) { - var tmp = def.templates[tmp_index.Value]; - tmp.is_selected = true; - } else def.is_selected = true; + async Task _save(IEnumerable definitions) { + var orig_selected = selected_definition; + var orig_selected_index = orig_selected == null ? -1 : this.definitions.IndexOf(orig_selected); + using (_definitions_update_buffer.buffer_updates()) { + var def_list = definitions as IList ?? definitions.ToList(); + await Task.WhenAll(definitions.Select(async d => { + var def = await _model.algo_model.save_definition(d.create_algo_def()); + if (def.HasValue) { + // don't forget to save metadata + await _model.algo_model.metadata_service.update_metadata(def.Value.key, _ => d.create_metadata().with(def.Value.key)); } - } - }; - } - - /// Despite the existence of these properties: , and , - /// still implements to update the properties that depend on the . - /// [John]I am going to Uganda in 3 days (today is 1/3/2019), and decided against removing this legacy-dependency at the last minute. - /// Fwiw, and are bound to . - /// They serve to prevent a quirky default behavior in TreeView: double clicking any TreeViewItem collapses the entire TreeView. - /// This behavior wasn't just quirky either, it actually conflicted with the double-click-to-set-template-as-default behavior. - /// (for details, ) - void IHandleSelectedItemChanged.on_selected_item_changed(object old_value, object new_value) { - if (new_value is AlgoDefinitionVM def) { - selected_definition = def; - selected_template = null; - } else if (new_value is AlgoDefinitionTemplateVM templ) { - selected_definition = templ.owner; - selected_template = templ; - } else { - selected_definition = null; - selected_template = null; + return def; + })).ContinueOnDispatcher(t => { + if (t.Status != TaskStatus.RanToCompletion) { + Log.Ui.debug_fatal($"Algo Studio error saving definitions: unexpected Task.Status '{t.Status}'."); + return; + } + var persisted_data = t.Result; + Log.assert(persisted_data.Length == def_list.Count); + for (int i = 0; i < persisted_data.Length; i++) { + if (!def_list[i].key.is_persisted && persisted_data[i].HasValue) + _definitions.Replace(def_list[i].key, def_list[i].to_persisted(persisted_data[i].Value)); + } + if (selected_definition != orig_selected && orig_selected_index >= 0 && orig_selected_index < this.definitions.Count) + selected_definition = this.definitions[orig_selected_index]; + }); } - _selected_changed?.Invoke((selected_definition, selected_template)); - RaisePropertyChanged(nameof(content_source)); } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 82e95beb8..00c27f954 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -136,7 +136,7 @@ namespace mbts.spark { c.PropertyChanged -= on_prop_changed; } AccountTuple? account_factory(KeyValuePair kv) => new AccountTuple(kv.Key, kv.Value); - AlgoDefinitionVM algo_factory(AlgoData data) => AlgoDefinitionVM.create(_model, data, false); + AlgoDefinitionVM algo_factory(AlgoData data) => AlgoDefinitionVM.create(_model, data); } public event Action result_changed; diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 7dc1fb12a..5411de8cf 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -318,14 +318,13 @@ namespace mbts.spark { }), // custom templates (created by user) _model.algo_model.definitions.OnDispatcher().Subscribe(u => - AlgoCollectionHelper.update_persisted_definition_wrappers(attach_vm.algo_vm.algo_roots_index, u, mk_factory(true))))); + AlgoCollectionHelper.update_persisted_definition_wrappers(attach_vm.algo_vm.algo_roots_index, u, mk_algo_def_vm)))); // vm factory - Func mk_factory(bool connect) => - fd => { - var vm = AlgoDefinitionVM.create(_model, fd, connect, OrderDataHelper.get_algo_format(_selected_params_mon.security?.ContractCase), AlgoEditingPermissions.AllowEditValues); - vm.selected_format = OrderDataHelper.get_algo_format(_selected_params_mon.security?.ContractCase); - return vm; - }; + AlgoDefinitionVM mk_algo_def_vm(AlgoData fd) { + var vm = AlgoDefinitionVM.create(_model, fd, OrderDataHelper.get_algo_format(_selected_params_mon.security?.ContractCase), AlgoEditingPermissions.AllowEditValues); + vm.selected_format = OrderDataHelper.get_algo_format(_selected_params_mon.security?.ContractCase); + return vm; + } } void on_end_attaching() => Disposable.dispose(ref attach_vm_hooks); diff --git a/src/client/spark/Orders/OrderPropertiesEditorVM.cs b/src/client/spark/Orders/OrderPropertiesEditorVM.cs index 81354eae0..27ecfe045 100644 --- a/src/client/spark/Orders/OrderPropertiesEditorVM.cs +++ b/src/client/spark/Orders/OrderPropertiesEditorVM.cs @@ -52,7 +52,6 @@ namespace mbts.spark { class OrderPropertiesEditorVM : DisposableViewModelBase { readonly OrdersModel _model; readonly Property _security; - readonly Property _selected_algo_template, _selected_favorite_algo_template; readonly SelectorProperty _accounts; readonly SelectorProperty _time_in_force; readonly SelectionModel _selected_algo_definition; @@ -60,8 +59,6 @@ namespace mbts.spark { readonly ObservableKeyedCollection _algo_definitions = new ObservableKeyedCollection(d => d.key); readonly ListCollectionView _algo_definitions_view, _favorite_algo_definitions_view; readonly TableUpdateBuffer _definitions_buffer = new TableUpdateBuffer(d => d.key); - readonly EditableCollectionModel _algo_template_collection_model; - readonly ObservableKeyedCollection _favorite_templates = new ObservableKeyedCollection(tvm => tvm.key); readonly ObservableAggregateCollection _algo_param_rows = new ObservableAggregateCollection(); IReadOnlyDictionary _algo_metadata = DictionaryHelper.empty(); IReadOnlyDictionary _favorite_algos_index = DictionaryHelper.empty(); @@ -74,7 +71,6 @@ namespace mbts.spark { var security_source = security_md_source.Select(smd => smd.security); _security = backing(nameof(security), security_source, SecurityDataHelper.Defaults.SecurityImmutable); security_source = _security.observe_current(true); - _selected_algo_template = backing(nameof(selected_algo_template)); _selected_algo_definition = new SelectionModel(algo_definitions, () => { var t = _model.factory.default_order_type(security); @@ -131,29 +127,8 @@ namespace mbts.spark { order_properties.Select(ops => ops.time_in_force), () => model.factory.default_time_in_force(security), tif => time_in_forces.Contains(tif)); - _selected_favorite_algo_template = backing(nameof(selected_favorite_algo_template)); - // items source views - algo_templates_view = CollectionViewSource.GetDefaultView(algo_templates); - var favorite_algo_templates_view = new ListCollectionView(ObservableAggregateCollection.create(_algo_definitions.AsCollection(), d => d.templates)); - favorite_algo_templates_view.LiveGroupingProperties.Add(nameof(AlgoDefinitionTemplateVM.owner_key)); - favorite_algo_templates_view.LiveFilteringProperties.Add(nameof(AlgoDefinitionTemplateVM.is_favorite)); - favorite_algo_templates_view.Filter = item => (item as AlgoDefinitionTemplateVM)?.is_favorite ?? false; - favorite_algo_templates_view.IsLiveFiltering = true; - this.favorite_algo_templates_view = favorite_algo_templates_view; // commands save_definition_cmd = new AsyncCommand(save_definition_async); - save_template_cmd = new AsyncCommand(save_template_async); - remove_template_cmd = new AsyncCommand(tvm => _model.algo_model.remove_template(tvm.key)); - toggle_is_favorite_cmd = new DelegateCommand(tvm => _favorite_templates.Add(tvm)); - _algo_template_collection_model = new EditableCollectionModel(d => selected_algo_definition == null ? default : selected_algo_definition.template_factory(), d => d != null); - redo_cmd = ObservantCommand.create( - t => t?.redo(), - t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_redo_changed += cb, cb => t.can_redo_changed -= cb, () => t.can_redo).OnDispatcher(), - out IObserver redo_observer); - undo_cmd = ObservantCommand.create( - t => t?.undo(), - t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_undo_changed += cb, cb => t.can_undo_changed -= cb, () => t.can_undo).OnDispatcher(), - out IObserver undo_observer); undo_definition_cmd = ObservantCommand.create( d => d?.undo(), d => d == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => d.can_undo_changed += cb, cb => d.can_undo_changed -= cb, () => d.can_undo).OnDispatcher(), @@ -186,31 +161,10 @@ namespace mbts.spark { algo_params_dirty_mon.Subscribe(set_default_algo_params_observer, revert_algo_params_observer), ObservableAutoWrapper.connect_item_hooks(_algo_param_rows, algo_params_dirty_mon.join), _selected_algo_definition.subscribe_changed(_selected_algo_definition_Changed), - as_demuxer().subscribe(nameof(selected_algo_template)).Subscribe( - undo_observer, - redo_observer), as_demuxer().subscribe(nameof(selected_algo_definition)).Subscribe( undo_definition_observer, redo_definition_observer, revert_definition_observer), - algo_definitions.Subscribe((s, e) => { - if (e.OldItems != null) - e.OldItems.OfType().DisposeAll(); - if (e.NewItems != null) { - foreach (AlgoDefinitionVM d in e.NewItems) { - switch (d.key.mode) { - case AlgoKey.Mode.Persisted: - if (_model.algo_model.ui_configuration.include_custom_templates && _model.algo_model.templates_enabled()) - d.prepend_custom_template(); - break; - case AlgoKey.Mode.Factory: - if (_model.algo_model.ui_configuration.include_legacy_custom_templates && _model.algo_model.templates_enabled()) - d.prepend_custom_template(); - break; - } - } - } - }), security_md_source.Select(smd => model.factory.default_algos(smd.security)).Table(s => s.key).Subscribe(factory_defs => { using (_selected_algo_definition.begin_update()) AlgoCollectionHelper.update_persisted_definitions(_algo_definitions, factory_defs, fd => { @@ -222,43 +176,41 @@ namespace mbts.spark { model.algo_model.definitions.Subscribe(_definitions_buffer), _definitions_buffer.OnDispatcher().Subscribe(u => { AlgoCollectionHelper.update_persisted_definitions(_algo_definitions, u, def_data => { - var def_vm = AlgoDefinitionVM.create(model, def_data, true, OrderDataHelper.get_algo_format(security.ContractCase), AlgoEditingPermissions.AllowEditValues); + var def_vm = AlgoDefinitionVM.create(model, def_data, OrderDataHelper.get_algo_format(security.ContractCase), AlgoEditingPermissions.AllowEditValues); def_vm.selected_format = OrderDataHelper.get_algo_format(security.ContractCase); def_vm.favorite_index = _get_favorite_algo_index(def_vm.key); return def_vm; }); - }), + }), model.algo_model.metadata_service.subscribe_metadata().OnDispatcher().Subscribe(_on_template_metadata), ObservableAutoWrapper.connect_item_hooks(algo_definitions, vm => - Disposable.Create( - vm.connect_data(nullable_security_md_source), - security_source.Subscribe(s => { - var fmt = OrderDataHelper.get_algo_format(s.ContractCase); - foreach (var d in algo_definitions) - d.selected_format = fmt; - }), - vm), true)) { - () => { - Disposable.dispose(ref _template_collection_sync); - Disposable.dispose(ref _algo_param_rows_hook); - } + Disposable.Create( + vm.connect_data(nullable_security_md_source), + security_source.Subscribe(s => { + var fmt = OrderDataHelper.get_algo_format(s.ContractCase); + foreach (var d in algo_definitions) + d.selected_format = fmt; + }), + vm), + true)) { + () => Disposable.dispose(ref _algo_param_rows_hook) }; // local methods - async Task save_template_async(AlgoDefinitionTemplateVM template) { - using (_preserve_selection_by_index()) - await AlgoPersistenceHelper.persist_template(model, algo_definitions, template); - } async Task save_definition_async(AlgoDefinitionVM definition) { switch (definition.key.mode) { case AlgoKey.Mode.Persisted: - await AlgoPersistenceHelper.persist_definition(model, algo_definitions, definition); + using (_definitions_buffer.buffer_updates()) + await _model.algo_model.save_definition(definition.create_algo_def()); break; case AlgoKey.Mode.Factory: case AlgoKey.Mode.Unpersisted: using (_definitions_buffer.buffer_updates()) { - var persisted_key_lookup = await AlgoPersistenceHelper.persist_definition(model, algo_definitions, definition); - if (persisted_key_lookup.TryGetValue(definition.key, out AlgoKey pkey) && _algo_definitions.TryGetValue(pkey, out AlgoDefinitionVM pdef)) - selected_algo_definition = pdef; + var persisted_def = await model.algo_model.save_definition(definition.create_algo_def()); + if (persisted_def.HasValue) { + int pos = algo_definitions.IndexOf(definition); + if (pos >= 0) + selected_algo_definition = algo_definitions[pos] = definition.to_persisted(persisted_def.Value); + } if (definition.key.mode == AlgoKey.Mode.Factory) { /// Revert the factory definition to its original state. /// Note: this won't revert the parameters in the algo popup since the newly persisted definition () is selected now. @@ -283,32 +235,19 @@ namespace mbts.spark { new InlineUserProperty(); } - IDisposable _template_collection_sync; void _selected_algo_definition_Changed(AlgoDefinitionVM old_value, AlgoDefinitionVM new_value, SelectionReason reason) { - Disposable.dispose(ref _template_collection_sync); Disposable.dispose(ref _algo_param_rows_hook); if (new_value != null) { - _template_collection_sync = ObservableAutoWrapper.connect_two_way(new_value.templates_with_custom, algo_templates); if (use_algo_param_grid) _algo_param_rows_hook = _connect_algo_param_rows(new_value, _algo_param_rows); if (reason != SelectionReason.Explicit) _selected_algo_stack.Push(new_value.key); - } else { - algo_templates.Clear(); } if (open_algo_popup_on_select) { if (reason == SelectionReason.UserSelectedNewValue && new_value?.algo_root is IAlgoVM ar && ar.can_expand) ar.is_expanded = true; } - if (selected_algo_template == null && _model.algo_model.templates_enabled()) { - if (new_value != null) - selected_algo_template = new_value.templates.FirstOrDefault(tvm => tvm.is_default, tvm => _favorite_templates.ContainsKey(tvm.key), tvm => true) ?? new_value.templates_with_custom.FirstOrDefault(); - } else { - RaisePropertyChanged(nameof(selected_algo_template)); - } RaisePropertyChanged(nameof(selected_algo_definition), nameof(selected_favorite_algo_definition), nameof(can_expand), nameof(is_expanded)); - _selected_favorite_algo_template.Value = selected_algo_template is AlgoDefinitionTemplateVM t && t.is_favorite ? t : null; - _algo_template_collection_model.OnNext(new_value); } public SecurityImmutable security => _security.Value; @@ -316,7 +255,6 @@ namespace mbts.spark { public AccountTuple? selected_account { get => _accounts.selected_item; set => _accounts.selected_item = value; } public ReadOnlyObservableCollection time_in_forces => _time_in_force.items; public TimeInForce selected_time_in_force { get => _time_in_force.selected_item; set => _time_in_force.selected_item = value; } - public CustomOrderDefinitionImmutable? definition => selected_algo_template?.algo_root?.definition; public int favorite_algos_column_count { get => _favorite_algos_column_count; private set => Set(ref _favorite_algos_column_count, value); } public bool use_algo_param_grid { get => _use_algo_param_grid; @@ -331,47 +269,14 @@ namespace mbts.spark { } // items sources public ObservableCollection algo_definitions => _algo_definitions; - public ObservableCollection algo_templates { get; } = new ObservableCollection(); public IReadOnlyObservableCollection algo_param_rows => _algo_param_rows; public ICollectionView algo_definitions_view => _algo_definitions_view; public ICollectionView favorite_algo_definitions_view => _favorite_algo_definitions_view; - public ICollectionView algo_templates_view { get; } - public ICollectionView favorite_algo_templates_view { get; } // selected items public AlgoDefinitionVM selected_algo_definition { get => _selected_algo_definition.selected_item; set => _selected_algo_definition.selected_item = value; } - public AlgoDefinitionTemplateVM selected_algo_template { - get => selected_algo_definition?.selected_template; - set { - if (value != selected_algo_template) { - if (value == null) { - if (selected_algo_definition != null) - selected_algo_definition.selected_template = null; - _selected_favorite_algo_template.set(null); - } else { - value.owner.selected_template = value; - _selected_algo_definition.select_explicit(value.owner); // idempotent - _selected_favorite_algo_template.Value = value.is_favorite ? value : null; // idempotent - } - RaisePropertyChanged(); - } - } - } - public AlgoDefinitionTemplateVM selected_favorite_algo_template { - get => _selected_favorite_algo_template.Value; - set { - if (_selected_favorite_algo_template.set_new(value)) { - if (value != null) { - value.owner.selected_template = value; - if (value.owner != selected_algo_definition) - _selected_algo_definition.select_explicit(value.owner); - RaisePropertyChanged(nameof(selected_algo_template)); - } - } - } - } public AlgoDefinitionVM selected_favorite_algo_definition { get => selected_algo_definition?.is_favorite ?? false ? selected_algo_definition : null; set { @@ -403,13 +308,10 @@ namespace mbts.spark { public ICommand revert_definition_cmd { get; } public ICommand set_default_algo_params_cmd { get; } public ICommand revert_algo_params_cmd { get; } - public ICommand undo_cmd { get; } - public ICommand redo_cmd { get; } public ICommand undo_definition_cmd { get; } public ICommand redo_definition_cmd { get; } public ICommand remove_template_cmd { get; } public ICommand toggle_is_favorite_cmd { get; } - public IEditableCollectionModel algo_template_collection_model => _algo_template_collection_model; public IObservable observe_account(bool start_immediately = false) => _accounts.observe_current(start_immediately); @@ -418,9 +320,7 @@ namespace mbts.spark { _time_in_force.observe_current(start_immediately); public IObservable observe_order_type(bool start_immediately = false) => - _model.algo_model.templates_enabled() ? - _selected_algo_template.observe_current(start_immediately).Select(t => t?.algo_root) : - _selected_algo_definition.observe_selected_item(start_immediately).Select(d => d?.algo_root); + _selected_algo_definition.observe_selected_item(start_immediately).Select(d => d?.algo_root); public OrderDefinition create(Side side) { if (HasErrors) @@ -428,25 +328,21 @@ namespace mbts.spark { var order = _model.factory.order(security, selected_account); order.TimeInForce = selected_time_in_force; order.Side = side; - if (_model.algo_model.templates_enabled()) { - selected_algo_template.algo_root.assign_to(order.CustomDef); - } else { - var algo = new CustomOrderDefinition(); - selected_algo_definition.algo_root.assign_to(algo); - switch (selected_algo_definition.key.mode) { - case AlgoKey.Mode.Persisted: - order.CustomDef = new CustomOrderDefinition { - Composite = new CompositeOrderDefinition { - AlgoTemplateKey = selected_algo_definition.key.persisted.id_uint, - Definition = algo - } - }; - selected_algo_definition.algo_root.assign_to(order.CustomDef.Composite.Definition); - break; - default: - order.CustomDef = algo; - break; - } + var algo = new CustomOrderDefinition(); + selected_algo_definition.algo_root.assign_to(algo); + switch (selected_algo_definition.key.mode) { + case AlgoKey.Mode.Persisted: + order.CustomDef = new CustomOrderDefinition { + Composite = new CompositeOrderDefinition { + AlgoTemplateKey = selected_algo_definition.key.persisted.id_uint, + Definition = algo + } + }; + selected_algo_definition.algo_root.assign_to(order.CustomDef.Composite.Definition); + break; + default: + order.CustomDef = algo; + break; } return order; } @@ -458,10 +354,6 @@ namespace mbts.spark { _favorite_algos_index = value.favorite_algos.Select((v, i) => (v, i)).ToDictionaryCrushed(t => t.v, t => t.i); foreach (var d in algo_definitions) { d.favorite_index = _get_favorite_algo_index(d.key); - foreach (var t in d.templates) { - if (t.key.mode == AlgoKey.Mode.Persisted) - t.is_favorite = _favorite_algos_index.ContainsKey(t.key); - } } favorite_algos_column_count = value.column_count; } @@ -479,29 +371,7 @@ namespace mbts.spark { void _on_template_metadata(IReadOnlyDictionary meta) { _algo_metadata = meta; - var curr_selected_template = selected_algo_template; - if (curr_selected_template?.is_favorite ?? false) - selected_favorite_algo_template = curr_selected_template; algo_definitions_view.Refresh(); } - - // This preserves the selected items through a save op. - // When a definition/template is persisted for the first time, its unpersisted-placeholder item is removed and replaced with a new persisted one. - // If an unpersisted item is selected when it is removed, the view will automatically select null - this method is a workaround for that. - IDisposable _preserve_selection_by_index() { - (int? selected_def_index, int? selected_tmp_index) = AlgoPersistenceHelper.selection_snapshot(algo_definitions, selected_algo_definition, selected_algo_template); - return new CompositeDisposable(_definitions_buffer.buffer_updates()) { - () => { - if (selected_def_index is int di && algo_definitions.check_index(di)) { - var def = algo_definitions[di]; - if (selected_tmp_index is int ti && def.templates.check_index(ti)) { - selected_algo_template = def.templates[ti]; - } else { - selected_algo_definition = def; - } - } - } - }; - } } } diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index a3b82fee1..ffde55bfd 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -55,14 +55,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -378,36 +188,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs index 5aff7d5e5..cd208a10b 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs @@ -1,16 +1,7 @@ using mbts.spark; -using mbts.ui.controls; -using mbts.ui.converters; -using mbts.ui.extensions; using mbts.ui.utility; -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.ComponentModel; -using System.Linq; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; using System.Windows.Input; namespace mbts.ui.spark.algo { @@ -18,51 +9,6 @@ namespace mbts.ui.spark.algo { /// Interaction logic for AlgoTemplateToolBar.xaml /// class AlgoTemplateToolBar : HeaderedItemsControl { - // The Favorites toggle button exists in 2 forms: as a MenuItem in AlgoTemplateToolBar, and as a ToggleButton in AlgoStudioView - // The const strings below are used to store this text in 1 place. - public const string - AddToFavoritesToolTip = "Add to Favorites", - RemoveFromFavoritesToolTip = "Remove from Favorites"; - - public static readonly IValueConverter FavoriteToggleButtonToolTipConverter = - ValueConverter.create(is_checked => is_checked ? RemoveFromFavoritesToolTip : AddToFavoritesToolTip); - - public static readonly RoutedUICommand - BeginAddNewCommand = new RoutedUICommand("Add New Template", "Add New", typeof(AlgoTemplateToolBar)), - BeginSaveCommand = new RoutedUICommand("Save Template", "Save", typeof(AlgoTemplateToolBar)), - BeginSaveDefinitionCommand = new RoutedUICommand("Save Definition", "Save", typeof(AlgoTemplateToolBar)), - BeginSaveAsCommand = new RoutedUICommand("Save Template As", "Save As", typeof(AlgoTemplateToolBar)), - BeginSaveDefinitionAsCommand = new RoutedUICommand("Save Definition As", "Save As", typeof(AlgoTemplateToolBar)), - BeginRevertCommand = new RoutedUICommand("Revert to Template", "Revert", typeof(AlgoTemplateToolBar)), - BeginRevertDefinitionCommand = new RoutedUICommand("Revert to Definition", "Revert", typeof(AlgoTemplateToolBar)), - BeginRedoCommand = new RoutedUICommand("Redo", "Redo", typeof(AlgoTemplateToolBar)), - BeginUndoCommand = new RoutedUICommand("Undo", "Undo", typeof(AlgoTemplateToolBar)), - BeginRedoDefinitionCommand = new RoutedUICommand("Redo Definition", "Redo Definition", typeof(AlgoTemplateToolBar)), - BeginUndoDefinitionCommand = new RoutedUICommand("Undo Definition", "Undo Definition", typeof(AlgoTemplateToolBar)), - BeginRemoveCommand = new RoutedUICommand("Delete Template", "Delete", typeof(AlgoTemplateToolBar)), - BeginToggleFavoriteCommand = new RoutedUICommand("Toggle Favorite", "Toggle Favorite", typeof(AlgoTemplateToolBar)); - - public static readonly ExecutedRoutedEventHandler - BeginAddNewExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._add_new()), - BeginSaveExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save()), - BeginSaveDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save_definition()), - BeginSaveAsExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save_as()), - BeginSaveDefinitionAsExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save_definition_as()), - BeginRevertExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._revert()), - BeginRevertDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._revert_definition()), - BeginRedoExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._redo()), - BeginUndoExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._undo()), - BeginRedoDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._redo_definition()), - BeginUndoDefinitionExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._undo_definition()), - BeginRemoveExecuted = WPFHelper.create_cmd_executed((tb, t) => tb._remove(t)), - BeginToggleFavoriteExecuted = WPFHelper.create_cmd_executed((tb, param) => tb.ToggleFavoriteCommand.TryExecuteIfCan(param)); - - ComboBox _templates_combo; - EditableTextBlock _definition_name_text; - IDisposable _input_hooks; - // this is a casted backing for 'Items' - IEditableCollectionViewAddNewItem _editable_items; - static AlgoTemplateToolBar() => DefaultStyleKeyProperty.OverrideMetadata(typeof(AlgoTemplateToolBar), new FrameworkPropertyMetadata(typeof(AlgoTemplateToolBar))); @@ -73,37 +19,6 @@ namespace mbts.ui.spark.algo { VisualStateNames.apply_all(this); } - #region ShowTemplatesDropDown - public static readonly DependencyProperty ShowTemplatesDropDownProperty = - WPFHelper.create_dp( - nameof(ShowTemplatesDropDown), - (o, v) => { - string dropdown_state, rename_state; - if (v) { - dropdown_state = VisualStateNames.TemplatesDropDown.Active; - rename_state = VisualStateNames.RenameTemplate.Active; - } else { - dropdown_state = VisualStateNames.TemplatesDropDown.Inactive; - rename_state = VisualStateNames.RenameTemplate.Inactive; - } - o._vsm.set_state(VisualStateNames.TemplatesDropDown.GroupName, dropdown_state); - o._vsm.set_state(VisualStateNames.RenameTemplate.GroupName, rename_state); - }, - coerce_require_persisted); - - public bool ShowTemplatesDropDown { get => (bool)GetValue(ShowTemplatesDropDownProperty); set => SetValue(ShowTemplatesDropDownProperty, value); } - #endregion - - #region ShowToggleFavorite - public static readonly DependencyProperty ShowToggleFavoriteProperty = - WPFHelper.create_dp( - nameof(ShowToggleFavorite), - (o, v) => VisualStateNames.ToggleFavorite.apply(o._vsm, v), - coerce_require_persisted); - - public bool ShowToggleFavorite { get => (bool)GetValue(ShowToggleFavoriteProperty); set => SetValue(ShowToggleFavoriteProperty, value); } - #endregion - #region ShowDefinitionName public static readonly DependencyProperty ShowDefinitionNameProperty = WPFHelper.create_dp(nameof(ShowDefinitionName)); @@ -130,16 +45,6 @@ namespace mbts.ui.spark.algo { public bool ShowRevertDefinition { get => (bool)GetValue(ShowRevertDefinitionProperty); set => SetValue(ShowRevertDefinitionProperty, value); } #endregion - #region ShowRenameDefinition - public static readonly DependencyProperty ShowRenameDefinitionProperty = - WPFHelper.create_dp( - nameof(ShowRenameDefinition), - (o, v) => VisualStateNames.RenameDefinition.apply(o._vsm, v), - coerce_require_persisted); - - public bool ShowRenameDefinition { get => (bool)GetValue(ShowRenameDefinitionProperty); set => SetValue(ShowRenameDefinitionProperty, value); } - #endregion - #region ShowSaveDefinition public static readonly DependencyProperty ShowSaveDefinitionProperty = WPFHelper.create_dp( @@ -154,7 +59,6 @@ namespace mbts.ui.spark.algo { public static readonly DependencyProperty DefinitionProperty = WPFHelper.create_dp(nameof(Definition), (o, v) => { - o.CoerceValue(ShowRenameDefinitionProperty); o.CoerceValue(ShowSaveDefinitionProperty); //o.CoerceValue(ShowRevertDefinitionProperty); o.CoerceValue(ShowUndoRedoButtonsProperty); @@ -163,51 +67,6 @@ namespace mbts.ui.spark.algo { public AlgoDefinitionVM Definition { get => GetValue(DefinitionProperty) as AlgoDefinitionVM; set => SetValue(DefinitionProperty, value); } #endregion - #region SelectedItem - public static readonly DependencyProperty SelectedItemProperty = - WPFHelper.create_dp( - nameof(SelectedItem), - FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, - (o, oldval, newval) => { - // TODO: consider an alternate solution to the problem here, subscribing to PropertyChanged for 1 property feels a little hackey. - // We could make IsEdited a non-readonly dp, and just bind to SelectedItem.is_edited; however, this compromises the AlgoTemplateToolBar's public interface bc the concept this value represents is not ambiguous. - // IsEdited always means 1 thing: the SelectedItem's (algo-template) order-definition is 'dirty' - different from the values currently in the OrderPropertiesEditor. - // The reason this is useful: - /// When the algo template becomes dirty (which is easy to tell via a dpchanged callback), we call . - /// Then, in any subsequent Save/SaveAs Executed callbacks, we check if an item is currently being edited (). - /// If that is true, we update the IAlgoTemplateItem's order-definition (). - /// - if (oldval is INotifyPropertyChanged oldnpc) oldnpc.PropertyChanged -= o._on_SelectedItem_PropertyChanged; - if (newval is INotifyPropertyChanged newnpc) newnpc.PropertyChanged += o._on_SelectedItem_PropertyChanged; - o.CoerceValue(IsEditedProperty); - }); - - void _on_SelectedItem_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(AlgoDefinitionTemplateVM.is_dirty)) - CoerceValue(IsEditedProperty); - } - - public AlgoDefinitionTemplateVM SelectedItem { get => GetValue(SelectedItemProperty) as AlgoDefinitionTemplateVM; set => SetValue(SelectedItemProperty, value); } - #endregion - - #region EditableCollectionModel - public static readonly DependencyProperty EditableCollectionModelProperty = - WPFHelper.create_dp, AlgoTemplateToolBar>(nameof(EditableCollectionModel)); - public IEditableCollectionModel EditableCollectionModel { get => GetValue(EditableCollectionModelProperty) as IEditableCollectionModel; set => SetValue(EditableCollectionModelProperty, value); } - #endregion - - #region AddNew - public static readonly DependencyProperty AddNewCommandProperty = - WPFHelper.create_dp(nameof(AddNewCommand)); - public ICommand AddNewCommand { get => GetValue(AddNewCommandProperty) as ICommand; set => SetValue(AddNewCommandProperty, value); } - #endregion - - #region Save - public static readonly DependencyProperty SaveCommandProperty = - WPFHelper.create_dp(nameof(SaveCommand)); - public ICommand SaveCommand { get => GetValue(SaveCommandProperty) as ICommand; set => SetValue(SaveCommandProperty, value); } - #endregion - #region SaveDefinition public static readonly DependencyProperty SaveDefinitionCommandProperty = WPFHelper.create_dp(nameof(SaveDefinitionCommand)); @@ -220,36 +79,12 @@ namespace mbts.ui.spark.algo { public ICommand SetDefaultsCommand { get => GetValue(SetDefaultsCommandProperty) as ICommand; set => SetValue(SetDefaultsCommandProperty, value); } #endregion - #region Revert - public static readonly DependencyProperty RevertCommandProperty = - WPFHelper.create_dp(nameof(RevertCommand)); - public ICommand RevertCommand { get => GetValue(RevertCommandProperty) as ICommand; set => SetValue(RevertCommandProperty, value); } - #endregion - #region RevertDefinition public static readonly DependencyProperty RevertDefinitionCommandProperty = WPFHelper.create_dp(nameof(RevertDefinitionCommand)); public ICommand RevertDefinitionCommand { get => GetValue(RevertDefinitionCommandProperty) as ICommand; set => SetValue(RevertDefinitionCommandProperty, value); } #endregion - #region Remove - public static readonly DependencyProperty RemoveCommandProperty = - WPFHelper.create_dp(nameof(RemoveCommand)); - public ICommand RemoveCommand { get => GetValue(RemoveCommandProperty) as ICommand; set => SetValue(RemoveCommandProperty, value); } - #endregion - - #region Undo - public static readonly DependencyProperty UndoCommandProperty = - WPFHelper.create_dp(nameof(UndoCommand)); - public ICommand UndoCommand { get => GetValue(UndoCommandProperty) as ICommand; set => SetValue(UndoCommandProperty, value); } - #endregion - - #region Redo - public static readonly DependencyProperty RedoCommandProperty = - WPFHelper.create_dp(nameof(RedoCommand)); - public ICommand RedoCommand { get => GetValue(RedoCommandProperty) as ICommand; set => SetValue(RedoCommandProperty, value); } - #endregion - #region UndoDefinition public static readonly DependencyProperty UndoDefinitionCommandProperty = WPFHelper.create_dp(nameof(UndoDefinitionCommand)); @@ -262,33 +97,6 @@ namespace mbts.ui.spark.algo { public ICommand RedoDefinitionCommand { get => GetValue(RedoDefinitionCommandProperty) as ICommand; set => SetValue(RedoDefinitionCommandProperty, value); } #endregion - #region ToggleFavorite - public static readonly DependencyProperty ToggleFavoriteCommandProperty = - WPFHelper.create_dp(nameof(ToggleFavoriteCommand)); - public ICommand ToggleFavoriteCommand { get => GetValue(ToggleFavoriteCommandProperty) as ICommand; set => SetValue(ToggleFavoriteCommandProperty, value); } - #endregion - - #region ToggleFavorite Parameter - public static readonly DependencyProperty ToggleFavoriteCommandParameterProperty = - WPFHelper.create_dp(nameof(ToggleFavoriteCommandParameter)); - public object ToggleFavoriteCommandParameter { get => GetValue(ToggleFavoriteCommandParameterProperty); set => SetValue(ToggleFavoriteCommandParameterProperty, value); } - #endregion - - #region IsEdited - /// - /// This property indicates whether or not the order definition held by the is 'dirty' - different from the one that would be created by the OrderPropertiesEditor at this time. - /// When it becomes dirty, we call . This leads the viewmodel to take a snapshot of its current state, - /// so in the event that another template is selected, or the values in the OrderPropertiesEditor are reverted, we can snap everything back into place by calling - /// - static readonly DependencyPropertyKey IsEditedPropertyKey = - WPFHelper.create_ro_dp( - nameof(IsEdited), - null, - (o, v) => o.SelectedItem is AlgoDefinitionTemplateVM t && t.is_dirty && !t.is_custom_placeholder); - internal static readonly DependencyProperty IsEditedProperty = IsEditedPropertyKey.DependencyProperty; - internal bool IsEdited { get => (bool)GetValue(IsEditedProperty); private set => SetValue(IsEditedPropertyKey, value); } - #endregion - #region IsEditing public static readonly DependencyProperty IsEditingProperty = WPFHelper.create_dp(nameof(IsEditing)); @@ -301,152 +109,14 @@ namespace mbts.ui.spark.algo { public bool IsReadOnly { get => (bool)GetValue(IsReadOnlyProperty); set => SetValue(IsReadOnlyProperty, value); } #endregion - public override void OnApplyTemplate() { - Disposable.dispose(ref _input_hooks); - _templates_combo = this.assert_template_child("PART_Templates_ComboBox"); - _definition_name_text = this.assert_template_child("PART_DefinitionName_EditableTextBlock"); - _input_hooks = Disposable.Create( - _definition_name_text.SubscribePreviewKeyDown((s, e) => { - switch (e.Key) { - case Key.Escape: - if (_definition_name_text.IsEditing) - _definition_name_text.abort(); - break; - } - }), - _templates_combo.SubscribePreviewKeyDown((s, e) => { - switch (e.Key) { - case Key.Enter: - Keyboard.ClearFocus(); - _commit(); - break; - case Key.Escape: - _cancel(); - Keyboard.ClearFocus(); - SetCurrentValue(IsEditingProperty, false); - break; - } - })); - } - - protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { - base.OnItemsChanged(e); - _editable_items = Items; - } - static bool coerce_require_persisted(AlgoTemplateToolBar d, bool value) => value && d.Definition is AlgoDefinitionVM vm && vm.key.mode == AlgoKey.Mode.Persisted; - #region Transactional - void _add_new() { - _prepare_edit(); - var model = EditableCollectionModel; - if (model != null && model.can_add_new) - _editable_items.AddNewItem(model.new_item_factory()); - } - - void _cancel() => _editable_items.CancelAny(); - - void _commit(bool execute_save = false) { - if (_editable_items.IsEditingItem) { - var editing_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentEditItem; - editing_item.name = _templates_combo.Text; - _editable_items.CommitEdit(); - SetCurrentValue(SelectedItemProperty, editing_item); - if (execute_save) - SaveCommand.TryExecuteIfCan(editing_item); - SetCurrentValue(IsEditingProperty, false); - } else if (_editable_items.IsAddingNew) { - var new_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentAddItem; - new_item.name = _templates_combo.Text; - _editable_items.CommitNew(); - SetCurrentValue(SelectedItemProperty, new_item); - if (execute_save) - SaveCommand.TryExecuteIfCan(new_item); - SetCurrentValue(IsEditingProperty, false); - } else { - SaveCommand.TryExecuteIfCan(SelectedItem); - } - } - - void _revert() => RevertCommand.TryExecuteIfCan(null); - void _revert_definition() => RevertDefinitionCommand.TryExecuteIfCan(null); - - void _redo() => _redo(SelectedItem); - void _undo() => _undo(SelectedItem); - void _redo_definition() => _redo(Definition); - void _undo_definition() => _undo(Definition); - - void _redo(IAlgoStudioContentVM c) { - if (c != null && c.can_redo) - c.redo(); - } - void _undo(IAlgoStudioContentVM c) { - if (c != null && c.can_undo) - c.undo(); - } - - void _remove(object template) { - _editable_items?.Remove(template); - RemoveCommand.TryExecuteIfCan(template); - } - - void _save() { - if (SelectedItem == null) { - _add_new(); - } else _commit(true); - } - - void _save_definition() { - if (Definition is AlgoDefinitionVM d && d.is_dirty) { - _definition_name_text?.end_edit(); - SaveDefinitionCommand?.TryExecuteIfCan(d); - } - } - - void _save_as() { - if (SelectedItem == null) { - _add_new(); - } else { - _prepare_edit(); - _editable_items.EditItem(SelectedItem); - } - } - - void _save_definition_as() { - _definition_name_text?.begin_edit(); - } - - bool _focus() { - if (_templates_combo == null || !_templates_combo.IsEnabled) return false; - var textbox = _templates_combo.descendants_of_type().FirstOrDefault(); - if (textbox == null) return false; - FocusManager.SetFocusedElement(this, textbox); - Focus(); - Keyboard.Focus(textbox); - return true; - } - - void _prepare_edit() { - _editable_items.CancelAny(); - SetCurrentValue(IsEditingProperty, true); - // This is a hack ¯\_(ツ)_/¯ but it is effective and simple. - // The correct/non-hack way to achieve this behavior most likely requires re-templating the ComboBox - a lot of work. - /// The goal: auto-focus the textbox inside the ComboBox. - /// The dilemma: the textbox does not exist until the ComboBox is rendered again (since we just changed ComboBox.IsEditing to true in the previous line, this has not happened yet). - WPFHelper.push_frame_until(() => _focus()); - } - #endregion - public static class VisualStateNames { public static void apply_all(AlgoTemplateToolBar toolbar) { UndoRedo.apply(toolbar._vsm, toolbar.ShowUndoRedoButtons); RevertDefinition.apply(toolbar, toolbar.ShowRevertDefinition); - RenameDefinition.apply(toolbar._vsm, toolbar.ShowRenameDefinition); SaveDefinition.apply(toolbar._vsm, toolbar.ShowSaveDefinition); - TemplatesDropDown.apply(toolbar._vsm, toolbar.ShowTemplatesDropDown); - RenameTemplate.apply(toolbar._vsm, toolbar.ShowTemplatesDropDown); // there is no corresponding 'CanUserRenameTemplate' property at the moment, for now it just refers to ShowTemplates. - ToggleFavorite.apply(toolbar._vsm, toolbar.ShowToggleFavorite); } public static class UndoRedo { public const string GroupName = nameof(UndoRedo); @@ -466,15 +136,6 @@ namespace mbts.ui.spark.algo { public static void apply(AlgoTemplateToolBar owner, bool active) => owner.Dispatcher.InvokeAsync(() => owner._vsm.set_state(GroupName, active ? Active : Inactive)); } - public static class RenameDefinition { - public const string GroupName = nameof(RenameDefinition); - public static readonly string - Active = $"{GroupName}{VisualStates.Active}", - Inactive = $"{GroupName}{VisualStates.Inactive}"; - - public static void apply(VisualStateModel vsm, bool active) => - vsm.set_state(GroupName, active ? Active : Inactive); - } public static class SaveDefinition { public const string GroupName = nameof(SaveDefinition); public static readonly string @@ -484,33 +145,6 @@ namespace mbts.ui.spark.algo { public static void apply(VisualStateModel vsm, bool active) => vsm.set_state(GroupName, active ? Active : Inactive); } - public static class TemplatesDropDown { - public const string GroupName = nameof(TemplatesDropDown); - public static readonly string - Active = $"{GroupName}{VisualStates.Active}", - Inactive = $"{GroupName}{VisualStates.Inactive}"; - - public static void apply(VisualStateModel vsm, bool active) => - vsm.set_state(GroupName, active ? Active : Inactive); - } - public static class RenameTemplate { - public const string GroupName = nameof(RenameTemplate); - public static readonly string - Active = $"{GroupName}{VisualStates.Active}", - Inactive = $"{GroupName}{VisualStates.Inactive}"; - - public static void apply(VisualStateModel vsm, bool active) => - vsm.set_state(GroupName, active ? Active : Inactive); - } - public static class ToggleFavorite { - public const string GroupName = nameof(ToggleFavorite); - public static readonly string - Active = $"{GroupName}{VisualStates.Active}", - Inactive = $"{GroupName}{VisualStates.Inactive}"; - - public static void apply(VisualStateModel vsm, bool active) => - vsm.set_state(GroupName, active ? Active : Inactive); - } } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml b/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml deleted file mode 100644 index aae546707..000000000 --- a/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml +++ /dev/null @@ -1,270 +0,0 @@ - - - White - - - - #FFCCCCCC - #FF888888 - #FF444444 - #FF888888 - #FF444444 - #FFAAAAAA - #FF888888 - Black - - - - - - - - - - - - #FFFFFFFF - #FF818181 - #FFCCEEFB - #FF27C7F7 - #FF595959 - #FF262626 - #FF82DFFB - #FF1CC4F7 - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml.cs deleted file mode 100644 index 815a421b5..000000000 --- a/src/client/spark/view/Controls/Order/AlgoTemplateTreeView.xaml.cs +++ /dev/null @@ -1,33 +0,0 @@ -using mbts.ui.converters; -using System.Windows; -using System.Windows.Data; - -namespace mbts.ui.spark.algo { - static class AlgoTemplateTreeView { - public static readonly IMultiValueConverter ItemsMarginConverter = - ValueConverter.create((margin, level) => new Thickness(level * margin.Left, margin.Top, margin.Right, margin.Bottom)); - - public static readonly IValueConverter NodeLevelConverter = - ValueConverter.create(pnode => pnode + 1); - - public static readonly DependencyProperty ItemsMarginProperty = - DependencyProperty.RegisterAttached( - "ItemsMargin", - typeof(Thickness), - typeof(AlgoTemplateTreeView), - new PropertyMetadata(new Thickness(1))); - - public static Thickness GetItemsMargin(DependencyObject d) => (Thickness)d.GetValue(ItemsMarginProperty); - public static void SetItemsMargin(DependencyObject d, Thickness value) => d.SetValue(ItemsMarginProperty, value); - - public static readonly DependencyProperty NodeLevelProperty = - DependencyProperty.RegisterAttached( - "NodeLevel", - typeof(int), - typeof(AlgoTemplateTreeView), - new PropertyMetadata(0)); - - public static int GetNodeLevel(DependencyObject d) => (int)d.GetValue(NodeLevelProperty); - public static void SetNodeLevel(DependencyObject d, int value) => d.SetValue(NodeLevelProperty, value); - } -} \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index 28976f379..fea5ff2e8 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -22,12 +22,6 @@ 12 -