From 18eb28dfd2faf7b086fa3b42bafe2116df422213 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 22:08:03 -0400 Subject: [PATCH] waps --- src/lib/api/services/OrderService.proto | 20 +- .../orderman/events/OrderStateUpdateEvent.h | 12 + .../orderman/order_factory/OrderFactory.cpp | 5 + .../algo/algo_orders/bluex/TWAPAlgo.cpp | 227 ++++++++++++++++++ .../orders/algo/algo_orders/bluex/TWAPAlgo.h | 32 +++ src/lib/utils/AuthSessionManager.cpp | 90 +++++++ 6 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp create mode 100644 src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h create mode 100644 src/lib/utils/AuthSessionManager.cpp diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index ff6ac6872..7d01b5589 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -104,6 +104,11 @@ message CustomOrderDefinition ScratchOrderDefinition scratch = 31; ProRataOrderDefinition prorata = 32; + TWAPOrderDefinition twap = 100; + VWAPOrderDefinition vwap = 101; + POVOrderDefinition pov = 102; + ArrivalPriceOrderDefinition is = 103; + DynamicOrderDefinition dynamic = 1000; } } @@ -226,7 +231,7 @@ message OrderDuration { google.protobuf.Timestamp start_time = 10; oneof end_time { - //lldatetime when to conclude + //when to conclude google.protobuf.Timestamp end_timestamp = 20; //alternative to end_time, minutes from start time at which to stop uint32 duration_in_minutes = 30; @@ -270,6 +275,10 @@ message VWAPOrderDefinition { OrderAggression aggression = 20; VolumeDistribution volume_distribution = 30; } +message POVOrderDefinition { + OrderDuration duration = 10; + OrderAggression aggression = 20; +} message ArrivalPriceOrderDefinition { OrderDuration duration = 10; OrderAggression aggression = 20; @@ -332,6 +341,11 @@ message OrderState { ScratchOrderState scratch = 31; ProRataOrderState prorata = 32; + TWAPOrderState twap = 100; + VWAPOrderState vwap = 101; + POVOrderState pov = 102; + ArrivalPriceOrderState is = 103; + DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } } @@ -411,6 +425,10 @@ message ScratchOrderState {} message ProRataOrderState {} message DynamicOrderState {} message QueueHolderState {} +message TWAPOrderState {} +message VWAPOrderState {} +message POVOrderState {} +message ArrivalPriceOrderState {} enum OrderStatus { diff --git a/src/lib/orderman/events/OrderStateUpdateEvent.h b/src/lib/orderman/events/OrderStateUpdateEvent.h index 7cde10761..20ac81b0d 100644 --- a/src/lib/orderman/events/OrderStateUpdateEvent.h +++ b/src/lib/orderman/events/OrderStateUpdateEvent.h @@ -156,6 +156,18 @@ class OrderStateUpdateEvent : public mercury::Event { case api::orderman::OrderState::kProrata: result.mutable_prorata(); break; + case api::orderman::OrderState::kTwap: + result.mutable_twap(); + break; + case api::orderman::OrderState::kVwap: + result.mutable_vwap(); + break; + case api::orderman::OrderState::kPov: + result.mutable_pov(); + break; + case api::orderman::OrderState::kIs: + result.mutable_is(); + break; case api::orderman::OrderState::kDynamic: result.mutable_dynamic(); break; diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index abd576b37..ef96232f0 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -21,6 +21,11 @@ #include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h" #include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" #include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/TWAPAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/VWAPAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/POVAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/ArrivalPriceAlgo.h" + #include "lib/orderman/orders/book_algos/OCOOrder.h" #include "lib/orderman/orders/book_algos/OffsetOrder.h" #include "lib/orderman/orders/book_algos/PricerOrder.h" diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp new file mode 100644 index 000000000..0f04c7fb1 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp @@ -0,0 +1,227 @@ +#include "lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/algo/DynamicHelpers.h" + +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "lib/os/os.h" +#include "lib/utils/PBUtils.h" + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +enum class TWAPAlgo::Status { WAITING_FOR_MD, WAITING_TO_SCRATCH, SCRATCHED }; + +struct TWAPAlgo::PImpl +{ + IAlgoServices *algo; + OrderDefinition definition; + + Quantity scratch_threshold; + Price chase_diff; + Status status = Status::WAITING_FOR_MD; + OrderKey child_key = null_order_key(); + ChildTemplate *child_template = nullptr; + Book last_book; + Book book_at_inception; + Price scratch_price=Price(os::nan()); +}; + +TWAPAlgo::TWAPAlgo() : me(new PImpl()) {} +TWAPAlgo::~TWAPAlgo() {} +IAlgoServices &TWAPAlgo::algo() const { return *me->algo; } + +OrderOpResult TWAPAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) +{ + const auto& def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_scratch()); + // Validate the scratch part of the definition + auto &scratch = def.custom_def().scratch(); + if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) + { + return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Scratch order must have either a child order definition or an order to attach to"}; + } + if (!serv.is_attaching()) + { + // if we are not attaching create a child template so we can + // quickly launch our child when the time comes. + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().scratch().child(); + me->child_template = &(serv.make_child_template(child_def)); + } + return {}; +} + +void TWAPAlgo::do_launch(IAlgoServices &algo) +{ + me->algo = &algo; + update_scratch_threshold(); + bts_assert(me->child_template); + me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); + algo.subscribe_book(SecurityKey(algo.security().id())); +} + +void TWAPAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) +{ + me->algo = &algo; + update_scratch_threshold(); + me->child_key = k; + algo.subscribe_book(SecurityKey(algo.security().id())); + on_child_change(k, st); +} + +void TWAPAlgo::do_update(api::orderman::Update const &update) +{ + LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); + + if (update.has_custom() && !update.custom().has_scratch()) + { + algo().terminate_error("Invalid update"); + return; + } + + Update child_update; + Price price_to_pass = get_price_to_pass(Price(update.price())); + child_update.set_price(price_to_pass.value()); + child_update.set_quantity(update.quantity()); + if (update.has_custom()) + { + update_scratch_threshold(); + auto &scratch_update = update.custom().scratch(); + *child_update.mutable_custom() = scratch_update.child(); + } + algo().update_child(me->child_key, child_update); + + // If we were not yet scratched it is possible we now can, since we + // could have changed the scratch_threshold + if (me->status == Status::WAITING_TO_SCRATCH) + try_to_scratch(); +} + +void TWAPAlgo::on_book(SecurityKey, Book const &b) +{ + me->last_book = b; + + LOG(OrderMan, Diag, "ScratchOrder order {} on book. BID: {}@{} ASK: {}@{}", + algo().order_key().value(), b.side(api::SIDE_BUY)[0].size(), + b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), + b.side(api::SIDE_SELL)[0].price()); + + switch (me->status) + { + case Status::WAITING_FOR_MD: + { + auto other_side = securities::other_side(algo().definition().side()); + auto &other_side_top = me->last_book.side(other_side)[0]; + + if (other_side_top.size() > 0) + { + me->status = Status::WAITING_TO_SCRATCH; + me->book_at_inception = b; + LOG(OrderMan, Diag, + "ScratchOrder order {} receiving first book: {} Target: {}", + algo().order_key().value(), me->scratch_threshold.value(), + other_side_top.price()); + try_to_scratch(); + } + } break; + case Status::SCRATCHED: + break; + case Status::WAITING_TO_SCRATCH: + try_to_scratch(); + break; + } +} + +void TWAPAlgo::try_to_scratch() +{ + bts_assert(me->status == Status::WAITING_TO_SCRATCH); + auto my_side = algo().definition().side(); + auto other_side = securities::other_side(my_side); + + Quantity my_quan; + auto &my_side_psz = me->last_book.side(my_side)[0]; + auto &other_side_psz = me->last_book.side(other_side)[0]; + auto other_side_price = Price(other_side_psz.price()); + auto inception_other_side_price = Price(me->book_at_inception.side(other_side)[0].price()); + + bool market_moved = !securities::is_equal_or_better(other_side, other_side_price, inception_other_side_price); + bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); + if (market_moved || (on_market && my_side_psz.size() <= me->scratch_threshold.value()) ) + { + Price scratch_price = securities::worsen_price(other_side, inception_other_side_price, me->chase_diff); + LOG(OrderMan, Diag, "ScratchOrder order {} scratching: {}@{}", + algo().order_key().value(), algo().quantity().value(), scratch_price); + + // Time to scratch + Update child_update; + me->scratch_price = scratch_price; + child_update.set_price(scratch_price.value()); + //we still want to try to scratch all of it + child_update.set_quantity(algo().quantity().value()); + algo().update_child(me->child_key, child_update); + me->status = Status::SCRATCHED; + } + else + { + LOG(OrderMan, Diag, + "ScratchOrder order {} still waiting to scratch. Order: {}@{} " + "My side: {}@{}" + "Thresh : {}@{}", + algo().order_key().value(), algo().quantity().value(), algo().price().value(), + my_side_psz.size(), my_side_psz.price(), + me->scratch_threshold.value(), my_quan); + } +} + +Price TWAPAlgo::get_price_to_pass (Price price) const +{ + Price rv; + switch (me->status) { + case Status::WAITING_FOR_MD: + case Status::WAITING_TO_SCRATCH: + rv = price; + break; + case Status::SCRATCHED: + rv = me->scratch_price; + break; + } + return rv; +} + +void TWAPAlgo::update_scratch_threshold() +{ + if (algo().definition().custom_def().scratch().scratch_threshold().quantity_case() == + bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + { + algo().terminate_error("Invalid quantity, terminating"); + return; + } + me->scratch_threshold = SpreaderUtils::_quantity_threshold( + algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); + me->chase_diff = Price(securities::tick_size( + algo().security()).value() * algo().definition().custom_def().scratch().chase_ticks()); +} + +void TWAPAlgo::on_child_change(OrderKey, ChildState const &st) +{ + algo().update_fill_state(st.total_fills, st.last_price); + switch (st.status) + { + case ChildStatus::WORKING: + return; + case ChildStatus::COMPLETED: + algo().complete(); + return; + case ChildStatus::CANCELED: + algo().cancel("Unexpected child cancel"); + return; + } +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h new file mode 100644 index 000000000..4b6112fe0 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h @@ -0,0 +1,32 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { + +class TWAPAlgo : public IAlgoOrder +{ +public: + TWAPAlgo(); + ~TWAPAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices&) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices&,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: + enum class Status; + struct PImpl; + std::unique_ptr me; + IAlgoServices& algo() const; + + void update_scratch_threshold(); + void try_to_scratch(); + Price get_price_to_pass (Price price) const; +}; + +}} diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp new file mode 100644 index 000000000..3c8f1874c --- /dev/null +++ b/src/lib/utils/AuthSessionManager.cpp @@ -0,0 +1,90 @@ +#ifndef _WIN32 +#include "AuthSessionManager.h" + +#include "lib/utils/datetime.h" +#include "lib/utils/crypto/KeyCodeHelper.h" +#include "lib/utils/crypto/CryptoInit.h" +#include "lib/utils/crypto/PrivateKey.h" +#include "lib/utils/crypto/PublicKey.h" + +#include "lib/logging/logging.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/helper_macros.h" +#include "lib/config/config.h" +#include "lib/config/protos/keycode.pb.h" + +#include +#include +#include +#include + +namespace bts { +namespace api { +namespace auth { + +const std::string AuthSessionManager::PUBLIC_KEY = +"-----BEGIN PUBLIC KEY-----\n" +"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" +"gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue" +"rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy" +"UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES" +"mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2" +"92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9" +"3QIDAQAB" +"\n-----END PUBLIC KEY-----\n"; + +AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() +{ + const auto& ekc = config::get(); + auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY); + bts::config::KeyCode kc; + auto valid = bts::crypto::decrypt_keycode(*pub, ekc, &kc); + bts_assert_msg(valid, "Your BTS Keycode is invalid"); + + _nusers = kc.nusers(); + _expiration = kc.expiration(); + + auto current = datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0; + if(!current) + LOG(Gen,Error, "Your BTS Keycode is expired"); + bts_assert_msg(current, "Your BTS Keycode is EXPIRED"); +} + +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) +{ + if(tok.wheel()) + return true; + auto it = _map.find(tok.id()); + if(it != _map.end()) + { + _map[tok.id()] = Session{tok}; + return true; + } + else if(_map.size() < _nusers) + { + _map[tok.id()] = Session{tok}; + return true; + } + return false; +} + +bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) +{ + auto now = datetime::ll_now(); + if(datetime::ll_diff_in_us(now, _expiration) > 0) + { + if(tok.wheel()) + return true; + auto it = _map.find(tok.id()); + if(it != _map.end()) + { + //return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) //this is probably slow + return tok.date() == it->second.token.date() && //ids are equal + datetime::ll_diff_in_us(tok.date(), _expiration) > 0; + } + } + return false; +} + +}}} +#endif \ No newline at end of file