Compare commits

...
19 changed files with 1641 additions and 17 deletions

No files matched your search

+1
View File
@@ -32,3 +32,4 @@ trace_data/
/src/lib/orderman/perf_analysis/last_pretty_log.csv
/src/lib/orderman/perf_analysis/log.csv
/doc/user/public/*
.ionide
+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/MBOService.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/keycode.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/keycode.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/MBOProcessor.cpp;md/MBOServiceServer.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/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.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_manLine 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/MBOService.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/keycode.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/keycode.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/MBOProcessor.cpp;md/MBOServiceServer.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/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.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_manLine truncated
+10 -1
View File
@@ -101,6 +101,7 @@ message CustomOrderDefinition
OCOOrderDefinition oco = 25;
PricerOrderDefinition pricer = 26;
CompositeOrderDefinition composite = 27;
SplitterOrderDefinition splitter = 29;
DynamicOrderDefinition dynamic = 1000;
}
@@ -204,6 +205,12 @@ message CompositeOrderDefinition {
uint32 algo_template_key = 1;
CustomOrderDefinition definition = 2;
}
message SplitterOrderDefinition {
uint32 max_order_qty = 1;
uint32 exposed_qty = 2;
// uint32 target_qty = 3;
CustomOrderDefinition child = 10;
}
message DynamicOrderDefinition {
string dynamic_order_type = 1;
map<string, DynamicOrderParam> order_params = 2;
@@ -253,9 +260,10 @@ message OrderState {
TriggerOrderState trigger = 21;
CoverOrderState cover = 22;
OffsetOrderState offset = 24;
QueueHolderState queue_holder = 30;
OCOOrderState oco = 25;
PricerOrderState pricer = 26;
QueueHolderState queue_holder = 30;
SplitterOrderState splitter = 31;
DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break
}
@@ -334,6 +342,7 @@ message OCOOrderState {}
message PricerOrderState {}
message DynamicOrderState {}
message QueueHolderState {}
message SplitterOrderState {}
enum OrderStatus {
+3 -3
View File
@@ -34,9 +34,9 @@ class IntPrice {
#endif
public:
IntPrice() : _mantissa(0), _exponent(127) {/*nan*/}
IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {}
IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {}
constexpr IntPrice() : _mantissa(0), _exponent(127) {/*nan*/}
constexpr IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {}
constexpr IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {}
bool operator==(const IntPrice& other) const {
if(_exponent == other._exponent) {
+1 -1
View File
@@ -77,7 +77,7 @@ public:
extern PriceSize NO_MARKET;
static_assert(sizeof(PriceSize) == 16, "PriceSize should be small");
template <uint8_t PRECISION>
template <uint8_t PRECISION=11>
PriceSize PriceSize_from_double(double price, Size size) {
if (size == 0 || os::is_nan(price)) return NO_MARKET;
return { IntPrice_from_double<PRECISION>(price), size };
+42 -9
View File
@@ -11,6 +11,7 @@
#include "lib/md/protos/co2.pb.h"
#include "lib/md/SecurityIDRecorder.h"
#include "lib/config/protos/system.pb.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/utils/bts_chrono.h"
#include <boost/filesystem.hpp>
@@ -173,7 +174,7 @@ void replay_file(const std::string& filename, IRelayWriter& writer, int32_t nagg
} while (loop);
}
void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) {
bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) {
// split up url parts
size_t http_len = BTS_ARRAYSIZE("http://") - 1;
auto after_host = url.find('/', http_len);
@@ -184,21 +185,53 @@ void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms)
filename = os::sparkdata_path("data/replay/") + filename;
utils::download_file_if_newer(host, path, filename);
if (boost::filesystem::exists(filename)) {
replay_file(filename, writer, naggle_ms, true);
replay_file(filename, writer, naggle_ms, loop);
return true;
}
return false;
}
void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) {
if (!replay_url(url, writer, naggle_ms, loop)) {
const auto fallback = config::get<ReplayOptions>().fallback();
if (boost::filesystem::exists(fallback)) {
replay_file(fallback, writer, naggle_ms, loop);
}
}
}
void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) {
// loops forever
replay_url(url, writer, naggle_ms);
// If it returns it means it failed to download or play cached
const auto fallback = config::get<ReplayOptions>().fallback();
if (boost::filesystem::exists(fallback)) {
replay_file(fallback, writer, naggle_ms);
struct SecurityExtractorDone : public std::bad_exception {};
struct SecurityExtractor : public IRelayWriter {
size_t _count;
std::function<void(const api::Security&)> _cb;
std::set<uint32_t> _keys;
SecurityExtractor(std::function<void(const api::Security&)>& cb, size_t count)
: _count(count), _cb(cb), _keys() { }
~SecurityExtractor() override {};
void write(const co2::MDReply& data, bool*) override {
for (auto const &ch : data.channels()) {
for (auto const &cr : ch.updates()) {
if (cr.has_security() && _keys.find(cr.security().id()) == _keys.end()) {
_keys.insert(cr.security().id());
_cb(cr.security());
}
}
}
if (_keys.size() >= _count) {
throw SecurityExtractorDone();
}
}
};
void get_securities_from_replay(std::function<void(const api::Security&)> cb) {
SecurityExtractor extractor(cb, 100);
auto url = config::get<ReplayOptions>().url();
try {
replay_url_with_fallback(url, extractor, 0, false);
} catch(const SecurityExtractorDone&) { }
}
shared_ptr<IRelayWriter> mkrelayproducer(bool shared);
/*this is the public one*/
+3 -2
View File
@@ -29,9 +29,10 @@ void print_recording(const std::string&);
void unprint_recording(const std::string& src, const std::string& dest);
void trace_recording(const std::string& file, security_key key);
void replay_recording(const std::string&, int32_t delay_ms, const std::string fallback="");
void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms);
void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms);
bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true);
void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true);
void replay_file(const std::string& filename, IRelayWriter& writer, int32_t naggle_ms, bool loop=false);
void get_securities_from_replay(std::function<void(const api::Security&)>);
}}//namespace
@@ -153,6 +153,9 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
case api::orderman::OrderState::kDynamic:
result.mutable_dynamic();
break;
case api::orderman::OrderState::kSplitter:
result.mutable_splitter();
break;
case api::orderman::OrderState::CUSTOM_NOT_SET:
break;
}
@@ -19,6 +19,7 @@
#include "lib/orderman/orders/algo/algo_orders/TopAlgo.h"
#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h"
#include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h"
#include "lib/orderman/orders/algo/algo_orders/SplitterAlgo.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"
@@ -208,6 +209,10 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
case CustomOrderDefinition::kComposite:
order_ptr.reset(new orderman::CompositeOrder());
break;
case CustomOrderDefinition::kSplitter:
order_ptr.reset(
new orderman::AlgoWrapperOrder(std::make_unique<splitter::SplitterAlgo>()));
break;
case CustomOrderDefinition::CUSTOM_NOT_SET:
bts_assert(false);
break;
@@ -49,5 +49,68 @@ bool should_forward_attachment(
}
}
api::orderman::DynamicOrderParam dynamic_parameter(int32_t v) {
api::orderman::DynamicOrderParam res;
res.set_as_int32(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(uint32_t v) {
api::orderman::DynamicOrderParam res;
res.set_as_uint32(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(double v) {
api::orderman::DynamicOrderParam res;
res.set_as_double(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(std::string v) {
api::orderman::DynamicOrderParam res;
res.set_as_string(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(bool v) {
api::orderman::DynamicOrderParam res;
res.set_as_bool(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition& v) {
api::orderman::DynamicOrderParam res;
res.mutable_as_order_def()->CopyFrom(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray& v) {
api::orderman::DynamicOrderParam res;
res.mutable_as_array()->CopyFrom(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct& v) {
api::orderman::DynamicOrderParam res;
res.mutable_as_struct()->CopyFrom(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated& v) {
api::orderman::DynamicOrderParam res;
res.mutable_as_enumerated()->CopyFrom(v);
return res;
}
api::orderman::DynamicOrderParam dynamic_array(const std::vector<api::orderman::DynamicOrderParam>& v) {
api::orderman::DynamicOrderArray a;
for (const auto& item : v) {
a.add_element()->CopyFrom(item);
}
return dynamic_parameter(a);
}
api::orderman::DynamicOrderParam dynamic_map(
const std::vector<std::tuple<std::string,api::orderman::DynamicOrderParam>>& v) {
DynamicOrderStruct res;
auto& map = *res.mutable_field();
for (const auto& tup : v) {
map[std::get<std::string>(tup)] = std::get<1>(tup);
}
return dynamic_parameter(res);
}
} // namespace orderman
} // namespace bts
@@ -1,4 +1,6 @@
#include "lib/api/services/OrderService.pb.h"
#include <optional>
#include <variant>
namespace bts {
namespace orderman {
@@ -30,5 +32,64 @@ inline bool is_parent_order(const bts::api::orderman::OrderRecord &ord) {
bool should_forward_attachment(const api::orderman::CustomOrderDefinition &def);
bool can_consume_attachment(api::orderman::CustomOrderDefinition::CustomCase) ;
api::orderman::DynamicOrderParam dynamic_parameter(int32_t);
api::orderman::DynamicOrderParam dynamic_parameter(uint32_t);
api::orderman::DynamicOrderParam dynamic_parameter(double);
api::orderman::DynamicOrderParam dynamic_parameter(std::string);
api::orderman::DynamicOrderParam dynamic_parameter(bool);
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition&);
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray&);
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct&);
api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated&);
api::orderman::DynamicOrderParam dynamic_array(const std::vector<api::orderman::DynamicOrderParam>&);
api::orderman::DynamicOrderParam dynamic_map( const std::vector<std::tuple<std::string,api::orderman::DynamicOrderParam>>&);
template <class T>
std::optional<T> get_if(const api::orderman::DynamicOrderParam&) { return nullptr; }
template <>
inline std::optional<int32_t> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsInt32 ? std::optional<int32_t>{p.as_int32()} : std::nullopt;
}
template <>
inline std::optional<uint32_t> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsUint32 ? std::optional<uint32_t>{p.as_uint32()} : std::nullopt;
}
template <>
inline std::optional<double> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsDouble ? std::optional<double>{p.as_double()} : std::nullopt;
}
template <>
inline std::optional<bool> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsBool ? std::optional<bool>{p.as_bool()} : std::nullopt;
}
template <>
inline std::optional<api::orderman::CustomOrderDefinition> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsOrderDef ? std::optional<api::orderman::CustomOrderDefinition>{p.as_order_def()} : std::nullopt;
}
template <>
inline std::optional<api::orderman::DynamicOrderArray> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsArray ? std::optional<api::orderman::DynamicOrderArray>{p.as_array()} : std::nullopt;
}
template <>
inline std::optional<api::orderman::DynamicOrderStruct> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsStruct ? std::optional<api::orderman::DynamicOrderStruct>{p.as_struct()} : std::nullopt;
}
template <>
inline std::optional<api::orderman::DynamicOrderEnumerated> get_if(const api::orderman::DynamicOrderParam& p) {
return p.value_case() == api::orderman::DynamicOrderParam::kAsEnumerated ? std::optional<api::orderman::DynamicOrderEnumerated>{p.as_enumerated()} : std::nullopt;
}
template <class T>
std::optional<T> get_if(const google::protobuf::Map<std::string,api::orderman::DynamicOrderParam>& map, const std::string& key) {
auto c = map.find(key);
if (c != map.end()) {
return get_if<T>(c->second);
}
return std::nullopt;
}
} // namespace orderman
} // namespace bts
@@ -1 +1,8 @@
#include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h"
namespace bts {
namespace orderman {
TimeStamp IBeforeLaunchAlgoServices::get_time() const { return Timer::now(); }
} // namespace orderman
} // namespace bts
@@ -3,6 +3,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/SecurityKey.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/bts_chrono.h"
namespace bts {
namespace orderman {
@@ -42,6 +43,8 @@ class IBeforeLaunchAlgoServices {
virtual ChildTemplate &
make_child_template(api::orderman::OrderDefinition const &child_def,
size_t reserve = 1) = 0;
virtual TimeStamp get_time() const;
};
} // namespace orderman
@@ -0,0 +1,259 @@
#include "SplitterAlgo.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "lib/orderman/order_factory/IOrderFactory.h"
#include "lib/orderman/orderman_utils/orderman_utils.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include <algorithm>
#include <tuple>
namespace bts {
namespace orderman {
namespace splitter {
using namespace api::orderman;
struct ChildOrderState {
OrderKey key;
ChildState state;
uint32_t min_qty() const { return state.total_fills.value(); }
};
struct SplitterAlgo::Impl {
IAlgoServices* algo = nullptr;
ChildTemplate* child_template = nullptr;
uint32_t clip = 0;
uint32_t exposed_qty = 0;
uint32_t target_qty = 0;
bool is_terminated = false;
Quantity total_fills = {};
Price last_fill_price = {};
std::vector<ChildOrderState> orders;
};
SplitterAlgo::SplitterAlgo() : me(new Impl()) { }
SplitterAlgo::~SplitterAlgo() {}
OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) {
// Validate and store parameters
auto& def = ibl.definition();
bts_assert(def.has_custom_def());
bts_assert(def.custom_def().has_splitter());
auto& splitter = def.custom_def().splitter();
me->clip = splitter.max_order_qty();
me->exposed_qty = splitter.exposed_qty();
if (me->clip <= 0 || me->exposed_qty <= 0) {
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
"max order quantity and exposed quantity must be greater than 0"};
}
if (me->exposed_qty < me->target_qty) {
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Exposed quantity must be greater than or equal to target quantity"};
}
update_child_template(ibl);
me->orders.reserve(me->exposed_qty / me->clip + 1);
if (ibl.is_attaching()) {
return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED,
"Splitter does not support attach."};
}
return {};
}
void SplitterAlgo::do_launch(IAlgoServices& ias) {
me->algo = &ias;
me->target_qty = ias.quantity().value();
// launches initial child orders
sync_orders();
}
void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) {
// rejected in before_launch
bts_assert(false);
}
void SplitterAlgo::do_update(const api::orderman::Update& update) {
const CustomOrderDefinition* new_child_def = nullptr;
if (update.has_custom() && update.custom().has_splitter()) {
auto& splitter = update.custom().splitter();
if (splitter.max_order_qty() > 0) {
me->clip = splitter.max_order_qty();
}
me->exposed_qty = splitter.exposed_qty();
if (splitter.has_child()) {
update_child_template(algo());
new_child_def = &splitter.child();
}
}
me->target_qty = update.quantity();
if (me->exposed_qty < me->target_qty) {
algo().cancel("Exposed quantity must be greater than or equal to target quantity");
return;
}
sync_orders(new_child_def);
}
void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) {
for (auto& child : me->orders) {
if (child.key == key) {
if (state.total_fills < child.state.total_fills) {
algo().cancel("Unexpected fill reduction in fill quantityt");
return;
}
auto delta = state.total_fills.value() - child.state.total_fills.value();
if (delta != 0) {
me->total_fills = Quantity { me->total_fills.value() + delta };
me->last_fill_price = state.last_price;
algo().update_fill_state(me->total_fills, me->last_fill_price);
}
child.state = state;
break;
}
}
sync_orders();
}
// TEST case:
// total = 300, clip = 100
// constraints: 50,50,50,50
void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector<MinMax>& constraints, std::vector<Quantity>* results) {
bts_assert(results);
results->clear();
uint32_t irreducible = 0;
for (const auto& mm : constraints) irreducible += mm.min;
// total > overfills ?
auto citer = constraints.begin();
while(total > 0) {
auto current = std::min(total, clip);
// fit to constraint
if (citer != constraints.end()) {
irreducible -= citer->min;
if (current > citer->max) current = citer->max;
if (current < citer->min) current = citer->min;
// I can't be higher than (total - irreducible)
if (current > (total - irreducible)) current = (total-irreducible);
citer++;
}
results->push_back(Quantity{current});
total -= current;
}
}
void get_constraints(const std::vector<ChildOrderState>& orders, std::vector<MinMax>* constraints) {
bts_assert(constraints);
for (const auto& order : orders) {
//Note: to maintain queue position set max to current quantity. This will cause more orders to be added to the end if size increases
constraints->push_back(MinMax {
order.state.total_fills.value(),
order.state.status == ChildStatus::WORKING ?
std::numeric_limits<uint32_t>::max() : order.state.total_fills.value()
});
}
}
bool SplitterAlgo::is_complete() const {
uint32_t fills = 0;
for (const auto& o : me->orders) { fills += o.state.total_fills.value(); }
return fills >= me->target_qty;
}
void SplitterAlgo::terminate() const {
algo().complete();
//?? Do we need to clean up children?
for (const auto& o : me->orders) {
if (o.state.status == ChildStatus::WORKING) {
algo().cancel_child(o.key);
}
}
me->is_terminated = true;
}
void SplitterAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl) {
// Create child template
auto child_def = ibl.definition();
bts_assert(child_def.has_custom_def());
bts_assert(child_def.custom_def().has_splitter());
auto& splitter = child_def.custom_def().splitter();
*child_def.mutable_custom_def() = splitter.child();
me->child_template = &ibl.make_child_template(child_def);
}
void SplitterAlgo::sync_orders(const api::orderman::CustomOrderDefinition* new_child_def) {
if (me->is_terminated) return;
if (is_complete()) {
terminate();
return;
}
std::sort(me->orders.begin(), me->orders.end(), [](auto& a, auto b) { return b.min_qty() < a.min_qty(); });
std::vector<MinMax> constraints;
get_constraints(me->orders, &constraints);
std::vector<Quantity> desired;
compute_desired_sizes(me->exposed_qty, me->clip, constraints, &desired);
auto oiter = me->orders.begin();
// iterate to the current number of orders, don't consider orders that we're adding
auto order_count = me->orders.size();
auto price = algo().price();
for (const auto& qty : desired) {
if (order_count == 0) {
// Ran out of existing orders, time to add more
auto key = algo().launch_child(*me->child_template, price, qty);
// Assume the order begins successfully. Hopefully we get back some sort of NACK otherwise
me->orders.push_back({ key, {} });
me->orders.back().state.price = price;
me->orders.back().state.quantity = Quantity { qty };
me->orders.back().state.status = ChildStatus::WORKING;
}
else {
// Check the current order's quantity and modify it to match desired qty
if (oiter->state.status == ChildStatus::WORKING) {
if (oiter->state.price != price ||
oiter->state.quantity != qty ||
new_child_def != nullptr) {
Update update;
update.set_quantity(qty.value());
update.set_price(price.value());
if (new_child_def != nullptr) {
*update.mutable_custom() = *new_child_def;
}
algo().update_child(oiter->key, update);
}
}
oiter++;
order_count -= 1;
}
}
// cancel any orders past desired
if (desired.size() < me->orders.size()) {
for (size_t x = desired.size(); x < me->orders.size(); ++x) {
auto& order= me->orders[x];
if (order.state.status == ChildStatus::WORKING) {
order.state.status = ChildStatus::CANCELED;
algo().cancel_child(order.key);
}
}
}
}
IAlgoServices& SplitterAlgo::algo() const { bts_assert(me->algo); return *me->algo; }
}}} //namespace
@@ -0,0 +1,60 @@
#pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
namespace bts {
namespace orderman {
namespace splitter {
/*
SplitterAlgo
Parameters:
max_order_qty
-- Clip size. No single order can exceed this quantity.
exposed_qty
-- The total quantity we want visible on the exchange.
-- This will typically be higher than the quantity which helps capture fills in pro-rata algorithms
price, quantity
-- Common to all orders.
-- Splitter is filled when it reaches (or exceeds) quantity not exposed_qty
SplitterAlgo acts as a single order to its parent but actually divides the total exposure
by the clip size and working several orders at once.
- It completes when the quantity is matched or exceeded.
- It tries to maximize the size of the orders and it may sacrifice queue position to do that.
- It reacts to fills and parameter changes and adjusts it's children appropriately.
- See splitter_tests.cpp for examples
- Future work: By constraining maximum size we can force this algorithm to maintain queue position.
*/
class SplitterAlgo : public IAlgoOrder
{
struct Impl;
std::unique_ptr<Impl> me;
public:
SplitterAlgo();
~SplitterAlgo();
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:
IAlgoServices& algo() const;
void sync_orders(const api::orderman::CustomOrderDefinition* new_child_def = nullptr);
bool is_complete() const;
void terminate() const;
void update_child_template(IBeforeLaunchAlgoServices&);
};
// exposed for unit testing purposes.
struct MinMax {uint32_t min, max;};
void compute_desired_sizes(uint32_t , uint32_t ,
const std::vector<MinMax>& , std::vector<Quantity>* );
}}} //namespace
@@ -0,0 +1,378 @@
#include "AlgoTestDriver.h"
#include "lib/md/relay/RelayRecorder.h"
#include "catch/catch.hpp"
#include "lib/api/services/OrderService.pb.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/logging/logging.h"
#include <optional>
#include <queue>
namespace bts {
namespace orderman {
namespace test {
using namespace api::orderman;
constexpr bool has_flag(ATDFlags prop, ATDFlags flag) {
return ((uint32_t)prop & (uint32_t)flag) == (uint32_t)flag;
}
const std::map<SecurityKey, api::Security>& get_some_securities() {
static std::map<SecurityKey, api::Security> results;
if (results.empty()) {
md::get_securities_from_replay([&](const api::Security& s) {
results.insert(std::make_pair(SecurityKey(s.id()), s));
});
}
return results;
}
struct RecursionCounter {
size_t depth = 0;
virtual void flush() = 0;
virtual ~RecursionCounter() {}
};
struct RecursionGuard {
RecursionGuard(RecursionCounter& c) : counter(c) {
counter.depth += 1;
REQUIRE(counter.depth < 100);
}
~RecursionGuard() {
counter.depth -= 1;
if (counter.depth == 0) { counter.flush(); }
}
private:
RecursionCounter& counter;
};
struct AlgoTestDriverBase::Impl : public RecursionCounter {
IAlgoOrder& algo;
api::orderman::OrderDefinition definition;
Price price;
Quantity size;
ATDFlags flags;
TimeStamp now;
api::Security security;
OrderKey order_key, parent_key;
std::tuple<Quantity, Price> fill_state;
AlgoState state;
std::vector<ChildOrder> orders;
std::map<SecurityKey, Book> books;
std::map<SecurityKey, bool> book_subs;
bool sub_timer = false;
// Callbacks
std::vector<std::function<void(OrderKey)>> on_child_launching;
std::vector<std::function<void(const ChildOrder&)>> on_child_changing;
std::vector<std::function<void(const ChildOrder&, const ChildOrder&)>> on_child_changed;
Impl(IAlgoOrder& o) : algo(o) {}
OrderKey next_order() {
_next_order.value() += 1;
return _next_order;
}
RecursionGuard recusion_guard() { return {*this}; }
void flush() override;
bool flushing = false;
// child changes must be asyncronous
std::vector<std::tuple<OrderKey, ChildState>> deferred;
void defer_child_change(OrderKey k, ChildState cs) { deferred.push_back(std::make_tuple(k,cs)); }
private:
OrderKey _next_order = OrderKey(99);
};
void AlgoTestDriverBase::Impl::flush() {
if(flushing) return;
flushing = true;
for (const auto& update : deferred) {
algo.on_child_change(std::get<OrderKey>(update), std::get<ChildState>(update));
}
deferred.clear();
flushing = false;
}
AlgoTestDriverBase::AlgoTestDriverBase(
IAlgoOrder& algo,
const api::orderman::OrderDefinition& def,
Price price, Quantity size,
ATDFlags flags)
: me(new Impl(algo)) {
me->definition = def;
me->price = price;
me->size = size;
me->flags = flags;
me->now = Timer::now();
me->state = AlgoState::CREATED;
auto securities = get_some_securities();
REQUIRE(securities.size() > 0);
me->security = securities.begin()->second;
me->order_key = me->next_order();
if (has_flag(flags, ATDFlags::HAS_PARENT)) {
me->parent_key = me->next_order();
}
}
AlgoTestDriverBase::~AlgoTestDriverBase() {}
OrderOpResult AlgoTestDriverBase::before_launch() {
REQUIRE(me->state == AlgoState::CREATED);
me->state = AlgoState::READY;
return me->algo.do_before_launch(*this);
}
void AlgoTestDriverBase::launch() {
REQUIRE(me->state == AlgoState::READY);
me->algo.do_launch(*this);
me->state = AlgoState::RUNNING;
}
void AlgoTestDriverBase::launch_attached(OrderKey key, ChildState state) {
REQUIRE(me->state == AlgoState::READY);
me->algo.do_launch_attached(*this, key, state);
me->state = AlgoState::RUNNING;
}
void AlgoTestDriverBase::do_update(const api::orderman::Update& update) {
auto guard = me->recusion_guard();
REQUIRE(me->state == AlgoState::RUNNING);
me->algo.do_update(update);
}
void AlgoTestDriverBase::update_book(const Book& book) {
auto key = book.security_key();
me->books[key] = book;
auto iter = me->book_subs.find(key);
if (iter != me->book_subs.end()) {
me->algo.on_book(key, book);
}
}
void AlgoTestDriverBase::advance_time(Duration d) {
me->now += d;
if (me->sub_timer) {
me->algo.on_timer();
}
}
const std::vector<ChildOrder>& AlgoTestDriverBase::child_orders() const {
return me->orders;
}
AlgoState AlgoTestDriverBase::state() const {
return me->state;
}
void AlgoTestDriverBase::fill(OrderKey k, Price price, Quantity qty) {
auto guard = me->recusion_guard();
for (auto& child : me->orders) {
if (child.key == k) {
REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value());
for (const auto& cb : me->on_child_changing) cb(child);
auto old = child;
auto total_price = child.cstate.avg_price.value() * child.cstate.total_fills.value();
total_price += price.value() * qty.value();
child.cstate.last_fills = qty;
child.cstate.total_fills.value() += qty.value();
child.cstate.last_price = price;
child.cstate.avg_price = Price { total_price / child.cstate.total_fills.value() };
if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) {
child.cstate.status = ChildStatus::COMPLETED;
}
for (const auto& cb : me->on_child_changed) cb(old, child);
// No defer because this is called from outside
if (child.subscribed) me->algo.on_child_change(k, child.cstate);
break;
}
}
}
const ChildOrder* largest_order(const std::vector<ChildOrder>& orders) {
uint32_t sz = 0;
const ChildOrder* result = nullptr;
for (const auto& order : orders) {
auto available =
order.cstate.quantity.value() - order.cstate.total_fills.value();
if (order.cstate.status == ChildStatus::WORKING &&
available > sz) {
sz = available;
result = &order;
}
}
return result;
}
void AlgoTestDriverBase::fill(Price p, Quantity q) {
auto to_fill = q.value();
while(to_fill > 0) {
auto order = largest_order(this->child_orders());
REQUIRE(order);
auto available = (order->cstate.quantity.value() - order->cstate.total_fills.value());
if (available >= to_fill) {
this->fill(order->key, p, Quantity {to_fill});
return;
}
else {
this->fill(order->key, p, Quantity {available});
to_fill -= available;
}
}
}
void AlgoTestDriverBase::on_child_launching(std::function<void(OrderKey)>&& cb) {
me->on_child_launching.push_back(cb);
}
void AlgoTestDriverBase::on_child_changing(std::function<void(const ChildOrder&)>&& cb) {
me->on_child_changing.push_back(cb);
}
void AlgoTestDriverBase::on_child_changed(std::function<void(const ChildOrder&, const ChildOrder&)>&& cb) {
me->on_child_changed.push_back(cb);
}
//
// Utility
ChildOrder mkchild(OrderKey key, Price price, Quantity qty, bool sub) {
ChildOrder result = {};
result.key = key;
result.cstate.price = price;
result.cstate.quantity = qty;
result.subscribed = sub;
return result;
}
//
// IBeforeLaunch
const api::orderman::OrderDefinition& AlgoTestDriverBase::definition() const { return me->definition; }
const api::Security& AlgoTestDriverBase::security() const { return me->security; }
OrderKey AlgoTestDriverBase::order_key() const { return me->order_key; }
OrderKey AlgoTestDriverBase::parent_order_key() const { return me->parent_key; }
const api::Security* AlgoTestDriverBase::lookup_security(SecurityKey key) const {
auto s = get_some_securities();
auto iter = s.find(key);
if (iter != s.end()) {
return &iter->second;
}
return nullptr;
}
bool AlgoTestDriverBase::is_attaching() const { return has_flag(me->flags, ATDFlags::IS_ATTACHING); }
ChildTemplate& AlgoTestDriverBase::make_child_template(const api::orderman::OrderDefinition&, size_t) {
ChildTemplate* t = nullptr;
return *t;
}
TimeStamp AlgoTestDriverBase::get_time() const { return me->now; }
//
// IAlgoServices
Price AlgoTestDriverBase::price() const { return me->price; }
Quantity AlgoTestDriverBase::quantity() const { return me->size; }
Quantity AlgoTestDriverBase::total_fills() const {
return std::get<Quantity>(me->fill_state);
}
void AlgoTestDriverBase::subscribe_book(SecurityKey key){
me->book_subs[key] = true;
auto iter = me->books.find(key);
if (iter != me->books.end()) {
me->algo.on_book(key, iter->second);
}
}
void AlgoTestDriverBase::unsubscribe_book(SecurityKey key) {
me->book_subs.erase(key);
}
void AlgoTestDriverBase::subscribe_child_change(OrderKey k) {
for (auto& child : me->orders) {
if (child.key == k) {
child.subscribed = true;
}
}
}
void AlgoTestDriverBase::unsubscribe_child_change(OrderKey k) {
for (auto& child : me->orders) {
if (child.key == k) {
child.subscribed = false;
}
}
}
void AlgoTestDriverBase::subscribe_timer() { me->sub_timer = true; }
void AlgoTestDriverBase::unsubscribe_timer() { me->sub_timer = false; }
OrderKey AlgoTestDriverBase::launch_child(ChildTemplate&, Price price, Quantity qty, bool sub) {
OrderKey key = me->next_order();
me->orders.push_back(
mkchild(key, price, qty, sub));
for(const auto& cb : me->on_child_launching) cb(key);
return key;
}
OrderKey AlgoTestDriverBase::launch_child(api::orderman::OrderDefinition const & od, Price price, Quantity qty, bool sub) {
return launch_child(make_child_template(od), price, qty, sub);
}
void AlgoTestDriverBase::update_child(OrderKey key, api::orderman::Update const & update) {
auto guard = me->recusion_guard();
for (auto& child : me->orders) {
if (child.key == key) {
for (const auto& cb : me->on_child_changing) cb(child);
auto old = child;
if (update.price() != 0) {
child.cstate.price = Price(update.price());
}
if (update.quantity() != 0) {
child.cstate.quantity = Quantity(update.quantity());
}
if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) {
child.cstate.status = ChildStatus::COMPLETED;
}
for (const auto& cb : me->on_child_changed) cb(old, child);
if (child.subscribed) {
me->defer_child_change(key, child.cstate);
}
}
}
}
void AlgoTestDriverBase::cancel_child(OrderKey key) {
auto guard = me->recusion_guard();
for (auto& child : me->orders) {
if (child.key == key) {
REQUIRE(child.cstate.status != ChildStatus::CANCELED);
for (const auto& cb : me->on_child_changing) cb(child);
auto before = child;
child.cstate.status = ChildStatus::CANCELED;
for (const auto& cb : me->on_child_changed) cb(before, child);
if(child.subscribed) {
me->defer_child_change(key, child.cstate);
}
break;
}
}
}
void AlgoTestDriverBase::detach_child(OrderKey key) {
std::remove_if(me->orders.begin(), me->orders.end(), [key](const auto& order) { return order.key == key; });
}
void AlgoTestDriverBase::detach_all_children() {
me->orders.clear();
}
void AlgoTestDriverBase::cancel(std::string const &) {
me->state = AlgoState::CANCELED;
}
void AlgoTestDriverBase::terminate_error(std::string const &) {
me->state = AlgoState::TERMINATED;
}
void AlgoTestDriverBase::complete() {
me->state = AlgoState::COMPLETED;
}
void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ }
void AlgoTestDriverBase::update_fill_state(Quantity q, Price p) { me->fill_state = { q, p }; }
}}}//namespace
@@ -0,0 +1,140 @@
#pragma once
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
#include "lib/md/IntPrice.h"
#include <memory>
#include <vector>
#include <string>
namespace bts {
namespace orderman {
namespace test {
const std::map<SecurityKey, api::Security>& get_some_securities();
enum class ATDFlags {
NONE = 0,
IS_ATTACHING = 1,
HAS_PARENT = 2,
};
enum class AlgoState {
CREATED,
// before_lauh() ->
READY,
// launch() ->
RUNNING,
CANCELED,
TERMINATED,
COMPLETED
};
struct ChildOrder {
OrderKey key;
bool subscribed;
ChildState cstate;
uint32_t active_qty() const {
return cstate.status == ChildStatus::WORKING ?
cstate.quantity.value() - cstate.total_fills.value() : 0;
}
};
class AlgoTestDriverBase : private IAlgoServices {
struct Impl;
std::unique_ptr<Impl> me;
public:
AlgoTestDriverBase(
IAlgoOrder& algo,
const api::orderman::OrderDefinition& def,
Price price, Quantity size,
ATDFlags flags=ATDFlags::NONE);
~AlgoTestDriverBase();
//
// Expose some things from IAlgoServices
const api::Security& security() const override;
Price price() const override;
Quantity quantity() const override;
Quantity total_fills() const override;
//
// Change state
OrderOpResult before_launch();
void launch();
void launch_attached(OrderKey, ChildState);
void do_update(const api::orderman::Update&);
void update_book(const Book&);
void advance_time(Duration);
//
// Inspection methods
const std::vector<ChildOrder>& child_orders() const;
AlgoState state() const;
// Fills for this specific order
void fill(OrderKey, Price, Quantity);
// Fill this quantity, start with the largest child order
void fill(Price, Quantity);
void on_child_launching(std::function<void(OrderKey)>&&);
void on_child_changing(std::function<void(const ChildOrder&)>&&);
void on_child_changed(std::function<void(const ChildOrder&, const ChildOrder&)>&&);
private:
//
// IBeforeLaunchAlgoServices
const api::orderman::OrderDefinition& definition() const override;
// const api::Security& security() const override;
OrderKey order_key() const override;
OrderKey parent_order_key() const override;
const api::Security* lookup_security(SecurityKey) const override;
bool is_attaching() const override;
ChildTemplate& make_child_template(const api::orderman::OrderDefinition& child_def, size_t reserve = 1) override;
TimeStamp get_time() const override;
//
// IAlgoServices
void subscribe_book(SecurityKey)override;
void unsubscribe_book(SecurityKey) override;
void subscribe_child_change(OrderKey) override;
void unsubscribe_child_change(OrderKey) override;
void subscribe_timer() override;
void unsubscribe_timer() override;
OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) override;
OrderKey launch_child(api::orderman::OrderDefinition const &, Price,
Quantity, bool) override;
void update_child(OrderKey , api::orderman::Update const &) override;
void cancel_child(OrderKey) override;
void detach_child(OrderKey) override;
void detach_all_children() override;
void cancel(std::string const &) override;
void terminate_error(std::string const &) override;
void complete() override;
void set_ladder_visibility(bool) override;
void update_fill_state(Quantity , Price) override;
};
template <class Algo>
struct AlgoTestDriver : public AlgoTestDriverBase
{
// Avoids mutual ownership problem
Algo _algo;
AlgoTestDriver(
const api::orderman::OrderDefinition& def,
const md::IntPrice& price, Quantity size,
ATDFlags flags=ATDFlags::NONE)
: AlgoTestDriverBase(_algo, def, Price {price.price()}, size, flags), _algo()
{ }
const Algo& algo() const { return _algo; }
};
}}}//namespace
@@ -0,0 +1,600 @@
#include "../SplitterAlgo.h"
#include "AlgoTestDriver.h"
#include "catch/catch.hpp"
#include "lib/orderman/orderman_utils/Book.h"
#include <ctime>
#include <optional>
#include <tuple>
#include <variant>
#include "lib/utils/bts_chrono.h"
using namespace std;
using namespace bts::orderman::splitter;
using namespace bts::api::orderman;
namespace bts {
namespace orderman {
namespace test {
/*
- max/exposed, assert correct # of orders on the market
- completes on target_qty reached
- completes on target_qty exceeded
- send fills, check order state
- send fills, then change target_qty
*/
constexpr auto some_price = md::IntPrice{128000, -3};
using ExpectedCount = utils::ValueWrapper<struct ExpectedCountTag, uint32_t>;
struct SimpleCase {
ExpectedCount expected_orders;
Quantity target;
SplitterOrderDefinition sod;
};
SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed,
uint32_t target) {
SimpleCase result;
result.expected_orders = expected;
result.target = Quantity{target};
result.sod.set_max_order_qty(max_size);
result.sod.set_exposed_qty(exposed);
return result;
}
TEST_CASE("compute_desired_sizes fuzzing", "[algo][splitter]") {
srand((unsigned)time(0));
auto start = Timer::now();
while (sec_diff(Timer::now(),start) < 5) {
uint32_t total = rand() % 100 + 1;
uint32_t clip = rand() % 4 + 1;
std::vector<MinMax> constraints(rand() % 10);
std::vector<Quantity> result;
uint32_t all_fills = 0;
for (auto &mm : constraints) {
uint32_t order_working = rand() % 2;
uint32_t fills = rand() % 2 * clip;
all_fills += fills;
mm = {fills, order_working ? fills + rand() % clip + 1 : fills};
}
if (all_fills < total) {
compute_desired_sizes(total, clip, constraints, &result);
uint32_t exposed_qty = 0;
for (auto q : result)
exposed_qty += q.value();
bool clip_size_honored = true;
for (auto q : result)
clip_size_honored = clip_size_honored && (q.value() <= clip);
if (exposed_qty != total || !clip_size_honored) {
std::cout << "total: " << total << "\n"
<< "clip: " << clip << "\n{ ";
for (auto mm : constraints)
std::cout << "{" << mm.min << "," << mm.max << "},";
std::cout << " }\n";
}
REQUIRE(clip_size_honored);
REQUIRE(exposed_qty == total);
}
}
}
TEST_CASE("initial orders", "[algo][splitter]") {
std::vector<SimpleCase> cases;
cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50));
cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50));
cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50));
cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150));
for (const auto &tcase : cases) {
auto expected = tcase.expected_orders.value();
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
*def.mutable_custom_def()->mutable_splitter() = tcase.sod;
AlgoTestDriver<SplitterAlgo> state(def, some_price, tcase.target);
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.child_orders().size() == expected);
REQUIRE(state.state() == AlgoState::RUNNING);
}
}
TEST_CASE("completes on target", "[algo][splitter]") {
std::vector<SimpleCase> cases;
cases.push_back(mkSimple(ExpectedCount{3}, 30, 90, 50));
cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50));
cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50));
cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50));
cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150));
cases.push_back(mkSimple(ExpectedCount{4}, 13, 50, 150));
cases.push_back(mkSimple(ExpectedCount{12}, 13, 150, 50));
for (const auto &tcase : cases) {
auto expected = tcase.expected_orders.value();
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
*def.mutable_custom_def()->mutable_splitter() = tcase.sod;
AlgoTestDriver<SplitterAlgo> state(def, some_price, tcase.target);
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.child_orders().size() == expected);
REQUIRE(state.state() == AlgoState::RUNNING);
SECTION("Fill completely") {
auto orders = state.child_orders();
// Fill the order
size_t filled = 0;
size_t iterations = 0;
while (state.state() == AlgoState::RUNNING) {
// loop to allow more orders to be added
++iterations;
REQUIRE(iterations < 999);
for (const auto &order : orders) {
state.fill(order.key, Price(some_price.price()),
order.cstate.quantity);
filled += order.cstate.quantity.value();
REQUIRE(filled == state.total_fills().value());
if (filled >= tcase.target.value()) {
REQUIRE(state.state() == AlgoState::COMPLETED);
break;
} else {
REQUIRE(state.state() == AlgoState::RUNNING);
}
}
}
}
SECTION("fill one by one") {
const auto &orders = state.child_orders();
// Fill 1 per iteration.
// Algo should add more orders if not enough are visible initially
size_t filled = 0;
size_t iterations = 0;
while (state.state() == AlgoState::RUNNING) {
++iterations;
REQUIRE(iterations < 999); // not making progress
for (const auto &order : orders) {
if (order.cstate.status == ChildStatus::WORKING) {
auto price = Price{some_price.price()};
state.fill(order.key, price, Quantity{1});
filled += 1;
REQUIRE(filled == state.total_fills().value());
// Fills should never cause new orders to get added
REQUIRE(state.child_orders().size() == expected);
if (filled >= tcase.target.value()) {
REQUIRE(state.state() == AlgoState::COMPLETED);
break;
} else {
REQUIRE(state.state() == AlgoState::RUNNING);
}
}
}
}
}
}
}
TEST_CASE("updates", "[algo][splitter]") {
// Start in state 0, update to state 1
std::vector<std::tuple<SimpleCase, std::vector<uint32_t>, SimpleCase>> cases;
// reduce max order size, increase # orders
cases.push_back({mkSimple(ExpectedCount{10} /*order count*/, 10 /*max*/,
100 /*exposed*/, 50 /*target*/),
{},
mkSimple(ExpectedCount{20}, 5, 100, 50)});
/// reduce max order size, keep # orders constant
cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50),
{},
mkSimple(ExpectedCount{10}, 5, 50, 50)});
cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50),
{5, 5},
mkSimple(ExpectedCount{8}, 5, 50, 40)});
cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50),
{5, 5},
mkSimple(ExpectedCount{18}, 5, 100, 50)});
for (const auto &tcase : cases) {
auto case_init = std::get<0>(tcase);
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
*def.mutable_custom_def()->mutable_splitter() = case_init.sod;
AlgoTestDriver<SplitterAlgo> state(def, some_price, case_init.target);
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == case_init.expected_orders.value());
for (const auto &o : state.child_orders()) {
if (o.cstate.status == ChildStatus::WORKING) {
REQUIRE(o.cstate.quantity.value() <= case_init.sod.exposed_qty());
}
}
uint32_t fills = 0;
for (auto q : std::get<1>(tcase)) {
state.fill({}, Quantity{q});
fills += q;
}
REQUIRE(state.total_fills().value() == fills);
Update update;
auto case_next = std::get<2>(tcase);
update.set_price(some_price.price());
update.set_quantity(case_next.target.value());
*def.mutable_custom_def()->mutable_splitter() = case_next.sod;
*update.mutable_custom() = def.custom_def();
state.do_update(update);
REQUIRE(state.state() == AlgoState::RUNNING);
size_t working = 0;
for (const auto &c : state.child_orders())
if (c.cstate.status == ChildStatus::WORKING)
working += 1;
REQUIRE(working == case_next.expected_orders.value());
for (const auto &o : state.child_orders()) {
if (o.cstate.status == ChildStatus::WORKING) {
REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty());
}
}
}
}
TEST_CASE("Reduction with fills", "[algo][splitter]") {
// Fills represent a floor on how much a trade can be reduced
// IF we have expose:400, clip: 100 we get
// 100,100,100,100
// Receive fills
// 100/50, 100/50, 100/50, 100/50
// Change expose to 300
// naive: 100, 100, 100 but we *can't* reduce the last to 0
// expected: 100, 100, 50, 50
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(400);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
// Add fills
for (const auto &order : state.child_orders()) {
state.fill(order.key, {}, Quantity{50});
}
// Reduce exposed quantity
Update update;
update.set_quantity(300);
update.mutable_custom()->mutable_splitter()->set_exposed_qty(300);
state.do_update(update);
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
size_t total = 0;
for (const auto &order : state.child_orders()) {
total += order.cstate.quantity.value();
}
REQUIRE(total == 300);
// need this to be 100,100,50,50 not 100,100,100
std::set<uint32_t> actual, expected{100, 100, 50, 50};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
REQUIRE(state.total_fills().value() == 200);
}
TEST_CASE("Reduction with fills 2", "[algo][splitter]") {
// Fills represent a floor on how much a trade can be reduced
// IF we have expose:400, clip: 100 we get
// 100,100,100,100
// Receive fills
// 100/35, 100/45, 100/55, 100/65
// Change expose to 300
// naive: 100, 100, 100 but we *can't* reduce the last to 0
// expected: 100, 100, 65, 35
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(400);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
// Add fills
state.fill({}, Quantity{35});
state.fill({}, Quantity{45});
state.fill({}, Quantity{55});
state.fill({}, Quantity{65});
// Reduce exposed quantity
Update update;
update.set_quantity(300);
update.mutable_custom()->mutable_splitter()->set_exposed_qty(300);
state.do_update(update);
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
size_t total = 0;
for (const auto &order : state.child_orders()) {
total += order.cstate.quantity.value();
}
REQUIRE(total == 300);
// need this to be 100,100,50,50 not 100,100,100
std::set<uint32_t> actual, expected{100, 100, 65, 35};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
}
TEST_CASE("Reduction with fills 3", "[algo][splitter]") {
// Fills represent a floor on how much a trade can be reduced
// IF we have expose:400, clip: 100 we get
// 100,100,100,100
// Receive fills
// 100/35, 100/45, 100/55, 100/65
// Change expose to 201
// naive: 100, 100, 1 but we *can't* reduce the last to 0
// expected: 66, 55, 45, 35
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(400);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
// Add fills
state.fill({}, Quantity{35});
state.fill({}, Quantity{45});
state.fill({}, Quantity{55});
state.fill({}, Quantity{65});
// Reduce exposed quantity
Update update;
update.set_quantity(201);
update.mutable_custom()->mutable_splitter()->set_exposed_qty(201);
state.do_update(update);
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
size_t total = 0;
for (const auto &order : state.child_orders()) {
total += order.cstate.quantity.value();
}
REQUIRE(total == 201);
std::set<uint32_t> actual, expected{66, 55, 45, 35};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
}
TEST_CASE("reduce to completion", "[algo][splitter]") {
// Fills represent a floor on how much a trade can be reduced
// IF we have expose:400, clip: 100 we get
// 100,100,100,100
// Receive fills
// 100/100, 100/100, 100/0, 100/00
// Change expose to 200
// expected: algo transitions to copmleted state canceling 2 orders
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(400);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
// Add fills
state.fill({}, Quantity{100});
state.fill({}, Quantity{100});
// Reduce exposed quantity
Update update;
update.set_quantity(200);
def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(200);
state.do_update(update);
REQUIRE(state.state() == AlgoState::COMPLETED);
size_t total = 0;
for (const auto &order : state.child_orders()) {
if (order.cstate.status == ChildStatus::WORKING) {
total += order.cstate.quantity.value();
} else {
total += order.cstate.total_fills.value();
}
}
REQUIRE(total == 200);
}
TEST_CASE("reduce to completion 2", "[algo][splitter]") {
// Fills represent a floor on how much a trade can be reduced
// IF we have expose:400, clip: 100 we get
// 100,100,100,100
// Receive fills
// 100/100, 100/100, 100/0, 100/00
// Change expose to 200
// expected: algo transitions to copmleted state canceling 2 orders
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(400);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
// Add fills
state.fill({}, Quantity{100});
state.fill({}, Quantity{50});
state.fill({}, Quantity{50});
// Reduce exposed quantity
Update update;
update.set_quantity(200);
state.do_update(update);
REQUIRE(state.state() == AlgoState::COMPLETED);
}
TEST_CASE("Async Fills", "[algo][splitter]") {
// OK, NOW it gets tricky because we're going to test the asyncronous stuff
// Start with 4 orders size 20 outstanding (80 total exposed)
// Change exposure to 64 so one of the four will be reduced to 5
// Before the change is confirmed fill the one that is reducing by 7
// which would constitute an overfill
// Algo must reduce a second order to compensate
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(20);
splitter->set_exposed_qty(80);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{80});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 4);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 20);
}
// Set up some instant fills
bool applied_fills = false;
state.on_child_changing([&](const auto &) {
if (applied_fills)
return;
applied_fills = true;
// 7 fills on the last order which is has a size reduction in flight
state.fill(state.child_orders().rbegin()->key, {}, Quantity{7});
});
Update update;
update.set_price(state.price().value());
update.set_quantity(65);
update.mutable_custom()->mutable_splitter()->set_exposed_qty(65);
// This should trigger 1 order to be reduced to size 5 from 20
// in the meantime we'll trigger fills of size 7
// This results in an overfill for the 5 lot order
// And the
state.do_update(update);
// expected result:
// 7 fills, exposed_qty 65 = 58 active
uint32_t active = 0;
for (const auto &o : state.child_orders())
active += o.active_qty();
REQUIRE(active == 58);
}
TEST_CASE("Increase then decrease", "[algo][splitter]") {
// Start with 3 orders of size 100
// Increase the size to 400 so we launch a 4th order
// Then instantly send another update to 350
// NOTE: this test is not very good due to limitations in the test driver
// I'm leaving it in because it passes but if it starts failing in the future
// it's probably OK to disable it
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
auto *splitter = def.mutable_custom_def()->mutable_splitter();
splitter->set_max_order_qty(100);
splitter->set_exposed_qty(300);
AlgoTestDriver<SplitterAlgo> state(def, some_price, Quantity{300});
REQUIRE(state.before_launch().ok());
state.launch();
// check initial state
REQUIRE(state.state() == AlgoState::RUNNING);
REQUIRE(state.child_orders().size() == 3);
for (const auto &c : state.child_orders()) {
REQUIRE(c.cstate.quantity.value() == 100);
}
Update update;
update.set_price(state.price().value());
update.set_quantity(state.quantity().value());
update.mutable_custom()->mutable_splitter()->set_exposed_qty(400);
// Set up some instant fills
bool applied_second_update = false;
state.on_child_launching([&, update](const auto &) mutable {
if (!applied_second_update) {
applied_second_update = true;
update.mutable_custom()->mutable_splitter()->set_exposed_qty(350);
state.do_update(update);
}
});
state.do_update(update);
// expected result:
// 350 exposed, NOT 450
uint32_t quantity = 0;
for (const auto &o : state.child_orders())
quantity += o.active_qty();
REQUIRE(quantity == 350);
}
} // namespace test
} // namespace orderman
} // namespace bts
+1
View File
@@ -24,6 +24,7 @@ public:
bool operator==(ValueWrapper const&v) const {return _v == v._v;}
bool operator!=(ValueWrapper const&v) const {return _v != v._v;}
bool operator<(ValueWrapper const&v) const {return _v < v._v;}
bool operator>(ValueWrapper const&v) const {return _v > v._v;}
ValueT const& value() const {return _v;}
ValueT & value() {return _v;}
private: