diff --git a/src/lib/orderman/orders/queue_holder/QHOOrderManager.h b/src/lib/orderman/orders/queue_holder/QHOOrderManager.h index 874e31ee7..fa7d5ac29 100644 --- a/src/lib/orderman/orders/queue_holder/QHOOrderManager.h +++ b/src/lib/orderman/orders/queue_holder/QHOOrderManager.h @@ -16,24 +16,41 @@ namespace orderman { // through function calls class QHOOrderManager{ private: + bool _inited; + bool _orders_placed_once; OrderBuilder* _order_builder; api::Side _side; double _tick_size; + uint16_t _n_eff_orders; + // the order of this does is not asc/desc wrt + // price (modify of price will make it unordered) + std::vector _orders; Price _target_price; Quantity _target_qty; - uint16_t _n_eff_orders; - std::vector _orders; public: QHOOrderManager() : + _inited(false), + _orders_placed_once(false), _order_builder(nullptr), _side(api::Side::SIDE_BUY), - _tick_size(os::nan()), - _target_price(os::nan()), - _target_qty(0), + _tick_size(os::nan()), _n_eff_orders(1), - _orders(){ + _orders(), + _target_price(os::nan()), + _target_qty(0){ } + // public interface ////////////////////////////// + ////////////////////////////////////////////////// + + // We could have done away with init and + // moved it in the cons. The problem is that + // the QueueHolderOrder does not have the data + // at its construction time to construct + // QHOOrderManager. (It could alternatively + // have used a pointer to QueueHolderOrder + // but for contigous memory allocations it + // did not) void init(OrderBuilder& order_builder, SecurityKey key, api::Side side, @@ -41,6 +58,7 @@ public: uint16_t n_orders, Price price, Quantity qty){ + _inited=true; _order_builder=&order_builder; _side=side; _tick_size=tick_size; @@ -53,47 +71,76 @@ public: _target_price=price; _target_qty=qty; } + // this can be called multiple times. only + // submits orders on levels where there are + // no working orders void place_order(){ + bts_assert(_inited); + bts_assert(!_orders_placed_once); + _orders_placed_once=true; Quantity target_qty=_remaining(); + bts_assert(target_qty.value()>0); double price_i=_target_price.value(), - price_diff=_side==api::Side::SIDE_BUY?-1.0*_tick_size:_tick_size; + move_inc=_top_down_tick_inc(_tick_size,_side); // only places orders where there are holes for(SpreaderChildOrderState& o : _orders){ - if(!_find_order(price_i)){ - ChildOrder child_order=_order_builder->build(); - o.re_init(child_order); - o.place_order(Price(price_i), - target_qty, - bts::Timer::now(), - trace::CorID()); - } - else{ - // TODO if it needs pruning - } - price_i+=price_diff; + bts_assert(o.out_quantity().value()==0); + ChildOrder child_order=_order_builder->build(); + o.re_init(child_order); + o.place_order(Price(price_i), + target_qty, + bts::Timer::now(), + trace::CorID()); + price_i+=move_inc; } } - bool modify_order(Price price, Quantity qty){ - bts_assert(qty.value() > filled_qty().value()); - bts_assert(all_orders_acked() && !all_orders_terminal()); + void reenter_filled_orders(){ + bts_assert(_inited); + bts_assert(_orders_placed_once); + Quantity target_qty=_remaining(); + bts_assert(target_qty.value()>0); + for(SpreaderChildOrderState& o : _orders){ + if(SpreaderUtils::order_in_terminal_state(o.status())){ + Price price=o.out_price();//re-init, resets the price + ChildOrder child_order=_order_builder->build(); + o.re_init(child_order); + o.place_order(price, + target_qty, + bts::Timer::now(), + trace::CorID()); + } + else + _prune_qty(o); + } + } + void modify_order(Price price, Quantity qty){ + bts_assert(_inited); + bts_assert(_orders_placed_once); + Price prev_price=_target_price; + Quantity prev_qty=_target_qty; + bts_assert(all_orders_acked() && !_has_terminal_order()); _target_price=price; _target_qty=qty; - _modify_order(); - return true; + bts_assert(_remaining().value()>0); + _modify_order(prev_price,prev_qty); } // cancels all the orders (and is smart // enough to about working orders only) void cancel(){ - for (auto& co : _orders){ - // is_cancelled() is checked so as not to keeo + bts_assert(_inited); + bts_assert(_orders_placed_once); + for (auto& o : _orders){ + // is_cancelled() is checked so as not to keep // sending cancel requests - if(!co.is_cancelled()&&co.acknowledged()&&!co.terminal_status()) - co.cancel_order(); + if(!o.is_cancelled()&&o.acknowledged()&&!o.terminal_status()) + o.cancel_order(); } } - // finds the orders and subsumes the update - // return false if it cant locate the order key + // finds the orders and subsumes the update. + // returns false if it cant locate the order key bool on_order_update(const OrderStateUpdateEvent& ev){ + bts_assert(_inited); + bts_assert(_orders_placed_once); for (auto& o : _orders) if(o.order_key()==ev.key){ o.on_order_update(ev); @@ -101,7 +148,13 @@ public: } return false; } - // readonly accumulated (over sub orders) state + // decreases qty in case of fills + void prune_quantities(){ + bts_assert(_inited); + bts_assert(_orders_placed_once); + for (auto& o : _orders) + _prune_qty(o); + } /////////////////////////////////////////////// inline Quantity filled_qty() const{ uint32_t rv=0; @@ -161,45 +214,49 @@ public: return false; return true; } - void _modify_order(){ - double move_inc=_side==api::Side::SIDE_BUY?-1.0*_tick_size:_tick_size; +private: + inline bool _has_terminal_order() const{ + for (const auto& o : _orders) + if(SpreaderUtils::order_in_terminal_state(o.status())) + return true; + return false; + } + void _modify_order(Price prev_price, Quantity){ + double move_inc=_top_down_tick_inc(_tick_size,_side); int tick_diff=bts::securities::price_diff_to_ticks( - _top_order().out_price().value(), + prev_price.value(), _target_price.value(), _tick_size); Quantity remaining_target=_remaining(); bool moving_up=tick_diff>0; double price_i=_target_price.value(); for(uint16_t i=0;i<_n_eff_orders;i++){ - SpreaderChildOrderState* order_i=_find_order(price_i); + SpreaderChildOrderState* order_i=_find_order_at_price(price_i); if(order_i==nullptr){ - SpreaderChildOrderState& to_move=moving_up?_lowest_price_order():_highest_price_order(); - // this order is changing its place so we can - // adjust its qty + SpreaderChildOrderState& to_move= + moving_up?_lowest_price_order():_highest_price_order(); uint32_t new_ord_qty=_adjusted_qty(to_move.current_fills().value(), to_move.out_quantity().value(), remaining_target.value(), - true); + true); // qty inc since its + // changing price levels to_move.modify_order(Price(price_i),Quantity(new_ord_qty)); } - else { - SpreaderChildOrderState& exst_ord=*order_i; - uint32_t new_ord_qty=_adjusted_qty(exst_ord.current_fills().value(), - exst_ord.out_quantity().value(), - remaining_target.value(), - false); - // order exists already in this price slot - // if the qty needs a decrease do it, however never - // increase the qty or position in queue would be lost - order_i->modify_order(Price(price_i),Quantity(new_ord_qty)); - } + else + _prune_qty(*order_i); price_i+=move_inc; } } - // decreases qty in case of fills - void prune_quantities(){ + // decreases qty if the order at any level can overfill + // the total, ie for an order out_qty-filled > total_rem + // if rem=5, and this order is 3/9 this will make it 3/8 + void _prune_qty(SpreaderChildOrderState& o){ + uint32_t new_ord_qty=_adjusted_qty(o.current_fills().value(), + o.out_quantity().value(), + _remaining().value(), + false); // dont inc the qty + o.modify_order(o.out_price(),Quantity(new_ord_qty)); } -private: Quantity _remaining() const{ if(filled_qty().value()>=_target_qty.value()) return Quantity(0); @@ -208,7 +265,7 @@ private: } // finds the order who's out_price matches // the arg - SpreaderChildOrderState* _find_order(double price){ + SpreaderChildOrderState* _find_order_at_price(double price){ for(auto& o : _orders) if(bts::securities::test_price_equality( o.out_price().value(), @@ -232,16 +289,6 @@ private: rv=&o; return *rv; } - // the order closest to the market (highest price - // for buy orders, lowest for sell orders) - SpreaderChildOrderState& _top_order(){ - SpreaderChildOrderState* rv=&_orders[0]; - for(auto& o : _orders) - if((_side==api::Side::SIDE_BUY && o.out_price().value()>rv->out_price().value())|| - (_side==api::Side::SIDE_SELL && o.out_price().value()out_price().value())) - rv=&o; - return *rv; - } // given an order's filled and order qty, and the // target left on the total qho, finds the adjusted // qty of the order @@ -261,5 +308,11 @@ private: } return order_qty; } + // the tick inc when traversing from the top + // (highest buy, lowest sell) down + static double _top_down_tick_inc (double tick_size, + api::Side side){ + return side==api::Side::SIDE_BUY?-1.0*tick_size:tick_size; + } }; }} \ No newline at end of file diff --git a/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp b/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp index 9e71cb947..f65ae965f 100644 --- a/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp +++ b/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp @@ -17,13 +17,25 @@ namespace orderman{ QHOWaitingForAckStateWorker::QHOWaitingForAckStateWorker( QueueHolderOrder* parent) : -_porder(parent) +_porder(parent), +_orders_placed_once(false) +{} +QHOWaitingForAckStateWorker::QHOWaitingForAckStateWorker ( + QueueHolderOrder* parent, + const QHOWaitingForAckStateWorker&) +: +QHOWaitingForAckStateWorker(parent) {} void QHOWaitingForAckStateWorker::on_transition (){ - // one and only place where orders - // are entered - _porder->_order_manager.place_order(); + // this is the only place where orders + // are placed + if(!_orders_placed_once){ + _porder->_order_manager.place_order(); + _orders_placed_once=true; + } + else + _porder->_order_manager.reenter_filled_orders(); } void QHOWaitingForAckStateWorker::on_init_new( diff --git a/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.h b/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.h index 0af9769a6..57d138a94 100644 --- a/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.h +++ b/src/lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.h @@ -19,14 +19,12 @@ class QHOWaitingForAckStateWorker : public IQueueHolderOrderStateWorker { private: QueueHolderOrder* const _porder; + bool _orders_placed_once; public: QHOWaitingForAckStateWorker(QueueHolderOrder* parent); QHOWaitingForAckStateWorker ( QueueHolderOrder* parent, - const QHOWaitingForAckStateWorker&) - : - _porder(parent) - {} + const QHOWaitingForAckStateWorker&); QueueHolderOrderState state () const override { return QueueHolderOrderState::WAITING_FOR_FIRST_ACK; } diff --git a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp index b34004e3e..355e37591 100644 --- a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp +++ b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp @@ -19,21 +19,12 @@ QHOWorkingStateWorker::QHOWorkingStateWorker(QueueHolderOrder* parent) _porder(parent) {} -void QHOWorkingStateWorker::on_transition (){ - QHOOrderManager& order_man=_porder->_order_manager; - if(_needs_cancelling()){ - _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL); - return; +void QHOWorkingStateWorker::on_transition (){ + if(!_analyze_moving_out()){ + api::orderman::OrderState& state=_porder->_reported_state; + on_update(Price(state.price()), + Quantity(state.quantity())); } - if (order_man.has_completed_order()){ - _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_FIRST_ACK); - return; - } - // this is so that any upates cached in - // the WAITING_FOR_FIRST_ACK are now acted on - api::orderman::OrderState& state=_porder->_reported_state; - on_update(Price(state.price()), - Quantity(state.quantity())); } void QHOWorkingStateWorker::on_init_new( @@ -69,16 +60,12 @@ void QHOWorkingStateWorker::on_child_order_update( const OrderStateUpdateEvent& ev){ QHOOrderManager& order_man=_porder->_order_manager; if(order_man.on_order_update(ev)){ - if(_needs_cancelling()){ - _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL); - return; - } - if (order_man.has_completed_order()){ - _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_FIRST_ACK); - return; - } - if(order_man.last_update_has_fills()){ - order_man.prune_quantities(); + if(!_analyze_moving_out()){ + if(order_man.last_update_has_fills()) + // in case of fills, we can reduce + // the qty of the trailing orders to + // decrease probablity of overfills + order_man.prune_quantities(); } } else @@ -86,13 +73,27 @@ void QHOWorkingStateWorker::on_child_order_update( "key:{} order:{} could not be found", _porder->key(),ev.key); } -// need to cancel if over filled or one -// order has been cancelled or errored out -bool QHOWorkingStateWorker::_needs_cancelling() const{ +// returns true if state is changed +bool QHOWorkingStateWorker::_analyze_moving_out(){ const QHOOrderManager& order_man=_porder->_order_manager; - return _porder->_filled() || - order_man.has_cancelled_order() || - order_man.has_errored_order(); + // order is cancelled if over filled or any of the + // constituent orders end up in an unexpected terminal + // state + if(_porder->_filled() || + order_man.has_cancelled_order() || + order_man.has_errored_order()){ + _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL); + return true; + } + // QHO order's target qty has not been reached + // yet one or more component orders has been filled. + // switching to WAITING_FOR_FIRST_ACK will cause + // filled orders to be replaced with new orders + if (order_man.has_completed_order()){ + _porder->_switch_state(QueueHolderOrderState::WAITING_FOR_FIRST_ACK); + return true; + } + return false; } void QHOWorkingStateWorker::on_book(const Book&, const trace::CorID&){ diff --git a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.h b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.h index 4329530d1..769d55434 100644 --- a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.h +++ b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.h @@ -34,7 +34,8 @@ public: const trace::CorID&) override; void on_time(const TimeStamp&) override; private: - void _analyze_and_act(); + // returns true if the state is changed + bool _analyze_moving_out(); bool _needs_cancelling() const; };