Files
spark/src/lib/orderman/orders/book_algos/TopOrder.cpp
T
2018-12-05 21:52:49 +00:00

130 lines
4.5 KiB
C++

#include "TopOrder.h"
#include "lib/domain_model/Security.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 TopOrder::PImpl {
PImpl() {}
uint32_t watch_level_tick_offset;
spark::QuantityMeasure top_threshold;
};
TopOrder::TopOrder() : me(new PImpl()) {}
TopOrder::~TopOrder() {}
void TopOrder::setup_child(const api::orderman::OrderDefinition &this_def,
const PreActivationOrderServices &,
api::orderman::OrderState &initial_state,
api::orderman::OrderDefinition *out_child) {
initial_state.mutable_top();
auto const &top_def = this_def.custom_def().top();
*out_child->mutable_custom_def() = top_def.child();
me->watch_level_tick_offset = top_def.watch_level_tick_offset();
me->top_threshold = top_def.top_threshold();
}
void TopOrder::on_book_update(const bts::orderman::BookUpdateEvent &book) {
switch (this_state().status()) {
case ORDER_STATUS_WORKING:
switch (child_state().status()) {
case ORDER_STATUS_INACTIVE: {
Quantity thresh = SpreaderUtils::_quantity_threshold(
me->top_threshold, Quantity(this_state().quantity()));
if (definition().side() == api::SIDE_BUY) {
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
if (securities::test_price_equality(this_state().price(),
same.price(), tick_size())) {
if (same.size() <= thresh.value()) {
transition(ORDER_STATUS_CANCELED);
report_state();
return;
}
}
} else {
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
if (securities::test_price_equality(this_state().price(),
same.price(), tick_size())) {
if (same.size() <= thresh.value()) {
transition(ORDER_STATUS_CANCELED);
report_state();
return;
}
}
}
activate_child(Price(this_state().price()),
Quantity(this_state().quantity()));
transition(ORDER_STATUS_WORKING);
report_state();
} break;
case ORDER_STATUS_WORKING: {
Quantity thresh = SpreaderUtils::_quantity_threshold(
me->top_threshold, Quantity(this_state().quantity()));
if (definition().side() == api::SIDE_BUY) {
const auto &same = book.book.side(api::Side::SIDE_BUY)[0];
if (securities::test_price_equality(this_state().price(),
same.price(), tick_size())) {
if (same.size() <= thresh.value()) {
cancel_child();
transition(ORDER_STATUS_CANCELED);
report_state();
}
}
} else {
const auto &same = book.book.side(api::Side::SIDE_SELL)[0];
if (securities::test_price_equality(this_state().price(),
same.price(), tick_size())) {
if (same.size() <= thresh.value()) {
cancel_child();
transition(ORDER_STATUS_CANCELED);
report_state();
}
}
}
} 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;
}
}
} // namespace orderman
} // namespace bts