Lean adhering to the new API

This commit is contained in:
Taha Afzal committed 2019-04-23 20:02:40 -04:00
1 parent d4ee0db19b
commit d8f118cfe9
2 files changed
+183 -208

No files matched your search

+158 -189
View File
@@ -8,243 +8,212 @@
#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 { WORKING, DONE };
enum class LeanOrder2::Status { WAITING_FOR_MD, WORKING, DONE };
struct LeanOrder2::PImpl {
Status status = Status::WORKING;
Price target_price = Price(os::nan());
uint32_t num_jumps = 0;
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() {}
OrderOpResult LeanOrder2::do_on_init_new(const PreActivationOrderServices&) {
// initialization /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
OrderOpResult LeanOrder2::do_before_launch(){
auto &def = definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_lean());
// validate the top part of the definition
auto &lean = def.custom_def().lean();
if (!lean.has_child() &&
OrderKey(def.attach_target_order_key()) == null_order_key()) {
return OrderOpResult{
ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Lean order must have either a child order definition or an "
"order to attach to"};
// validate that lean is properly formed
if(is_attaching()){
}
// set the custom component of our state
state().mutable_lean();
return OrderOpResult{};
}
void LeanOrder2::do_on_ready(OrderServices&,bool is_attached) {
bts_assert(me->status == Status::WORKING);
me->target_price = Price(other_side_top_of_book().price());
Quantity trigger_quantity = SpreaderUtils::_quantity_threshold(
jump_threshold(), Quantity(state().quantity()));
LOG(OrderMan, Diag,
"Starting LeanOrder2 {}, {} {}@{} target price: {} trigger quantity:{}",
key().value(), securities::side_name(definition().side()),
state().quantity(), state().price(), me->target_price.value(),
trigger_quantity.value());
if (!is_attached) {
api::orderman::OrderDefinition child_def = definition();
else {
auto child_def = definition();
*child_def.mutable_custom_def() =
definition().custom_def().lean().child();
launch_child(child_def);
LOG(OrderMan, Diag, "LeanOrder2 {} launching child", key().value());
return;
} else {
LOG(OrderMan, Diag, "LeanOrder2 {} attaching to child", key().value());
me->child_template = &(make_child_template(child_def));
}
try_jump();
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));
void LeanOrder2::do_on_book(Book const &) { try_jump(); }
void LeanOrder2::try_jump() {
if (me->status != Status::WORKING)
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);
if (book_moved_toward_me())
on_toward_me_move();
else {
Quantity trigger_quantity = SpreaderUtils::_quantity_threshold(
jump_threshold(), Quantity(state().quantity()));
auto book_moved_away = book_moved_away_from_me();
auto trigger_activated =
other_side_top_of_book().size() <= trigger_quantity.value();
if (book_moved_away)
LOG(OrderMan, Diag, "LeanOrder2 {} book moved away from {} to {}",
key().value(), me->target_price.value(),
other_side_top_of_book().price());
if (trigger_activated)
LOG(OrderMan, Diag,
"LeanOrder2 {} trigger condition met. Other side size is {} "
"whichs less or equal than {} ",
key().value(), other_side_top_of_book().size(),
trigger_quantity.value());
if (book_moved_away || trigger_activated)
payup();
switch (me->status){
case LeanOrder2::Status::WAITING_FOR_MD:
break;
case LeanOrder2::Status::WORKING:
_analyze_and_act();
break;
case LeanOrder2::Status::DONE:
break;
}
}
void LeanOrder2::on_toward_me_move() {
switch (on_toward_behaviour()) {
// 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:
cancel();
LOG(OrderMan, Diag,
"Canceling LeanOrder2 {} because opposite moved towards order",
key().value());
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",
key().value());
done("Opposite market moved towards");
order_key().value());
terminate_detach("Opposite inside market moved towards order");
break;
case LeanOrderDefinition_OnToward_REPRICE:
me->target_price = Price(other_side_top_of_book().price());
case LeanOrderDefinition_OnToward_REPRICE:
LOG(OrderMan, Diag,
"LeanOrder2 {} changing its target to {} because opposite moved "
"towards order",
key().value(), me->target_price.value());
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.",
key().value());
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::payup() {
bts_assert(me->status == Status::WORKING);
LOG(OrderMan, Diag, "Paying up in LeanOrder2, order: {}", key().value());
auto new_price = me->target_price;
LOG(OrderMan, Diag, "Payup in LeanOrder2 {}, new price: {}", key().value(),
new_price.value());
update_child(new_price, Quantity(state().quantity()));
// Now find ou next target, if any
for (auto const &level : last_book().side(other_side()))
if (level.size() == 0)
break;
else if (securities::is_better(my_side(), Price(level.price()),
new_price))
me->target_price = Price(level.price());
// If we could not find a new target, bail out_
if (!securities::is_better(my_side(), me->target_price, new_price)) {
LOG(OrderMan, Diag,
"Could not find opposite market to set new target price in "
"LeanOrder2, order: {} is done.",
key().value());
done("Could not find new opposite market");
return;
}
// We successfully jumped, increment our count and if it was our
// last jump transition to done
me->num_jumps++;
LOG(OrderMan, Diag, "LeanOrder2 {} jumped {} out of {}", key().value(),
me->num_jumps, max_jumps());
if (me->num_jumps >= max_jumps()) {
LOG(OrderMan, Diag,
"Reached limit of jumps in LeanOrder2 {}. Order is done.",
key().value());
done("Max number of jumps reached");
return;
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();
}
OrderOpResult LeanOrder2::do_on_update(api::orderman::Update const &update) {
// validate update
if (update.has_custom() && !update.custom().has_lean())
return {ORDER_ERROR_INVALID_OP,
"Update is not valid for this order type"};
LOG(OrderMan, Diag, "LeanOrder2 order {} receiving update: {}",
key().value(), utils::pb::to_string(update));
// 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 &lean_update = update.custom().lean();
*child_update.mutable_custom() = lean_update.child();
}
update_child(child_update);
if (me->status == Status::WORKING) {
// re-eval given that our parameters might have changed
try_jump();
}
return OrderOpResult();
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);
}
}
}
void LeanOrder2::done(std::string const&msg)
{
bts_assert(me->status == Status::WORKING);
me->status = Status::DONE;
detach(msg);
}
bool LeanOrder2::book_moved_toward_me() const {
auto other_side_price = Price(other_side_top_of_book().price());
return securities::is_better(other_side(), other_side_price,
me->target_price);
}
bool LeanOrder2::book_moved_away_from_me() const {
auto other_side_price = Price(other_side_top_of_book().price());
return securities::is_better(other_side(), me->target_price,
other_side_price);
}
uint32_t LeanOrder2::max_jumps() const {
return definition().custom_def().lean().max_chase_ticks();
}
spark::QuantityMeasure LeanOrder2::jump_threshold() const {
return definition().custom_def().lean().cross_threshold();
}
api::orderman::LeanOrderDefinition::OnToward
LeanOrder2::on_toward_behaviour() const {
return definition().custom_def().lean().trade_toward_behavior();
}
} // namespace orderman
} // namespace bts
+25 -19
View File
@@ -1,34 +1,40 @@
#pragma once
#include "lib/orderman/orders/book_algos/BookOrderBase.h"
#include "lib/orderman/orders/algo/AlgoOrder.h"
#include "lib/md/PriceSize.h"
#include "lib/api/services/OrderService.server.pb.h"
namespace bts {
namespace orderman {
class LeanOrder2 : public BookOrderBase {
public:
class LeanOrder2 : public AlgoOrder {
public:
LeanOrder2();
~LeanOrder2();
public:
OrderOpResult do_before_launch() override;
void do_launch() override;
void do_launch_attached(OrderKey, ChildState) override;
void do_update(api::orderman::Update const &) override;
void on_book(SecurityKey, Book const &) override;
void on_child_change(OrderKey, ChildState const &) override;
private:
OrderOpResult do_on_init_new(const PreActivationOrderServices&) override;
void do_on_ready(OrderServices&,bool is_attached) override;
void do_on_book(Book const &) override;
OrderOpResult do_on_update(api::orderman::Update const &) override;
struct PImpl;
enum class Status;
spark::QuantityMeasure jump_threshold() const;
uint32_t max_jumps() const ;
api::orderman::LeanOrderDefinition::OnToward on_toward_behaviour() const;
bool book_moved_toward_me() const;
bool book_moved_away_from_me() const;
void on_toward_me_move();
void try_jump();
void payup();
void done(std::string const&msg);
std::unique_ptr<PImpl> me;
spark::QuantityMeasure _cross_threshold() const;
uint32_t _max_chase_ticks() const;
api::orderman::LeanOrderDefinition::OnToward _on_toward_behaviour() const;
bool _book_moved_toward() const;
bool _book_moved_away() const;
md::PriceSize _opp_inside_market() const;
api::Side _opp_side() const;
void _analyze_and_act();
void _try_cross();
void _on_toward_me();
private:
std::unique_ptr<PImpl> me;
};
} // namespace orderman