From ad757495bd2f38698d9bd98171e9ffef79fa9004 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 25 May 2018 18:26:23 -0400 Subject: [PATCH] comments --- .../spreader_orders/spreader/SpreaderUtils.h | 222 +++++++++--------- 1 file changed, 113 insertions(+), 109 deletions(-) diff --git a/src/server/sparkd/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/server/sparkd/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index 9c94f5112..d07af8777 100644 --- a/src/server/sparkd/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/server/sparkd/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -27,9 +27,9 @@ namespace orderman { class SpreaderUtils { public: - //////////////////////////////////////////// - // misc //////////////////////////////////// - //////////////////////////////////////////// + //////////////////////////////////////////////////// + // misc //////////////////////////////////////////// + //////////////////////////////////////////////////// // based on the side of the spreader order, // returns the side of the leg api::Side static get_leg_side(api::Side spreader_order_side, @@ -66,9 +66,9 @@ public: const api::orderman::OrderDefinition& order_def, std::vector& out_resolved, bool resolve_synthetic = true); - //////////////////////////////////////////// - // orders ////////////////////////////////// - //////////////////////////////////////////// + //////////////////////////////////////////////////// + // orders ////////////////////////////////////////// + //////////////////////////////////////////////////// // checks for the 3 states that are designated error states static bool order_in_error_state(bts::api::orderman::OrderStatus order_status){ return order_status == api::orderman::OrderStatus::ORDER_STATUS_ERROR || @@ -97,9 +97,9 @@ public: static bool order_in_cancelled_state (bts::api::orderman::OrderStatus order_status){ return order_status == api::orderman::OrderStatus::ORDER_STATUS_CANCELED; } - //////////////////////////////////////////// - // market data ///////////////////////////// - //////////////////////////////////////////// + //////////////////////////////////////////////////// + // market data ///////////////////////////////////// + //////////////////////////////////////////////////// // CRITICAL PATH (quoting) // this traverses the depth of the book, accumulating the quantity, // till the target quantity is reached. @@ -167,9 +167,9 @@ public: price_qty = Quantity(0); cumm_qty = Quantity(0); return false; } - //////////////////////////////////////////// - // quoting params ////////////////////////// - //////////////////////////////////////////// + //////////////////////////////////////////////////// + // quoting params ////////////////////////////////// + //////////////////////////////////////////////////// // given the new price, applies the quoting parameters (mute etc) // to see if quote needs to be repriced static bool reprice_quote_mute_zone(double current_price, @@ -199,9 +199,9 @@ public: else return false; } - //////////////////////////////////////////// - // pre-hedge params //////////////////////// - //////////////////////////////////////////// + //////////////////////////////////////////////////// + // pre-hedge params //////////////////////////////// + //////////////////////////////////////////////////// // (unused and untested) this function caps the hedge price // to only be a user defined interval away from the opposite // inside market @@ -292,40 +292,39 @@ public: return out_price; return out_price; } - /**********************************************************************/ - /****************************Post Hedge Params*************************/ - /**********************************************************************/ - static bool post_hedge_time_based_price_action (bts::TimeStamp order_time, bts::TimeStamp now_time, - const LegPropertyData& leg_property_data, const bts::orderman::Book& book, double& out_new_price) - { + //////////////////////////////////////////////////// + // post-hedge params /////////////////////////////// + //////////////////////////////////////////////////// + static bool post_hedge_time_based_price_action (bts::TimeStamp order_time, + bts::TimeStamp now_time, + const LegPropertyData& leg_property_data, + const bts::orderman::Book& book, + double& out_new_price){ const bts::spark::PostHedgeParams& post_hedge_params = leg_property_data.leg_params().post_hedge_params(); if (post_hedge_params.has_time_based_forced_cross()) return post_hedge_time_based_forced_cross (order_time, now_time, leg_property_data.trading_side(), book, leg_property_data.tick_size(), post_hedge_params.time_based_forced_cross(), out_new_price); return false; } - static bool post_hedge_time_based_forced_cross (bts::TimeStamp order_time, bts::TimeStamp now_time, - api::Side hedge_side, const bts::orderman::Book& book, double tick_size, + static bool post_hedge_time_based_forced_cross (bts::TimeStamp order_time, + bts::TimeStamp now_time, + api::Side hedge_side, + const bts::orderman::Book& book, + double tick_size, const bts::spark::PostHedgeTimeBasedForcedCross& time_based_forced_cross, - double& out_new_price) - { + double& out_new_price){ int64_t time_diff = ms_diff (now_time, order_time); - if (time_diff > time_based_forced_cross.time_in_ms_threshold()) - { + if (time_diff > time_based_forced_cross.time_in_ms_threshold()){ LOG(OrderMan, Info, "SpreaderUtils::post_hedge_time_based_forced_cross order_time: {} now_time: {} ms_time_diff: {} threshold: {}", order_time, now_time, time_diff, time_based_forced_cross.time_in_ms_threshold()); - if (hedge_side == api::SIDE_BUY) - { + if (hedge_side == api::SIDE_BUY){ const md::PriceSize& opp_inside_price_size = book.side(api::Side::SIDE_SELL)[0]; - if (opp_inside_price_size.size() > 0) - { + if (opp_inside_price_size.size() > 0){ out_new_price = opp_inside_price_size.price() + tick_size * time_based_forced_cross.market_cross_ticks(); return true; } } - else - { + else{ const md::PriceSize& opp_inside_price_size = book.side(api::Side::SIDE_BUY)[0]; - if (opp_inside_price_size.size() > 0) - { + if (opp_inside_price_size.size() > 0){ out_new_price = opp_inside_price_size.price() - tick_size * time_based_forced_cross.market_cross_ticks(); return true; } @@ -333,53 +332,57 @@ public: } return false; } - // all market based post hedging (there is only now) are + // all market based post hedging (there are only 2) are // in this function - static bool post_hedge_market_based_price_action (double curr_order_price, uint32_t, uint32_t remaining_to_fill_qty, - const LegPropertyData& leg_property_data, const bts::orderman::Book& book, - double& out_new_price) - { + static bool post_hedge_market_based_price_action (double curr_order_price, + uint32_t, uint32_t remaining_to_fill_qty, + const LegPropertyData& leg_property_data, + const bts::orderman::Book& book, + double& out_new_price){ const bts::spark::PostHedgeParams& post_hedge_params = leg_property_data.leg_params().post_hedge_params(); if (post_hedge_params.has_opposite_market_volume_based_forced_cross()){ - if (post_hedge_volume_based_forced_cross(curr_order_price, remaining_to_fill_qty, leg_property_data.trading_side(), - book, leg_property_data.tick_size(), + if (post_hedge_volume_based_forced_cross(curr_order_price, + remaining_to_fill_qty, + leg_property_data.trading_side(), + book, + leg_property_data.tick_size(), post_hedge_params.opposite_market_volume_based_forced_cross(), out_new_price)) return true; } if (post_hedge_params.has_price_based_cross()){ - if(post_hedge_price_based_forced_cross (curr_order_price, leg_property_data.trading_side(), book, leg_property_data.tick_size(), post_hedge_params.price_based_cross(), out_new_price)) + if(post_hedge_price_based_forced_cross (curr_order_price, + leg_property_data.trading_side(), + book, + leg_property_data.tick_size(), + post_hedge_params.price_based_cross(), + out_new_price)) return true; } return false; } - static bool post_hedge_price_based_forced_cross (double curr_order_price, api::Side hedge_side, const bts::orderman::Book& book, double tick_size, + static bool post_hedge_price_based_forced_cross (double curr_order_price, + api::Side hedge_side, + const bts::orderman::Book& book, + double tick_size, const bts::spark::PostHedgePriceBasedForcedCross& price_based_forced_cross, - double& out_new_price) - { + double& out_new_price){ uint32_t opp_inside_market_dist_threshold_ticks = price_based_forced_cross.ticks_threshold(); - if (hedge_side == api::SIDE_BUY) - { + if (hedge_side == api::SIDE_BUY){ const md::PriceSize& opp_inside_price_size = book.side(api::Side::SIDE_SELL)[0]; - - if (opp_inside_price_size.size() > 0) - { + if (opp_inside_price_size.size() > 0){ uint32_t dist = bts::securities::price_diff_to_ticks (curr_order_price, opp_inside_price_size.price(), tick_size); - if (dist > opp_inside_market_dist_threshold_ticks) - { + if (dist > opp_inside_market_dist_threshold_ticks){ out_new_price = opp_inside_price_size.price() + tick_size * price_based_forced_cross.market_cross_ticks(); return true; } } } - else - { + else{ const md::PriceSize& opp_inside_price_size = book.side(api::Side::SIDE_BUY)[0]; - if (opp_inside_price_size.size() > 0) - { + if (opp_inside_price_size.size() > 0){ uint32_t dist = bts::securities::price_diff_to_ticks (opp_inside_price_size.price(), curr_order_price, tick_size); - if (dist > opp_inside_market_dist_threshold_ticks) - { + if (dist > opp_inside_market_dist_threshold_ticks){ out_new_price = opp_inside_price_size.price() - tick_size * price_based_forced_cross.market_cross_ticks(); return true; } @@ -387,7 +390,12 @@ public: } return false; } - static bool post_hedge_volume_based_forced_cross (double out_price, uint32_t remaining_to_fill_qty, api::Side hedge_side, + // if the opposite inside market qty is greater than the + // threshold, this function moves the working hedge order + // n_ticks into the opposite inside market + static bool post_hedge_volume_based_forced_cross (double out_price, + uint32_t remaining_to_fill_qty, + api::Side hedge_side, const bts::orderman::Book& book, double tick_size, const bts::spark::PostHedgeOppositeMarketVolumeBasedForcedCross& volume_based_forced_cross, @@ -411,9 +419,9 @@ public: } return false; } - /**********************************************************************/ - /**************************Price/Size 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) @@ -421,24 +429,26 @@ public: // so if we get an N lot fill in l2, we send a hedge on l1 // sized s1 * N / s2 ////////////////////////general functions - static Quantity get_leg_quantity_from_another_leg_quantity (const LegPropertyData& source_leg, Quantity source_leg_quantity, - const LegPropertyData& target_leg) - { + static Quantity get_leg_quantity_from_another_leg_quantity (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 ()); return Quantity(_round_leg_qty_computed_from_another_leg_qty(rv)); } // given the quantity of the spreader order, determines the quantity // of the leg - static Quantity get_leg_quantity_from_spreader_quantity(const LegPropertyData& leg, Quantity spreader_order_quantity) - { + static Quantity get_leg_quantity_from_spreader_quantity(const LegPropertyData& leg, + Quantity spreader_order_quantity){ double rv = std::abs (leg.size_multiplier () * spreader_order_quantity.value()); return Quantity (_round_leg_qty_computed_from_spreader_qty (rv)); } // finds the average weighted price and total quantity // from 2 prices and qty's pair - static void compute_average_price (Price base_price, Quantity base_qty, Price inc_price, Quantity inc_qty, - Price& out_avg_price, Quantity& out_total_qty) - { + static void compute_average_price (Price base_price, + Quantity base_qty, + Price inc_price, + Quantity inc_qty, + Price& out_avg_price, Quantity& out_total_qty){ out_total_qty = Quantity(base_qty.value() + inc_qty.value()); if (out_total_qty.value() == 0) out_avg_price = Price(0); @@ -448,11 +458,12 @@ public: ////////////////////////2 legged spreads // 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, + static void compute_price_quantity_2_legs(Price target_spread_price, + Quantity this_leg_target_quantity, const LegPropertyData& this_leg_data, - const LegPropertyData& other_leg_data, const bts::orderman::Book& other_leg_book, - Price& price, Quantity& quantity) - { + const LegPropertyData& other_leg_data, + const bts::orderman::Book& other_leg_book, + Price& price, Quantity& quantity){ // other_leg_price and other_leg_quantity are the price and quantity calculated // from traversing the other book, for the requires size Price other_leg_price; Quantity other_leg_quantity; @@ -474,9 +485,12 @@ public: 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, - const LegPropertyData& hedge_leg, Price& out_hedge_price, Quantity& out_hedge_quantity) - { + const LegPropertyData& quote_filled_leg, + Price quote_fill_price, + Quantity quote_fill_quantity, + const LegPropertyData& hedge_leg, + Price& out_hedge_price, + Quantity& out_hedge_quantity){ // for ratio spreads hedge sizes are simply rounded off (the fraction can be a setting) out_hedge_quantity = get_leg_quantity_from_another_leg_quantity (quote_filled_leg, quote_fill_quantity, hedge_leg); double out_hedge_price_d = (spread_price.value() - quote_fill_price.value() * quote_filled_leg.price_multiplier())/hedge_leg.price_multiplier(); @@ -490,14 +504,12 @@ public: const ISpreaderManager& spreader, std::vector& driver_vector, Price& out_price, - Quantity& out_quantity) - { + Quantity& out_quantity){ compute_drivers (quoting_leg_target_quantity, quoting_leg_index, spreader, driver_vector); out_quantity = compute_quoting_quantity_from_drivers (quoting_leg_index, driver_vector); if (out_quantity.value() == 0) out_price = Price (0); - else - { + else{ compute_drivers (out_quantity, quoting_leg_index, spreader, driver_vector); out_price = compute_quoting_price_from_drivers (target_spread_price, quoting_leg_index, spreader, driver_vector); } @@ -508,13 +520,11 @@ public: static void compute_drivers(Quantity quoting_leg_target_quantity, size_t quoting_leg_index, const ISpreaderManager& spreader, - std::vector& out_driver_vector) - { + std::vector& out_driver_vector){ const LegPropertyData& quoting_leg_data = spreader.get_leg_data (quoting_leg_index); size_t n_legs = out_driver_vector.size(); for (size_t idx = 0; idx < n_legs; idx++) - if (idx != quoting_leg_index) - { + if (idx != quoting_leg_index){ const bts::orderman::Book& driver_leg_book = spreader.get_book(idx); const LegPropertyData& driver_leg_data = spreader.get_leg_data(idx); double driver_effective_mult = std::abs(driver_leg_data.leg_params().quote_params().lean_quantity_ratio() * driver_leg_data.size_multiplier() / quoting_leg_data.size_multiplier()); @@ -530,8 +540,7 @@ public: static Price compute_quoting_price_from_drivers(Price target_spread_price, size_t quoting_leg_index, const ISpreaderManager& spreader, - const std::vector& driver_vector) - { + const std::vector& driver_vector){ const LegPropertyData& quoting_leg_data = spreader.get_leg_data (quoting_leg_index); size_t n_legs = driver_vector.size(); double sum = 0; @@ -546,8 +555,7 @@ public: // what each hedging leg can support. this function // simply finds that minimum static Quantity compute_quoting_quantity_from_drivers (size_t quoting_leg_index, - const std::vector& driver_vector) - { + const std::vector& driver_vector){ uint32_t rv = std::numeric_limits::max(); for (size_t idx = 0; idx < driver_vector.size(); idx++) if (idx != quoting_leg_index) @@ -555,25 +563,25 @@ public: rv = driver_vector[idx].quoter_leg_quantity.value(); return Quantity(rv); } - static void compute_hedge_price_size_n_legs (const LegPropertyData& quote_filled_leg, Price, Quantity quote_fill_quantity, - const LegPropertyData& hedge_leg, const QuoterLegDriverData& leg_driver_data, - Price& out_hedge_price, Quantity& out_hedge_quantity) - { + static void compute_hedge_price_size_n_legs (const LegPropertyData& quote_filled_leg, + Price, + Quantity quote_fill_quantity, + const LegPropertyData& hedge_leg, + const QuoterLegDriverData& leg_driver_data, + Price& out_hedge_price, Quantity& out_hedge_quantity){ out_hedge_price = leg_driver_data.driver_price; out_hedge_quantity = get_leg_quantity_from_another_leg_quantity (quote_filled_leg, quote_fill_quantity, hedge_leg); } - /**********************************************************************/ - /**************************rounding helpers****************************/ - /**********************************************************************/ - static uint32_t _round_quantity(double qty, bool is_up) - { + //////////////////////////////////////////////////// + // Rounding functions ////////////////////////////// + //////////////////////////////////////////////////// + static uint32_t _round_quantity(double qty, bool is_up){ double round_err = 0.000000001; double round_qty = std::round(qty); // if its merely a representation error if (std::abs(round_qty - qty) <= round_err) qty = round_qty; - else - { + else{ if (is_up) qty = std::ceil(qty); else @@ -581,22 +589,18 @@ public: } return static_cast(qty); } - static uint32_t _round_hedge_target(double hedge_qty) - { + static uint32_t _round_hedge_target(double hedge_qty){ // hedge target is always rounded up, to be more // conservative when looking for the hedge qty return _round_quantity(hedge_qty, true); } - static uint32_t _round_quoting_qty(double quote_qty) - { + static uint32_t _round_quoting_qty(double quote_qty){ return _round_quantity(quote_qty, false); } - static uint32_t _round_leg_qty_computed_from_spreader_qty (double qty) - { + static uint32_t _round_leg_qty_computed_from_spreader_qty (double qty){ return static_cast(std::round(qty)); } - static uint32_t _round_leg_qty_computed_from_another_leg_qty (double qty) - { + static uint32_t _round_leg_qty_computed_from_another_leg_qty (double qty){ return static_cast(std::round(qty)); } /**********************************************************************/