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,