Files
spark/src/lib/orderman/orders/book_algos/JoinOrder2.cpp
T
2019-04-02 16:36:24 -04:00

137 lines
4.0 KiB
C++

#include "JoinOrder2.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orderman_utils/orderman_utils.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
namespace bts {
namespace orderman {
using namespace api::orderman;
enum class JoinOrder2::Status { WAITING_TO_JOIN, JOINED };
struct JoinOrder2::PImpl {
Quantity min_quantity_to_join;
Status status = Status::WAITING_TO_JOIN;
};
JoinOrder2::JoinOrder2() : me(new PImpl()) {}
JoinOrder2::~JoinOrder2() {}
OrderOpResult JoinOrder2::do_on_init_new() {
auto &def = definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_join());
// validate the join part of the definition
auto &join = def.custom_def().join();
if (!join.has_child() &&
OrderKey(def.attach_target_order_key()) == null_order_key()) {
return OrderOpResult{
ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Join order must have either a child order definition or an "
"order to attach to"};
}
// set the custom component of our state
state().mutable_join();
return OrderOpResult{};
}
void JoinOrder2::do_on_ready(bool is_attached) {
bts_assert(me->status == Status::WAITING_TO_JOIN);
me->min_quantity_to_join = SpreaderUtils::_quantity_threshold(
definition().custom_def().join().join_threshold(),
Quantity(state().quantity()));
if (!is_attached) {
auto const &def = definition();
api::orderman::OrderDefinition child_def = def;
*child_def.mutable_custom_def() =
definition().custom_def().join().child();
launch_child(child_def);
}
try_to_join();
}
void JoinOrder2::do_on_book(Book const &) {
bts_assert(me->status == Status::WAITING_TO_JOIN);
try_to_join();
}
void JoinOrder2::try_to_join() {
bts_assert(me->status == Status::WAITING_TO_JOIN);
auto my_side = definition().side();
auto other_side = securities::other_side(my_side);
auto &my_side_psz = last_book().side(my_side)[0];
auto &opposite_at_inception_psz = book_at_inception().side(other_side)[0];
if (my_side_psz.size() &&
my_side_psz.size() >= me->min_quantity_to_join.value() &&
securities::is_equal_or_better(
my_side, Price(my_side_psz.price()),
Price(opposite_at_inception_psz.price()))) {
// Time to join
Update child_update;
child_update.set_price(my_side_psz.price());
child_update.set_quantity(state().quantity());
update_child(child_update);
done();
}
}
OrderOpResult JoinOrder2::do_on_update(api::orderman::Update const &update) {
if (update.has_custom() && !update.custom().has_join())
return {ORDER_ERROR_INVALID_OP,
"Update is not valid for this order type"};
// copy new price, size to our state
state().set_price(update.price());
state().set_quantity(update.quantity());
// Build child update
Update child_update;
child_update.set_price(update.price());
child_update.set_quantity(update.quantity());
if (update.has_custom()) {
// process custom part of the udpate if any
auto &join_update = update.custom().join();
*child_update.mutable_custom() = join_update.child();
// Update quantity threshold
me->min_quantity_to_join = SpreaderUtils::_quantity_threshold(
definition().custom_def().join().join_threshold(),
Quantity(update.quantity()));
}
update_child(child_update);
// If we were not yet joined it is possible we now can, since we
// could have changed the min_quantity_to_join
if (me->status == Status::WAITING_TO_JOIN)
try_to_join();
return OrderOpResult();
}
void JoinOrder2::done()
{
bts_assert(me->status == Status::WAITING_TO_JOIN);
auto my_side = definition().side();
auto &my_side_psz = last_book().side(my_side)[0];
me->status = Status::JOINED;
LOG(OrderMan, Diag, "JoinOrder2 order {} joined the market {}@{}",
key().value(), state().quantity(), my_side_psz.price());
detach("Joined successfully");
}
} // namespace orderman
} // namespace bts