Moved join, cover, iceberg to refactored API.

This commit is contained in:
Pedro Pinto committed 2019-04-24 17:43:31 -04:00
1 parent 80b19b616a
commit bb14b8ca3d
17 files changed
+377 -552

No files matched your search

+1 -1
View File
@@ -1 +1 @@
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderLine truncated
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderLine truncated
@@ -10,14 +10,14 @@
// order types
#include "lib/orderman/order_factory/DynamicOrderRegistry.h"
#include "lib/orderman/orders/DynamicOrder.h"
#include "lib/orderman/orders/IcebergOrder.h"
#include "lib/orderman/orders/NativeOrder.h"
#include "lib/orderman/orders/TimeBoxOrder.h"
#include "lib/orderman/orders/TriggerOrder.h"
#include "lib/orderman/orders/algo/AlgoWrapperOrder.h"
#include "lib/orderman/orders/algo/algo_orders/CoverAlgo.h"
#include "lib/orderman/orders/algo/algo_orders/JoinAlgo.h"
#include "lib/orderman/orders/algo/algo_orders/TopAlgo.h"
#include "lib/orderman/orders/book_algos/CoverOrder.h"
#include "lib/orderman/orders/book_algos/JoinOrder.h"
#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h"
#include "lib/orderman/orders/book_algos/LeanOrder2.h"
#include "lib/orderman/orders/book_algos/OCOOrder.h"
#include "lib/orderman/orders/book_algos/OffsetOrder.h"
@@ -148,7 +148,8 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
order_ptr.reset(new NativeOrder());
break;
case CustomOrderDefinition::kIceberg:
order_ptr.reset(new IcebergOrder());
order_ptr.reset(
new orderman::AlgoWrapperOrder(std::make_unique<IcebergAlgo>()));
break;
case CustomOrderDefinition::kSpreader:
order_ptr.reset(new orderman::SpreaderOrder());
@@ -163,7 +164,8 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
order_ptr.reset(new orderman::LeanOrder2());
break;
case CustomOrderDefinition::kJoin:
order_ptr.reset(new orderman::JoinOrder());
order_ptr.reset(
new orderman::AlgoWrapperOrder(std::make_unique<JoinAlgo>()));
break;
case CustomOrderDefinition::kTop:
order_ptr.reset(
@@ -173,7 +175,8 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
order_ptr.reset(new orderman::TriggerOrder());
break;
case CustomOrderDefinition::kCover:
order_ptr.reset(new orderman::CoverOrder());
order_ptr.reset(
new orderman::AlgoWrapperOrder(std::make_unique<CoverAlgo>()));
break;
case CustomOrderDefinition::kOffset:
order_ptr.reset(new orderman::OffsetOrder());
-34
View File
@@ -1,34 +0,0 @@
#pragma once
#include "lib/orderman/orders/algo/AlgoOrder.h"
#include "lib/utils/PBUtils.h"
namespace bts {
namespace orderman {
// IcebergOrders split a large order into small pieces typically to
// avoid moving the market or reduce risk.
class IcebergOrder : public AlgoOrder {
public:
IcebergOrder();
~IcebergOrder();
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_child_change(OrderKey, ChildState const &) override;
void on_timer() override;
private:
enum class Status;
struct PImpl;
std::unique_ptr<PImpl> me;
void init_child_template(api::orderman::CustomOrderDefinition const &);
void send_next_order();
Quantity remaining_quantity() const;
};
} // namespace orderman
} // namespace bts
@@ -0,0 +1,156 @@
#include "lib/orderman/orders/algo/algo_orders/CoverAlgo.h"
#include "lib/domain_model/Security.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "lib/utils/PBUtils.h"
namespace bts {
namespace orderman {
using namespace api::orderman;
struct CoverAlgo::PImpl {
IAlgoServices *algo;
OrderKey child_key = null_order_key();
Price price_improvement_delta;
ChildTemplate *child_template = nullptr;
ChildTemplate *hedge_template = nullptr;
};
CoverAlgo::CoverAlgo() : me(new PImpl()) {}
CoverAlgo::~CoverAlgo() {}
IAlgoServices &CoverAlgo::algo() const { return *me->algo; }
OrderOpResult CoverAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
auto &def = serv.definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_cover());
// Validate the cover part of the definition
auto &cover = def.custom_def().cover();
if (!cover.has_child() &&
OrderKey(def.attach_target_order_key()) == null_order_key()) {
return OrderOpResult{
ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Cover order must have either a child order definition or an "
"order to attach to"};
}
if (!cover.has_hedge())
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Cover order must have a hedge order definition"};
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().cover().child();
me->child_template = &(serv.make_child_template(child_def));
}
init_hedge_template(serv,cover.hedge());
set_price_improvement_delta(cover);
return {};
}
void CoverAlgo::do_launch(IAlgoServices &algo) {
me->algo = &algo;
LOG(OrderMan, Diag, "Starting CoverAlgo {}, {} {}@{}. Will cover {} ticks",
algo.order_key().value(),
securities::side_name(algo.definition().side()), algo.quantity().value(),
algo.price().value(),
algo.definition().custom_def().cover().hedge_ticks());
LOG(OrderMan, Diag, "CoverAlgo {} launching child",
algo.order_key().value());
me->child_key =
algo.launch_child(*me->child_template, algo.price(), algo.quantity());
}
void CoverAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k,
ChildState st) {
me->algo = &algo;
me->child_key = k;
on_child_change(k, st);
}
void CoverAlgo::on_child_change(OrderKey k, ChildState const &st) {
bts_assert(k == me->child_key); // We are not subscribed to hedge changes
if (st.total_fills.value() > algo().total_fills().value()) {
auto hedge_qty =
Quantity(st.total_fills.value() - algo().total_fills().value());
auto hedge_price =
securities::improve_price(algo().definition().side(), st.last_price,
me->price_improvement_delta);
algo().launch_child(*me->hedge_template, hedge_price, hedge_qty,
false); // do not subscribe to hedge changes
LOG(OrderMan, Diag,
"CoverAlgo {}, launched new hedge in response to child fill. Hedge: "
"{} {}@{}",
algo().order_key().value(),
securities::side_name(algo().definition().side()), hedge_qty.value(),
hedge_price.value());
}
algo().update_fill_state(st.total_fills, st.last_price);
switch (st.status) {
case ChildStatus::WORKING:
return;
case ChildStatus::COMPLETED:
algo().detach_all_children();
algo().complete();
return;
case ChildStatus::CANCELED:
algo().terminate_error("Unexpected child cancel");
return;
}
}
void CoverAlgo::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "CoverAlgo order {} receiving update: {}",
algo().order_key().value(), utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_cover()) {
algo().terminate_error("Invalid update");
return;
}
Update child_update;
child_update.set_price(update.price());
child_update.set_quantity(update.quantity());
// process custom part of the udpate if any
if (update.has_custom()) {
auto &cover_update = update.custom().cover();
set_price_improvement_delta(cover_update);
if (cover_update.has_child()) {
*child_update.mutable_custom() = cover_update.child();
}
if (cover_update.has_hedge())
init_hedge_template(algo(),cover_update.hedge());
}
algo().update_child(me->child_key, child_update);
}
void orderman::CoverAlgo::init_hedge_template(
IBeforeLaunchAlgoServices &algo,
api::orderman::CustomOrderDefinition const &hedge_cdef) {
auto hedge_def = algo.definition();
hedge_def.set_side(securities::other_side(algo.definition().side()));
hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG);
hedge_def.clear_attach_target_order_key();
*hedge_def.mutable_custom_def() = hedge_cdef;
me->hedge_template = &(algo.make_child_template(hedge_def));
}
void CoverAlgo::set_price_improvement_delta(
api::orderman::CoverOrderDefinition const &cover) {
me->price_improvement_delta =
Price(securities::tick_size(algo().security()).value() * cover.hedge_ticks());
}
} // namespace orderman
} // namespace bts
@@ -0,0 +1,33 @@
#pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
namespace bts {
namespace orderman {
// Cover initializes the child order as normal. Fills trigger hedge
// orders to the opposite side of the market, n ticks away.
class CoverAlgo : public IAlgoOrder {
public:
CoverAlgo();
~CoverAlgo();
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_child_change(OrderKey, ChildState const &) override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
IAlgoServices &algo() const;
void
set_price_improvement_delta(api::orderman::CoverOrderDefinition const &);
void init_hedge_template(IBeforeLaunchAlgoServices &,
api::orderman::CustomOrderDefinition const &);
};
} // namespace orderman
} // namespace bts
@@ -1,8 +1,10 @@
#include "lib/orderman/orders/IcebergOrder.h"
#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_chrono.h"
namespace bts {
@@ -10,9 +12,10 @@ namespace orderman {
using namespace api::orderman;
enum class IcebergOrder::Status { PAUSED, WORKING };
enum class IcebergAlgo::Status { PAUSED, WORKING };
struct IcebergOrder::PImpl {
struct IcebergAlgo::PImpl {
IAlgoServices *algo;
OrderKey child_key = null_order_key();
ChildTemplate *child_template = nullptr;
TimeStamp start_of_pause = TimeStamp();
@@ -21,11 +24,12 @@ struct IcebergOrder::PImpl {
Status status = Status::PAUSED;
};
IcebergOrder::IcebergOrder() : me(new PImpl()) {}
IcebergOrder::~IcebergOrder() {}
IcebergAlgo::IcebergAlgo() : me(new PImpl()) {}
IcebergAlgo::~IcebergAlgo() {}
IAlgoServices &IcebergAlgo::algo() const { return *me->algo; }
OrderOpResult IcebergOrder::do_before_launch() {
auto &def = definition();
OrderOpResult IcebergAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
auto &def = serv.definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_iceberg());
@@ -44,45 +48,49 @@ OrderOpResult IcebergOrder::do_before_launch() {
// Create a child template so we can quickly launch our child when
// the time comes.
init_child_template(definition().custom_def().iceberg().child());
init_child_template(serv, def.custom_def().iceberg().child());
return {};
}
void IcebergOrder::do_launch() {
void IcebergAlgo::do_launch(IAlgoServices &algo) {
me->algo = &algo;
LOG(OrderMan, Diag, "Starting Iceberg Order {}. {} {}@{} Def: {}",
order_key().value(), securities::side_name(definition().side()),
quantity().value(), price().value(), utils::pb::to_string(definition()));
algo.order_key().value(),
securities::side_name(algo.definition().side()), algo.quantity().value(),
algo.price().value(), utils::pb::to_string(algo.definition()));
if (me->pause_between_orders_ms > 0)
subscribe_timer();
algo.subscribe_timer();
me->status = Status::PAUSED;
on_timer();
}
void IcebergOrder::do_launch_attached(OrderKey k, ChildState st) {
void IcebergAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k,
ChildState st) {
me->algo = &algo;
LOG(OrderMan, Diag,
"Starting Iceberg Order {} attached to child: {} {} {}@{} Def: {}",
order_key().value(), k.value(),
securities::side_name(definition().side()), quantity().value(),
price().value(), utils::pb::to_string(definition()));
algo.order_key().value(), k.value(),
securities::side_name(algo.definition().side()), algo.quantity().value(),
algo.price().value(), utils::pb::to_string(algo.definition()));
if (me->pause_between_orders_ms > 0)
subscribe_timer();
algo.subscribe_timer();
me->child_key = k;
me->status = Status::WORKING;
on_child_change(k, st);
}
void IcebergOrder::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "IcebergOrder order {} receiving update: {}",
order_key().value(), utils::pb::to_string(update));
void IcebergAlgo::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "IcebergAlgo order {} receiving update: {}",
algo().order_key().value(), utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_iceberg()) {
terminate_error("Invalid update");
algo().terminate_error("Invalid update");
return;
}
if (update.quantity() < total_fills().value()) {
complete();
if (update.quantity() < algo().total_fills().value()) {
algo().complete();
return;
}
@@ -94,7 +102,7 @@ void IcebergOrder::do_update(api::orderman::Update const &update) {
auto &iceberg_update = update.custom().iceberg();
if (iceberg_update.show_quantity() == 0) {
terminate_error("Invalid update");
algo().terminate_error("Invalid update");
return;
}
@@ -103,27 +111,28 @@ void IcebergOrder::do_update(api::orderman::Update const &update) {
bool should_have_timer = iceberg_update.pause_between_orders_ms() > 0;
if (had_timer && !should_have_timer)
unsubscribe_timer();
algo().unsubscribe_timer();
else if (!had_timer && should_have_timer)
subscribe_timer();
algo().subscribe_timer();
if (iceberg_update.has_child()) {
*child_update.mutable_custom() = iceberg_update.child();
init_child_template(iceberg_update.child());
init_child_template(algo(), iceberg_update.child());
}
}
child_update.set_quantity(remaining_quantity().value());
if (me->status == Status::WORKING)
update_child(me->child_key, child_update);
algo().update_child(me->child_key, child_update);
}
void IcebergOrder::on_child_change(OrderKey k, ChildState const &st) {
void IcebergAlgo::on_child_change(OrderKey k, ChildState const &st) {
if (k == me->child_key) {
update_fill_state(Quantity(total_fills().value() + st.last_fills.value()),
st.last_price);
algo().update_fill_state(
Quantity(algo().total_fills().value() + st.last_fills.value()),
st.last_price);
if (total_fills().value() >= quantity().value()) {
complete();
if (algo().total_fills().value() >= algo().quantity().value()) {
algo().complete();
return;
}
switch (st.status) {
@@ -136,45 +145,47 @@ void IcebergOrder::on_child_change(OrderKey k, ChildState const &st) {
on_timer();
return;
case ChildStatus::CANCELED:
terminate_error("Unexpected child cancel");
algo().terminate_error("Unexpected child cancel");
return;
}
}
}
void IcebergOrder::on_timer() {
void IcebergAlgo::on_timer() {
if (me->status == Status::PAUSED &&
ms_diff(Timer::now(), me->start_of_pause) >=
me->pause_between_orders_ms) {
me->pause_between_orders_ms) {
me->status = Status::WORKING;
send_next_order();
}
}
void IcebergOrder::send_next_order() {
void IcebergAlgo::send_next_order() {
bts_assert(me->status == Status::WORKING);
bts_assert(quantity().value() > total_fills().value());
bts_assert(algo().quantity().value() > algo().total_fills().value());
me->child_key =
launch_child(*me->child_template, price(), remaining_quantity());
algo().launch_child(*me->child_template, algo().price(), remaining_quantity());
LOG(OrderMan, Info,
"Iceberg order {} creating new child order {} with size "
"{} price {}. Filled {} so far out of {}",
order_key().value(), me->child_key.value(), remaining_quantity().value(),
price().value(), total_fills().value(), quantity().value());
algo().order_key().value(), me->child_key.value(),
remaining_quantity().value(), algo().price().value(), algo().total_fills().value(),
algo().quantity().value());
}
void orderman::IcebergOrder::init_child_template(
void orderman::IcebergAlgo::init_child_template(
IBeforeLaunchAlgoServices &serv,
api::orderman::CustomOrderDefinition const &iceberg_def) {
auto child_def = definition();
auto child_def = serv.definition();
*child_def.mutable_custom_def() = iceberg_def;
me->child_template = &(make_child_template(child_def));
me->child_template = &(serv.make_child_template(child_def));
}
Quantity IcebergOrder::remaining_quantity() const {
bts_assert(quantity().value() > total_fills().value());
auto remaining = quantity().value() - total_fills().value();
Quantity IcebergAlgo::remaining_quantity() const {
bts_assert(algo().quantity().value() > algo().total_fills().value());
auto remaining = algo().quantity().value() - algo().total_fills().value();
return Quantity(std::min(remaining, me->show_quantity.value()));
}
@@ -0,0 +1,36 @@
#pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
namespace bts {
namespace orderman {
// Iceberg splits a large order into small pieces typically to avoid
// moving the market or reduce risk.
class IcebergAlgo : public IAlgoOrder {
public:
IcebergAlgo();
~IcebergAlgo();
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_child_change(OrderKey, ChildState const &) override;
void on_timer() override;
private:
enum class Status;
struct PImpl;
std::unique_ptr<PImpl> me;
IAlgoServices &algo() const;
void init_child_template(IBeforeLaunchAlgoServices&,
api::orderman::CustomOrderDefinition const &);
void send_next_order();
Quantity remaining_quantity() const;
};
} // namespace orderman
} // namespace bts
@@ -1,4 +1,5 @@
#include "lib/orderman/orders/book_algos/JoinOrder.h"
#include "lib/orderman/orders/algo/algo_orders/JoinAlgo.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
@@ -9,9 +10,10 @@ namespace orderman {
using namespace api::orderman;
enum class JoinOrder::Status { WAITING_FOR_MD, WAITING_TO_JOIN, JOINED };
enum class JoinAlgo::Status { WAITING_FOR_MD, WAITING_TO_JOIN, JOINED };
struct JoinOrder::PImpl {
struct JoinAlgo::PImpl {
IAlgoServices *algo;
Quantity min_quantity_to_join;
Status status = Status::WAITING_FOR_MD;
OrderKey child_key = null_order_key();
@@ -20,11 +22,12 @@ struct JoinOrder::PImpl {
Book last_book;
};
JoinOrder::JoinOrder() : me(new PImpl()) {}
JoinOrder::~JoinOrder() {}
JoinAlgo::JoinAlgo() : me(new PImpl()) {}
JoinAlgo::~JoinAlgo() {}
IAlgoServices &JoinAlgo::algo() const { return *me->algo; }
OrderOpResult JoinOrder::do_before_launch() {
auto &def = definition();
OrderOpResult JoinAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) {
auto &def = serv.definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_join());
@@ -38,38 +41,41 @@ OrderOpResult JoinOrder::do_before_launch() {
"order to attach to"};
}
if (!is_attaching()) {
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 = definition();
*child_def.mutable_custom_def() =
definition().custom_def().join().child();
me->child_template = &(make_child_template(child_def));
auto child_def = def;
*child_def.mutable_custom_def() = def.custom_def().join().child();
me->child_template = &(serv.make_child_template(child_def));
}
return {};
}
void JoinOrder::do_launch() {
void JoinAlgo::do_launch(IAlgoServices &algo) {
me->algo = &algo;
update_join_threshold();
bts_assert(me->child_template);
me->child_key = launch_child(*me->child_template, price(), quantity());
subscribe_book(SecurityKey(definition().security_key()));
me->child_key =
algo.launch_child(*me->child_template, algo.price(), algo.quantity());
algo.subscribe_book(SecurityKey(algo.security().id()));
}
void JoinOrder::do_launch_attached(OrderKey k, ChildState st) {
void JoinAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k,
ChildState st) {
me->algo = &algo;
update_join_threshold();
me->child_key = k;
subscribe_book(SecurityKey(definition().security_key()));
algo.subscribe_book(SecurityKey(algo.security().id()));
on_child_change(k, st);
}
void JoinOrder::do_update(api::orderman::Update const &update) {
void JoinAlgo::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "JoinOrder order {} receiving update: {}",
order_key().value(), utils::pb::to_string(update));
algo().order_key().value(), utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_join()) {
terminate_error("Invalid update");
algo().terminate_error("Invalid update");
return;
}
@@ -81,7 +87,7 @@ void JoinOrder::do_update(api::orderman::Update const &update) {
auto &join_update = update.custom().join();
*child_update.mutable_custom() = join_update.child();
}
update_child(me->child_key, child_update);
algo().update_child(me->child_key, child_update);
// If we were not yet joined it is possible we now can, since we
// could have changed the min_quantity_to_join
@@ -89,17 +95,17 @@ void JoinOrder::do_update(api::orderman::Update const &update) {
try_to_join();
}
void JoinOrder::on_book(SecurityKey, Book const &b) {
void JoinAlgo::on_book(SecurityKey, Book const &b) {
me->last_book = b;
LOG(OrderMan, Diag, "JoinOrder order {} on book. BID: {}@{} ASK: {}@{}",
order_key().value(), b.side(api::SIDE_BUY)[0].size(),
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(definition().side());
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) {
@@ -107,7 +113,7 @@ void JoinOrder::on_book(SecurityKey, Book const &b) {
me->book_at_inception = b;
LOG(OrderMan, Diag,
"JoinOrder order {} receiving first book: {} Target: {}",
order_key().value(), me->min_quantity_to_join.value(),
algo().order_key().value(), me->min_quantity_to_join.value(),
other_side_top.price());
try_to_join();
}
@@ -120,10 +126,10 @@ void JoinOrder::on_book(SecurityKey, Book const &b) {
}
}
void JoinOrder::try_to_join() {
void JoinAlgo::try_to_join() {
bts_assert(me->status == Status::WAITING_TO_JOIN);
auto my_side = definition().side();
auto my_side = algo().definition().side();
auto other_side = securities::other_side(my_side);
auto &my_side_psz = me->last_book.side(my_side)[0];
auto &opposite_at_inception_psz = me->book_at_inception.side(other_side)[0];
@@ -135,41 +141,42 @@ void JoinOrder::try_to_join() {
Price(opposite_at_inception_psz.price()))) {
LOG(OrderMan, Diag, "JoinOrder order {} joining: {}@{}",
order_key().value(), quantity().value(), my_side_psz.price());
algo().order_key().value(), algo().quantity().value(),
my_side_psz.price());
// Time to join
Update child_update;
child_update.set_price(my_side_psz.price());
child_update.set_quantity(quantity().value());
update_child(me->child_key, child_update);
child_update.set_quantity(algo().quantity().value());
algo().update_child(me->child_key, child_update);
me->status = Status::JOINED;
} else {
LOG(OrderMan, Diag,
"JoinOrder order {} still waiting to join. Order: {}@{} Top of the "
"book: {}@{}"
" Target: {}@{}",
order_key().value(), quantity().value(), price().value(),
algo().order_key().value(), algo().quantity().value(), algo().price().value(),
my_side_psz.size(), my_side_psz.price(),
me->min_quantity_to_join.value(), opposite_at_inception_psz.price());
}
}
void JoinOrder::update_join_threshold() {
void JoinAlgo::update_join_threshold() {
me->min_quantity_to_join = SpreaderUtils::_quantity_threshold(
definition().custom_def().join().join_threshold(), quantity());
algo().definition().custom_def().join().join_threshold(), algo().quantity());
}
void JoinOrder::on_child_change(OrderKey, ChildState const &st) {
update_fill_state(st.total_fills, st.last_price);
void JoinAlgo::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:
complete();
algo().complete();
return;
case ChildStatus::CANCELED:
cancel("Unexpected child cancel");
algo().cancel("Unexpected child cancel");
return;
}
}
@@ -0,0 +1,36 @@
#pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
namespace bts {
namespace orderman {
// Join starts working N ticks away from the market (usually) and then
// watches for the opposite inside to become same side inside market;
// if there is then enough size on that market, it will join.
class JoinAlgo : public IAlgoOrder
{
public:
JoinAlgo();
~JoinAlgo();
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<PImpl> me;
IAlgoServices &algo() const ;
void update_join_threshold();
bool has_running_child() const;
void try_to_join();
};
}}
@@ -156,8 +156,7 @@ bool TopAlgo::at_top_of_book() const {
bool TopAlgo::better_than_top() const {
auto my_side = algo().definition().side();
return securities::is_better(my_side, algo().price(),
top_of_book_price());
return securities::is_better(my_side, algo().price(), top_of_book_price());
}
Quantity TopAlgo::min_quantity_at_top() const {
@@ -5,8 +5,8 @@
namespace bts {
namespace orderman {
// If TopAlgo becomes the top of book, it cancels if there is less
// than some threshold of size joined with it.
// If Top becomes the top of book, it cancels if there is less than
// some threshold of size joined with it.
class TopAlgo : public IAlgoOrder {
public:
@@ -1,147 +0,0 @@
#include "CoverOrder.h"
#include "lib/domain_model/Security.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/utils/PBUtils.h"
namespace bts {
namespace orderman {
using namespace api::orderman;
struct CoverOrder::PImpl {
OrderKey child_key = null_order_key();
Price price_improvement_delta;
ChildTemplate *child_template = nullptr;
ChildTemplate *hedge_template = nullptr;
};
CoverOrder::CoverOrder() : me(new PImpl()) {}
CoverOrder::~CoverOrder() {}
OrderOpResult CoverOrder::do_before_launch() {
auto &def = definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_cover());
// Validate the cover part of the definition
auto &cover = def.custom_def().cover();
if (!cover.has_child() &&
OrderKey(def.attach_target_order_key()) == null_order_key()) {
return OrderOpResult{
ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Cover order must have either a child order definition or an "
"order to attach to"};
}
if (!cover.has_hedge())
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Cover order must have a hedge order definition"};
if (!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 = definition();
*child_def.mutable_custom_def() =
definition().custom_def().cover().child();
me->child_template = &(make_child_template(child_def));
}
init_hedge_template(cover.hedge());
set_price_improvement_delta(cover);
return {};
}
void CoverOrder::do_launch() {
LOG(OrderMan, Diag,
"Starting CoverOrder {}, {} {}@{}. Will cover {} ticks",
order_key().value(), securities::side_name(definition().side()),
quantity().value(), price().value(),
definition().custom_def().cover().hedge_ticks());
LOG(OrderMan, Diag, "CoverOrder {} launching child", order_key().value());
me->child_key = launch_child(*me->child_template, price(), quantity());
}
void CoverOrder::do_launch_attached(OrderKey k, ChildState st) {
me->child_key = k;
on_child_change(k, st);
}
void CoverOrder::on_child_change(OrderKey k, ChildState const &st) {
bts_assert(k == me->child_key); // We are not subscribed to hedge changes
if (st.total_fills.value() > total_fills().value()) {
auto hedge_qty = Quantity(st.total_fills.value() - total_fills().value());
auto hedge_price = securities::improve_price(
definition().side(), st.last_price, me->price_improvement_delta);
launch_child(*me->hedge_template, hedge_price, hedge_qty,
false); // do not subscribe to hedge changes
LOG(OrderMan, Diag,
"CoverOrder {}, launched new hedge in response to child fill. Hedge: "
"{} {}@{}",
order_key().value(), securities::side_name(definition().side()),
hedge_qty.value(), hedge_price.value());
}
update_fill_state(st.total_fills, st.last_price);
switch (st.status) {
case ChildStatus::WORKING:
return;
case ChildStatus::COMPLETED:
detach_all();
complete();
return;
case ChildStatus::CANCELED:
terminate_error("Unexpected child cancel");
return;
}
}
void CoverOrder::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "CoverOrder order {} receiving update: {}",
order_key().value(), utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_cover()) {
terminate_error("Invalid update");
return;
}
Update child_update;
child_update.set_price(update.price());
child_update.set_quantity(update.quantity());
// process custom part of the udpate if any
if (update.has_custom()) {
auto &cover_update = update.custom().cover();
set_price_improvement_delta(cover_update);
if (cover_update.has_child()) {
*child_update.mutable_custom() = cover_update.child();
}
if (cover_update.has_hedge())
init_hedge_template(cover_update.hedge());
}
update_child(me->child_key, child_update);
}
void orderman::CoverOrder::init_hedge_template(
api::orderman::CustomOrderDefinition const &hedge_cdef) {
auto hedge_def = definition();
hedge_def.set_side(securities::other_side(definition().side()));
hedge_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG);
hedge_def.clear_attach_target_order_key();
*hedge_def.mutable_custom_def() = hedge_cdef;
me->hedge_template = &(make_child_template(hedge_def));
}
void CoverOrder::set_price_improvement_delta(
api::orderman::CoverOrderDefinition const &cover) {
auto my_security = lookup_security(SecurityKey(definition().security_key()));
bts_assert(my_security);
me->price_improvement_delta =
Price(securities::tick_size(*my_security).value() * cover.hedge_ticks());
}
} // namespace orderman
} // namespace bts
@@ -1,31 +0,0 @@
#pragma once
#include "lib/orderman/orders/algo/AlgoOrder.h"
namespace bts {
namespace orderman {
// CoverOrder initializes the child order as normal. Fills trigger
// hedge orders to the opposite side of the market, n ticks away.
class CoverOrder : public AlgoOrder {
public:
CoverOrder();
~CoverOrder();
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_child_change(OrderKey, ChildState const &) override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
void
set_price_improvement_delta(api::orderman::CoverOrderDefinition const &);
void init_hedge_template(api::orderman::CustomOrderDefinition const &);
};
} // namespace orderman
} // namespace bts
@@ -1,35 +0,0 @@
#pragma once
#include "lib/orderman/orders/algo/AlgoOrder.h"
namespace bts {
namespace orderman {
// JoinOrder starts working N ticks away from the market (usually) and
// then watches for the opposite inside to become same side inside
// market; if there is then enough size on that market, it will join.
class JoinOrder : public AlgoOrder
{
public:
JoinOrder();
~JoinOrder();
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:
enum class Status;
struct PImpl;
std::unique_ptr<PImpl> me;
void update_join_threshold();
bool has_running_child() const;
void try_to_join();
};
}}
@@ -1,172 +0,0 @@
#include "lib/orderman/orders/book_algos/TopOrder.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
#include "lib/utils/PBUtils.h"
namespace bts {
namespace orderman {
using namespace api::orderman;
struct TopOrder::PImpl {
spark::QuantityMeasure top_threshold_measure;
OrderKey child_key = null_order_key();
ChildTemplate *child_template = nullptr;
Book last_book;
};
TopOrder::TopOrder() : me(new PImpl()) {}
TopOrder::~TopOrder() {}
OrderOpResult TopOrder::do_before_launch() {
auto &def = definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_top());
// Validate the top part of the definition
auto &top = def.custom_def().top();
if (!top.has_child() &&
OrderKey(def.attach_target_order_key()) == null_order_key()) {
return OrderOpResult{
ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Top order must have either a child order definition or an "
"order to attach to"};
}
me->top_threshold_measure = top.top_threshold();
if (!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 = definition();
*child_def.mutable_custom_def() = definition().custom_def().top().child();
me->child_template = &(make_child_template(child_def));
}
return {};
}
void TopOrder::do_launch() {
subscribe_book(SecurityKey(definition().security_key()));
bts_assert(me->child_template);
me->child_key = launch_child(*me->child_template, price(), quantity());
}
void TopOrder::do_launch_attached(OrderKey k, ChildState st) {
me->child_key = k;
subscribe_book(SecurityKey(definition().security_key()));
on_child_change(k, st);
}
void TopOrder::do_update(api::orderman::Update const &update) {
LOG(OrderMan, Diag, "Top order {} receiving update: {}", order_key().value(),
utils::pb::to_string(update));
if (update.has_custom() && !update.custom().has_top()) {
terminate_error("Invalid update");
return;
}
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 &top_update = update.custom().top();
*child_update.mutable_custom() = top_update.child();
me->top_threshold_measure = top_update.top_threshold();
}
update_child(me->child_key, child_update);
}
void TopOrder::on_book(SecurityKey, Book const &b) {
me->last_book = b;
ensure_min_quantity();
}
void TopOrder::on_child_change(OrderKey, ChildState const &st) {
update_fill_state(st.total_fills, st.last_price);
switch (st.status) {
case ChildStatus::WORKING:
return;
case ChildStatus::COMPLETED:
complete();
return;
case ChildStatus::CANCELED:
cancel("Unexpected child cancel");
return;
}
}
bool TopOrder::ensure_min_quantity() {
LOG(OrderMan, Diag,
"Top order {} received book update. Current top of book: {}@{}",
order_key().value(), top_of_book_quantity().value(),
top_of_book_price().value());
if (better_than_top()) {
LOG(OrderMan, Diag,
"Top order {} became better than the top of the book, canceling",
order_key().value());
cancel("Order would become BBO");
return false;
}
if (at_top_of_book()) {
auto total_min_quantity =
quantity().value() + min_quantity_at_top().value();
LOG(OrderMan, Diag,
"Top order {} is now at the top of the book, min quantity for book "
"is: {}"
" book quantity is: {}",
order_key().value(), total_min_quantity,
top_of_book_quantity().value());
if (top_of_book_quantity() < Quantity(total_min_quantity)) {
LOG(OrderMan, Diag,
"Top order {} is canceling because min quantiy is not met",
order_key().value());
cancel("Top of book is below threshold");
return false;
}
} else
LOG(OrderMan, Diag,
"Top order {} is not at the top of the book. Order: {} Book: {}",
order_key().value(), price().value(), top_of_book_price().value());
return true;
}
bool TopOrder::at_top_of_book() const {
return securities::is_same(price(), top_of_book_price());
}
bool TopOrder::better_than_top() const {
auto my_side = definition().side();
return securities::is_better(my_side, price(), top_of_book_price());
}
Quantity TopOrder::min_quantity_at_top() const {
return SpreaderUtils::_quantity_threshold(me->top_threshold_measure,
quantity());
}
Price TopOrder::top_of_book_price() const {
auto my_side = definition().side();
auto &my_side_psz = me->last_book.side(my_side)[0];
return Price(my_side_psz.price());
}
Quantity TopOrder::top_of_book_quantity() const {
auto my_side = definition().side();
auto &my_side_psz = me->last_book.side(my_side)[0];
return Quantity(my_side_psz.size());
}
} // namespace orderman
} // namespace bts
@@ -1,37 +0,0 @@
#pragma once
#include "lib/orderman/orders/algo/AlgoOrder.h"
namespace bts {
namespace orderman {
// If TopOrder becomes the top of book, it cancels if there is less
// than some threshold of size joined with it.
class TopOrder : public AlgoOrder {
public:
TopOrder();
~TopOrder();
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:
struct PImpl;
std::unique_ptr<PImpl> me;
bool at_top_of_book() const;
bool better_than_top() const;
Quantity min_quantity_at_top() const;
bool ensure_min_quantity();
bool has_running_child() const;
Price top_of_book_price() const;
Quantity top_of_book_quantity() const;
};
} // namespace orderman
} // namespace bts
+1 -1
View File
@@ -1 +1 @@
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/cssstyle/scripts/run_tests.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)