Files
spark/src/lib/orderman/orders/book_algos/LeanOrder2.cpp
T

220 lines
7.5 KiB
C++

// READY TO REVIEW
#include "LeanOrder2.h"
#include "lib/domain_model/Security.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/orderman_utils/orderman_utils.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
#include "lib/utils/PBUtils.h"
#include "lib/os/os.h"
namespace bts {
namespace orderman {
using namespace api::orderman;
enum class LeanOrder2::Status { WAITING_FOR_MD, WORKING, DONE };
struct LeanOrder2::PImpl {
double tick_size = 0;
Status status = Status::WAITING_FOR_MD;
Price initial_observed_price = Price(os::nan());
Price observed_price = Price(os::nan());
OrderKey child_key = null_order_key();
ChildTemplate *child_template = nullptr;
Book last_book;
};
LeanOrder2::LeanOrder2() : me(new PImpl()) {}
LeanOrder2::~LeanOrder2() {}
// initialization /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
OrderOpResult LeanOrder2::do_before_launch(){
auto &def = definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_lean());
// validate that lean is properly formed
if(is_attaching()){
}
else {
auto child_def = definition();
*child_def.mutable_custom_def() =
definition().custom_def().lean().child();
me->child_template = &(make_child_template(child_def));
}
return {};
}
void LeanOrder2::do_launch(){
bts_assert(me->child_template);
me->child_key = launch_child(*me->child_template, price(), quantity());
subscribe_book(SecurityKey(definition().security_key()));
}
void LeanOrder2::do_launch_attached(OrderKey, ChildState){
bts_assert(false);
}
// updates ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void LeanOrder2::do_update(const api::orderman::Update& update){
LOG(OrderMan, Diag, "LeanOrder order {} receiving update: {}",
order_key().value(), utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_lean()) {
terminate_error("Invalid update");
return;
}
Update child_update;
child_update.set_price(update.price());
child_update.set_quantity(update.quantity());
if (update.has_custom()) {
auto& lean_update = update.custom().lean();
*child_update.mutable_custom() = lean_update.child();
}
update_child(me->child_key, child_update);
switch (me->status){
case LeanOrder2::Status::WAITING_FOR_MD:
break;
case LeanOrder2::Status::WORKING:
_analyze_and_act();
break;
case LeanOrder2::Status::DONE:
break;
}
}
// callbacks //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void LeanOrder2::on_book(SecurityKey, const Book& book){
me->last_book=book;
switch (me->status){
case LeanOrder2::Status::WAITING_FOR_MD:
if(_opp_inside_market().size()!=0){
me->initial_observed_price=Price(_opp_inside_market().price());
me->observed_price=me->initial_observed_price;
me->status=LeanOrder2::Status::WORKING;
_analyze_and_act();
}
break;
case LeanOrder2::Status::WORKING:
_analyze_and_act();
break;
case LeanOrder2::Status::DONE:
break;
}
}
void LeanOrder2::on_child_change(OrderKey, const ChildState& st){
update_fill_state(st.total_fills, st.last_price);
switch (st.status) {
case ChildStatus::WORKING:
// if this update had fills and qty threshold is
// ratio, should it re-evaluate ?
return;
case ChildStatus::COMPLETED:
me->status=LeanOrder2::Status::DONE;
terminate_complete();
return;
case ChildStatus::CANCELED:
me->status=LeanOrder2::Status::DONE;
terminate_cancel("Unexpected child cancel");
return;
}
}
// helpers ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
spark::QuantityMeasure LeanOrder2::_cross_threshold() const {
return definition().custom_def().lean().cross_threshold();
}
uint32_t LeanOrder2::_max_chase_ticks() const {
return definition().custom_def().lean().max_chase_ticks();
}
api::orderman::LeanOrderDefinition::OnToward
LeanOrder2::_on_toward_behaviour() const {
return definition().custom_def().lean().trade_toward_behavior();
}
bool LeanOrder2::_book_moved_toward() const {
return securities::is_better(_opp_side(),
Price(_opp_inside_market().price()),
me->observed_price);
}
bool LeanOrder2::_book_moved_away() const {
return securities::is_better(_opp_side(),
me->observed_price,
Price(_opp_inside_market().price()));
}
md::PriceSize LeanOrder2::_opp_inside_market() const{
return me->last_book.side(_opp_side())[0];
}
api::Side LeanOrder2::_opp_side() const {
return securities::other_side(definition().side());
}
void LeanOrder2::_on_toward_me(){
switch (_on_toward_behaviour()) {
case LeanOrderDefinition_OnToward_CANCEL:
LOG(OrderMan, Diag,
"Canceling LeanOrder2 {} because opposite moved towards order",
order_key().value());
terminate_cancel("Opposite inside market moved towards order");
break;
case LeanOrderDefinition_OnToward_DETACH:
LOG(OrderMan, Diag,
"LeanOrder2 {} is done because opposite moved towards order",
order_key().value());
terminate_detach("Opposite inside market moved towards order");
break;
case LeanOrderDefinition_OnToward_REPRICE:
LOG(OrderMan, Diag,
"LeanOrder2 {} changing its target to {} because opposite moved "
"towards order",
order_key().value(), _opp_inside_market().price());
me->observed_price=Price(_opp_inside_market().price());
_try_cross();
break;
case LeanOrderDefinition_OnToward_NO_CHANGE:
LOG(OrderMan, Diag, "LeanOrder2 {} ignoring opposite move towards order.",
order_key().value());
// Change of behaviour : nothing == reprice
me->observed_price=Price(_opp_inside_market().price());
_try_cross();
break;
default:
bts_assert(false);
break;
}
}
void LeanOrder2::_analyze_and_act(){
bts_assert(me->status==LeanOrder2::Status::WORKING);
if (_book_moved_toward())
_on_toward_me();
else if (_book_moved_away()){
me->observed_price=Price(_opp_inside_market().price());
_try_cross();
}
else
_try_cross();
}
void LeanOrder2::_try_cross(){
md::PriceSize opp_inside=_opp_inside_market();
Quantity trigger_quantity = SpreaderUtils::_quantity_threshold(
_cross_threshold(), Quantity(quantity()));
if(opp_inside.size() != 0 &&
opp_inside.size() < trigger_quantity.value()){
// cross threshold is good, check
// for chase
uint32_t diff=std::abs(
securities::price_diff_to_ticks(opp_inside.price(),
me->initial_observed_price.value(),
me->tick_size));
if(diff <= _max_chase_ticks()){
// payup
Update child_update;
child_update.set_price(opp_inside.price());
child_update.set_quantity(quantity().value());
update_child(me->child_key, child_update);
}
}
}
} // namespace orderman
} // namespace bts