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

343 lines
12 KiB
C++

#pragma once
#include "lib/os/os.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.h"
#include "lib/domain_model/Security.h"
namespace bts {
namespace orderman {
// submit, modifies and cancels all orders
// that constitute the QHO.
// i. places, modifies and cancels orders
// ii. helper functons to get the accumulated
// state of the orders
// all the order interactions are done explicitely
// 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;
uint32_t _show_qty;
// the order of this does is not asc/desc wrt
// price (modify of price will make it unordered)
std::vector<SpreaderChildOrderState> _orders;
Price _target_price;
Quantity _target_qty;
public:
QHOOrderManager()
:
_inited(false),
_orders_placed_once(false),
_order_builder(nullptr),
_side(api::Side::SIDE_BUY),
_tick_size(os::nan()),
_n_eff_orders(1),
_show_qty(0),
_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,
double tick_size,
uint16_t n_orders,
uint32_t show_qty,
Price price,
Quantity qty){
_inited=true;
_order_builder=&order_builder;
_side=side;
_tick_size=tick_size;
// 1 trailing order means 2 total orders and so forth
_n_eff_orders=n_orders+1;
_orders.reserve(_n_eff_orders);
_show_qty=show_qty;
for(uint16_t i=0;i<_n_eff_orders;i++)
_orders.emplace_back(key,true,side,true,nullptr);
_target_price=price;
_target_qty=qty;
}
// this can be called only once
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(),
move_inc=_top_down_tick_inc(_tick_size,_side);
for(SpreaderChildOrderState& o : _orders){
bts_assert(o.out_quantity().value()==0);
ChildOrder child_order=_order_builder->build();
o.re_init(child_order);
_place_order(o,Price(price_i),target_qty);
price_i+=move_inc;
}
}
// sends orders (for the remaining qty)
// for all terminal orders
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(o.terminal_status()){
Price price=o.out_price();//re-init resets the price
ChildOrder child_order=_order_builder->build();
o.re_init(child_order);
_place_order(o,price,target_qty);
}
else
_prune_qty(o);
}
}
// can only modifiy if all acked and working
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;
// all acked and working
bts_assert(all_orders_acked() && !has_terminal_order());
_target_price=price;
_target_qty=qty;
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(){
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(!o.is_cancelled()&&o.acknowledged()&&!o.terminal_status())
o.cancel_order();
}
}
// 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);
return true;
}
return false;
}
// decreases qty in case of fills.
// can only modifiy if all acked and working
void prune_quantities(){
bts_assert(_inited);
bts_assert(_orders_placed_once);
bts_assert(all_orders_acked() && !has_terminal_order());
for (auto& o : _orders)
_prune_qty(o);
}
///////////////////////////////////////////////
inline Quantity filled_qty() const{
uint32_t rv=0;
for (const auto& o : _orders)
rv+=o.total_fills().value();
return Quantity(rv);
}
inline void filled_price(double& filled_price, uint32_t& filled_qty) const{
double tot_price=0.0;
uint32_t tot_qty=0;
for (const auto& o : _orders){
Price so_price; Quantity so_qty;
o.total_fills(so_qty,so_price);
if(so_qty.value()>0){
tot_qty+=so_qty.value();
tot_price+=so_price.value()*so_qty.value();
}
}
if(tot_qty>0)
tot_price/=tot_qty;
filled_price=tot_price;
filled_qty=tot_qty;
}
inline bool last_update_has_fills() const{
for (const auto& o : _orders)
if(o.last_update_filled_quantity().value()>0)
return true;
return false;
}
inline bool has_cancelled_order() const{
for (const auto& o : _orders)
if(o.cancelled())
return true;
return false;
}
inline bool has_completed_order() const{
for (const auto& o : _orders)
if(o.completed())
return true;
return false;
}
inline bool has_errored_order() const{
for (const auto& o : _orders)
if(o.errored())
return true;
return false;
}
inline bool has_terminal_order() const{
for (const auto& o : _orders)
if(o.terminal_status())
return true;
return false;
}
inline bool all_orders_terminal() const{
for (const auto& o : _orders)
if(!o.terminal_status())
return false;
return true;
}
inline bool all_orders_acked() const{
for (const auto& o : _orders)
if(!o.acknowledged())
return false;
return true;
}
private:
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(
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_working_order_at_price(price_i);
if(order_i==nullptr){
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); // qty inc since its
// changing price levels
//to_move.modify_order(Price(price_i),Quantity(new_ord_qty));
_modify_child_order(to_move,Price(price_i),Quantity(new_ord_qty));
}
else
_prune_qty(*order_i);
price_i+=move_inc;
}
}
// 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
bts_assert(new_ord_qty>0);
//o.modify_order(o.out_price(),Quantity(new_ord_qty));
_modify_child_order(o,o.out_price(),Quantity(new_ord_qty));
}
Quantity _remaining() const{
if(filled_qty().value()>=_target_qty.value())
return Quantity(0);
else
return Quantity(_target_qty.value()-filled_qty().value());
}
// finds the order who's out_price matches
// the arg
SpreaderChildOrderState* _find_working_order_at_price(double price){
for(auto& o : _orders)
if(bts::securities::test_price_equality(
o.out_price().value(),
price,
_tick_size) &&
!o.terminal_status())
return &o;
return nullptr;
}
SpreaderChildOrderState& _highest_price_order(){
SpreaderChildOrderState* rv=&_orders[0];
for(auto& o : _orders)
if(o.out_price().value()>rv->out_price().value())
rv=&o;
return *rv;
}
SpreaderChildOrderState& _lowest_price_order(){
SpreaderChildOrderState* rv=&_orders[0];
for(auto& o : _orders)
if(o.out_price().value()<rv->out_price().value())
rv=&o;
return *rv;
}
Quantity _force_show_qty(Quantity target_qty){
return _show_qty==0 ? target_qty :
Quantity(std::min(target_qty.value(),
_show_qty));
}
void _place_order(SpreaderChildOrderState& ord,
Price price, Quantity qty){
qty=_force_show_qty(qty);
ord.place_order(price,qty,bts::Timer::now());
}
void _modify_child_order(SpreaderChildOrderState& ord,
Price price,
Quantity qty){
qty=_force_show_qty(qty);
if(ord.out_quantity() == qty &&
bts::securities::test_price_equality(
price.value(),
ord.out_price().value(),
_tick_size))
return;
ord.modify_order(price,qty);
}
// given an order's filled and order qty, and the
// target left on the total qho, finds the adjusted
// qty of the order
static uint32_t _adjusted_qty (uint32_t filled_qty,
uint32_t order_qty,
uint32_t rem_target,
bool allow_inc){
bts_assert(filled_qty<=order_qty);
uint32_t rem_fill=order_qty-filled_qty;
if(rem_fill>rem_target){
uint32_t dec=rem_fill-rem_target;
return order_qty-dec;
}
else if(rem_fill<rem_target && allow_inc){
uint32_t inc=rem_target-rem_fill;
return order_qty+inc;
}
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;
}
};
}}