257 lines
8.1 KiB
C++
257 lines
8.1 KiB
C++
#include "OCOOrder.h"
|
|
|
|
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
|
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include "lib/utils/helper_macros.h"
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
using namespace api::orderman;
|
|
|
|
struct OCOOrder::PImpl
|
|
{
|
|
PImpl() {}
|
|
|
|
int other_ticks;
|
|
bts::api::RelativeSide other_side;
|
|
OrderDefinition other_def;
|
|
bool hedge_sent;
|
|
};
|
|
|
|
|
|
OCOOrder::OCOOrder() : me(new PImpl()) {}
|
|
|
|
OCOOrder::~OCOOrder() {}
|
|
|
|
void OCOOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
|
api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child)
|
|
{
|
|
initial_state.mutable_oco();
|
|
auto const &oco_def = this_def.custom_def().oco();
|
|
*out_child->mutable_custom_def() = oco_def.child();
|
|
|
|
me->other_ticks = oco_def.other_ticks();
|
|
me->other_side = oco_def.other_side();
|
|
me->hedge_sent = false;
|
|
}
|
|
|
|
//hedge here means 'other'
|
|
bool OCOOrder::setup_hedge(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&,
|
|
api::orderman::OrderDefinition* out_hedge)
|
|
{
|
|
auto const &oco_def = this_def.custom_def().oco();
|
|
if(oco_def.has_other())
|
|
{
|
|
*out_hedge->mutable_custom_def() = oco_def.other();
|
|
bts_assert_msg(me->other_side != api::RELATIVESIDE_UNDEFINED, "undefined relativeside");
|
|
|
|
if(oco_def.other_side() == api::RELATIVESIDE_BUY)
|
|
out_hedge->set_side(api::SIDE_BUY);
|
|
else if(oco_def.other_side() == api::RELATIVESIDE_SELL)
|
|
out_hedge->set_side(api::SIDE_SELL);
|
|
else if(oco_def.other_side() == api::RELATIVESIDE_SAME)
|
|
out_hedge->set_side(definition().side());
|
|
else if(oco_def.other_side() == api::RELATIVESIDE_OPPOSITE)
|
|
{
|
|
if(definition().side() == api::SIDE_BUY)
|
|
out_hedge->set_side(api::SIDE_SELL);
|
|
else
|
|
out_hedge->set_side(api::SIDE_BUY);
|
|
}
|
|
else
|
|
bts_assert_msg(false, "unknown relativeside");
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
void OCOOrder::process_child_state_change(const api::orderman::OrderState& orig_state, const OrderStateUpdateEvent& new_state)
|
|
{
|
|
uint32_t fill_qty = new_state.filled_quantity - orig_state.filled_quantity();
|
|
const auto& the_state = hedges_state();
|
|
auto& the_hedges = hedges();
|
|
for(const auto& i : the_state)
|
|
{
|
|
const auto& hedge_state = i.second;
|
|
|
|
uint32_t working_qty = hedge_state.quantity() - hedge_state.filled_quantity();
|
|
uint32_t newqty = working_qty - std::min(fill_qty, working_qty);
|
|
bts_assert(newqty <= working_qty);
|
|
|
|
auto it = the_hedges.find(i.first);
|
|
if(it != the_hedges.end())
|
|
{
|
|
if(newqty != working_qty)
|
|
{
|
|
if(newqty > 0)
|
|
{
|
|
LOG(OrderMan, Info, "OCO reducing hedge qty from {} to {}", working_qty, newqty);
|
|
it->second.update(Price(hedge_state.price()), Quantity(newqty));
|
|
}
|
|
else
|
|
it->second.cancel();
|
|
}
|
|
else if(new_state.status != api::orderman::OrderStatus::ORDER_STATUS_PENDING_WORKING &&
|
|
new_state.status != api::orderman::OrderStatus::ORDER_STATUS_WORKING)
|
|
{
|
|
it->second.cancel();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LOG(OrderMan, Error, "OCO Can't find hedge order {}", i.first);
|
|
cancel_child();
|
|
}
|
|
}
|
|
}
|
|
|
|
void OCOOrder::process_hedge_state_change(const api::orderman::OrderState& orig_state, const OrderStateUpdateEvent& new_state)
|
|
{
|
|
uint32_t fill_qty = new_state.filled_quantity - orig_state.filled_quantity();
|
|
uint32_t working_qty = child_state().quantity() - child_state().filled_quantity();
|
|
uint32_t newqty = working_qty - std::min(fill_qty, working_qty);
|
|
bts_assert(newqty <= working_qty);
|
|
if(newqty != working_qty)
|
|
{
|
|
if(newqty > 0)
|
|
{
|
|
LOG(OrderMan, Info, "OCO reducing child qty from {} to {}", working_qty, newqty);
|
|
update_child(Price(child_state().price()), Quantity(newqty));
|
|
}
|
|
else
|
|
cancel_child();
|
|
}
|
|
else if(new_state.status != api::orderman::OrderStatus::ORDER_STATUS_PENDING_WORKING &&
|
|
new_state.status != api::orderman::OrderStatus::ORDER_STATUS_WORKING)
|
|
{
|
|
cancel_child();
|
|
}
|
|
}
|
|
|
|
void OCOOrder::on_book_update(const bts::orderman::BookUpdateEvent& )
|
|
{
|
|
switch (this_state().status())
|
|
{
|
|
case ORDER_STATUS_WORKING:
|
|
switch (child_state().status())
|
|
{
|
|
case ORDER_STATUS_INACTIVE:
|
|
{
|
|
activate_child(Price(this_state().price()), Quantity(this_state().quantity()));
|
|
|
|
if(!me->hedge_sent)
|
|
{
|
|
double prx = this_state().price();
|
|
if(definition().side() == api::SIDE_BUY)
|
|
prx = prx + tick_size() * me->other_ticks;
|
|
else
|
|
prx = prx - tick_size() * me->other_ticks;
|
|
build_and_send_hedge( Price(prx), Quantity(this_state().quantity()) );
|
|
me->hedge_sent = true;
|
|
}
|
|
transition(ORDER_STATUS_WORKING);
|
|
report_state();
|
|
}
|
|
break;
|
|
case ORDER_STATUS_WORKING:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
break;
|
|
|
|
// child cannot be in any of these states if we are in the working state
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_ERROR:
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_CANCELED:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_CANCELED:
|
|
case ORDER_STATUS_ERROR:
|
|
break;
|
|
|
|
case ORDER_STATUS_INACTIVE:
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void OCOOrder::on_cancel(trace::CorID const&)
|
|
{
|
|
LOG(OrderMan, Info, "Canceling OCOOrder Order {}", key().value());
|
|
|
|
switch (this_state().status())
|
|
{
|
|
case ORDER_STATUS_WORKING:
|
|
switch (child_state().status())
|
|
{
|
|
case ORDER_STATUS_INACTIVE:
|
|
transition(ORDER_STATUS_CANCELED);
|
|
break;
|
|
case ORDER_STATUS_WORKING:
|
|
if(!is_detached())
|
|
{
|
|
cancel_child();
|
|
for(auto& i : hedges())
|
|
i.second.cancel();
|
|
}
|
|
transition(ORDER_STATUS_CANCELED);
|
|
break;
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
// child is running but it cannot be canceled at this point
|
|
transition(this_state().status(), "Cannot cancel order");
|
|
break;
|
|
|
|
// child cannot be in any of these if we are in the working state
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_ERROR:
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_CANCELED:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
break;
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_CANCELED:
|
|
case ORDER_STATUS_ERROR:
|
|
transition(this_state().status(), "Cannot cancel order");
|
|
break;
|
|
|
|
// these are unreacheable for this order type
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_INACTIVE:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
report_state();
|
|
}
|
|
|
|
}
|
|
}
|
|
|