Files
spark/src/lib/orderman/orders/book_algos/OCOOrder.cpp
T
Kevin Darby f3adf780d0 OCOOrder will be the basis of a StopLoss template
Added the beginnings of pricer order but need something from John
2018-12-19 18:15:04 -05:00

248 lines
7.5 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 <google/protobuf/timestamp.pb.h>
namespace bts {
namespace orderman {
using namespace api::orderman;
struct OCOOrder::PImpl
{
PImpl() {}
int other_ticks;
bts::api::Side 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();
if(me->other_side == api::SIDE_UNDEFINED)
{
if(definition().side() == api::SIDE_BUY)
out_hedge->set_side(api::SIDE_SELL);
else
out_hedge->set_side(api::SIDE_BUY);
}
else
out_hedge->set_side(me->other_side);
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 orig_qty = hedge_state.quantity();
uint32_t newqty = orig_qty - std::min(fill_qty, orig_qty);
bts_assert(newqty <= orig_qty);
auto it = the_hedges.find(i.first);
if(it != the_hedges.end())
{
if(newqty != orig_qty)
{
if(newqty > 0)
{
LOG(OrderMan, Info, "OCO reducing hedge qty from {} to {}", hedge_state.quantity(), 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 orig_qty = child_state().quantity();
uint32_t newqty = orig_qty - std::min(fill_qty, orig_qty);
bts_assert(newqty <= orig_qty);
if(newqty != orig_qty)
{
if(newqty > 0)
{
LOG(OrderMan, Info, "OCO reducing child qty from {} to {}", child_state().quantity(), 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(PerfTrace 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();
}
}
}