diff --git a/src/lib/domain_model/Security.h b/src/lib/domain_model/Security.h index 9fa8ffd6f..95559a207 100644 --- a/src/lib/domain_model/Security.h +++ b/src/lib/domain_model/Security.h @@ -95,4 +95,7 @@ inline bool test_price_equality (double price_1, double price_2, double tick_siz { return std::fabs(price_2 - price_1) < 0.01 * tick_size; } +inline bool gr_than_eq(double p1, double p2, double tick_size){ + return test_price_equality(p1,p2,tick_size) || p1>=p2; +} }} diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp b/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp index 685fe035a..1b36be36a 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp @@ -125,44 +125,64 @@ void SingleLegQuoter::on_time (const bts::TimeStamp& ts){ } bool SingleLegQuoter::is_active_order() const{ QuoterState state = _worker->state(); - return state == QuoterState::WAITING_FOR_FIRST_ACK || state == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED || - state == QuoterState::WORKING || state == QuoterState::WAITING_FOR_CANCEL_ACK; + return state == QuoterState::WAITING_FOR_FIRST_ACK || + state == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED || + state == QuoterState::WORKING || + state == QuoterState::WAITING_FOR_CANCEL_ACK; } bool SingleLegQuoter::is_terminal_state () const { return _worker->state() == QuoterState::END; } bool SingleLegQuoter::accept_modifications() const { - return _worker->state() == QuoterState::WAITING_FOR_SIZE || _worker->state() == QuoterState::WORKING; + return _worker->state() == QuoterState::WAITING_FOR_SIZE || + _worker->state() == QuoterState::WORKING; } void SingleLegQuoter::_incorporate_other_leg_fill (size_t, Quantity hedge_reduction){ _price_quantity_state.add_hedged_reduction (hedge_reduction); } -void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity){ +void SingleLegQuoter::_compute_price_quantity(Price& price, + Quantity& quantity){ Quantity remaining_quantity = _price_quantity_state.remaining_to_fill(); if (_two_legged){ 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(), remaining_quantity, - _this_leg_data, _spreader.get_leg_data(other_leg_index), _spreader.get_book(other_leg_index), + SpreaderUtils::compute_price_quantity_2_legs (_price_quantity_state.target_spread_price(), + remaining_quantity, + _this_leg_data, + _spreader.get_leg_data(other_leg_index), + _spreader.get_book(other_leg_index), price, quantity); bts_assert (quantity.value() <= remaining_quantity.value()); } else - SpreaderUtils::compute_quoting_price_size_n_legs (_price_quantity_state.target_spread_price(), remaining_quantity, - _this_leg_data.leg_index(), _spreader, _drivers_temp_usage, price, quantity); + SpreaderUtils::compute_quoting_price_size_n_legs (_price_quantity_state.target_spread_price(), + remaining_quantity, + _this_leg_data.leg_index(), + _spreader, + _drivers_temp_usage, + price, + quantity); +} +void SingleLegQuoter::_adjust_for_hidden_order(Price& price, + Quantity& quantity){ + const Book& book=_spreader.get_book(_this_leg_data.leg_index()); + SpreaderUtils::adjust_for_hidden_order(_this_leg_data, + book, + price,quantity); } void SingleLegQuoter::_commit_drivers(Price quote_price){ if (!_two_legged) _driver_cache.add(_drivers_temp_usage,quote_price); } -/*********************************************************************************/ -/*******************************state transformations*****************************/ -/*********************************************************************************/ +/*****************************************************************************/ +/*******************************state transformations*************************/ +/*****************************************************************************/ void SingleLegQuoter::_transform_to_START (){ bts_assert (_worker == nullptr); _switch_state (&_start_state_worker); } void SingleLegQuoter::_transform_to_WAITING_FOR_SIZE (){ - bts_assert (_worker->state() == QuoterState::START || _worker->state() == QuoterState::END); + bts_assert (_worker->state() == QuoterState::START || + _worker->state() == QuoterState::END); _switch_state (&_waiting_for_size_state_worker); } void SingleLegQuoter::_transform_to_WAITING_FOR_FIRST_ACK (){ @@ -178,7 +198,8 @@ void SingleLegQuoter::_transform_to_WORKING(){ _switch_state (&_working_state_worker); } void SingleLegQuoter::_transform_to_WAITING_FOR_CANCEL_ACK(){ - bts_assert (_worker->state() == QuoterState::WORKING || _worker->state() == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED); + bts_assert (_worker->state() == QuoterState::WORKING || + _worker->state() == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED); _switch_state (&_waiting_for_cancel_ack_state_worker); } void SingleLegQuoter::_transform_to_END(){ @@ -187,7 +208,8 @@ void SingleLegQuoter::_transform_to_END(){ } void SingleLegQuoter::_switch_state (IQuoterStateWorker* new_state){ std::string curr_state = _worker == nullptr ? "NULL" : _worker->state_text(); - LOG(OrderMan, Info, "SingleLegQuoter::_switch_state {} {} -> {} ", _log_prefix(), curr_state, new_state->state_text()); + LOG(OrderMan, Info, "SingleLegQuoter::_switch_state {} {} -> {} ", + _log_prefix(), curr_state, new_state->state_text()); _worker = new_state; _worker->on_transition(); } diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.h b/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.h index 30b32e096..08849b8f4 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.h @@ -68,7 +68,8 @@ public: SpreaderChildOrderState& child_order_state); public: SingleLegQuoter() = delete; - SingleLegQuoter(SingleLegQuoter&&); // implementation has an assert since it will not be called + // implementation has an assert since it will not be called + SingleLegQuoter(SingleLegQuoter&&); SingleLegQuoter(const SingleLegQuoter&) = delete; SingleLegQuoter& operator=(const SingleLegQuoter&) = delete; SingleLegQuoter& operator=(SingleLegQuoter&&) = delete; @@ -87,7 +88,9 @@ public: void update_order_status(const SpreaderChildOrderState& u); //c. called when the market data associated // with this security is changed - void on_book(bts::orderman::TickToTradeCorID cid, size_t leg_index, const bts::orderman::Book&); + void on_book(bts::orderman::TickToTradeCorID cid, + size_t leg_index, + const bts::orderman::Book&); //d. called when the order is cancelled, either // manually or automatically void cancel_order(); @@ -118,25 +121,29 @@ public: } // tells if it is expecting to get a cancel (this helps catch // unsolicited cancels). returns true is its expecting a cancel - bool expecting_cancel_update() const {return _worker->state() == QuoterState::WAITING_FOR_CANCEL_ACK;} + bool expecting_cancel_update() const { + return _worker->state() == QuoterState::WAITING_FOR_CANCEL_ACK; + } public: // test only functions QuoterState test_only_func_quoter_state() const { return _worker->state (); } - const QuoteLegPriceQuantityState& test_only_func_price_quantity_state () const {return _price_quantity_state;} - bool test_only_func_ended_by_spreader () const {return _is_permenantly_cancelled;} + const QuoteLegPriceQuantityState& test_only_func_price_quantity_state () const { + return _price_quantity_state; + } + bool test_only_func_ended_by_spreader () const { + return _is_permenantly_cancelled; + } private: bts::orderman::OrderKey _key() {return _child_order_state.order_key();} TimeStamp _current_time () const {return _spreader.current_time();} // TODO: state out and move the string to a const std::string _log_prefix(){ std::ostringstream oss; - oss << " spreader_order_key: " << _spreader.spreader_order_key(); - oss << " sec_key: " << _this_leg_data.security_key(); - oss << " leg_idx: " << _this_leg_data.leg_index(); - //oss << " order:key: " << (_worker == nullptr || _worker->state() == QuoterState::START) ? "NULL" : _worker->state_text(); - oss << " state: " << (_worker == nullptr ? "NULL" : _worker->state_text()); + oss << " spreader_order_key:" << _spreader.spreader_order_key(); + oss << " sec_key:" << _this_leg_data.security_key(); + oss << " leg_idx:" << _this_leg_data.leg_index(); + oss << " state:" << (_worker == nullptr ? "NULL" : _worker->state_text()); return oss.str(); - return ""; } void _update_target(Price price, Quantity quantity){ _price_quantity_state.update_target_price_quantity(price, quantity); @@ -156,7 +163,12 @@ private: // this is the function that computes the quoting price and the // quantity for this leg. void _compute_price_quantity(Price& price, Quantity& quantity); - // copies the computed driver to committed dri + // this takes in a price and qty that already has + // computed values and applies the hidden order logic + // to them (ie if the price results in a cross, and adjusts + // the qty value) + void _adjust_for_hidden_order(Price& price, Quantity& quantity); + // copies the computed driver to committed drivers void _commit_drivers (Price quote_price); void _transform_to_START (); void _transform_to_WAITING_FOR_SIZE (); diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index 599ba7c64..ad5d68f5d 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -146,7 +146,8 @@ public: api::Side book_side, Price target_price, double tick_size, - Quantity& price_qty, Quantity& cumm_qty){ + Quantity& price_qty, + Quantity& cumm_qty){ price_qty = Quantity(0); cumm_qty = Quantity(0); const size_t n_levels = book.n_levels(); const bts::orderman::BookSide& book_depth = book.side(book_side); @@ -155,7 +156,9 @@ public: double level_price = level_price_qty.price(); price_qty = Quantity(level_price_qty.size()); cumm_qty = Quantity(cumm_qty.value() + price_qty.value()); - if (bts::securities::test_price_equality (level_price, target_price.value(), tick_size)) + if (bts::securities::test_price_equality (level_price, + target_price.value(), + tick_size)) return true; } price_qty = Quantity(0); cumm_qty = Quantity(0); @@ -459,6 +462,25 @@ public: else out_avg_price = Price((base_price.value() * base_qty.value() + inc_price.value() * inc_qty.value())/out_total_qty.value()); } + static void adjust_for_hidden_order(const LegPropertyData& leg, + const Book& book, + Price& price, + Quantity& quantity){ + Quantity out_qty(0), out_cum_qty(0); + if(leg.trading_side()==api::Side::SIDE_BUY) + find_qty_at_price_level(book,api::Side::SIDE_SELL, + price, + leg.tick_size(), + out_qty, + out_cum_qty); + else if(leg.trading_side()==api::Side::SIDE_SELL) + find_qty_at_price_level(book,api::Side::SIDE_SELL, + price, + leg.tick_size(), + out_qty, + out_cum_qty); + quantity = Quantity(std::min(quantity.value(),out_cum_qty.value())); + } ////////////////////////2 legged spreads // given a quoting target qty, finds the "driver" price // and qty, thats needed to hedge the quote. It also diff --git a/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp b/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp index 6c02fa0a9..e0ce7f31b 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp @@ -11,15 +11,17 @@ QuoterState QuoterWaitingForSizeStateWorker::state () const { return QuoterState::WAITING_FOR_SIZE; } void QuoterWaitingForSizeStateWorker::one_time_activate (Price, Quantity){ - bts_assert_msg (false, "QuoterWaitingForSizeStateWorker::one_time_activate is an illegal operation"); + bts_assert_msg (false, "QuoterWaitingForSizeStateWorker::one_time_activate " + "is an illegal operation"); } void QuoterWaitingForSizeStateWorker::on_order_update (const SpreaderChildOrderState&){ // no order has been sent yet - bts_assert_msg (false, "QuoterWaitingForSizeStateWorker::on_order_update is an illegal operation"); + bts_assert_msg (false, "QuoterWaitingForSizeStateWorker::on_order_update " + "is an illegal operation"); } -void QuoterWaitingForSizeStateWorker::on_book (bts::orderman::TickToTradeCorID corid, +void QuoterWaitingForSizeStateWorker::on_book (TickToTradeCorID corid, size_t, - const bts::orderman::Book&){ + const Book&){ _longpath_analyze_and_act (corid); } void QuoterWaitingForSizeStateWorker::cancel(){ @@ -38,13 +40,16 @@ void QuoterWaitingForSizeStateWorker::on_other_leg_quoter_fill (size_t leg_index _quoter->_incorporate_other_leg_fill (leg_index, filled_quantity); _longpath_analyze_and_act (orderman::NullTickToTradeCorID); } -void QuoterWaitingForSizeStateWorker::update_spreader_order (Price price, Quantity quantity){ +void QuoterWaitingForSizeStateWorker::update_spreader_order (Price price, + Quantity quantity){ _quoter->_price_quantity_state.update_target_price_quantity (price, quantity); _longpath_analyze_and_act (orderman::NullTickToTradeCorID); } -void QuoterWaitingForSizeStateWorker::_longpath_analyze_and_act (orderman::TickToTradeCorID cid){ +void QuoterWaitingForSizeStateWorker::_longpath_analyze_and_act(TickToTradeCorID cid){ if (_quoter->_price_quantity_state.target_reached()){ - LOG(OrderMan, Info, "QuoterWaitingForSizeStateWorker key: {}, target reached, transforming to end", _quoter->_key().value()); + LOG(OrderMan, Info, "QuoterWaitingForSizeStateWorker key:{} " + "target reached, transforming to end", + _quoter->_key().value()); _quoter->_transform_to_END (); } else{ @@ -52,11 +57,13 @@ void QuoterWaitingForSizeStateWorker::_longpath_analyze_and_act (orderman::TickT _quoter->_compute_price_quantity (out_price, out_quantity); if (out_quantity.value() > 0){ _quoter->_commit_drivers (out_price); - LOG(OrderMan, Info, "QuoterWaitingForSizeStateWorker sending order key: {} price: {} quantity: {}", _quoter->_key(), out_price, out_quantity); + LOG(OrderMan, Info, "QuoterWaitingForSizeStateWorker sending order " + "key:{} price:{} quantity:{}", + _quoter->_key(), out_price, out_quantity); // this is the only place where a quoter order is entered _quoter->_place_order(out_price, out_quantity, cid); _quoter->_transform_to_WAITING_FOR_FIRST_ACK (); } } } -}} +}} \ No newline at end of file