Files
spark/src/lib/orderman/orders/queue_holder/QueueHolderOrder.h
T

167 lines
6.5 KiB
C++

#pragma once
#include <memory>
#include "lib/orderman/orders/Order.h"
#include "lib/api/services/OrderService.server.pb.h"
#include "lib/orderman/orders/queue_holder/IQueueHolderOrderStateWorker.h"
#include "lib/orderman/orders/queue_holder/state_workers/QHOStartStateWorker.h"
#include "lib/orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.h"
#include "lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.h"
#include "lib/orderman/orders/queue_holder/state_workers/QHOWaitingForCancelAllStateWorker.h"
#include "lib/orderman/orders/queue_holder/state_workers/QHOEndStateWorker.h"
#include "lib/orderman/orders/queue_holder/QHOOrderManager.h"
#include "lib/orderman/orders/OrderOpResult.h"
#include "lib/domain_model/Security.h"
namespace bts {
namespace api {namespace orderman {class OrderDefinition;}}
namespace mercury {class TimerEvent;}
namespace md {class PriceSize;}
namespace orderman {
class OrderStateUpdateEvent;
class BookUpdateEvent;
class PreActivationOrderServices;
class QueueHolderOrder : public Order
{
friend QHOStartStateWorker;
friend QHOWaitingForAckStateWorker;
friend QHOWorkingStateWorker;
friend QHOWaitingForCancelAllStateWorker;
friend QHOEndStateWorker;
public:
QueueHolderOrder();
QueueHolderOrder(const QueueHolderOrder&)=delete;
QueueHolderOrder(QueueHolderOrder&&)=delete;
QueueHolderOrder& operator=(const QueueHolderOrder&)=delete;
QueueHolderOrder& operator=(QueueHolderOrder&&)=delete;
virtual ~QueueHolderOrder();
api::orderman::OrderDefinition const& definition() const;
void on_init_new(api::orderman::OrderDefinition const&,
const PreActivationOrderServices&) override;
void on_init_recovered(api::orderman::OrderRecord const&,
const PreActivationOrderServices &) override;
OrderOpResult on_activate(Price, Quantity,
OrderServices&) override;
OrderOpResult on_update(Price, Quantity) override;
OrderOpResult on_cancel() override;
OrderOpResult on_detach() 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&);
void _on_child_order_update(OrderStateUpdateEvent const&);
void _on_time (const bts::mercury::TimerEvent&);
private:
QueueHolderOrderState _state() const{
return _state_worker->state();
}
int _state_int() const{
if (_state_worker==nullptr)
return -1;
return static_cast<int>(_state_worker->state());
}
bool _activated() const{
return _services!=nullptr;
}
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()){
std::string& err_msg = *(_reported_state.mutable_last_msg());
// both status and err_msg are set here
_reported_state.set_status(_status(err_msg));
double filled_price;
uint32_t filled_qty;
_order_manager.filled_price(filled_price,filled_qty);
if(filled_qty>_reported_state.filled_quantity()){
// this means there was a fill reported bw last report
// and this, hence last_price needs to be set
uint32_t last_trade_qty=filled_qty-
_reported_state.filled_quantity();
double last_trade_price=filled_price*filled_qty-
_reported_state.avg_price()*_reported_state.filled_quantity();
last_trade_price/=last_trade_qty;
_reported_state.set_last_price(last_trade_price);
}
_reported_state.set_avg_price(filled_price);
_reported_state.set_filled_quantity(filled_qty);
if(!_state_eq()){
_sent_state=_reported_state;
_services->report_state(_reported_state);
}
}
}
void _set_state_price_qty(Price price, Quantity qty){
_reported_state.set_price(price.value());
_reported_state.set_quantity(qty.value());
}
bool _filled() const{
return _order_manager.filled_qty().value()>=
_reported_state.quantity();
}
void _switch_state(QueueHolderOrderState new_state);
void _assert_false(const std::string& str) const{
std::ostringstream oss;
oss << str << " key:" << key();
bts_assert_msg(false, oss.str());
}
bool _state_eq() const{
return _reported_state.status() == _sent_state.status() &&
securities::is_same(Price(_reported_state.price()),
Price(_sent_state.price())) &&
_reported_state.quantity() == _sent_state.quantity() &&
_reported_state.filled_quantity() == _sent_state.filled_quantity() &&
securities::is_same(Price(_reported_state.last_price()),
Price(_sent_state.last_price())) &&
securities::is_same(Price(_reported_state.avg_price()),
Price(_sent_state.avg_price())) &&
_reported_state.last_msg() == _sent_state.last_msg();
}
private:
// if the init call succeeded which means
// i. order is properly built
// ii. security was successfully resolved
bool _is_succ_inited;
// tells if the order is a recovered order
// (as opposed to a new order)
bool _is_recovered;
TerminationReason _termination_reason;
api::orderman::OrderDefinition _order_def;
double _tick_size;
OrderBuilder _order_builder;
QHOOrderManager _order_manager;
OrderServices *_services;
api::orderman::OrderState _reported_state, _sent_state;
Book _book;
IQueueHolderOrderStateWorker* _state_worker;
QHOStartStateWorker _start_state_worker;
QHOWaitingForAckStateWorker _waiting_for_ack_state_worker;
QHOWorkingStateWorker _working_state_worker;
QHOWaitingForCancelAllStateWorker _waiting_for_cancel_all_state_worker;
QHOEndStateWorker _end_state_worker;
};
}}