simplified reporting

This commit is contained in:
Taha Afzal committed 2019-04-09 12:12:15 -04:00
1 parent 250fd8f570
commit 044d7725dc
5 files changed
+86 -79

No files matched your search

@@ -17,9 +17,7 @@ QueueHolderOrder::QueueHolderOrder()
:
_is_succ_inited(false),
_is_recovered(false),
_cancelled_externally(false),
_unexpected_child_cancel(false),
_child_error(false),
_termination_reason(TerminationReason::NONE),
_order_def(),
_tick_size(bts::os::nan()),
_order_builder(),
@@ -72,22 +70,14 @@ void QueueHolderOrder::on_update(Price price,
void QueueHolderOrder::on_crack(trace::CorID const &) {
}
void QueueHolderOrder::on_cancel(const trace::CorID&) {
if(_state()!=QueueHolderOrderState::WAITING_FOR_CANCEL_ALL &&
_state()!=QueueHolderOrderState::END){
_cancelled_externally=true;
_state_worker->on_cancel();
_report_state();
}
_state_worker->on_cancel();
_report_state();
}
void QueueHolderOrder::_on_book_update(const BookUpdateEvent& b) {
_state_worker->on_book(b.book,b.trace);
}
void QueueHolderOrder::_on_child_order_update(
const OrderStateUpdateEvent& c) {
_unexpected_child_cancel = _unexpected_child_cancel ||
_is_unexpected_child_cancel(c);
_child_error = _child_error ||
SpreaderUtils::order_in_error_state(c);
const OrderStateUpdateEvent& c) {
_state_worker->on_child_order_update(c);
_report_state();
}
@@ -128,12 +118,44 @@ void QueueHolderOrder::_switch_state(QueueHolderOrderState new_state) {
LOG(OrderMan, Info, "QueueHolderOrder::_switch_state key:{} {}->{}",
key(), s0,_state_int());
}
bool QueueHolderOrder::_is_unexpected_child_cancel(
const OrderStateUpdateEvent& c) const{
bool expecting_cancel=
_state()==QueueHolderOrderState::WAITING_FOR_CANCEL_ALL;
return !expecting_cancel &&
SpreaderUtils::order_in_cancelled_state(c.status);
}
api::orderman::OrderStatus QueueHolderOrder::status(std::string& message) const{
QueueHolderOrderState curr_state=
_state_worker==nullptr?QueueHolderOrderState::START:_state();
switch (curr_state){
case QueueHolderOrderState::START:
case QueueHolderOrderState::WAITING_FOR_FIRST_ACK:
case QueueHolderOrderState::WORKING:
case QueueHolderOrderState::WAITING_FOR_CANCEL_ALL:
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
case QueueHolderOrderState::END:
switch(_termination_reason){
case TerminationReason::NONE:{
// this will happen if the order is
// recovered or the init failed
bts_assert(!_is_succ_inited||_is_recovered);
if(!_is_succ_inited){
message="Could not initialize order";
return api::orderman::OrderStatus::ORDER_STATUS_REJECTED_BY_SPARK;
}
else{
message="Recovered order";
// we could have done better here by [reserving ]
return api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
}
}
case TerminationReason::EXTERNAL_CANCEL:
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
case TerminationReason::FILLED:
return api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
case TerminationReason::UNEXPECTED_CHILD_CANCEL:
message="Child order cancelled";
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
case TerminationReason::CHILD_ERROR:
message="Child order error";
return api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
}
}
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
}
} // namespace orderman
} // namespace bts
@@ -29,7 +29,7 @@ friend QHOWorkingStateWorker;
friend QHOWaitingForCancelAllStateWorker;
friend QHOEndStateWorker;
public:
QueueHolderOrder();
QueueHolderOrder();
QueueHolderOrder(const QueueHolderOrder&)=delete;
QueueHolderOrder(QueueHolderOrder&&)=delete;
QueueHolderOrder& operator=(const QueueHolderOrder&)=delete;
@@ -46,6 +46,20 @@ public:
void on_update(Price, Quantity, trace::CorID const&) override;
void on_crack(trace::CorID const&) override;
void on_cancel(trace::CorID const&) override;
private:
enum class TerminationReason
{
// initial value at the time of the first cancel
NONE,
EXTERNAL_CANCEL,
// order got filled (since there are multiple
// orders we need to cancel the rest)
FILLED,
// one or more child orders got cancelled
UNEXPECTED_CHILD_CANCEL,
// child order errored out
CHILD_ERROR
};
private:
// callbacks from book and child order udpates
void _on_book_update(const BookUpdateEvent&);
@@ -54,47 +68,22 @@ private:
private:
QueueHolderOrderState _state() const{
return _state_worker->state();
}
int _state_int() const{
if (_state_worker==nullptr)
return -1;
// LOG(OrderMan, Error, "_state_int");
// return 0;
return static_cast<int>(_state_worker->state());
}
bool _activated() const{
return _services!=nullptr;
}
bool _is_unexpected_child_cancel(const OrderStateUpdateEvent& c) const;
api::orderman::OrderStatus status(std::string& message) const{
QueueHolderOrderState curr_state=
_state_worker==nullptr?QueueHolderOrderState::START:_state();
switch (curr_state){
case QueueHolderOrderState::START:
return api::orderman::OrderStatus::ORDER_STATUS_INACTIVE;
case QueueHolderOrderState::WAITING_FOR_FIRST_ACK:
return api::orderman::OrderStatus::ORDER_STATUS_PENDING_WORKING;
case QueueHolderOrderState::WORKING:
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
case QueueHolderOrderState::WAITING_FOR_CANCEL_ALL:
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
case QueueHolderOrderState::END:
if(_cancelled_externally){
return bts::api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
}
else if(_unexpected_child_cancel){
message="Queue Holder Order's child order cancelled";
return bts::api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
}
else if(_child_error){
message="Queue Holder Order's child order error";
return bts::api::orderman::OrderStatus::ORDER_STATUS_CANCELED;
}
return bts::api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
}
return api::orderman::OrderStatus::ORDER_STATUS_WORKING;
void _cancel_order(TerminationReason termination_reason){
bts_assert(termination_reason!=TerminationReason::NONE);
bts_assert(_termination_reason==TerminationReason::NONE);
_termination_reason=termination_reason;
_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
}
api::orderman::OrderStatus status(std::string& message) const;
// sends orders for the first time
void _report_state(){
if(_activated()){
@@ -124,7 +113,7 @@ private:
_reported_state.set_quantity(qty.value());
}
bool _filled() const{
return _order_manager.filled_qty().value() >=
return _order_manager.filled_qty().value()>=
_reported_state.quantity();
}
@@ -143,14 +132,7 @@ private:
// tells if the order is a recovered order
// (as opposed to a new order)
bool _is_recovered;
// true if on_cancel is called explicitly
bool _cancelled_externally;
// if the qho is not expecting child cancels
// but got one; most likely because 'someone'
// - human or computer - cancelled a child order
bool _unexpected_child_cancel;
// received an error condition on the child order
bool _child_error;
TerminationReason _termination_reason;
api::orderman::OrderDefinition _order_def;
double _tick_size;
OrderBuilder _order_builder;
@@ -99,8 +99,6 @@ bool QHOStartStateWorker::_on_init_new(
"QHOStartStateWorker::_on_init_new key:{} "
"OrderDefinition does not have QueueHolderDefinition",
_porder->key());
_porder->_reported_state.set_last_msg("QueueHolderOrder not "
"properly formed");
return false;
}
const bts::api::Security* sec_ptr =
@@ -110,8 +108,6 @@ bool QHOStartStateWorker::_on_init_new(
"QHOStartStateWorker::_on_init_new key:{} "
"Could not resolve sec_key:{}",
_porder->key(), order_def.security_key());
_porder->_reported_state.set_last_msg("Could not resolve "
"Trigger Order security");
return false;
}
_porder->_tick_size=sec_ptr->contract_spec().low_tick();
@@ -53,17 +53,17 @@ void QHOWaitingForAckStateWorker::on_update(Price price,
// transferring to WORKING
_porder->_set_state_price_qty(price,quantity);
if(_porder->_filled())
_porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
_porder->_cancel_order(QueueHolderOrder::TerminationReason::FILLED);
}
void QHOWaitingForAckStateWorker::on_cancel(){
_porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
_porder->_cancel_order(QueueHolderOrder::TerminationReason::EXTERNAL_CANCEL);
}
void QHOWaitingForAckStateWorker::on_child_order_update(
const OrderStateUpdateEvent& ev){
QHOOrderManager& order_man=_porder->_order_manager;
if(order_man.on_order_update(ev)){
if (_porder->_filled())
_porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
_porder->_cancel_order(QueueHolderOrder::TerminationReason::FILLED);
else if(order_man.all_orders_acked())
_porder->_switch_state(QueueHolderOrderState::WORKING);
}
@@ -20,7 +20,7 @@ _porder(parent)
{}
void QHOWorkingStateWorker::on_transition (){
if(!_analyze_moving_out()){
api::orderman::OrderState& state=_porder->_reported_state;
const api::orderman::OrderState& state=_porder->_reported_state;
// cached updates while waiting for acks
on_update(Price(state.price()),
Quantity(state.quantity()));
@@ -45,13 +45,13 @@ void QHOWorkingStateWorker::on_update(Price price,
Quantity qty){
_porder->_set_state_price_qty(price,qty);
if(_porder->_filled())
_porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
_porder->_cancel_order(QueueHolderOrder::TerminationReason::FILLED);
else
_porder->_order_manager.modify_order(price, qty);
}
void QHOWorkingStateWorker::on_cancel(){
_porder->_order_manager.cancel();
_porder->_switch_state(QueueHolderOrderState::WAITING_FOR_CANCEL_ALL);
_porder->_cancel_order(QueueHolderOrder::TerminationReason::EXTERNAL_CANCEL);
}
void QHOWorkingStateWorker::on_child_order_update(
const OrderStateUpdateEvent& ev){
@@ -71,16 +71,23 @@ void QHOWorkingStateWorker::on_child_order_update(
_porder->key(),ev.key);
}
// returns true if state is changed
bool QHOWorkingStateWorker::_analyze_moving_out(){
const QHOOrderManager& order_man=_porder->_order_manager;
bool QHOWorkingStateWorker::_analyze_moving_out(){
// 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;
if(_porder->_filled()){
_porder->_cancel_order(QueueHolderOrder::TerminationReason::FILLED);
return true;
}
const QHOOrderManager& order_man=_porder->_order_manager;
if(order_man.has_cancelled_order()){
_porder->_cancel_order(
QueueHolderOrder::TerminationReason::UNEXPECTED_CHILD_CANCEL);
return true;
}
if(order_man.has_cancelled_order()){
_porder->_cancel_order(QueueHolderOrder::TerminationReason::CHILD_ERROR);
return true;
}
// QHO order's target qty has not been reached
// yet one or more component orders has been filled.