From 812ba5cd0fbc21b5f4fcf27a4d3d5c854a038bec Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Tue, 14 Nov 2017 22:31:46 -0500 Subject: [PATCH 01/12] Spark-283 partially done --- .vscode/launch.json | 48 +++++- .../orderman/tests/spreader_order_tests.cpp | 163 +++++++++++++++++- src/server/sparkd/spreader/SpreaderUtils.h | 6 +- 3 files changed, 210 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4838001e2..415f56d1c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -130,6 +130,52 @@ "sourceFileMap": { "/bts/": "${workspaceRoot}" } - } + }, + { + "name": "Docker Launch", + "type": "cppdbg", + "request": "launch", + "program": "/bts/build/gcc_debug/src/server/sparkd/sparkd", + "args": ["test"], + "stopAtEntry": false, + "cwd": "/bts/build/gcc_debug/src/server/sparkd", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "preLaunchTask": "", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "enable pretty-printer", + "ignoreFailures": true + } + ], + "pipeTransport": { + "pipeCwd": "${workspaceRoot}", + "pipeProgram": "c:\\program files\\git\\usr\\bin\\ssh.exe", + "pipeArgs": ["-p 2000","root@localhost"], + "debuggerPath": "/usr/bin/gdb" + }, + "sourceFileMap": { + "/bts/": "${workspaceRoot}" + } + }, + { + "name": "Docker attach", + "type": "cppdbg", + "request": "attach", + "program": "/bts/build/gcc_debug/src/server/sparkd/sparkd", + "processId": "${command:pickRemoteProcess}", + "MIMode": "gdb", + "pipeTransport": { + "pipeCwd": "${workspaceRoot}", + "pipeProgram": "c:\\program files\\git\\usr\\bin\\ssh.exe", + "pipeArgs": ["-p 2000","root@localhost"], + "debuggerPath": "/usr/bin/gdb" + }, + "sourceFileMap": { + "/bts/": "${workspaceRoot}" + } + } ] } \ No newline at end of file diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 0e4cca40b..36c405c95 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -192,6 +192,78 @@ void _fill_book(Book& book, double inside_market, double tick_size, uint16_t* si inside_market += tick_size; } } +/**********************************************************************/ +/*****************creating sample data for tests***********************/ +/**********************************************************************/ +void fill_leg_spreader_params (bts::spark::LegSpreaderParams& sp, double price_multiplier, bool is_active_quoting, bts::spark::QuoteParams& qp) +{ + sp.set_price_multiplier (price_multiplier); + sp.set_is_active_quoting (is_active_quoting); + sp.mutable_quote_params()->MergeFrom (qp); + +} +void fill_quote_params (bts::spark::QuoteParams& qp, double lean_ratio) +{ + qp.set_lean_quantity_ratio (lean_ratio); +} +void fill_future_security (Security& sec, uint32_t key, const std::string& symbol, Venue exchange, + uint32_t expiration_year, uint32_t expiration_month, uint32_t expiration_day) +{ + sec.set_id (key); + sec.set_symbol (symbol); + sec.set_exchange (exchange); + FutureContract fc; + fc.set_expiration_year(expiration_year); + fc.set_expiration_month(expiration_month); + fc.set_expiration_day(expiration_day); + sec.mutable_future()->MergeFrom (fc); +} +void fill_contract_leg (ContractLeg& leg, const Security& sec, double leg_quantity, Side side, double leg_delta) +{ + leg.mutable_security()->MergeFrom (sec); + leg.set_quantity (leg_quantity); + leg.set_side (side); + leg.set_leg_delta (leg_delta); +} +void fill_future_contract_leg (ContractLeg& leg, + uint32_t key, const std::string& symbol, Venue exchange, + uint32_t year, uint32_t month, uint32_t day, + double leg_quantity, Side side, double leg_delta) +{ + Security sec; + fill_future_security (sec, key, symbol, exchange, year, month, day); + fill_contract_leg (leg, sec, leg_quantity, side, leg_delta); +} +void fill_synthetic_contract (SyntheticContract& sc, const std::string& name, const std::vector& legs) +{ + sc.set_name (name); + for (auto it = legs.begin(); it != legs.end(); it++) + sc.mutable_legs()->Add()->MergeFrom(*it); +} +void fill_execution_strategy (SyntheticExecutionStrategy& s_exs, const Security& sec, const std::vector& leg_params) +{ + size_t l_idx = 0; + ExecutionStrategy exs; + for (auto it = sec.synthetic().legs().begin(); it != sec.synthetic().legs().end(); it++) + { + const ContractLeg& leg = *it; + exs.mutable_leg_params_map()->insert(google::protobuf::MapPair(leg.security().id(), leg_params[l_idx])); + l_idx++; + } + s_exs.mutable_security()->MergeFrom (sec); + s_exs.mutable_execution_strategy()->MergeFrom (exs); +} +/**********************************************************************/ +/**************************misc helpers********************************/ +/**********************************************************************/ +double gen_rand (double min, double max) +{ + double f = (double)rand() / RAND_MAX; + return min + f * (max - min); +} +/**********************************************************************/ +/**********************************************************************/ +/**********************************************************************/ // QuoteLegPriceQuantityState is used by the quoter // to keep the state of the order state, ie @@ -199,12 +271,95 @@ void _fill_book(Book& book, double inside_market, double tick_size, uint16_t* si // filled qty // reduced quantity due to other legs // output price, qty -void test_QuoteLegPriceQuantityState () -{ +TEST_CASE ("quoter price quantity state", "[spreader]") +{ + // construction + QuoteLegPriceQuantityState s (Price (-1.0), Quantity (200)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (200)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (0)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (0)); + REQUIRE (s.remaining_to_fill() == Quantity (200)); + REQUIRE (s.out_price() == Price (0.0)); + REQUIRE (s.out_quantity() == Quantity (0)); + // add fills + s.add_filled_quantity (Quantity(10)); + s.add_filled_quantity (Quantity(15)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (200)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (25)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (0)); + REQUIRE (s.remaining_to_fill() == Quantity (175)); + REQUIRE (s.out_price() == Price (0.0)); + REQUIRE (s.out_quantity() == Quantity (0)); + // add hedged reduction + s.add_hedged_reduction (Quantity(15)); + s.add_hedged_reduction (Quantity(5)); + s.add_hedged_reduction (Quantity(30)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (200)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (25)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (50)); + REQUIRE (s.remaining_to_fill() == Quantity (125)); + REQUIRE (s.out_price() == Price (0.0)); + REQUIRE (s.out_quantity() == Quantity (0)); + // set out price and quantity + s.update_out_price_quantity (Price (-0.35), Quantity (123)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (200)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (25)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (50)); + REQUIRE (s.remaining_to_fill() == Quantity (125)); + REQUIRE (s.out_price() == Price (-0.35)); + REQUIRE (s.out_quantity() == Quantity (123)); + // change target price / quantity + s.update_target_leg_quantity (Quantity (105)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (105)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (25)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (50)); + REQUIRE (s.remaining_to_fill() == Quantity (30)); + REQUIRE (s.out_price() == Price (-0.35)); + REQUIRE (s.out_quantity() == Quantity (123)); + // make the hedges go over + s.add_hedged_reduction (Quantity(35)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (105)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (25)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (85)); + REQUIRE (s.remaining_to_fill() == Quantity (0)); + REQUIRE (s.out_price() == Price (-0.35)); + REQUIRE (s.out_quantity() == Quantity (123)); + // make the fills go over + s.add_filled_quantity (Quantity(15)); + REQUIRE (s.target_spread_price() == Price (-1.0)); + REQUIRE (s.target_leg_quantity() == Quantity (105)); + REQUIRE (s.aggregated_filled_quantity() == Quantity (40)); + REQUIRE (s.aggregated_hedged_reduction() == Quantity (85)); + REQUIRE (s.remaining_to_fill() == Quantity (0)); + REQUIRE (s.out_price() == Price (-0.35)); + REQUIRE (s.out_quantity() == Quantity (123)); } // tests spread_qty -> leg_qty and leg_2_qty -> leg_1_qty -void test_leg_quantity_normalization () -{ +TEST_CASE ("test_leg_quantity_normalization", "[spreader]") +{ + int n_runs = 20; + for (int i = 0; i < n_runs; i++) + { + double leg_mult = gen_rand(-5.0, 2.0); + uint32_t spr_qty = gen_rand(0, 100); + std::string symbol = "ES"; + ContractLeg leg; + fill_future_contract_leg (leg, 0, symbol, bts::api::Venue::VENUE_GLBX, 2018, 12, 15, leg_mult, api::Side::SIDE_BUY, 1.0); + QuoteParams qp; + fill_quote_params(qp, 1.0); + LegSpreaderParams lsp; + fill_leg_spreader_params (lsp, 1.0, true, qp); + LegPropertyData leg_prop_data (0, leg, lsp, api::Side::SIDE_BUY); + double calc_value = SpreaderUtils::get_leg_quantity_from_spreader_quantity (leg_prop_data, Quantity (spr_qty)), + expected_value = std::round(std::abs(leg_mult * spr_qty)); + REQUIRE (calc_value == expected_value); + } } // given a qty, traverses the book to find the level // where the accumulated qty >= given qty diff --git a/src/server/sparkd/spreader/SpreaderUtils.h b/src/server/sparkd/spreader/SpreaderUtils.h index 5f0130bb0..320c06aa3 100644 --- a/src/server/sparkd/spreader/SpreaderUtils.h +++ b/src/server/sparkd/spreader/SpreaderUtils.h @@ -92,8 +92,10 @@ public: // given the quantity of the spreader order, determines the quality // of the leg static double get_leg_quantity_from_spreader_quantity(const LegPropertyData& leg, Quantity spreader_order_quantity) - { - return leg.size_multiplier () * spreader_order_quantity.value(); + { + double rv = std::abs (leg.size_multiplier () * spreader_order_quantity.value()); + rv = std::round (rv); + return rv; } /**********************************************************************/ From 08a62988f09c4d56eca2bb41b4be86e5d8658e06 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 10:18:09 -0500 Subject: [PATCH 02/12] SPARK-283 more tests --- .../orderman/tests/spreader_order_tests.cpp | 21 +++++++++++++++++++ src/server/sparkd/spreader/SpreaderUtils.h | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 36c405c95..29850d84b 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -360,6 +360,27 @@ TEST_CASE ("test_leg_quantity_normalization", "[spreader]") expected_value = std::round(std::abs(leg_mult * spr_qty)); REQUIRE (calc_value == expected_value); } + for (int i = 0; i < n_runs; i++) + { + double leg_mult_1 = gen_rand(-5.0, 2.0), + leg_mult_2 = gen_rand(-5.0, 2.0); + uint32_t leg_qty = gen_rand(0, 100); + std::string symbol = "ES"; + ContractLeg leg_1, leg_2; + fill_future_contract_leg (leg_1, 0, symbol, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_BUY, 1.0); + fill_future_contract_leg (leg_2, 1, symbol, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_SELL, 1.0); + QuoteParams qp_1, qp_2; + fill_quote_params(qp_1, 1.0); + fill_quote_params(qp_2, 1.0); + LegSpreaderParams lsp_1, lsp_2; + fill_leg_spreader_params (lsp_1, 1.0, true, qp_1); + fill_leg_spreader_params (lsp_2, 1.0, true, qp_2); + LegPropertyData leg_prop_data_1 (0, leg_1, lsp_1, api::Side::SIDE_BUY); + LegPropertyData leg_prop_data_2 (1, leg_2, lsp_2, api::Side::SIDE_BUY); + double calc_value = SpreaderUtils::get_equivelant_size (leg_prop_data_1, Quantity(leg_qty), leg_prop_data_2), + expected_value = std::round(std::abs(leg_qty * leg_mult_2 / leg_mult_1)); + REQUIRE (calc_value == expected_value); + } } // given a qty, traverses the book to find the level // where the accumulated qty >= given qty diff --git a/src/server/sparkd/spreader/SpreaderUtils.h b/src/server/sparkd/spreader/SpreaderUtils.h index 320c06aa3..b8a47f7cb 100644 --- a/src/server/sparkd/spreader/SpreaderUtils.h +++ b/src/server/sparkd/spreader/SpreaderUtils.h @@ -87,7 +87,9 @@ public: static double get_equivelant_size (const LegPropertyData& source_leg, Quantity source_leg_quantity, const LegPropertyData& target_leg) { - return target_leg.size_multiplier () * source_leg_quantity.value () / source_leg.size_multiplier (); + double rv = std::abs(target_leg.size_multiplier () * source_leg_quantity.value () / source_leg.size_multiplier ()); + rv = std::round(rv); + return rv; } // given the quantity of the spreader order, determines the quality // of the leg From 0bb0a4ed343cdb5b355e7ad897a34f49ac4f3482 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 12:55:57 -0500 Subject: [PATCH 03/12] SPARK-283 book traversal test --- .../orderman/tests/spreader_order_tests.cpp | 99 ++++++++++++++++--- 1 file changed, 83 insertions(+), 16 deletions(-) diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 29850d84b..d622e6d17 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -179,19 +179,6 @@ const char *fut2 = result.mutable_spreader()->MergeFrom(create_order); return result; } - -void _fill_book(Book& book, double inside_market, double tick_size, uint16_t* sizes, bts::api::Side side) -{ - BookSide& bookside = book.side(side); - for (size_t i = 0; i < book.n_levels(); i++) - { - bookside[i] = md2::PriceSize::from_double<2>(inside_market, sizes[i]); - if (side == bts::api::SIDE_BUY) - inside_market -= tick_size; - else - inside_market += tick_size; - } -} /**********************************************************************/ /*****************creating sample data for tests***********************/ /**********************************************************************/ @@ -253,6 +240,24 @@ void fill_execution_strategy (SyntheticExecutionStrategy& s_exs, const Security& s_exs.mutable_security()->MergeFrom (sec); s_exs.mutable_execution_strategy()->MergeFrom (exs); } +void fill_book(Book& book, double inside_market, double tick_size, uint16_t* sizes, bts::api::Side side) +{ + BookSide& bookside = book.side(side); + for (size_t i = 0; i < book.n_levels(); i++) + { + bookside[i] = md2::PriceSize::from_double<2>(inside_market, sizes[i]); + if (side == bts::api::SIDE_BUY) + inside_market -= tick_size; + else + inside_market += tick_size; + } +} +void fill_book(Book& book, double* prices, uint16_t* sizes, bts::api::Side side) +{ + BookSide& bookside = book.side(side); + for (size_t i = 0; i < book.n_levels(); i++) + bookside[i] = md2::PriceSize::from_double<2>(prices[i], sizes[i]); +} /**********************************************************************/ /**************************misc helpers********************************/ /**********************************************************************/ @@ -384,8 +389,70 @@ TEST_CASE ("test_leg_quantity_normalization", "[spreader]") } // given a qty, traverses the book to find the level // where the accumulated qty >= given qty -void test_book_traversal () +TEST_CASE ("book traversal", "[spreader]") { + Price calc_price; + Quantity calc_quantity; + SECTION("test level 1 sell") + { + Book book; + uint16_t sizes[] = { 11, 13, 17, 19, 23 }; + double prices[] = { 2500.25, 2500.50, 2501, 2502, 2503 }; + fill_book (book, prices, sizes, bts::api::Side::SIDE_SELL); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_SELL, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_price == Price (2500.25)); + REQUIRE (calc_quantity == Quantity (7)); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + } + SECTION("test level 2 sell") + { + Book book; + uint16_t sizes[] = { 23, 1, 17, 19, 23 }; + double prices[] = { 2500.25, 2500.75, 2501, 2502, 2503 }; + fill_book (book, prices, sizes, bts::api::Side::SIDE_SELL); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_SELL, Quantity (24), calc_price, calc_quantity); + REQUIRE (calc_price == Price (2500.75)); + REQUIRE (calc_quantity == Quantity (24)); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + } + SECTION("test req_qty == book sell") + { + Book book; + uint16_t sizes[] = { 23, 1, 16, 33, 27 }; + double prices[] = { 2500.25, 2500.75, 2501, 2502, 2503 }; + fill_book (book, prices, sizes, bts::api::Side::SIDE_SELL); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_SELL, Quantity (100), calc_price, calc_quantity); + REQUIRE (calc_price == Price (2503)); + REQUIRE (calc_quantity == Quantity (100)); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + } + SECTION("test req_qty > book buy") + { + Book book; + uint16_t sizes[] = { 13, 11, 16, 33, 27 }; + double prices[] = { 2500.25, 2500, 2499, 2498, 2497}; + fill_book (book, prices, sizes, bts::api::Side::SIDE_BUY); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (200), calc_price, calc_quantity); + REQUIRE (calc_price == Price (2497)); + REQUIRE (calc_quantity == Quantity (100)); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_SELL, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + } + SECTION("test_5 buy") + { + // empty book + Book book; + uint16_t sizes[] = { 0, 0, 0, 0, 0 }; + double prices[] = { 2500.00, 2500.25, 2500.50, 2500.75, 2501 }; + fill_book (book, prices, sizes, bts::api::Side::SIDE_BUY); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (1), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_SELL, Quantity (7), calc_price, calc_quantity); + REQUIRE (calc_quantity == Quantity (0)); + } } // this is how the spreader organizes the data void test_SpreaderBookkeepingData () @@ -429,7 +496,7 @@ void disabled_temp() db.secs[SecurityKey(sec2.second.id())] = &sec2.second; SECTION("test") - { + { api::orderman::CreateOrderRequest cor = make_spreader_order_create_request (api::Side::SIDE_BUY, Price (0), Quantity (100)); /*****************Creating the order********************/ SpreaderOrder order (db,factory,OrderKey(1),cor); @@ -453,7 +520,7 @@ void disabled_temp() /*********************Market*********************/ Book book; uint16_t book_sizes_1[] = { 11, 13, 17, 19, 23 }; - _fill_book (book, 2500, 0.25, book_sizes_1, bts::api::Side::SIDE_SELL); + fill_book (book, 2500, 0.25, book_sizes_1, bts::api::Side::SIDE_SELL); book = Book (bts::SecurityKey(cor.spreader().security().synthetic().legs(0).security().id()), book.side (api::Side::SIDE_BUY), book.side (api::Side::SIDE_SELL)); for (int i = 0; i < 100; i++) { From 40e9c4a1516dca9ccd1b4566c108abc21a075e2d Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 20:18:27 -0500 Subject: [PATCH 04/12] SPARK-283 spreader bookkeeper data test --- .../orderman/tests/spreader_order_tests.cpp | 119 +++++++++++++++++- .../sparkd/spreader/SpreaderBookkeepingData.h | 1 - src/server/sparkd/spreader/SpreaderLegData.h | 10 +- 3 files changed, 121 insertions(+), 9 deletions(-) diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index d622e6d17..a43b15d32 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -32,6 +32,32 @@ struct MySecDB : public securities::ISecurityDB {return secs;} }; +class SpreaderManagerForTest : public ISpreaderManager +{ +private: + std::vector _legs; + std::vector _books; +public: + SpreaderManagerForTest (const std::vector legs, + const std::vector books) + : + _legs (legs), + _books (books) + { + } + const bts::orderman::Book& get_book(size_t leg_index) const override + { + return _books[leg_index]; + } + // the caller can keep a reference to the returned type forever. the + // data in the returned type is all const, so nothing will change + // beneath it + const LegPropertyData& get_leg_data(size_t leg_index) const override + { + return _legs[leg_index]; + } +}; + const char *fut1 = "id: 1\n" "symbol: \"ES\"\n" @@ -416,7 +442,7 @@ TEST_CASE ("book traversal", "[spreader]") REQUIRE (calc_quantity == Quantity (24)); SpreaderUtils::traverse_depth_for_quantity (book, bts::api::Side::SIDE_BUY, Quantity (7), calc_price, calc_quantity); REQUIRE (calc_quantity == Quantity (0)); - } + } SECTION("test req_qty == book sell") { Book book; @@ -454,10 +480,97 @@ TEST_CASE ("book traversal", "[spreader]") REQUIRE (calc_quantity == Quantity (0)); } } -// this is how the spreader organizes the data -void test_SpreaderBookkeepingData () +void verify_leg_data (SpreaderLegData& l, OrderKey order_key, size_t leg_index, uint32_t sec_key, uint32_t exp_yr, uint32_t exp_m, uint32_t exp_day, const std::string& symbol, double leg_mult, double price_mult, api::Side side) { + REQUIRE(l.order_key() == order_key); + REQUIRE(l.leg_property_data.leg_index == leg_index); + REQUIRE(l.leg_property_data.security_key() == sec_key); + REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_year() == exp_yr); + REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_month() == exp_m); + REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_day() == exp_day); + REQUIRE(l.leg_property_data.leg_contract.security().symbol() == symbol); + REQUIRE(l.leg_property_data.size_multiplier() == leg_mult); + REQUIRE(l.leg_property_data.price_multiplier() == price_mult); + REQUIRE(l.leg_property_data.trading_side == side); } +// this is how the spreader organizes the data +TEST_CASE ("spreader bookkeeping data", "[spreader]") +{ + // constants + double leg_mult_1 = -1.25, leg_mult_2 = 3.33, leg_mult_3 = -1.75; + std::string symbol_1 = "ES", symbol_2 = "ZN", symbol_3 = "GC"; + uint32_t sec_key_1 = 1111, sec_key_2 = 2222, sec_key_3 = 3333; + double lean_ratio_1 = 1.33, lean_ratio_2 = 5.0, lean_ratio_3 = 2.75; + double price_mult_1 = -2.5, price_mult_2 = 5.0, price_mult_3 = -3.33; + Price spread_price (-0.35); + Quantity spread_quantity (125); + OrderKey order_key_1 (4), order_key_2 (22), order_key_3 (2007); + // leg property data + ContractLeg leg_1, leg_2, leg_3; + fill_future_contract_leg (leg_1, sec_key_1, symbol_1, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_SELL, 1.0); + fill_future_contract_leg (leg_2, sec_key_2, symbol_2, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_BUY, 1.0); + fill_future_contract_leg (leg_3, sec_key_3, symbol_3, bts::api::Venue::VENUE_GLBX, 2018, 6, 15, leg_mult_3, api::Side::SIDE_SELL, 1.0); + QuoteParams qp_1, qp_2, qp_3; + fill_quote_params(qp_1, lean_ratio_1); + fill_quote_params(qp_2, lean_ratio_2); + fill_quote_params(qp_3, lean_ratio_3); + LegSpreaderParams lsp_1, lsp_2, lsp_3; + fill_leg_spreader_params (lsp_1, price_mult_1, true, qp_1); + fill_leg_spreader_params (lsp_2, price_mult_2, true, qp_2); + fill_leg_spreader_params (lsp_3, price_mult_3, true, qp_3); + LegPropertyData leg_prop_data_1 (0, leg_1, lsp_1, api::Side::SIDE_SELL); + LegPropertyData leg_prop_data_2 (1, leg_2, lsp_2, api::Side::SIDE_BUY); + LegPropertyData leg_prop_data_3 (2, leg_3, lsp_3, api::Side::SIDE_SELL); + Book book1, book2, book3; + // SpreaderManager + std::vector legs; + legs.push_back (leg_prop_data_1);legs.push_back (leg_prop_data_2);legs.push_back (leg_prop_data_3); + std::vector books; + books.push_back (book1);books.push_back (book2);books.push_back (book3); + SpreaderManagerForTest spreader_manager (legs, books); + // quoter + SingleLegQuoter quoter_1 (spreader_manager, leg_prop_data_1, 3, spread_price , spread_quantity, bts::orderman::SpreaderOrderBase::ChildOrder()); + SingleLegQuoter quoter_2 (spreader_manager, leg_prop_data_2, 3, spread_price , spread_quantity, bts::orderman::SpreaderOrderBase::ChildOrder()); + SingleLegQuoter quoter_3 (spreader_manager, leg_prop_data_3, 3, spread_price , spread_quantity, bts::orderman::SpreaderOrderBase::ChildOrder()); + // bookkeeping data + std::vector leg_data_v; + leg_data_v.push_back (SpreaderLegData (leg_prop_data_1, quoter_1, order_key_1)); + leg_data_v.push_back (SpreaderLegData (leg_prop_data_2, quoter_2, order_key_2)); + leg_data_v.push_back (SpreaderLegData (leg_prop_data_3, quoter_3, order_key_3)); + SpreaderBookkeepingData bookkeeping_data (leg_data_v, spread_price, spread_quantity); + SECTION("verify security key access") + { + SpreaderLegData res_3 = bookkeeping_data.get_leg_data (SecurityKey (sec_key_3)); + verify_leg_data (res_3, order_key_3, 2, sec_key_3, 2018, 6, 15, symbol_3, leg_mult_3, price_mult_3, api::Side::SIDE_SELL); + SpreaderLegData res_2 = bookkeeping_data.get_leg_data (SecurityKey (sec_key_2)); + verify_leg_data (res_2, order_key_2, 1, sec_key_2, 2018, 3, 15, symbol_2, leg_mult_2, price_mult_2, api::Side::SIDE_BUY); + SpreaderLegData res_1 = bookkeeping_data.get_leg_data (SecurityKey (sec_key_1)); + verify_leg_data (res_1, order_key_1, 0, sec_key_1, 2017, 12, 15, symbol_1, leg_mult_1, price_mult_1, api::Side::SIDE_SELL); + } + SECTION("verify index access") + { + SpreaderLegData res_3 = bookkeeping_data.get_leg_data (2); + verify_leg_data (res_3, order_key_3, 2, sec_key_3, 2018, 6, 15, symbol_3, leg_mult_3, price_mult_3, api::Side::SIDE_SELL); + SpreaderLegData res_2 = bookkeeping_data.get_leg_data (1); + verify_leg_data (res_2, order_key_2, 1, sec_key_2, 2018, 3, 15, symbol_2, leg_mult_2, price_mult_2, api::Side::SIDE_BUY); + SpreaderLegData res_1 = bookkeeping_data.get_leg_data (0); + verify_leg_data (res_1, order_key_1, 0, sec_key_1, 2017, 12, 15, symbol_1, leg_mult_1, price_mult_1, api::Side::SIDE_SELL); + } + SECTION("verify order key access") + { + SpreaderLegData* res_3 = bookkeeping_data.find_leg_from_quoter_orderkey (order_key_3); + REQUIRE (res_3 != nullptr); + verify_leg_data (*res_3, order_key_3, 2, sec_key_3, 2018, 6, 15, symbol_3, leg_mult_3, price_mult_3, api::Side::SIDE_SELL); + SpreaderLegData* res_2 = bookkeeping_data.find_leg_from_quoter_orderkey (order_key_2); + REQUIRE (res_2 != nullptr); + verify_leg_data (*res_2, order_key_2, 1, sec_key_2, 2018, 3, 15, symbol_2, leg_mult_2, price_mult_2, api::Side::SIDE_BUY); + SpreaderLegData* res_1 = bookkeeping_data.find_leg_from_quoter_orderkey (order_key_1); + REQUIRE (res_1 != nullptr); + verify_leg_data (*res_1, order_key_1, 0, sec_key_1, 2017, 12, 15, symbol_1, leg_mult_1, price_mult_1, api::Side::SIDE_SELL); + } +} + + // quote price and quantity for a 2 legged spread void test_quote_price_quantity_2_legged() { diff --git a/src/server/sparkd/spreader/SpreaderBookkeepingData.h b/src/server/sparkd/spreader/SpreaderBookkeepingData.h index 7be548bdc..37c3b8f42 100644 --- a/src/server/sparkd/spreader/SpreaderBookkeepingData.h +++ b/src/server/sparkd/spreader/SpreaderBookkeepingData.h @@ -49,7 +49,6 @@ public: return _leg_data[index]; } size_t count() { return _leg_data.size(); } - SpreaderLegData* find_leg_from_quoter_orderkey(orderman::OrderKey order_key) { for (auto q_it = _leg_data.begin(); q_it != _leg_data.end(); q_it++) diff --git a/src/server/sparkd/spreader/SpreaderLegData.h b/src/server/sparkd/spreader/SpreaderLegData.h index af03354f4..7e75d3ecc 100644 --- a/src/server/sparkd/spreader/SpreaderLegData.h +++ b/src/server/sparkd/spreader/SpreaderLegData.h @@ -35,12 +35,9 @@ public: _book(), _order_update_message (SpreaderOrderUpdateMessage (order_key)), _hedger_data_vector() {} - + // accessors SingleLegQuoter& quoter() {return _quoter;} - const orderman::Book& book () const {return _book;} - // called for every book update. the values - // are copied by value - void set_book(const orderman::Book& book_arg) {_book = book_arg;} + const orderman::Book& book () const {return _book;} SpreaderOrderUpdateMessage& order_update_message () {return _order_update_message;} orderman::OrderKey order_key() {return _order_update_message.order_key;} // returns the number of active hedgers @@ -55,6 +52,9 @@ public: return &(*h_it); return nullptr; } + // mutators + // called for every book update (copied by value) + void set_book(const orderman::Book& book_arg) {_book = book_arg;} // adds a new hedger with the given values SpreaderHedgerData& add_new_hedger(ISpreaderManager& spreader_manager, bts::orderman::SpreaderOrderBase::ChildOrder order, From 4f1585f15fdc70728a582193d473de8ba7bd7fd9 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 22:14:36 -0500 Subject: [PATCH 05/12] made the public member vars private and exposed them through functions --- .../orderman/spreader_orders/SpreaderOrder.cpp | 4 ++-- .../SpreaderWorkingStateWorker.cpp | 8 ++++---- .../orderman/tests/spreader_order_tests.cpp | 4 ++-- src/server/sparkd/spreader/LegPropertyData.h | 15 ++++++++++----- src/server/sparkd/spreader/SingleLegQuoter.cpp | 8 ++++---- .../sparkd/spreader/SpreaderBookkeepingData.h | 2 +- src/server/sparkd/spreader/SpreaderUtils.h | 2 +- .../quoter_states/QuoterWorkingStateWorker.cpp | 2 +- 8 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp b/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp index 876edf297..487f677f3 100644 --- a/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp +++ b/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp @@ -248,7 +248,7 @@ void SpreaderOrder::_send_hedge(size_t filled_leg_index, Price filled_price, Qua _compute_hedge_price_quantity (filled_leg_index, filled_price, filled_quantity, hedge_leg_index, hedge_price, hedge_quantity); bts::orderman::SpreaderOrderBase::ChildOrder new_hedge_order = _get_new_order (leg_data.leg_property_data, false); - SpreaderHedgerData& hedger_data = leg_data.add_new_hedger (*this, new_hedge_order, hedge_price, hedge_quantity,leg_data.leg_property_data.trading_side); + SpreaderHedgerData& hedger_data = leg_data.add_new_hedger (*this, new_hedge_order, hedge_price, hedge_quantity,leg_data.leg_property_data.trading_side()); hedger_data.hedger().init (); } void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, @@ -287,7 +287,7 @@ bts::orderman::SpreaderOrderBase::ChildOrder SpreaderOrder::_get_new_order (cons bts::SecurityKey sec_key = bts::SecurityKey (leg_property_data.security_key()); api::Security const* db_sec = _db().resolve (sec_key); api::orderman::ChildOrderKind order_type = is_quote_order ? api::orderman::ChildOrderKind::COK_SPREADER_QUOTING : api::orderman::ChildOrderKind::COK_SPREADER_HEDGING; - bts::orderman::SpreaderOrderBase::ChildFactory child_factory = get_child_factory(*db_sec, leg_property_data.trading_side, order_type); + bts::orderman::SpreaderOrderBase::ChildFactory child_factory = get_child_factory(*db_sec, leg_property_data.trading_side(), order_type); return child_factory(); } /*******************************************************************************/ diff --git a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp index 716ed4cd1..4c025bb12 100644 --- a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp +++ b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp @@ -58,7 +58,7 @@ void SpreaderWorkingStateWorker::on_book(TickToTradeCorID cid, const Book& book) { SpreaderLegData& d = bookkeeping_data.get_leg_data(q_idx); if (d.leg_property_data.is_active_quoting()) - d.quoter().on_book(cid, md_leg.leg_property_data.leg_index, book); + d.quoter().on_book(cid, md_leg.leg_property_data.leg_index(), book); } // send md to hedgers hedging this security only for (size_t h_idx = 0; h_idx < md_leg.n_hedgers(); h_idx++) @@ -99,14 +99,14 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(bts::orderman::SpreaderLeg for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data.count(); q_idx++) { bts::orderman::SpreaderLegData other_leg_data = _spreader->_bookkeeping_data.get_leg_data (q_idx); - if (q_idx != leg_data.leg_property_data.leg_index) + if (q_idx != leg_data.leg_property_data.leg_index()) { // if it is an actively quoting leg, inform the quoter about it // so it can decrease the proportional size if (other_leg_data.leg_property_data.is_active_quoting()) - other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data.leg_index, filled_quantity); + other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data.leg_index(), filled_quantity); // a hedge is sent regardless - _spreader->_send_hedge (leg_data.leg_property_data.leg_index, average_price, filled_quantity, q_idx); + _spreader->_send_hedge (leg_data.leg_property_data.leg_index(), average_price, filled_quantity, q_idx); } } } diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index a43b15d32..6249e478c 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -483,7 +483,7 @@ TEST_CASE ("book traversal", "[spreader]") void verify_leg_data (SpreaderLegData& l, OrderKey order_key, size_t leg_index, uint32_t sec_key, uint32_t exp_yr, uint32_t exp_m, uint32_t exp_day, const std::string& symbol, double leg_mult, double price_mult, api::Side side) { REQUIRE(l.order_key() == order_key); - REQUIRE(l.leg_property_data.leg_index == leg_index); + REQUIRE(l.leg_property_data.leg_index() == leg_index); REQUIRE(l.leg_property_data.security_key() == sec_key); REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_year() == exp_yr); REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_month() == exp_m); @@ -491,7 +491,7 @@ void verify_leg_data (SpreaderLegData& l, OrderKey order_key, size_t leg_index, REQUIRE(l.leg_property_data.leg_contract.security().symbol() == symbol); REQUIRE(l.leg_property_data.size_multiplier() == leg_mult); REQUIRE(l.leg_property_data.price_multiplier() == price_mult); - REQUIRE(l.leg_property_data.trading_side == side); + REQUIRE(l.leg_property_data.trading_side() == side); } // this is how the spreader organizes the data TEST_CASE ("spreader bookkeeping data", "[spreader]") diff --git a/src/server/sparkd/spreader/LegPropertyData.h b/src/server/sparkd/spreader/LegPropertyData.h index 0f0ac53a8..842cb2f91 100644 --- a/src/server/sparkd/spreader/LegPropertyData.h +++ b/src/server/sparkd/spreader/LegPropertyData.h @@ -9,23 +9,28 @@ namespace orderman { // between the Spreader, SingleLegQuoter and SingleFillHedger class LegPropertyData { -public: - const size_t leg_index; +private: + const size_t _leg_index; +public: const bts::api::ContractLeg leg_contract; const bts::spark::LegSpreaderParams leg_params; - const api::Side trading_side; +private: + const api::Side _trading_side; +public: LegPropertyData(size_t leg_index_arg, const bts::api::ContractLeg& leg_contract_arg, const bts::spark::LegSpreaderParams& leg_params_arg, api::Side trading_side_arg) : // copying the values via copy cons - leg_index(leg_index_arg), + _leg_index(leg_index_arg), leg_contract(leg_contract_arg), leg_params(leg_params_arg), - trading_side(trading_side_arg) + _trading_side(trading_side_arg) { } + size_t leg_index() const {return _leg_index;} + api::Side trading_side() const {return _trading_side;} uint32_t security_key() const { return leg_contract.security().id(); } // TODO: use seckey //const bts::SecurityKey& security_key() const {return bts::SecurityKey (leg_contract.security().id());} const bts::api::Security& security() const { return leg_contract.security(); } diff --git a/src/server/sparkd/spreader/SingleLegQuoter.cpp b/src/server/sparkd/spreader/SingleLegQuoter.cpp index 286447f15..01440dc21 100644 --- a/src/server/sparkd/spreader/SingleLegQuoter.cpp +++ b/src/server/sparkd/spreader/SingleLegQuoter.cpp @@ -92,7 +92,7 @@ void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity) { if (_two_legged) { - size_t other_leg_index = _this_leg_data.leg_index == 0 ? 1 : 0; + size_t other_leg_index = _this_leg_data.leg_index() == 0 ? 1 : 0; SpreaderUtils::compute_price_quantity_2_legs (_price_quantity_state.target_spread_price(), _price_quantity_state.target_leg_quantity(), _this_leg_data, _spreader.get_leg_data(other_leg_index), _spreader.get_book(other_leg_index), price, quantity); @@ -104,7 +104,7 @@ void SingleLegQuoter::_compute_price_quantity_n_legs(Price& price, Quantity& qua size_t min_quoter_leg_quantity_index = 0; for (size_t idx = 0; idx < _n_spreader_legs; idx++) { - if (idx != _this_leg_data.leg_index) + if (idx != _this_leg_data.leg_index()) { const orderman::Book& other_leg_book = _spreader.get_book(idx); const LegPropertyData& other_leg_data = _spreader.get_leg_data(idx); @@ -112,7 +112,7 @@ void SingleLegQuoter::_compute_price_quantity_n_legs(Price& price, Quantity& qua Quantity this_leg_target_quantity = _price_quantity_state.remaining_to_fill(); double other_leg_total_lean_ratio = other_leg_data.leg_params.quote_params().lean_quantity_ratio() * other_leg_data.size_multiplier() / _this_leg_data.size_multiplier(); double other_leg_target_quantity = this_leg_target_quantity.value() * other_leg_total_lean_ratio; - SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side), Quantity(other_leg_target_quantity), other_leg_calc_price, other_leg_calc_quantity); + SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_calc_price, other_leg_calc_quantity); this_leg_calc_quantity = Quantity(static_cast(1.0 /*TODOit->lean_ratio*/ * other_leg_calc_quantity.value())); PQPair& leg_price_quantity = _aux_comp_data.at(idx); leg_price_quantity.set(other_leg_calc_price, other_leg_calc_quantity, this_leg_calc_quantity); @@ -128,7 +128,7 @@ Price SingleLegQuoter::_compute_leg_price_n_legs(const std::vector& leg_ double sum = 0; for (size_t idx = 0; idx < _n_spreader_legs; idx++) { - if (idx != _this_leg_data.leg_index) + if (idx != _this_leg_data.leg_index()) { const PQPair& leg_pq_pair = leg_values[idx]; const LegPropertyData& leg_data = _spreader.get_leg_data (idx); diff --git a/src/server/sparkd/spreader/SpreaderBookkeepingData.h b/src/server/sparkd/spreader/SpreaderBookkeepingData.h index 37c3b8f42..ea2af9f2f 100644 --- a/src/server/sparkd/spreader/SpreaderBookkeepingData.h +++ b/src/server/sparkd/spreader/SpreaderBookkeepingData.h @@ -30,7 +30,7 @@ public: _quantity (quantity) { for (auto it = leg_data.begin(); it != leg_data.end(); ++it) - _sec_key_to_index.insert(std::pair(it->leg_property_data.security_key(), it->leg_property_data.leg_index)); + _sec_key_to_index.insert(std::pair(it->leg_property_data.security_key(), it->leg_property_data.leg_index())); } SpreaderLegData& get_leg_data (SecurityKey security_key) { diff --git a/src/server/sparkd/spreader/SpreaderUtils.h b/src/server/sparkd/spreader/SpreaderUtils.h index b8a47f7cb..bf7899c4e 100644 --- a/src/server/sparkd/spreader/SpreaderUtils.h +++ b/src/server/sparkd/spreader/SpreaderUtils.h @@ -215,7 +215,7 @@ public: // this is the size ratio of the other leg vs this leg double size_ratio = abs(other_leg_data.size_multiplier() / this_leg_data.size_multiplier()); double other_leg_target_quantity = size_ratio * other_leg_data.leg_params.quote_params().lean_quantity_ratio() * this_leg_target_quantity.value(); - SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side), Quantity(other_leg_target_quantity), other_leg_price, other_leg_quantity); + SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_price, other_leg_quantity); quantity = Quantity (static_cast(other_leg_quantity.value())/size_ratio); price = Price((target_spread_price.value() - other_leg_price.value() * other_leg_data.price_multiplier())/this_leg_data.price_multiplier()); } diff --git a/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp b/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp index 3e69d904a..6fa0ca577 100644 --- a/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp +++ b/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp @@ -87,7 +87,7 @@ void QuoterWorkingStateWorker::_longpath_analyze_and_act (bts::orderman::TickToT { bool reprice = true; if (_quoter->_this_leg_data.leg_params.quote_params().has_quote_mute_zone()) - reprice = SpreaderUtils::reprice_quote_mute_zone(_quoter->_price_quantity_state.out_price().value(), new_price.value(), _quoter->_this_leg_data.trading_side, + reprice = SpreaderUtils::reprice_quote_mute_zone(_quoter->_price_quantity_state.out_price().value(), new_price.value(), _quoter->_this_leg_data.trading_side(), _quoter->_this_leg_data.tick_size(), _quoter->_this_leg_data.leg_params.quote_params().quote_mute_zone()); if (reprice) { From 06143332132bf64f927d07f320431940c3980743 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 22:28:44 -0500 Subject: [PATCH 06/12] In LegPropertyData, made the public member vars private and exposed them through functions --- src/server/sparkd/spreader/LegPropertyData.h | 11 ++++++----- src/server/sparkd/spreader/SingleLegQuoter.cpp | 2 +- src/server/sparkd/spreader/SpreaderUtils.h | 2 +- .../quoter/quoter_states/QuoterWorkingStateWorker.cpp | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/server/sparkd/spreader/LegPropertyData.h b/src/server/sparkd/spreader/LegPropertyData.h index 842cb2f91..ad79df8f1 100644 --- a/src/server/sparkd/spreader/LegPropertyData.h +++ b/src/server/sparkd/spreader/LegPropertyData.h @@ -12,9 +12,9 @@ class LegPropertyData private: const size_t _leg_index; public: - const bts::api::ContractLeg leg_contract; - const bts::spark::LegSpreaderParams leg_params; + const bts::api::ContractLeg leg_contract; private: + const bts::spark::LegSpreaderParams _leg_params; const api::Side _trading_side; public: LegPropertyData(size_t leg_index_arg, @@ -25,19 +25,20 @@ public: // copying the values via copy cons _leg_index(leg_index_arg), leg_contract(leg_contract_arg), - leg_params(leg_params_arg), + _leg_params(leg_params_arg), _trading_side(trading_side_arg) { } size_t leg_index() const {return _leg_index;} + const bts::spark::LegSpreaderParams& leg_params () const {return _leg_params;} api::Side trading_side() const {return _trading_side;} uint32_t security_key() const { return leg_contract.security().id(); } // TODO: use seckey //const bts::SecurityKey& security_key() const {return bts::SecurityKey (leg_contract.security().id());} const bts::api::Security& security() const { return leg_contract.security(); } - double price_multiplier() const { return leg_params.price_multiplier(); } + double price_multiplier() const { return _leg_params.price_multiplier(); } double size_multiplier() const { return leg_contract.quantity(); } double tick_size() const { return leg_contract.security().contract_spec().low_tick(); } - bool is_active_quoting() const {return leg_params.is_active_quoting();} + bool is_active_quoting() const {return _leg_params.is_active_quoting();} }; }} diff --git a/src/server/sparkd/spreader/SingleLegQuoter.cpp b/src/server/sparkd/spreader/SingleLegQuoter.cpp index 01440dc21..e756cd9f3 100644 --- a/src/server/sparkd/spreader/SingleLegQuoter.cpp +++ b/src/server/sparkd/spreader/SingleLegQuoter.cpp @@ -110,7 +110,7 @@ void SingleLegQuoter::_compute_price_quantity_n_legs(Price& price, Quantity& qua const LegPropertyData& other_leg_data = _spreader.get_leg_data(idx); Price other_leg_calc_price; Quantity other_leg_calc_quantity, this_leg_calc_quantity; Quantity this_leg_target_quantity = _price_quantity_state.remaining_to_fill(); - double other_leg_total_lean_ratio = other_leg_data.leg_params.quote_params().lean_quantity_ratio() * other_leg_data.size_multiplier() / _this_leg_data.size_multiplier(); + double other_leg_total_lean_ratio = other_leg_data.leg_params().quote_params().lean_quantity_ratio() * other_leg_data.size_multiplier() / _this_leg_data.size_multiplier(); double other_leg_target_quantity = this_leg_target_quantity.value() * other_leg_total_lean_ratio; SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_calc_price, other_leg_calc_quantity); this_leg_calc_quantity = Quantity(static_cast(1.0 /*TODOit->lean_ratio*/ * other_leg_calc_quantity.value())); diff --git a/src/server/sparkd/spreader/SpreaderUtils.h b/src/server/sparkd/spreader/SpreaderUtils.h index bf7899c4e..0cac65ca8 100644 --- a/src/server/sparkd/spreader/SpreaderUtils.h +++ b/src/server/sparkd/spreader/SpreaderUtils.h @@ -214,7 +214,7 @@ public: Price other_leg_price; Quantity other_leg_quantity; // this is the size ratio of the other leg vs this leg double size_ratio = abs(other_leg_data.size_multiplier() / this_leg_data.size_multiplier()); - double other_leg_target_quantity = size_ratio * other_leg_data.leg_params.quote_params().lean_quantity_ratio() * this_leg_target_quantity.value(); + double other_leg_target_quantity = size_ratio * other_leg_data.leg_params().quote_params().lean_quantity_ratio() * this_leg_target_quantity.value(); SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_price, other_leg_quantity); quantity = Quantity (static_cast(other_leg_quantity.value())/size_ratio); price = Price((target_spread_price.value() - other_leg_price.value() * other_leg_data.price_multiplier())/this_leg_data.price_multiplier()); diff --git a/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp b/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp index 6fa0ca577..17805642f 100644 --- a/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp +++ b/src/server/sparkd/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp @@ -86,9 +86,9 @@ void QuoterWorkingStateWorker::_longpath_analyze_and_act (bts::orderman::TickToT else { bool reprice = true; - if (_quoter->_this_leg_data.leg_params.quote_params().has_quote_mute_zone()) + if (_quoter->_this_leg_data.leg_params().quote_params().has_quote_mute_zone()) reprice = SpreaderUtils::reprice_quote_mute_zone(_quoter->_price_quantity_state.out_price().value(), new_price.value(), _quoter->_this_leg_data.trading_side(), - _quoter->_this_leg_data.tick_size(), _quoter->_this_leg_data.leg_params.quote_params().quote_mute_zone()); + _quoter->_this_leg_data.tick_size(), _quoter->_this_leg_data.leg_params().quote_params().quote_mute_zone()); if (reprice) { _quoter->_price_quantity_state.update_out_price_quantity(new_price, new_quantity); From 87e1d1bf37f0ee8f078a6c78b522c6a8c9202d22 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 15 Nov 2017 22:52:39 -0500 Subject: [PATCH 07/12] In LegPropertyData, made the public member vars private and exposed them through functions --- .../orderman/tests/spreader_order_tests.cpp | 8 ++++---- src/server/sparkd/spreader/LegPropertyData.h | 19 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 6249e478c..3e3c9ea70 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -485,10 +485,10 @@ void verify_leg_data (SpreaderLegData& l, OrderKey order_key, size_t leg_index, REQUIRE(l.order_key() == order_key); REQUIRE(l.leg_property_data.leg_index() == leg_index); REQUIRE(l.leg_property_data.security_key() == sec_key); - REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_year() == exp_yr); - REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_month() == exp_m); - REQUIRE(l.leg_property_data.leg_contract.security().future().expiration_day() == exp_day); - REQUIRE(l.leg_property_data.leg_contract.security().symbol() == symbol); + REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_year() == exp_yr); + REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_month() == exp_m); + REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_day() == exp_day); + REQUIRE(l.leg_property_data.leg_contract().security().symbol() == symbol); REQUIRE(l.leg_property_data.size_multiplier() == leg_mult); REQUIRE(l.leg_property_data.price_multiplier() == price_mult); REQUIRE(l.leg_property_data.trading_side() == side); diff --git a/src/server/sparkd/spreader/LegPropertyData.h b/src/server/sparkd/spreader/LegPropertyData.h index ad79df8f1..abda29e66 100644 --- a/src/server/sparkd/spreader/LegPropertyData.h +++ b/src/server/sparkd/spreader/LegPropertyData.h @@ -10,10 +10,8 @@ namespace orderman { class LegPropertyData { private: - const size_t _leg_index; -public: - const bts::api::ContractLeg leg_contract; -private: + const size_t _leg_index; + const bts::api::ContractLeg _leg_contract; const bts::spark::LegSpreaderParams _leg_params; const api::Side _trading_side; public: @@ -24,20 +22,21 @@ public: : // copying the values via copy cons _leg_index(leg_index_arg), - leg_contract(leg_contract_arg), + _leg_contract(leg_contract_arg), _leg_params(leg_params_arg), _trading_side(trading_side_arg) { } size_t leg_index() const {return _leg_index;} + const bts::api::ContractLeg& leg_contract() const {return _leg_contract;} const bts::spark::LegSpreaderParams& leg_params () const {return _leg_params;} api::Side trading_side() const {return _trading_side;} - uint32_t security_key() const { return leg_contract.security().id(); } // TODO: use seckey - //const bts::SecurityKey& security_key() const {return bts::SecurityKey (leg_contract.security().id());} - const bts::api::Security& security() const { return leg_contract.security(); } + uint32_t security_key() const { return _leg_contract.security().id(); } // TODO: use seckey + //const bts::SecurityKey& security_key() const {return bts::SecurityKey (_leg_contract.security().id());} + const bts::api::Security& security() const { return _leg_contract.security(); } double price_multiplier() const { return _leg_params.price_multiplier(); } - double size_multiplier() const { return leg_contract.quantity(); } - double tick_size() const { return leg_contract.security().contract_spec().low_tick(); } + double size_multiplier() const { return _leg_contract.quantity(); } + double tick_size() const { return _leg_contract.security().contract_spec().low_tick(); } bool is_active_quoting() const {return _leg_params.is_active_quoting();} }; From 387c225851a4d04c8be3a82846670d7d7fca54d7 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 16 Nov 2017 10:21:11 -0500 Subject: [PATCH 08/12] SpreaderLegData has SpreaderPorpertyData exposed as a cons ref& function now --- .../orderman/spreader_orders/SpreaderOrder.cpp | 18 +++++++++--------- .../SpreaderWaitingForFirstConnStateWorker.cpp | 4 ++-- .../SpreaderWorkingStateWorker.cpp | 16 ++++++++-------- .../orderman/tests/spreader_order_tests.cpp | 18 +++++++++--------- .../sparkd/spreader/SpreaderBookkeepingData.h | 4 ++-- src/server/sparkd/spreader/SpreaderLegData.h | 8 ++++---- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp b/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp index 487f677f3..92d03e929 100644 --- a/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp +++ b/src/server/sparkd/orderman/spreader_orders/SpreaderOrder.cpp @@ -132,7 +132,7 @@ const orderman::Book& SpreaderOrder::get_book(size_t leg_index) const } const LegPropertyData& SpreaderOrder::get_leg_data(size_t leg_index) const { - return _bookkeeping_data.get_leg_data(leg_index).leg_property_data; + return _bookkeeping_data.get_leg_data(leg_index).leg_property_data(); } void SpreaderOrder::_transform_to_START() { @@ -171,7 +171,7 @@ void SpreaderOrder::_cancel_orders(bool cancel_quotes, bool cancel_hedges) SpreaderLegData& leg_data = _bookkeeping_data.get_leg_data(q_idx); if (cancel_quotes) { - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) leg_data.quoter().cancel_order(); } if (cancel_hedges) @@ -187,7 +187,7 @@ bool SpreaderOrder::_all_quoter_orders_in_terminal_state() for (size_t q_idx = 0; q_idx < _bookkeeping_data.count(); q_idx++) { SpreaderLegData& leg_data = _bookkeeping_data.get_leg_data(q_idx); - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) if (!leg_data.quoter().is_terminal_state()) return false; } @@ -233,7 +233,7 @@ bool SpreaderOrder::_quoters_can_accept_modification () for (size_t q_idx = 0; q_idx < _bookkeeping_data.count(); q_idx++) { SpreaderLegData& leg_data = _bookkeeping_data.get_leg_data(q_idx); - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) if (!leg_data.quoter().accept_modifications()) return false; } @@ -247,9 +247,9 @@ void SpreaderOrder::_send_hedge(size_t filled_leg_index, Price filled_price, Qua SpreaderLegData& leg_data = _bookkeeping_data.get_leg_data (hedge_leg_index); _compute_hedge_price_quantity (filled_leg_index, filled_price, filled_quantity, hedge_leg_index, hedge_price, hedge_quantity); - bts::orderman::SpreaderOrderBase::ChildOrder new_hedge_order = _get_new_order (leg_data.leg_property_data, false); - SpreaderHedgerData& hedger_data = leg_data.add_new_hedger (*this, new_hedge_order, hedge_price, hedge_quantity,leg_data.leg_property_data.trading_side()); - hedger_data.hedger().init (); + bts::orderman::SpreaderOrderBase::ChildOrder new_hedge_order = _get_new_order (leg_data.leg_property_data(), false); + SpreaderHedgerData& hedger_data = leg_data.add_new_hedger (*this, new_hedge_order, hedge_price, hedge_quantity,leg_data.leg_property_data().trading_side()); + hedger_data.hedger().init (); } void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, Price fill_price, Quantity fill_quantity, @@ -260,8 +260,8 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index, if (_bookkeeping_data.count() == 2) { SpreaderUtils::compute_hedge_price_size_2_legs (_bookkeeping_data.price(), - _bookkeeping_data.get_leg_data (filled_leg_index).leg_property_data, fill_price, fill_quantity, - _bookkeeping_data.get_leg_data (hedge_leg_index).leg_property_data, hedge_price, hedge_quantity); + _bookkeeping_data.get_leg_data (filled_leg_index).leg_property_data(), fill_price, fill_quantity, + _bookkeeping_data.get_leg_data (hedge_leg_index).leg_property_data(), hedge_price, hedge_quantity); } else { diff --git a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp index a30405b94..9ce273ecc 100644 --- a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp +++ b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp @@ -90,7 +90,7 @@ void SpreaderWaitingForFirstConnStateWorker::_init_quoters () for (size_t index = 0; index < _spreader->_bookkeeping_data.count(); index++) { SpreaderLegData& leg_data = _spreader->_bookkeeping_data.get_leg_data(index); - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) leg_data.quoter().init(); } } @@ -126,7 +126,7 @@ void SpreaderWaitingForFirstConnStateWorker::_subscribe_to_marketdata() for (size_t index = 0; index < _spreader->_bookkeeping_data.count(); index++) { SpreaderLegData& leg_data = _spreader->_bookkeeping_data.get_leg_data(index); - bts::SecurityKey leg_key = bts::SecurityKey (leg_data.leg_property_data.security_key()); + bts::SecurityKey leg_key = bts::SecurityKey (leg_data.leg_property_data().security_key()); api::Security const* db_sec = _spreader->_db().resolve (leg_key); _spreader->subscribe_to_md (*db_sec); } diff --git a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp index 4c025bb12..57b0d6219 100644 --- a/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp +++ b/src/server/sparkd/orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp @@ -36,7 +36,7 @@ void SpreaderWorkingStateWorker::on_update(const api::orderman::UpdateSpreaderOr for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data.count(); q_idx++) { SpreaderLegData& leg_data = _spreader->_bookkeeping_data.get_leg_data(q_idx); - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) leg_data.quoter().update_order_price_quantity (price, quantity); } } @@ -57,8 +57,8 @@ void SpreaderWorkingStateWorker::on_book(TickToTradeCorID cid, const Book& book) for (size_t q_idx = 0; q_idx < bookkeeping_data.count(); q_idx++) { SpreaderLegData& d = bookkeeping_data.get_leg_data(q_idx); - if (d.leg_property_data.is_active_quoting()) - d.quoter().on_book(cid, md_leg.leg_property_data.leg_index(), book); + if (d.leg_property_data().is_active_quoting()) + d.quoter().on_book(cid, md_leg.leg_property_data().leg_index(), book); } // send md to hedgers hedging this security only for (size_t h_idx = 0; h_idx < md_leg.n_hedgers(); h_idx++) @@ -70,7 +70,7 @@ void SpreaderWorkingStateWorker::on_time(const TimeStamp& ts) for (size_t q_idx = 0; q_idx < bookkeeping_data.count(); q_idx++) { SpreaderLegData& leg_data = bookkeeping_data.get_leg_data(q_idx); - if (leg_data.leg_property_data.is_active_quoting()) + if (leg_data.leg_property_data().is_active_quoting()) { leg_data.quoter().on_time(ts); for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++) @@ -99,14 +99,14 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(bts::orderman::SpreaderLeg for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data.count(); q_idx++) { bts::orderman::SpreaderLegData other_leg_data = _spreader->_bookkeeping_data.get_leg_data (q_idx); - if (q_idx != leg_data.leg_property_data.leg_index()) + if (q_idx != leg_data.leg_property_data().leg_index()) { // if it is an actively quoting leg, inform the quoter about it // so it can decrease the proportional size - if (other_leg_data.leg_property_data.is_active_quoting()) - other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data.leg_index(), filled_quantity); + if (other_leg_data.leg_property_data().is_active_quoting()) + other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data().leg_index(), filled_quantity); // a hedge is sent regardless - _spreader->_send_hedge (leg_data.leg_property_data.leg_index(), average_price, filled_quantity, q_idx); + _spreader->_send_hedge (leg_data.leg_property_data().leg_index(), average_price, filled_quantity, q_idx); } } } diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 3e3c9ea70..919ae2dc7 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -483,15 +483,15 @@ TEST_CASE ("book traversal", "[spreader]") void verify_leg_data (SpreaderLegData& l, OrderKey order_key, size_t leg_index, uint32_t sec_key, uint32_t exp_yr, uint32_t exp_m, uint32_t exp_day, const std::string& symbol, double leg_mult, double price_mult, api::Side side) { REQUIRE(l.order_key() == order_key); - REQUIRE(l.leg_property_data.leg_index() == leg_index); - REQUIRE(l.leg_property_data.security_key() == sec_key); - REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_year() == exp_yr); - REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_month() == exp_m); - REQUIRE(l.leg_property_data.leg_contract().security().future().expiration_day() == exp_day); - REQUIRE(l.leg_property_data.leg_contract().security().symbol() == symbol); - REQUIRE(l.leg_property_data.size_multiplier() == leg_mult); - REQUIRE(l.leg_property_data.price_multiplier() == price_mult); - REQUIRE(l.leg_property_data.trading_side() == side); + REQUIRE(l.leg_property_data().leg_index() == leg_index); + REQUIRE(l.leg_property_data().security_key() == sec_key); + REQUIRE(l.leg_property_data().leg_contract().security().future().expiration_year() == exp_yr); + REQUIRE(l.leg_property_data().leg_contract().security().future().expiration_month() == exp_m); + REQUIRE(l.leg_property_data().leg_contract().security().future().expiration_day() == exp_day); + REQUIRE(l.leg_property_data().leg_contract().security().symbol() == symbol); + REQUIRE(l.leg_property_data().size_multiplier() == leg_mult); + REQUIRE(l.leg_property_data().price_multiplier() == price_mult); + REQUIRE(l.leg_property_data().trading_side() == side); } // this is how the spreader organizes the data TEST_CASE ("spreader bookkeeping data", "[spreader]") diff --git a/src/server/sparkd/spreader/SpreaderBookkeepingData.h b/src/server/sparkd/spreader/SpreaderBookkeepingData.h index ea2af9f2f..d9cfb2c07 100644 --- a/src/server/sparkd/spreader/SpreaderBookkeepingData.h +++ b/src/server/sparkd/spreader/SpreaderBookkeepingData.h @@ -30,7 +30,7 @@ public: _quantity (quantity) { for (auto it = leg_data.begin(); it != leg_data.end(); ++it) - _sec_key_to_index.insert(std::pair(it->leg_property_data.security_key(), it->leg_property_data.leg_index())); + _sec_key_to_index.insert(std::pair(it->leg_property_data().security_key(), it->leg_property_data().leg_index())); } SpreaderLegData& get_leg_data (SecurityKey security_key) { @@ -52,7 +52,7 @@ public: SpreaderLegData* find_leg_from_quoter_orderkey(orderman::OrderKey order_key) { for (auto q_it = _leg_data.begin(); q_it != _leg_data.end(); q_it++) - if (q_it->leg_property_data.is_active_quoting()) + if (q_it->leg_property_data().is_active_quoting()) if (q_it->order_key() == order_key) return &(*q_it); return nullptr; diff --git a/src/server/sparkd/spreader/SpreaderLegData.h b/src/server/sparkd/spreader/SpreaderLegData.h index 7e75d3ecc..2dd4285bf 100644 --- a/src/server/sparkd/spreader/SpreaderLegData.h +++ b/src/server/sparkd/spreader/SpreaderLegData.h @@ -18,9 +18,8 @@ namespace orderman{ // their state class SpreaderLegData { -public: - const LegPropertyData leg_property_data; private: + const LegPropertyData _leg_property_data; SingleLegQuoter _quoter; orderman::Book _book; SpreaderOrderUpdateMessage _order_update_message; @@ -30,12 +29,13 @@ public: SingleLegQuoter quoter_arg, orderman::OrderKey order_key) : - leg_property_data(leg_property_data_arg), + _leg_property_data(leg_property_data_arg), _quoter (quoter_arg), _book(), _order_update_message (SpreaderOrderUpdateMessage (order_key)), _hedger_data_vector() {} // accessors + const LegPropertyData& leg_property_data() const {return _leg_property_data;} SingleLegQuoter& quoter() {return _quoter;} const orderman::Book& book () const {return _book;} SpreaderOrderUpdateMessage& order_update_message () {return _order_update_message;} @@ -63,7 +63,7 @@ public: api::Side hedge_side) { SpreaderOrderUpdateMessage hedger_order_update (order.order_key()); - SingleFillHedger hedger(spreader_manager, leg_property_data, order, hedge_price, hedge_quantity, hedge_side); + SingleFillHedger hedger(spreader_manager, _leg_property_data, order, hedge_price, hedge_quantity, hedge_side); SpreaderHedgerData hedger_data(hedger, hedger_order_update); _hedger_data_vector.push_back(hedger_data); return _hedger_data_vector.back(); From 32be7accadda741316b3231bc4a87882a3ee68f3 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Thu, 16 Nov 2017 16:02:32 -0600 Subject: [PATCH 09/12] From Eric - Bus now uses ReplaySubject --- src/client/spark/core/bus/SingletonBus.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/core/bus/SingletonBus.cs b/src/client/spark/core/bus/SingletonBus.cs index fb21dd220..880bacee9 100644 --- a/src/client/spark/core/bus/SingletonBus.cs +++ b/src/client/spark/core/bus/SingletonBus.cs @@ -20,7 +20,7 @@ namespace bts.core if (inst.TryGetValue(tag, out ISubject result)) { return result; } - result = new Subject(); + result = new ReplaySubject(1); inst[tag] = result; return result; } From 7acbacfbf9aad5ca698500d43bb188d00ea0c655 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Fri, 17 Nov 2017 09:35:37 -0600 Subject: [PATCH 10/12] adding a parameterless ToString for Security --- src/client/spark_api/security/Security.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client/spark_api/security/Security.cs b/src/client/spark_api/security/Security.cs index 04c517a40..b77ca2c9b 100644 --- a/src/client/spark_api/security/Security.cs +++ b/src/client/spark_api/security/Security.cs @@ -32,6 +32,11 @@ namespace bts.api { public int CompareTo(SecurityImmutable other) { return Id.CompareTo(other.Id); } + + public override string ToString() + { + return this.ToString(""); + } public string ToString(string format, IFormatProvider formatProvider=null) { return Format(this, string.IsNullOrEmpty(format) ? _standard_format : Parse(format)); } From 954bb5939262b69b78e526b2af7252a3c93c9b40 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Fri, 17 Nov 2017 14:19:54 -0600 Subject: [PATCH 11/12] ContractPicker - the start of something grand --- .../ContractPicker/ContractPickerModel.cs | 97 +++++++++++++++++++ .../ContractPicker/ContractPickerView.xaml | 29 ++++++ .../ContractPicker/ContractPickerView.xaml.cs | 96 ++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 src/client/spark/mspark/ContractPicker/ContractPickerModel.cs create mode 100644 src/client/spark/mspark/ContractPicker/ContractPickerView.xaml create mode 100644 src/client/spark/mspark/ContractPicker/ContractPickerView.xaml.cs diff --git a/src/client/spark/mspark/ContractPicker/ContractPickerModel.cs b/src/client/spark/mspark/ContractPicker/ContractPickerModel.cs new file mode 100644 index 000000000..da6b9f32d --- /dev/null +++ b/src/client/spark/mspark/ContractPicker/ContractPickerModel.cs @@ -0,0 +1,97 @@ +using bts.api; +using bts.spark; +using mbts.co2; +using mbts.collections; +using mbts.ui.shell; +using mbts.ui.utility; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; +using System.Reactive.Subjects; +using static mbts.spark.SparkTags; +using mbts.collections.extensions; +using System.Collections.ObjectModel; + +namespace mbts.spark { + internal class ContractPickerModel : ViewModelBase { + + + public ContractPickerModel(IBus bus) { + _bus = bus; + _subs.Add(_bus.subscribe(SecuritiesTag).Subscribe(on_securities_changed)); + } + + private readonly IBus _bus; + private IList _subs = new List(); + + + internal void exchange_selection_changed(IEnumerable exchanges) + { + selected_exchanges = exchanges.ToList(); + symbols.sync_with(UserSymbol.get_all() + .Where(s => exchanges.Contains(s.exchange)) + .Select(s => s.symbol) + .Distinct() + .ToList()); + _update_securities(); + } + + internal void security_type_selection_changed(IEnumerable security_types) + { + selected_security_types = security_types.ToList(); + _update_securities(); + } + + internal void symbol_selection_changed(IEnumerable symbols) + { + selected_symbols = symbols.ToList(); + _update_securities(); + } + + private void on_securities_changed(IReadOnlyDictionary changed_securities) + { + all_securities = changed_securities; + securities.sync_with(changed_securities + .Select(entry => entry.Value.security) + ); + _update_securities(); + } + + public List all_exchanges { get; } = new List { Venue.Glbx, Venue.Cboe };//UserSymbol.get_all().Select(x => x.exchange).Distinct().ToList(); + public List selected_exchanges = new List(); + + public IEnumerable security_types { get; } = EnumHelper.get_values_except(SecurityType.Option, SecurityType.Unset); + public List selected_security_types = new List(); + + public ObservableCollection symbols { get; } = new ObservableCollection(UserSymbol.get_all().Select(s => s.symbol).Distinct()); + public List selected_symbols = new List(); + + private IReadOnlyDictionary all_securities = new Dictionary(); + public ObservableCollection securities { get; } = new ObservableCollection(); + + private void _update_securities() + { + securities.sync_with(all_securities + .Select(all => all.Value.security) + // exchange filtering + .Where(sec => selected_exchanges.Count == 0 || selected_exchanges + .Any(exchange => exchange == sec.Exchange)) + // type filtering + .Where(sec => selected_security_types.Count == 0 || selected_security_types + .Any(sec_type => sec_type == sec.security_type())) + // symbol filtering + .Where(sec => selected_symbols.Count == 0 || selected_symbols + .Any(sym => sym == sec.Symbol)) + ); + + } + + public void Dispose() + { + Disposable.dispose(_subs); + } + + + } +} \ No newline at end of file diff --git a/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml b/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml new file mode 100644 index 000000000..341c68abc --- /dev/null +++ b/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + diff --git a/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml.cs b/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml.cs new file mode 100644 index 000000000..1ce0d3d51 --- /dev/null +++ b/src/client/spark/mspark/ContractPicker/ContractPickerView.xaml.cs @@ -0,0 +1,96 @@ +using mbts.ui; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.IO; +using bts.api; +using System.ComponentModel.Composition; + +namespace mbts.spark +{ + /// + /// Interaction logic for ContractPickerView.xaml + /// + partial class ContractPickerView : UserControl + { + + public ContractPickerView(ContractPickerModel context) + { + InitializeComponent(); + this.DataContext = context; + + _exchanges.SelectionChanged += exchanges_changed; + _security_types.SelectionChanged += security_types_changed; + _symbols.SelectionChanged += symbols_changed; + } + + private void symbols_changed(object sender, SelectionChangedEventArgs e) + { + ((ContractPickerModel)DataContext).symbol_selection_changed(_symbols.SelectedItems.Cast()); + } + + private void security_types_changed(object sender, SelectionChangedEventArgs e) + { + ((ContractPickerModel)DataContext).security_type_selection_changed(_security_types.SelectedItems.Cast()); + } + + private void exchanges_changed(object sender, SelectionChangedEventArgs e) + { + ((ContractPickerModel)DataContext).exchange_selection_changed(_exchanges.SelectedItems.Cast()); + } + + } + + //[Export (typeof(IShellModule))] + public class TempModule : IShellModule { + private IBus _bus; + + [ImportingConstructor] + public TempModule(IBus bus) { + _bus = bus; + + } + + public ImageSource icon => Icons.Abacus; + + public string name => "test"; + + public int priority => 1; + + public IShellWindow launch_ui => null; + + public Task create(Stream load_stream) { + return null; + } + + public void Dispose() { + //throw new NotImplementedException(); + } + + public void get_state(Stream save_here) { + //throw new NotImplementedException(); + } + + public void load_state(Stream state = null) { + //throw new NotImplementedException(); + } + + public void start() { + var x = new Window { + Content = new ContractPickerView(new ContractPickerModel(_bus)) + }; + x.Show(); + } + } +} From 27fe42e90627e5deda319ff1cc9afcfacf29180b Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 17 Nov 2017 16:58:34 -0500 Subject: [PATCH 12/12] pricing tests with fractional price mults --- .../orderman/tests/spreader_order_tests.cpp | 180 ++++++++++++++++-- src/server/sparkd/spreader/SpreaderUtils.h | 58 +++--- 2 files changed, 199 insertions(+), 39 deletions(-) diff --git a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp index 919ae2dc7..2df444105 100644 --- a/src/server/sparkd/orderman/tests/spreader_order_tests.cpp +++ b/src/server/sparkd/orderman/tests/spreader_order_tests.cpp @@ -220,7 +220,8 @@ void fill_quote_params (bts::spark::QuoteParams& qp, double lean_ratio) qp.set_lean_quantity_ratio (lean_ratio); } void fill_future_security (Security& sec, uint32_t key, const std::string& symbol, Venue exchange, - uint32_t expiration_year, uint32_t expiration_month, uint32_t expiration_day) + uint32_t expiration_year, uint32_t expiration_month, uint32_t expiration_day, + double tick_size) { sec.set_id (key); sec.set_symbol (symbol); @@ -230,6 +231,10 @@ void fill_future_security (Security& sec, uint32_t key, const std::string& symbo fc.set_expiration_month(expiration_month); fc.set_expiration_day(expiration_day); sec.mutable_future()->MergeFrom (fc); + ContractSpecification c_spec; + c_spec.set_low_tick (tick_size); + c_spec.set_high_tick (tick_size); + sec.mutable_contract_spec()->MergeFrom (c_spec); } void fill_contract_leg (ContractLeg& leg, const Security& sec, double leg_quantity, Side side, double leg_delta) { @@ -241,10 +246,11 @@ void fill_contract_leg (ContractLeg& leg, const Security& sec, double leg_quanti void fill_future_contract_leg (ContractLeg& leg, uint32_t key, const std::string& symbol, Venue exchange, uint32_t year, uint32_t month, uint32_t day, - double leg_quantity, Side side, double leg_delta) + double leg_quantity, Side side, double leg_delta, + double tick_size) { Security sec; - fill_future_security (sec, key, symbol, exchange, year, month, day); + fill_future_security (sec, key, symbol, exchange, year, month, day, tick_size); fill_contract_leg (leg, sec, leg_quantity, side, leg_delta); } void fill_synthetic_contract (SyntheticContract& sc, const std::string& name, const std::vector& legs) @@ -284,6 +290,18 @@ void fill_book(Book& book, double* prices, uint16_t* sizes, bts::api::Side side) for (size_t i = 0; i < book.n_levels(); i++) bookside[i] = md2::PriceSize::from_double<2>(prices[i], sizes[i]); } +void fill_book(Book& book, double* bid_prices, uint16_t* bid_sizes, + double* ask_prices, uint16_t* ask_sizes) +{ + BookSide& bidside = book.side(api::Side::SIDE_BUY); + BookSide& askside = book.side(api::Side::SIDE_SELL); + for (size_t i = 0; i < book.n_levels(); i++) + { + bidside[i] = md2::PriceSize::from_double<2>(bid_prices[i], bid_sizes[i]); + askside[i] = md2::PriceSize::from_double<2>(ask_prices[i], ask_sizes[i]); + } +} +void fill_book_temp (std::vector){} /**********************************************************************/ /**************************misc helpers********************************/ /**********************************************************************/ @@ -381,7 +399,7 @@ TEST_CASE ("test_leg_quantity_normalization", "[spreader]") uint32_t spr_qty = gen_rand(0, 100); std::string symbol = "ES"; ContractLeg leg; - fill_future_contract_leg (leg, 0, symbol, bts::api::Venue::VENUE_GLBX, 2018, 12, 15, leg_mult, api::Side::SIDE_BUY, 1.0); + fill_future_contract_leg (leg, 0, symbol, bts::api::Venue::VENUE_GLBX, 2018, 12, 15, leg_mult, api::Side::SIDE_BUY, 1.0, 0.25); QuoteParams qp; fill_quote_params(qp, 1.0); LegSpreaderParams lsp; @@ -398,8 +416,8 @@ TEST_CASE ("test_leg_quantity_normalization", "[spreader]") uint32_t leg_qty = gen_rand(0, 100); std::string symbol = "ES"; ContractLeg leg_1, leg_2; - fill_future_contract_leg (leg_1, 0, symbol, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_BUY, 1.0); - fill_future_contract_leg (leg_2, 1, symbol, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_SELL, 1.0); + fill_future_contract_leg (leg_1, 0, symbol, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_BUY, 1.0, 0.25); + fill_future_contract_leg (leg_2, 1, symbol, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_SELL, 1.0, 0.25); QuoteParams qp_1, qp_2; fill_quote_params(qp_1, 1.0); fill_quote_params(qp_2, 1.0); @@ -507,9 +525,9 @@ TEST_CASE ("spreader bookkeeping data", "[spreader]") OrderKey order_key_1 (4), order_key_2 (22), order_key_3 (2007); // leg property data ContractLeg leg_1, leg_2, leg_3; - fill_future_contract_leg (leg_1, sec_key_1, symbol_1, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_SELL, 1.0); - fill_future_contract_leg (leg_2, sec_key_2, symbol_2, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_BUY, 1.0); - fill_future_contract_leg (leg_3, sec_key_3, symbol_3, bts::api::Venue::VENUE_GLBX, 2018, 6, 15, leg_mult_3, api::Side::SIDE_SELL, 1.0); + fill_future_contract_leg (leg_1, sec_key_1, symbol_1, bts::api::Venue::VENUE_GLBX, 2017, 12, 15, leg_mult_1, api::Side::SIDE_SELL, 1.0, 0.25); + fill_future_contract_leg (leg_2, sec_key_2, symbol_2, bts::api::Venue::VENUE_GLBX, 2018, 3, 15, leg_mult_2, api::Side::SIDE_BUY, 1.0, 0.03125); + fill_future_contract_leg (leg_3, sec_key_3, symbol_3, bts::api::Venue::VENUE_GLBX, 2018, 6, 15, leg_mult_3, api::Side::SIDE_SELL, 1.0, 0.05); QuoteParams qp_1, qp_2, qp_3; fill_quote_params(qp_1, lean_ratio_1); fill_quote_params(qp_2, lean_ratio_2); @@ -570,10 +588,148 @@ TEST_CASE ("spreader bookkeeping data", "[spreader]") } } - -// quote price and quantity for a 2 legged spread -void test_quote_price_quantity_2_legged() +LegPropertyData create_leg_property_data (const ContractLeg& leg, + size_t leg_index, double price_multiplier, bool is_active_quoting, double lean_mult) { + QuoteParams qp; + fill_quote_params(qp, lean_mult); + LegSpreaderParams lsp; + fill_leg_spreader_params (lsp, price_multiplier, is_active_quoting, qp); + return LegPropertyData (leg_index, leg, lsp, leg.side()); +} +// quote price and quantity for a 2 legged spread +TEST_CASE ("compute quoting price quantity 2 legs", "[spreader]") +{ + Price spreader_price (1.0); Quantity spreader_quantity (10); + Price out_price; Quantity out_quantity; + // simple buy 1 sell 1 spreader (same ratio) + ContractLeg leg_1, leg_2; + SECTION("basic ") + { + fill_future_contract_leg (leg_1, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 12, 15, 1, api::Side::SIDE_BUY, 1.0, 0.25); + fill_future_contract_leg (leg_2, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 3, 15, -1, api::Side::SIDE_SELL, 1.0, 0.25); + LegPropertyData leg_prop_data_1 = create_leg_property_data (leg_1, 0, 1.0, true, 2.0); + LegPropertyData leg_prop_data_2 = create_leg_property_data (leg_2, 1, -1.0, true, 2.0); + // md + Book book_1, book_2; + double bid_prices_1[] = {2500.0, 2499.75, 2499.5, 2499, 2498}; + uint16_t bid_sizes_1[] = {11, 25, 45, 6, 99}; + double ask_prices_1[] = {2500.25, 2500.50, 2500.75, 2501, 2502}; + uint16_t ask_sizes_1[] = {13, 12, 45, 5, 15}; + + double bid_prices_2[] = {2505.0, 2504.75, 2504, 2503.00, 2502.75}; + uint16_t bid_sizes_2[] = {1, 4, 5, 20, 20}; + double ask_prices_2[] = {2505.25, 2506.50, 2507, 2507.5, 2508}; + uint16_t ask_sizes_2[] = {10, 4, 6, 20, 15}; + fill_book (book_1, bid_prices_1, bid_sizes_1, ask_prices_1, ask_sizes_1); + fill_book (book_2, bid_prices_2, bid_sizes_2, ask_prices_2, ask_sizes_2); + + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2504.00); + REQUIRE (out_quantity.value() == 10); + } + SECTION("lean mult") + { + spreader_quantity = Quantity(5); + spreader_price = Price (-0.5); + double lean_mult = 1.0; + fill_future_contract_leg (leg_1, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 12, 15, 1, api::Side::SIDE_BUY, 1.0, 0.25); + fill_future_contract_leg (leg_2, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 3, 15, -1, api::Side::SIDE_SELL, 1.0, 0.25); + LegPropertyData leg_prop_data_1 = create_leg_property_data (leg_1, 0, 1.0, true, 1.0); + LegPropertyData leg_prop_data_2 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + // md + Book book_2; + double bid_prices_2[] = {2505.0, 2504.75, 2504, 2503.00, 2502.75}; uint16_t bid_sizes_2[] = {5, 10, 5, 10, 20}; + double ask_prices_2[] = {2505.25, 2506.50, 2507, 2507.5, 2508}; uint16_t ask_sizes_2[] = {0, 0, 0, 0, 0}; + fill_book (book_2, bid_prices_2, bid_sizes_2, ask_prices_2, ask_sizes_2); + + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2504.5); + REQUIRE (out_quantity.value() == 5); + // changing lean mult + lean_mult = 2.0; + LegPropertyData leg_prop_data_2_lm_2 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_2, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2504.25); + REQUIRE (out_quantity.value() == 5); + + lean_mult = 5.0; + LegPropertyData leg_prop_data_2_lm_5 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_5, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2502.5); + REQUIRE (out_quantity.value() == 5); + + lean_mult = 9.0; + LegPropertyData leg_prop_data_2_lm_9 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_9, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2502.25); + REQUIRE (out_quantity.value() == 5); + + lean_mult = 10.0; + LegPropertyData leg_prop_data_2_lm_10 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_10, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2502.25); + REQUIRE (out_quantity.value() == 5); + // with a large lean mult, make the leg 2 qty req exhaust the book + lean_mult = 20.0; + LegPropertyData leg_prop_data_2_lm_20 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_20, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2502.25); + REQUIRE (out_quantity.value() == 2); + // fractional leg mults + lean_mult = 1.5; + LegPropertyData leg_prop_data_2_lm_1_5 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_1_5, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2504.25); + REQUIRE (out_quantity.value() == 5); + + lean_mult = 4.3333; + LegPropertyData leg_prop_data_2_lm_3_33 = create_leg_property_data (leg_2, 1, -1.0, true, lean_mult); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, spreader_quantity, leg_prop_data_1, leg_prop_data_2_lm_3_33, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 2502.5); + REQUIRE (out_quantity.value() == 5); + } + SECTION("fractional leg mult ") + { + spreader_quantity = Quantity(10); + spreader_price = Price (0.5); + double mult_1 = 0.25, mult_2 = -1.1; // same used for price and qty + double lean_mult = 1.0; + fill_future_contract_leg (leg_1, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 12, 15, mult_1, api::Side::SIDE_BUY, 1.0, 0.25); + fill_future_contract_leg (leg_2, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 3, 15, mult_2, api::Side::SIDE_SELL, 1.0, 0.25); + LegPropertyData leg_prop_data_1 = create_leg_property_data (leg_1, 0, mult_1, true, lean_mult); + LegPropertyData leg_prop_data_2 = create_leg_property_data (leg_2, 1, mult_2, true, lean_mult); + // md + Book book_1, book_2; + double bid_prices_2[] = {2500.0, 2499.75, 2499.5, 2499, 2498};uint16_t bid_sizes_2[] = {5, 4, 1, 6, 99}; + double ask_prices_2[] = {2500.25, 2500.50, 2500.75, 2501, 2502};uint16_t ask_sizes_2[] = {13, 12, 45, 5, 15}; + + fill_book (book_2, bid_prices_2, bid_sizes_2, ask_prices_2, ask_sizes_2); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, Quantity(3), leg_prop_data_1, leg_prop_data_2, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 10997.5); + REQUIRE (out_quantity.value() == 3); + } + SECTION("price mult") + { + spreader_quantity = Quantity(10); + spreader_price = Price (0.5); + double size_mult_1 = 0.25, size_mult_2 = -1.1, // same used for price and qty + price_mult_1 = 0.35, price_mult_2 = -1.5; + double lean_mult = 1.0; + fill_future_contract_leg (leg_1, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 12, 15, size_mult_1, api::Side::SIDE_BUY, 1.0, 0.25); + fill_future_contract_leg (leg_2, 0, "ES", bts::api::Venue::VENUE_GLBX, 2017, 3, 15, size_mult_2, api::Side::SIDE_SELL, 1.0, 0.25); + LegPropertyData leg_prop_data_1 = create_leg_property_data (leg_1, 0, price_mult_1, true, lean_mult); + LegPropertyData leg_prop_data_2 = create_leg_property_data (leg_2, 1, price_mult_2, true, lean_mult); + // md + Book book_1, book_2; + double bid_prices_2[] = {2500.0, 2499.75, 2499.5, 2499, 2498};uint16_t bid_sizes_2[] = {5, 4, 1, 6, 99}; + double ask_prices_2[] = {2500.25, 2500.50, 2500.75, 2501, 2502};uint16_t ask_sizes_2[] = {13, 12, 45, 5, 15}; + + fill_book (book_2, bid_prices_2, bid_sizes_2, ask_prices_2, ask_sizes_2); + SpreaderUtils::compute_price_quantity_2_legs (spreader_price, Quantity(3), leg_prop_data_1, leg_prop_data_2, book_2, out_price, out_quantity); + REQUIRE (out_price.value() == 10711.25); + REQUIRE (out_quantity.value() == 3); + } } // hedge price and quantity for a 2 legged spread void test_hedge_price_quantity_2_legged() diff --git a/src/server/sparkd/spreader/SpreaderUtils.h b/src/server/sparkd/spreader/SpreaderUtils.h index 0cac65ca8..0deb2e363 100644 --- a/src/server/sparkd/spreader/SpreaderUtils.h +++ b/src/server/sparkd/spreader/SpreaderUtils.h @@ -3,6 +3,7 @@ #include #include "lib/config/protos/MarketData.pb.h" #include "lib/md/Structure/PriceSize.h" +#include "lib/domain_model/Security.h" #include "server/sparkd/orderman/md/Book.h" #include "server/sparkd/spreader/LegPropertyData.h" #include "server/sparkd/spreader/QuoteLegPriceQuantityState.h" @@ -78,28 +79,6 @@ public: } bts_assert(false); } - // finds the size of the target leg based on the size of the source leg - // (to keep their relative ratios intact) - // ex: for a spread s1.l1+s2.l2, size N in l2 (so l2 is the source) - // corresponds to s1 * N / s2 size on l1 (target). - // so if we get an N lot fill in l2, we send a hedge on l1 - // sized s1 * N / s2 - static double get_equivelant_size (const LegPropertyData& source_leg, Quantity source_leg_quantity, - const LegPropertyData& target_leg) - { - double rv = std::abs(target_leg.size_multiplier () * source_leg_quantity.value () / source_leg.size_multiplier ()); - rv = std::round(rv); - return rv; - } - // given the quantity of the spreader order, determines the quality - // of the leg - static double get_leg_quantity_from_spreader_quantity(const LegPropertyData& leg, Quantity spreader_order_quantity) - { - double rv = std::abs (leg.size_multiplier () * spreader_order_quantity.value()); - rv = std::round (rv); - return rv; - } - /**********************************************************************/ /*******************************Orders*********************************/ /**********************************************************************/ @@ -200,8 +179,29 @@ public: return false; } /**********************************************************************/ - /**********************Quoting Pricing Calculation*********************/ + /**************************Price/Size Calculation**********************/ /**********************************************************************/ + // finds the size of the target leg based on the size of the source leg + // (to keep their relative ratios intact) + // ex: for a spread s1.l1+s2.l2, size N in l2 (so l2 is the source) + // corresponds to s1 * N / s2 size on l1 (target). + // so if we get an N lot fill in l2, we send a hedge on l1 + // sized s1 * N / s2 + static double get_equivelant_size (const LegPropertyData& source_leg, Quantity source_leg_quantity, + const LegPropertyData& target_leg) + { + double rv = std::abs(target_leg.size_multiplier () * source_leg_quantity.value () / source_leg.size_multiplier ()); + rv = std::round(rv); + return rv; + } + // given the quantity of the spreader order, determines the quality + // of the leg + static double get_leg_quantity_from_spreader_quantity(const LegPropertyData& leg, Quantity spreader_order_quantity) + { + double rv = std::abs (leg.size_multiplier () * spreader_order_quantity.value()); + rv = std::round (rv); + return rv; + } // calculates the computed price and quantity for a quoting // leg given the data for the other leg static void compute_price_quantity_2_legs(Price target_spread_price, Quantity this_leg_target_quantity, @@ -213,11 +213,15 @@ public: // from traversing the other book, for the requires size Price other_leg_price; Quantity other_leg_quantity; // this is the size ratio of the other leg vs this leg - double size_ratio = abs(other_leg_data.size_multiplier() / this_leg_data.size_multiplier()); - double other_leg_target_quantity = size_ratio * other_leg_data.leg_params().quote_params().lean_quantity_ratio() * this_leg_target_quantity.value(); + double effective_mult = other_leg_data.leg_params().quote_params().lean_quantity_ratio() * abs(other_leg_data.size_multiplier() / this_leg_data.size_multiplier()); + // to be conservative round the required size up + double other_leg_target_quantity = std::round(effective_mult * this_leg_target_quantity.value()); SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_price, other_leg_quantity); - quantity = Quantity (static_cast(other_leg_quantity.value())/size_ratio); - price = Price((target_spread_price.value() - other_leg_price.value() * other_leg_data.price_multiplier())/this_leg_data.price_multiplier()); + double out_quantity = std::round(other_leg_quantity.value() / effective_mult); + quantity = Quantity (out_quantity); + double out_price = (target_spread_price.value() - other_leg_price.value() * other_leg_data.price_multiplier())/this_leg_data.price_multiplier(); + out_price = bts::securities::raw_round_to_quote_price (out_price, this_leg_data.tick_size(), this_leg_data.trading_side()); + price = Price(out_price); } static void compute_hedge_price_size_2_legs (Price spread_price, const LegPropertyData& quote_filled_leg, Price quote_fill_price, Quantity quote_fill_quantity,