Merge branch 'grid-settings' into SPARK-1799-add-hedge-perf-traces
This commit is contained in:
commit
58c5eeb6d1
257 files changed
+11368
-3954
No files matched your search
@@ -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/services/AdminService.proto;api/services/ApiUnitTestService.proto;api/services/ErrorReportService.proto;api/services/HelloService.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/services/UserService.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/Executor.conf;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cpp_config.conf;config/defaults/dynamic_orders.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.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/cpp_config.proto;config/protos/dynamic_orders.proto;config/protos/expiration_display_offset.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/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/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.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/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/OrderServicesData.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;ordLine 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/services/AdminService.proto;api/services/ApiUnitTestService.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/services/UserService.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/Executor.conf;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/dynamic_orders.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/dynamic_orders.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/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/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.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/OrderServicesData.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTLine truncated
|
||||
@@ -1,34 +1,40 @@
|
||||
#include "lib/api/services/AdminService.server.pb.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/md/get_symbols.h"
|
||||
#include "lib/config/protos/cme_reference_data.pb.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace bts{
|
||||
namespace admin{
|
||||
////////////////////////////////////////////
|
||||
// connection accessors ////////////////////
|
||||
////////////////////////////////////////////
|
||||
uint32_t AdminServiceUtils::find_max_connection_id (const bts::api::admin::AdminData& data){
|
||||
uint32_t AdminServiceUtils::find_max_connection_id (const AdminData& data){
|
||||
uint32_t rv = 0;
|
||||
for(const auto &conn : data.connections())
|
||||
if (conn.id() > rv)
|
||||
rv = conn.id();
|
||||
return rv;
|
||||
}
|
||||
const bts::api::admin::ExchangeConnection* AdminServiceUtils::find_connection(const bts::api::admin::AdminData& data,
|
||||
const std::string& connection_name){
|
||||
const ExchangeConnection* AdminServiceUtils::find_connection(
|
||||
const AdminData& data,
|
||||
const std::string& connection_name){
|
||||
for(const auto& connection : data.connections())
|
||||
if(connection.name()==connection_name)
|
||||
return &connection;
|
||||
return nullptr;
|
||||
}
|
||||
bool AdminServiceUtils::has_exchange(const bts::api::admin::ExchangeConnection& conn,
|
||||
bts::api::Venue exchange){
|
||||
bool AdminServiceUtils::has_exchange(const ExchangeConnection& conn,
|
||||
api::Venue exchange){
|
||||
bool rv = false;
|
||||
switch(exchange)
|
||||
{
|
||||
case api::Venue::VENUE_GLBX:
|
||||
rv = conn.has_cme_connection() &&
|
||||
(conn.cme_connection().has_cgw_connection() || conn.cme_connection().has_msgw_connection());
|
||||
(conn.cme_connection().has_cgw_connection() ||
|
||||
conn.cme_connection().has_msgw_connection());
|
||||
break;
|
||||
case api::Venue::VENUE_UNSET:
|
||||
case api::Venue::VENUE_SIM:
|
||||
@@ -870,6 +876,89 @@ void AdminServiceUtils::validate_user_exchange_data (const bts::api::admin::BTSU
|
||||
}
|
||||
}
|
||||
}
|
||||
void AdminServiceUtils::get_md_entitlement_report(const AdminData& admin_data,
|
||||
std::vector<MDEntitlementReportUnit>& entitlements){
|
||||
for (const auto& user : admin_data.users()){
|
||||
const CMEUserData* cme_data=AdminServiceUtils::get_user_cme_data(user,
|
||||
admin_data);
|
||||
if(cme_data!=nullptr){
|
||||
for (const auto& cme_mic_data : cme_data->md_permissions()){
|
||||
MDEntitlementReportUnit rv=get_cme_md_entitlement_unit(user.name(),
|
||||
cme_mic_data);
|
||||
entitlements.push_back(rv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
MDEntitlementReportUnit AdminServiceUtils::get_cme_md_entitlement_unit(
|
||||
const std::string& user,
|
||||
const CMEMicData& cme_mic_data){
|
||||
MDEntitlementReportUnit rv;
|
||||
rv.set_user(user);
|
||||
rv.set_exchange("GLBX");
|
||||
switch(cme_mic_data.mic())
|
||||
{
|
||||
case api::CMEMics::CME:
|
||||
rv.set_mic("CME");
|
||||
break;
|
||||
case api::CMEMics::NYMEX:
|
||||
rv.set_mic("NYMEX");
|
||||
break;
|
||||
case api::CMEMics::CBT:
|
||||
rv.set_mic("CBT");
|
||||
break;
|
||||
case api::CMEMics::COMEX:
|
||||
rv.set_mic("COMEX");
|
||||
break;
|
||||
case api::CMEMics::MGE:
|
||||
rv.set_mic("MGE");
|
||||
break;
|
||||
case api::CMEMics::DME:
|
||||
rv.set_mic("DME");
|
||||
break;
|
||||
case api::CMEMics::NYMSW:
|
||||
rv.set_mic("NYMSW");
|
||||
break;
|
||||
case api::CMEMics::FXS:
|
||||
rv.set_mic("FXS");
|
||||
break;
|
||||
case api::CMEMics::UNSET:
|
||||
case api::CMEMics::CMEMics_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case api::CMEMics::CMEMics_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
rv.set_mic("");
|
||||
break;
|
||||
}
|
||||
rv.set_enabled(cme_mic_data.enabled());
|
||||
std::ostringstream key;
|
||||
key << rv.user() << rv.exchange() << rv.mic();
|
||||
rv.set_record_id(key.str());
|
||||
return rv;
|
||||
}
|
||||
void AdminServiceUtils::get_user_symbol_permissions(const AdminData& data,
|
||||
const BTSUser& user,
|
||||
std::vector<UserSymbolMDPermissionUnit>& rv){
|
||||
std::map<api::CMEMics, bool> mic_perm_map;
|
||||
auto exchange = api::Venue::VENUE_GLBX;
|
||||
const CMEUserData* cme_data=get_user_cme_data(user,data);
|
||||
if(cme_data!=nullptr)
|
||||
for(const auto& mic_data : cme_data->md_permissions())
|
||||
if(mic_data.enabled())
|
||||
mic_perm_map[mic_data.mic()]=true;
|
||||
const auto& cme_symb_mics = config::get<config::CmeSymbolMics>().cme_mic();
|
||||
for(auto it=cme_symb_mics.begin();it!=cme_symb_mics.end();it++){
|
||||
const std::string& symbol=it->first;
|
||||
api::CMEMics mic=it->second;
|
||||
UserSymbolMDPermissionUnit u;
|
||||
std::ostringstream key;
|
||||
key << exchange << user.name() << symbol;
|
||||
u.set_record_id(key.str());
|
||||
u.set_user(user.name());
|
||||
u.set_exchange(exchange);
|
||||
u.set_symbol(symbol);
|
||||
u.set_enabled(mic_perm_map.find(mic)!=mic_perm_map.end());
|
||||
rv.push_back(u);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////
|
||||
// user mutators ///////////////////////////
|
||||
////////////////////////////////////////////
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
#include "lib/api/services/AdminService.server.pb.h"
|
||||
#include "lib/api/services/OrderService.server.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
|
||||
namespace bts{
|
||||
namespace admin{
|
||||
|
||||
using namespace bts::api::admin;
|
||||
using namespace bts::api::orderman;
|
||||
|
||||
class AdminServiceUtils{
|
||||
public:
|
||||
////////////////////////////////////////////
|
||||
@@ -17,233 +18,286 @@ public:
|
||||
////////////////////////////////////////////
|
||||
// finds the max id out of all the existing connections
|
||||
// (todo: deleted connections)
|
||||
static uint32_t find_max_connection_id (const bts::api::admin::AdminData& data);
|
||||
static uint32_t find_max_connection_id (const AdminData& data);
|
||||
// finds a connection by name and returns a mutable pointer to it
|
||||
static const bts::api::admin::ExchangeConnection* find_connection(const bts::api::admin::AdminData& data,
|
||||
const std::string& connection_name);
|
||||
static const ExchangeConnection* find_connection(const AdminData& data,
|
||||
const std::string& connection_name);
|
||||
// finds the presence of an exchange in a connection
|
||||
static bool has_exchange(const bts::api::admin::ExchangeConnection& conn,
|
||||
static bool has_exchange(const ExchangeConnection& conn,
|
||||
bts::api::Venue exchange);
|
||||
// finds the default connections for an exchange
|
||||
// in a given account
|
||||
static const bts::api::admin::ExchangeConnection* find_default_account_connection(const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& account,
|
||||
bts::api::Venue exchange);
|
||||
static const bts::api::admin::ExchangeConnection* resolve_cme_connection(const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& account,
|
||||
const bts::api::Security& sec);
|
||||
static const bts::api::admin::ExchangeConnection* resolve_sim_connection(const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& account,
|
||||
const bts::api::Security& sec);
|
||||
static const bts::api::admin::ExchangeConnection* resolve_connection(const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& account,
|
||||
const bts::api::Security& sec);
|
||||
static const ExchangeConnection* find_default_account_connection(
|
||||
const AdminData& data,
|
||||
const BTSAccount& account,
|
||||
bts::api::Venue exchange);
|
||||
static const ExchangeConnection* resolve_cme_connection(
|
||||
const AdminData& data,
|
||||
const BTSAccount& account,
|
||||
const api::Security& sec);
|
||||
static const ExchangeConnection* resolve_sim_connection(
|
||||
const AdminData& data,
|
||||
const BTSAccount& account,
|
||||
const api::Security& sec);
|
||||
static const ExchangeConnection* resolve_connection(
|
||||
const AdminData& data,
|
||||
const BTSAccount& account,
|
||||
const bts::api::Security& sec);
|
||||
////////////////////////////////////////////
|
||||
// connection mutators /////////////////////
|
||||
////////////////////////////////////////////
|
||||
// adds a new connection (does not check for name uniqueness)
|
||||
static bts::api::admin::ExchangeConnection* add_connection(bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::ExchangeConnection& new_connection);
|
||||
static ExchangeConnection* add_connection(AdminData& data,
|
||||
const ExchangeConnection& new_connection);
|
||||
// reads the connections from config and fills in the
|
||||
// admin data (with names only)
|
||||
static void fill_connections_from_config(bts::api::admin::AdminData& admin_data);
|
||||
static void fill_connections_from_config(AdminData& admin_data);
|
||||
////////////////////////////////////////////
|
||||
// account accessors ///////////////////////
|
||||
////////////////////////////////////////////
|
||||
// finds the account from the name and returns
|
||||
// a mutable pointer to the account
|
||||
static bts::api::admin::BTSAccount* find_account (bts::api::admin::AdminData& data, const std::string& acct_name, bool include_deleted=false);
|
||||
static BTSAccount* find_account (AdminData& data,
|
||||
const std::string& acct_name,
|
||||
bool include_deleted=false);
|
||||
// same as above for const pointer
|
||||
static const bts::api::admin::BTSAccount* find_account (const bts::api::admin::AdminData& data, const std::string& acct_name, bool include_deleted=false);
|
||||
static const BTSAccount* find_account (const AdminData& data,
|
||||
const std::string& acct_name,
|
||||
bool include_deleted=false);
|
||||
// finds the account from the id and returns
|
||||
// a mutable pointer to the account
|
||||
static bts::api::admin::BTSAccount* find_account (bts::api::admin::AdminData& data, uint32_t id, bool include_deleted=false);
|
||||
static BTSAccount* find_account (AdminData& data,
|
||||
uint32_t id,
|
||||
bool include_deleted=false);
|
||||
// like above but returns a const pointer
|
||||
static const bts::api::admin::BTSAccount* find_account (const bts::api::admin::AdminData& data, uint32_t id, bool include_deleted=false);
|
||||
static const BTSAccount* find_account (const AdminData& data,
|
||||
uint32_t id,
|
||||
bool include_deleted=false);
|
||||
// this function is an indirection layer for account access
|
||||
// so deletions are dealt in a more deliberate manner
|
||||
static std::vector<const bts::api::admin::BTSAccount*> accounts (const bts::api::admin::AdminData& data, bool include_deleted);
|
||||
static std::vector<bts::api::admin::BTSAccount*> accounts_mutable (bts::api::admin::AdminData& data, bool include_deleted);
|
||||
static std::vector<const BTSAccount*> accounts (const AdminData& data,
|
||||
bool include_deleted);
|
||||
static std::vector<BTSAccount*> accounts_mutable (AdminData& data,
|
||||
bool include_deleted);
|
||||
// finds the max id amongts all the existing accounts
|
||||
static uint32_t find_max_acct_id (const bts::api::admin::AdminData& data);
|
||||
static uint32_t find_max_acct_id (const AdminData& data);
|
||||
// gets the account data for cme (and goes up the hierarchy
|
||||
// if needed)
|
||||
static const bts::api::admin::CMEAccountData* get_account_cme_data(const bts::api::admin::BTSAccount& account,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static const bts::api::admin::SIMAccountData* get_account_sim_data(const bts::api::admin::BTSAccount& account,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static const CMEAccountData* get_account_cme_data(const BTSAccount& account,
|
||||
const AdminData& data);
|
||||
static const SIMAccountData* get_account_sim_data(const BTSAccount& account,
|
||||
const AdminData& data);
|
||||
// returns true if an account has a connection
|
||||
bool static account_has_connection(const bts::api::admin::BTSAccount& account, const std::string& connection_name);
|
||||
bool static account_has_connection(const BTSAccount& account,
|
||||
const std::string& connection_name);
|
||||
// finds the external/clearing account associated
|
||||
// with an account
|
||||
static std::string find_account_exchange_account (const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& account,
|
||||
api::Venue exchange,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static std::string find_account_exchange_account (const AdminData& data,
|
||||
const BTSAccount& account,
|
||||
api::Venue exchange,
|
||||
AdminServiceOperationResult& out_result);
|
||||
// returns a map of the accounts vs their 'is_active'
|
||||
static std::map<uint32_t,bool> accounts_map(const bts::api::admin::AdminData& admin_data);
|
||||
static std::map<uint32_t,bool> accounts_map(const AdminData& admin_data);
|
||||
// validates that the update to the account is valid
|
||||
static void validate_account_update (const bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::BTSAccount& updated_account,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void validate_account_update (const AdminData& admin_data,
|
||||
const BTSAccount& updated_account,
|
||||
AdminServiceOperationResult& result);
|
||||
// validates that the account exchange changes are valid
|
||||
// (ex > 12 characters for accounts at CME)
|
||||
static void validate_account_exchange_data (const bts::api::admin::BTSAccount& account,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static bts::api::orderman::RoutingRecord get_routing_record(const bts::api::admin::AdminData& data,
|
||||
const std::string& account_name,
|
||||
const std::string& user_name,
|
||||
const bts::api::Security& sec,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static bts::api::orderman::RoutingRecord get_routing_record(const bts::api::admin::AdminData& data,
|
||||
uint32_t account_id,
|
||||
uint32_t user_id,
|
||||
const bts::api::Security& sec,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static void validate_account_exchange_data (const BTSAccount& account,
|
||||
AdminServiceOperationResult& out_result);
|
||||
static RoutingRecord get_routing_record(
|
||||
const AdminData& data,
|
||||
const std::string& account_name,
|
||||
const std::string& user_name,
|
||||
const bts::api::Security& sec,
|
||||
AdminServiceOperationResult& out_result);
|
||||
static RoutingRecord get_routing_record(const AdminData& data,
|
||||
uint32_t account_id,
|
||||
uint32_t user_id,
|
||||
const bts::api::Security& sec,
|
||||
AdminServiceOperationResult& out_result);
|
||||
////////////////////////////////////////////
|
||||
// account mutators ////////////////////////
|
||||
////////////////////////////////////////////
|
||||
static bts::api::admin::BTSAccount* create_account(bts::api::admin::AdminData& data,
|
||||
uint32_t parent_account_id,
|
||||
const std::string& account_name,
|
||||
const std::string& description,
|
||||
bool is_active,
|
||||
bts::api::admin::AdminServiceCreateAccountResult& result);
|
||||
static BTSAccount* create_account(AdminData& data,
|
||||
uint32_t parent_account_id,
|
||||
const std::string& account_name,
|
||||
const std::string& description,
|
||||
bool is_active,
|
||||
AdminServiceCreateAccountResult& result);
|
||||
// writes over the account data completely
|
||||
static void update_account(bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSAccount& updated_account,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void delete_account (bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::IDAPIArgument& id,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void set_account_exchange_data(bts::api::admin::BTSAccount* account,
|
||||
const bts::api::admin::ExchangeAccountData& exchange_data);
|
||||
static void update_account(AdminData& data,
|
||||
const BTSAccount& updated_account,
|
||||
AdminServiceOperationResult& result);
|
||||
static void delete_account (AdminData& admin_data,
|
||||
const IDAPIArgument& id,
|
||||
AdminServiceOperationResult& result);
|
||||
static void set_account_exchange_data(BTSAccount* account,
|
||||
const ExchangeAccountData& exchange_data);
|
||||
// this is not a generally useful function but since
|
||||
// connection are in config, this cleans up the account's
|
||||
// connections and syncs it with the list in AdminData
|
||||
static void sync_account_connections(const bts::api::admin::AdminData& data,
|
||||
bts::api::admin::BTSAccount& account);
|
||||
static void sync_account_connections(const AdminData& data,
|
||||
BTSAccount& account);
|
||||
////////////////////////////////////////////
|
||||
// group accessors /////////////////////////
|
||||
////////////////////////////////////////////
|
||||
// given an id, finds the mutable pointer to a group
|
||||
static bts::api::admin::BTSUserGroup* find_group (bts::api::admin::AdminData& data, uint32_t id);
|
||||
static BTSUserGroup* find_group (AdminData& data, uint32_t id);
|
||||
// given an id, finds the const pointer to a group
|
||||
static const bts::api::admin::BTSUserGroup* const find_group (const bts::api::admin::AdminData& data, uint32_t id);
|
||||
static const BTSUserGroup* const find_group (const AdminData& data,
|
||||
uint32_t id);
|
||||
// given a name, finds the mutable pointer to a group
|
||||
static bts::api::admin::BTSUserGroup* find_group (bts::api::admin::AdminData& data, const std::string& group_name);
|
||||
static BTSUserGroup* find_group (AdminData& data,
|
||||
const std::string& group_name);
|
||||
// finds the max id out of all the groups
|
||||
static uint32_t find_max_group_id (const bts::api::admin::AdminData& data);
|
||||
static uint32_t find_max_group_id (const AdminData& data);
|
||||
// travels up the group tree until it finds a populated cme data
|
||||
static const bts::api::admin::CMEUserData* get_group_cme_data(const bts::api::admin::BTSUserGroup& group,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static const bts::api::admin::SIMUserData* get_group_sim_data(const bts::api::admin::BTSUserGroup& group,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static const bts::api::admin::UserAccountPermissions* const get_group_user_permissions (const bts::api::admin::AdminData& admin_data,
|
||||
uint32_t group_id);
|
||||
static const CMEUserData* get_group_cme_data(const BTSUserGroup& group,
|
||||
const AdminData& data);
|
||||
static const SIMUserData* get_group_sim_data(const BTSUserGroup& group,
|
||||
const AdminData& data);
|
||||
static const UserAccountPermissions* const get_group_user_permissions (
|
||||
const bts::api::admin::AdminData& admin_data,
|
||||
uint32_t group_id);
|
||||
////////////////////////////////////////////
|
||||
// group mutators/ /////////////////////////
|
||||
////////////////////////////////////////////
|
||||
static bts::api::admin::BTSUserGroup* create_group(bts::api::admin::AdminData& data,
|
||||
uint32_t parent_group_id,
|
||||
const std::string& group_name,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static BTSUserGroup* create_group(AdminData& data,
|
||||
uint32_t parent_group_id,
|
||||
const std::string& group_name,
|
||||
AdminServiceOperationResult& result);
|
||||
////////////////////////////////////////////
|
||||
// user accessors //////////////////////////
|
||||
////////////////////////////////////////////
|
||||
static bts::api::admin::BTSUser* find_user (bts::api::admin::AdminData& data, const std::string& user_name, bool include_deleted=false);
|
||||
static const bts::api::admin::BTSUser* find_user (const bts::api::admin::AdminData& data, const std::string& user_name, bool include_deleted=false);
|
||||
static bts::api::admin::BTSUser* find_user (bts::api::admin::AdminData& data, uint32_t id, bool include_deleted=false);
|
||||
static const bts::api::admin::BTSUser* const find_user (const bts::api::admin::AdminData& data, uint32_t id, bool include_deleted=false);
|
||||
static std::vector<const bts::api::admin::BTSUser*> users (const bts::api::admin::AdminData& data, bool include_deleted);
|
||||
static std::vector<bts::api::admin::BTSUser*> users_mutable (bts::api::admin::AdminData& data, bool include_deleted);
|
||||
static uint32_t find_max_user_id (const bts::api::admin::AdminData& data);
|
||||
static const bts::api::admin::CMEUserData* get_user_cme_data(const bts::api::admin::BTSUser& user,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static const bts::api::admin::SIMUserData* get_user_sim_data(const bts::api::admin::BTSUser& user,
|
||||
const bts::api::admin::AdminData& data);
|
||||
static BTSUser* find_user (AdminData& data,
|
||||
const std::string& user_name,
|
||||
bool include_deleted=false);
|
||||
static const BTSUser* find_user (const AdminData& data,
|
||||
const std::string& user_name,
|
||||
bool include_deleted=false);
|
||||
static BTSUser* find_user (AdminData& data,
|
||||
uint32_t id,
|
||||
bool include_deleted=false);
|
||||
static const BTSUser* const find_user (const AdminData& data,
|
||||
uint32_t id,
|
||||
bool include_deleted=false);
|
||||
static std::vector<const BTSUser*> users (const AdminData& data,
|
||||
bool include_deleted);
|
||||
static std::vector<BTSUser*> users_mutable (AdminData& data,
|
||||
bool include_deleted);
|
||||
static uint32_t find_max_user_id (const AdminData& data);
|
||||
static const CMEUserData* get_user_cme_data(const BTSUser& user,
|
||||
const AdminData& data);
|
||||
static const SIMUserData* get_user_sim_data(const BTSUser& user,
|
||||
const AdminData& data);
|
||||
// finds the "exchange user" (ex: "operator_id" at CME or "authorized_trader"
|
||||
// at ICE) for a user
|
||||
static std::string find_user_exchange_id (const bts::api::admin::AdminData& data,
|
||||
const bts::api::admin::BTSUser& user,
|
||||
api::Venue exchange,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static std::pair<const bts::api::admin::BTSAccount*, const bts::api::admin::BTSUser*>
|
||||
find_user_account_matches (const bts::api::admin::AdminData& admin_data,
|
||||
const std::string& connection, const std::string& exchange_account,
|
||||
static std::string find_user_exchange_id (
|
||||
const AdminData& data,
|
||||
const BTSUser& user,
|
||||
api::Venue exchange,
|
||||
AdminServiceOperationResult& out_result);
|
||||
static std::pair<const BTSAccount*, const BTSUser*>
|
||||
find_user_account_matches (const AdminData& admin_data,
|
||||
const std::string& connection,
|
||||
const std::string& exchange_account,
|
||||
const std::string& exchange_user,
|
||||
api::Venue exchange);
|
||||
// for a given user finds the account permissions (if permissions are
|
||||
// derived from the group, it traverses the group hierarcy to find the
|
||||
// correct permissions.
|
||||
static const bts::api::admin::UserAccountPermissions* get_user_permissions (const bts::api::admin::AdminData& admin_data, uint32_t user_id);
|
||||
static const UserAccountPermissions* get_user_permissions (
|
||||
const AdminData& admin_data,
|
||||
uint32_t user_id);
|
||||
// for a given user, fills in the associated_accounts ref arg
|
||||
// with the accounts the user has permission to trade in
|
||||
static void find_user_trading_accounts (const bts::api::admin::AdminData& admin_data,
|
||||
uint32_t user_id,
|
||||
std::vector<const api::admin::BTSAccount*>& associated_accounts);
|
||||
static void find_user_trading_accounts (
|
||||
const AdminData& admin_data,
|
||||
uint32_t user_id,
|
||||
std::vector<const BTSAccount*>& associated_accounts);
|
||||
// validates that changes in user data satisfy all the
|
||||
// constraints
|
||||
static void validate_user_update (const bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::BTSUser& updated_user,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void validate_user_update (const AdminData& admin_data,
|
||||
const BTSUser& updated_user,
|
||||
AdminServiceOperationResult& result);
|
||||
// vaidates the exchange part of user data updates
|
||||
static void validate_user_exchange_data (const bts::api::admin::BTSUser& updated_user,
|
||||
bts::api::admin::AdminServiceOperationResult& out_result);
|
||||
static void validate_user_exchange_data (
|
||||
const BTSUser& updated_user,
|
||||
AdminServiceOperationResult& out_result);
|
||||
static void get_md_entitlement_report(const AdminData& admin_data,
|
||||
std::vector<MDEntitlementReportUnit>& entitlements);
|
||||
// Gets a reporting unit out of CMEMicData
|
||||
static MDEntitlementReportUnit get_cme_md_entitlement_unit(
|
||||
const std::string& user,
|
||||
const CMEMicData& cme_mic_data);
|
||||
static void get_user_symbol_permissions(
|
||||
const AdminData& data,
|
||||
const BTSUser& user,
|
||||
std::vector<UserSymbolMDPermissionUnit>& res);
|
||||
////////////////////////////////////////////
|
||||
// user mutators ///////////////////////////
|
||||
////////////////////////////////////////////
|
||||
static bts::api::admin::BTSUser* create_user (bts::api::admin::AdminData& data,
|
||||
uint32_t parent_group_id,
|
||||
const std::string& name,
|
||||
const std::string& password,
|
||||
bool is_active,
|
||||
const bts::api::admin::LicenseData& license_data,
|
||||
const bts::api::admin::LicenseEntitlements& license_entitlements,
|
||||
bts::api::admin::AdminServiceCreateEntityResult& result);
|
||||
static bts::api::admin::BTSUser* update_user(bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::BTSUser& updated_user,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void delete_user (bts::api::admin::AdminData& admin_data,
|
||||
const bts::api::admin::IDAPIArgument& id,
|
||||
bts::api::admin::AdminServiceOperationResult& result);
|
||||
static void set_user_exchange_data(bts::api::admin::BTSUser* user,
|
||||
const bts::api::admin::ExchangeUserData& exchange_user_data);
|
||||
static void set_user_md_entitlement_data(bts::api::admin::BTSUser* user,
|
||||
const bts::api::admin::ExchangeUserMarketDataEntitlementData& exchange_user_md_entitelemnt_data);
|
||||
static void clear_user_accounts (bts::api::admin::BTSUser* user);
|
||||
static void set_user_accounts (bts::api::admin::BTSUser* user,
|
||||
const bts::api::admin::UserAccountPermissions& user_account_permissions);
|
||||
static BTSUser* create_user (AdminData& data,
|
||||
uint32_t parent_group_id,
|
||||
const std::string& name,
|
||||
const std::string& password,
|
||||
bool is_active,
|
||||
const LicenseData& license_data,
|
||||
const LicenseEntitlements& license_entitlements,
|
||||
AdminServiceCreateEntityResult& result);
|
||||
static BTSUser* update_user(AdminData& admin_data,
|
||||
const BTSUser& updated_user,
|
||||
AdminServiceOperationResult& result);
|
||||
static void delete_user (AdminData& admin_data,
|
||||
const IDAPIArgument& id,
|
||||
AdminServiceOperationResult& result);
|
||||
static void set_user_exchange_data(
|
||||
BTSUser* user,
|
||||
const ExchangeUserData& exchange_user_data);
|
||||
static void set_user_md_entitlement_data(BTSUser* user,
|
||||
const ExchangeUserMarketDataEntitlementData&);
|
||||
static void clear_user_accounts (BTSUser* user);
|
||||
static void set_user_accounts (BTSUser* user,
|
||||
const UserAccountPermissions& user_account_permissions);
|
||||
////////////////////////////////////////////
|
||||
//risk limits //////////////////////////////
|
||||
////////////////////////////////////////////
|
||||
// sorting function used by sort_limits to
|
||||
// sort SecurityScope's
|
||||
static bool _sort_security_scopes (const bts::api::admin::SecurityScope& a, const bts::api::admin::SecurityScope& b);
|
||||
static bool _sort_security_scopes (const SecurityScope& a,
|
||||
const SecurityScope& b);
|
||||
// sorting function used by sort_limits to
|
||||
// sort ProductScope's
|
||||
static bool _sort_product_scopes (const bts::api::admin::ProductScope& a, const bts::api::admin::ProductScope&);
|
||||
static bool _sort_product_scopes (const ProductScope& a,
|
||||
const ProductScope&);
|
||||
// this sorts the rules such that the most
|
||||
// specific ones are at the top. this makes it
|
||||
// easier to match rules with securities/products
|
||||
static void sort_limits (bts::api::admin::BTSAccount& account);
|
||||
static void sort_limits (BTSAccount& account);
|
||||
// creates a copy of AdminData with sorted rules
|
||||
static bts::api::admin::AdminData sort_limits (const bts::api::admin::AdminData& admin_data);
|
||||
static AdminData sort_limits (const AdminData& admin_data);
|
||||
// find the clip size for a security, account
|
||||
static const uint32_t find_account_security_clip_size(const bts::api::admin::BTSAccount& account,
|
||||
const api::Security& security);
|
||||
static const uint32_t find_account_security_clip_size(
|
||||
const BTSAccount& account,
|
||||
const api::Security& security);
|
||||
// find the position limit for an account, security
|
||||
static const bts::api::admin::AccountSecurityPositionLimitUnit* find_account_security_position_limit(const bts::api::admin::BTSAccount& account,
|
||||
static const AccountSecurityPositionLimitUnit*
|
||||
find_account_security_position_limit(const BTSAccount& account,
|
||||
const api::Security& security);
|
||||
// find the position limit for an account, product
|
||||
static const bts::api::admin::AccountProductPositionLimitUnit* find_account_product_position_limit(const bts::api::admin::BTSAccount& account,
|
||||
static const AccountProductPositionLimitUnit*
|
||||
find_account_product_position_limit(const BTSAccount& account,
|
||||
const api::Product& product);
|
||||
static bool match_security_scope (const bts::api::admin::SecurityScope& scope,
|
||||
const bts::api::Security& security);
|
||||
static bool match_product_scope (const bts::api::admin::ProductScope& scope,
|
||||
const bts::api::Product& product);
|
||||
static bool match_product(const api::Security& security, const api::Product& product);
|
||||
static bool _match_limit_rule_symbol (const std::string& symbol, const std::string& limit_symbol);
|
||||
static bool match_security_scope (const SecurityScope& scope,
|
||||
const api::Security& security);
|
||||
static bool match_product_scope (const ProductScope& scope,
|
||||
const api::Product& product);
|
||||
static bool match_product(const api::Security& security,
|
||||
const api::Product& product);
|
||||
static bool _match_limit_rule_symbol (const std::string& symbol,
|
||||
const std::string& limit_symbol);
|
||||
static api::SecurityType security_type(const api::Security& security);
|
||||
////////////////////////////////////////////
|
||||
// misc//////// ////////////////////////////
|
||||
|
||||
@@ -182,6 +182,17 @@ message AccountExchangeConnections{
|
||||
/***************************Exchange Data********************************/
|
||||
/************************************************************************/
|
||||
|
||||
message CMEMicData{
|
||||
api.CMEMics mic=10;
|
||||
bool enabled=20;
|
||||
// there are other things here
|
||||
}
|
||||
|
||||
message ICEMicData{
|
||||
api.CMEMics mic=10;
|
||||
bool enabled=20;
|
||||
}
|
||||
|
||||
message ExchangeUserMarketDataEntitlementData{
|
||||
CMEUserMarketDataEntitlementData cme_data = 10;
|
||||
ICEUserMarketDataEntitlementData ice_data = 20;
|
||||
@@ -276,6 +287,8 @@ message CMEUserData{
|
||||
string smp_instruction = 70;
|
||||
string max_order_rate_per_connection = 80;
|
||||
string location = 90;
|
||||
// add suff here
|
||||
repeated CMEMicData md_permissions = 1000;
|
||||
}
|
||||
|
||||
message SIMUserData {
|
||||
@@ -331,6 +344,48 @@ message ICEUserData{
|
||||
message ICEUserMarketDataEntitlementData{
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// Order Auxiliary Service ////////////////////////////////
|
||||
|
||||
message OrdersSummaryUnit{
|
||||
string account = 10;
|
||||
string user = 20;
|
||||
uint32 total_orders = 30;
|
||||
uint32 total_contracts = 40;
|
||||
}
|
||||
message OrdersSummary{
|
||||
repeated OrdersSummaryUnit units = 10;
|
||||
}
|
||||
message OrderSummaryRequest{
|
||||
}
|
||||
// top level orders that have account
|
||||
// and user that are present here, will
|
||||
// be cancelled. wild cards are not
|
||||
// supported.
|
||||
message OrderCancelRequest{
|
||||
repeated string accounts = 10;
|
||||
repeated string users = 20;
|
||||
}
|
||||
///////////////////////////////////////////////////////////
|
||||
// Reporting //////////////////////////////////////////////
|
||||
message MDEntitlementReportUnit{
|
||||
string record_id = 1 [(bts.api.key)=true];
|
||||
string user = 10;
|
||||
string exchange = 20;
|
||||
string mic = 30;
|
||||
bool enabled = 40;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Misc ///////////////////////////////////////////////////
|
||||
message UserSymbolMDPermissionUnit{
|
||||
string record_id = 1 [(bts.api.key)=true];
|
||||
string user=10;
|
||||
api.Venue exchange=20;
|
||||
string symbol=30;
|
||||
bool enabled=40;
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/************************************************************************/
|
||||
/************************************************************************/
|
||||
@@ -385,4 +440,11 @@ service AdminService {
|
||||
// subscription
|
||||
rpc admin_data(bts.api.VoidMessage) returns (stream AdminData);
|
||||
rpc get_admin_data(bts.api.VoidMessage) returns (AdminData);
|
||||
// orders
|
||||
rpc get_order_summary(bts.api.VoidMessage) returns (OrdersSummary);
|
||||
rpc cancel_orders(OrderCancelRequest) returns (bts.api.VoidMessage);
|
||||
// reports
|
||||
rpc md_entitlement_data(bts.api.VoidMessage) returns (stream MDEntitlementReportUnit);
|
||||
// misc
|
||||
rpc md_permissions_data(bts.api.VoidMessage) returns (stream UserSymbolMDPermissionUnit);
|
||||
}
|
||||
@@ -48,7 +48,8 @@ message TestResult {
|
||||
TEARDOWN = 2;
|
||||
PASS = 3;
|
||||
TIMEOUT = 4;
|
||||
IGNORED = 5;
|
||||
CRASH = 5;
|
||||
IGNORED = 6;
|
||||
}
|
||||
string test_name = 1;
|
||||
string message = 3;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
syntax="proto3";
|
||||
package bts.api.md.auxiliary;
|
||||
option csharp_namespace="bts.api.md.auxiliary";
|
||||
import "lib/api/apioptions.proto";
|
||||
import "lib/api/apicore.proto";
|
||||
|
||||
message VolumeAtPriceLevel{
|
||||
int32 record_id = 1 [(bts.api.key)=true];
|
||||
uint32 security_id = 20;
|
||||
double price = 30;
|
||||
uint32 volume = 40;
|
||||
uint32 buy_volume = 50;
|
||||
uint32 sell_volume = 60;
|
||||
uint32 passive_volume = 70;
|
||||
}
|
||||
|
||||
service MarketDataAuxiliaryService{
|
||||
option (bts.api.service_description)="Provides non-core supplementary market data";
|
||||
rpc volume_at_price(bts.api.VoidMessage) returns (stream VolumeAtPriceLevel);
|
||||
}
|
||||
+1438
-18
File diff suppressed because it is too large.
Load diff
@@ -2,17 +2,17 @@
|
||||
|
||||
DynamicOrderList {
|
||||
value {
|
||||
# dynamic_orders
|
||||
# {
|
||||
# type_name: 'DynamicOrder1'
|
||||
# parameter_keys: 'param1'
|
||||
# parameter_keys: 'param2'
|
||||
# }
|
||||
# dynamic_orders
|
||||
# {
|
||||
# type_name: 'DynamicOrder2'
|
||||
# parameter_keys: 'param3'
|
||||
# parameter_keys: 'param4'
|
||||
# }
|
||||
dynamic_orders
|
||||
{
|
||||
type_name: 'DynamicOrder1'
|
||||
parameter_keys: 'param1'
|
||||
parameter_keys: 'param2'
|
||||
}
|
||||
dynamic_orders
|
||||
{
|
||||
type_name: 'DynamicOrder2'
|
||||
parameter_keys: 'param3'
|
||||
parameter_keys: 'param4'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
ExpirationDisplayOffset
|
||||
{
|
||||
value
|
||||
{
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "lo"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "CL"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ob"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "RB"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "oh"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "HO"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "wbs"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "WBS"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "so"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "SI"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "og"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "GC"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "hxe"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "HG"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozs"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozw"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "osd"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "ZS"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozm"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "ZM"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozl"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "ZL"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozc"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "ZC"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "OPTION"
|
||||
security_name: "ozn"
|
||||
addition_count: 1
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
entries
|
||||
{
|
||||
security_type: "FUTURE"
|
||||
security_name: "ZN"
|
||||
addition_count: 0
|
||||
addition_type: MonthMoveToStart
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,6 @@ package bts.config;
|
||||
|
||||
message ExchangeSim {
|
||||
optional bool merge_with_md = 1 [default=true];
|
||||
optional bool forward_implied = 2 [default = false];
|
||||
}
|
||||
|
||||
@@ -78,10 +78,6 @@ message CMEOptions
|
||||
optional string thread_group = 10 [default=""]; // if present this will be used to retrieve affinity settings from Threads config key
|
||||
optional uint32 udp_socket_buffer_size = 11 [default = 33554432]; //32mb
|
||||
}
|
||||
enum BookType {
|
||||
BOOKTYPE_CONSOLIDATED = 0;
|
||||
BOOKTYPE_DIRECT = 1;
|
||||
}
|
||||
message CMEChannel {
|
||||
repeated string id = 2;
|
||||
optional string feed_engine = 3 [default = "default"];
|
||||
@@ -104,7 +100,8 @@ message CMEOptions
|
||||
optional bool join_snapshot_on_gap = 32 [default = false];
|
||||
|
||||
optional bool load_security_db = 33 [default = true];
|
||||
optional BookType book_type = 34 [default = BOOKTYPE_CONSOLIDATED];
|
||||
reserved 34;
|
||||
// optional BookType book_type = 34 [default = BOOKTYPE_CONSOLIDATED];
|
||||
|
||||
}
|
||||
optional string onixs_license = 1 [default = "{default}"];
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
syntax='proto2';
|
||||
package bts.config;
|
||||
import "lib/domain_model/Security.proto";
|
||||
|
||||
message CppConfig {
|
||||
map<string, double> cpp_entry = 1;
|
||||
}
|
||||
|
||||
message CmeSymbolMics {
|
||||
map<string, bts.api.CMEMics> cme_mic = 1;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
syntax='proto2';
|
||||
package bts.config;
|
||||
|
||||
message CppConfig {
|
||||
map<string, double> cpp_entry = 1;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
syntax='proto2';
|
||||
package bts.config;
|
||||
|
||||
message ExpirationOffsetType
|
||||
{
|
||||
enum type
|
||||
{
|
||||
Month=0;
|
||||
MonthMoveToStart=1;
|
||||
}
|
||||
}
|
||||
|
||||
message ExpirationDisplayOffsetEntry
|
||||
{
|
||||
required string security_type = 1;
|
||||
required string security_name = 2;
|
||||
required int32 addition_count = 3;
|
||||
required ExpirationOffsetType.type addition_type = 4;
|
||||
}
|
||||
|
||||
message ExpirationDisplayOffset
|
||||
{
|
||||
repeated ExpirationDisplayOffsetEntry entries = 1;
|
||||
}
|
||||
@@ -26,4 +26,8 @@ message OrdersSoftCleanup{
|
||||
|
||||
message ToastNotifications {
|
||||
optional bool enabled = 1 [default = false];
|
||||
}
|
||||
|
||||
message VolumeAtPrice{
|
||||
optional bool enabled = 1 [default = true];
|
||||
}
|
||||
@@ -40,6 +40,22 @@ enum Currency {
|
||||
CURRENCY_RMB = 14;
|
||||
}
|
||||
|
||||
enum CMEMics {
|
||||
UNSET = 0;
|
||||
CME = 1;
|
||||
NYMEX = 2;
|
||||
CBT = 3;
|
||||
COMEX = 4;
|
||||
MGE = 5;
|
||||
DME = 6;
|
||||
NYMSW = 7;
|
||||
FXS = 8;
|
||||
}
|
||||
|
||||
enum ICEMics{
|
||||
ICE_FUTURES_CANADA=0;
|
||||
}
|
||||
|
||||
enum Side {
|
||||
SIDE_UNDEFINED = 0;
|
||||
SIDE_BUY = 49;
|
||||
|
||||
@@ -212,7 +212,9 @@ const char* intern(const char* str) {
|
||||
|
||||
extern "C" BTS_EXPORT void Logger_log (int level, int subsystem, const char *message, const char *file, int line)
|
||||
{
|
||||
bts::log::_thread_logger.logger->enqueue(log::log_ts(),log::Subsystem{(uint8_t)subsystem}, log::Level{(uint8_t)level}, intern(file), line, "{}", std::string(message));
|
||||
if (bts::log::_thread_logger.logger != nullptr) {
|
||||
bts::log::_thread_logger.logger->enqueue(log::log_ts(), log::Subsystem{ (uint8_t)subsystem }, log::Level{ (uint8_t)level }, intern(file), line, "{}", std::string(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -39,7 +39,7 @@ void run_md_price_latency_test(UserSymbol symbol) {
|
||||
while (true) {
|
||||
for(auto& entry : handles) {
|
||||
PriceSize bid, ask;
|
||||
if (entry.handle.get_bbo(entry.time, bid, ask) == HandleReadResult::SUCCESS) {
|
||||
if (entry.handle.get_direct_book(entry.time, 1, &bid, &ask) == HandleReadResult::SUCCESS) {
|
||||
auto bid_price = bid.price();
|
||||
auto ask_price = ask.price();
|
||||
if (bid_price != entry.bid || ask_price != entry.ask) {
|
||||
|
||||
@@ -25,6 +25,11 @@ double dump(TimeStamp time) {
|
||||
return (TimeStamp::now() - time).seconds();
|
||||
}
|
||||
|
||||
string dump(IntPrice price) {
|
||||
stringstream res;
|
||||
res << std::setprecision(11) << price.price();
|
||||
return res.str();
|
||||
}
|
||||
string dump(PriceSize ps) {
|
||||
stringstream res;
|
||||
res << "{";
|
||||
@@ -144,9 +149,8 @@ string dump_entry(const ColdEntry& entry) {
|
||||
if (has_value(entry.last_trade)) res << "'last_trade': " << dump(entry.last_trade) << ", ";
|
||||
if (entry.total_volume > 0) res << "'total_volume': " << entry.total_volume << ", ";
|
||||
if (entry.last_trade_time > 0) res << "'last_trade_time_ll': " << entry.last_trade_time << ", ";
|
||||
if (!os::is_nan(entry.misc.settlement_price)) res << "'settlement_price': " << entry.misc.settlement_price << ", ";
|
||||
if (entry.misc.settlement_date.year > 0) res << "'settlement_date': " << dump(entry.misc.settlement_date) << ", ";
|
||||
if (!os::is_nan(entry.misc.previous_settlement_price)) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price << ", ";
|
||||
if (!(entry.misc.previous_settlement_price.is_nan())) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price.price() << ", ";
|
||||
if (entry.misc.previous_settlement_date.year > 0) res << "'previous_settlement_date': " << dump(entry.misc.previous_settlement_date) << ", ";
|
||||
res << "'deleted': " << std::boolalpha << entry.deleted << " ";
|
||||
res << "}";
|
||||
@@ -159,18 +163,31 @@ string dump_entry(const HotDepthEntry& entry) {
|
||||
res << "'timestamp': " << dump(entry.time()) << ", ";
|
||||
res << "'key': " << entry.key << ", ";
|
||||
|
||||
res << "'bids': [";
|
||||
for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
res << dump(entry.at(x)->bid()) << ", ";
|
||||
res << "\n'bid': [";
|
||||
for (const auto& level : entry.direct) {
|
||||
res << dump(level.bid()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
res << "'asks': [";
|
||||
for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
res << dump(entry.at(x)->ask()) << ", ";
|
||||
res << "\n'ask': [";
|
||||
for (const auto& level : entry.direct) {
|
||||
res << dump(level.ask()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
res << "'\nimplied_bid': [";
|
||||
for (const auto& level : entry.implied) {
|
||||
res << dump(level.bid()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
res << "'\nimplied_ask': [";
|
||||
for (const auto& level : entry.implied) {
|
||||
res << dump(level.ask()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
res << "\n'settlement': '" << dump(entry.settlement) << "', ";
|
||||
res << "'status': '" << CoconutSecurityStatus_Name((CoconutSecurityStatus)entry.status) << "', ";
|
||||
res << "'sequence': '" << entry.sequence << "', ";
|
||||
res << " }";
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
static const double powers_10[] = {
|
||||
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
|
||||
1E0,
|
||||
1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20
|
||||
};
|
||||
static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2;
|
||||
inline double pow10(int power) {
|
||||
const int offset = power + max_abs_power_10;
|
||||
const int powers_10_sz = BTS_ARRAYSIZE(powers_10);
|
||||
bts_assert_d(offset >= 0 && offset < powers_10_sz);
|
||||
if (offset >= 0 && offset < powers_10_sz) {
|
||||
return powers_10[offset];
|
||||
}
|
||||
return std::numeric_limits<double>().quiet_NaN();
|
||||
}
|
||||
|
||||
class IntPrice {
|
||||
#ifdef _WIN32
|
||||
int64_t _mantissa : 56;
|
||||
int64_t _exponent : 8;
|
||||
#else
|
||||
int64_t _mantissa : 56;
|
||||
int8_t _exponent : 8;
|
||||
#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) {}
|
||||
|
||||
bool operator==(const IntPrice& other) const {
|
||||
// this requires both mantissa and exponent to be equal
|
||||
// logically 100*pow10(1) == 10*pow10(2)
|
||||
// HOWEVER, once converted to double there are subtelties that can make them not equal so
|
||||
// this function is conservative
|
||||
if(_exponent == other._exponent)
|
||||
return _mantissa == other._mantissa;
|
||||
else
|
||||
return price()==other.price();
|
||||
}
|
||||
bool operator!=(const IntPrice& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
bool operator<(const IntPrice& other) const {
|
||||
if (_exponent == other._exponent) {
|
||||
return _mantissa < other._mantissa;
|
||||
}
|
||||
else {
|
||||
return price() < other.price();
|
||||
}
|
||||
}
|
||||
IntPrice operator - (const IntPrice& o) const {
|
||||
if(_exponent==o._exponent) {
|
||||
return IntPrice(_mantissa-o.mantissa(), _exponent);
|
||||
}
|
||||
else {
|
||||
// not sure there's ever a cause for a positive exponent unless we get into scientific computing
|
||||
if (is_nan() || o.is_nan()) return IntPrice();
|
||||
bts_assert_d(o.exponent() <= 0 && _exponent <= 0);
|
||||
if (o.exponent() < _exponent) {
|
||||
int8_t delta = _exponent - o.exponent();
|
||||
auto m = _mantissa * pow10(delta);
|
||||
return IntPrice(m - o.mantissa(), o.exponent());
|
||||
}
|
||||
else {
|
||||
int8_t delta = o.exponent() - _exponent;
|
||||
auto m = o.mantissa() * pow10(delta);
|
||||
return IntPrice(_mantissa - m, _exponent);
|
||||
}
|
||||
}
|
||||
}
|
||||
int64_t mantissa() const { return _mantissa; }
|
||||
int8_t exponent() const { return _exponent; }
|
||||
double price() const {
|
||||
if (is_nan()) return NO_PRICE;
|
||||
return mantissa() * pow10(exponent());
|
||||
}
|
||||
bool is_nan() const { return _exponent == 127; }
|
||||
};
|
||||
static_assert(sizeof(IntPrice) == 8, "IntPrice should be this big |--------|");
|
||||
extern IntPrice INT_NAN;
|
||||
|
||||
template <uint8_t PRECISION>
|
||||
IntPrice IntPrice_from_double(double price) {
|
||||
if (os::is_nan(price)) return INT_NAN;
|
||||
double temp;
|
||||
if (fabs(modf(price, &temp)) != 0.0) {
|
||||
const double rounding = 0.5 * pow10(-PRECISION);
|
||||
price += (price > 0) ? rounding : -rounding;
|
||||
price *= pow10(PRECISION);
|
||||
return {(int64_t)price, -PRECISION};
|
||||
}
|
||||
return {(int64_t)price, 0};
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const IntPrice& p) {
|
||||
os << p.price();
|
||||
return os;
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -419,6 +419,7 @@ private:
|
||||
auto iter = _data.find(m.security_key());
|
||||
if (iter != _data.end()) {
|
||||
iter->second->strategy.CopyFrom(m.strategy());
|
||||
iter->second->updated = TimeStamp::now();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,6 +477,7 @@ private:
|
||||
s->security.CopyFrom(*strategy.security);
|
||||
s->strategy.CopyFrom(strategy.strategy);
|
||||
s->legs = legs;
|
||||
s->updated = TimeStamp::now();
|
||||
result.insert(
|
||||
make_pair(strategy.security->id(),s));
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#ifndef NO_CPP0X
|
||||
#include "MDConsumerHandle.h"
|
||||
#include "lib/utils/sane_algorithm.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#define never bts_assert(false)
|
||||
|
||||
template <class T>
|
||||
using small_vector = boost::container::small_vector<T,4>;
|
||||
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
@@ -122,8 +125,7 @@ struct DepthTraversal {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void copy_depth(size_t count, api::Side side, const api::ContractSpecification& spec, std::vector<DepthTraversal>& leg_travs, Level* out_price) {
|
||||
void copy_depth(size_t count, api::Side side, const api::ContractSpecification& spec, small_vector<DepthTraversal>& leg_travs, Level* out_price) {
|
||||
size_t out_index = 0;
|
||||
bool first = true;
|
||||
while(out_index < count) {
|
||||
@@ -165,16 +167,40 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification&
|
||||
}
|
||||
|
||||
TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bid, PriceSize* ask) {
|
||||
std::vector<DepthTraversal> bid_leg_travs;
|
||||
std::vector<DepthTraversal> ask_leg_travs;
|
||||
small_vector<DepthTraversal> bid_leg_travs;
|
||||
small_vector<DepthTraversal> ask_leg_travs;
|
||||
bid_leg_travs.reserve(synth.legs.size());
|
||||
ask_leg_travs.reserve(synth.legs.size());
|
||||
TimeStamp rv = std::numeric_limits<TimeStamp>::min();
|
||||
for (size_t x = 0; x < synth.legs.size(); ++x) {
|
||||
std::array<PriceSize, COCONUT_MAX_DEPTH> bids, asks;
|
||||
TimeStamp t;
|
||||
HandleReadResult r = synth.legs[x].get_depth(t, count, &bids[0], &asks[0]);
|
||||
auto key = synth.legs[x].key();
|
||||
auto iter = synth.strategy.leg_params_map().find(key);
|
||||
const spark::LegSpreaderParams& leg_params =
|
||||
iter == synth.strategy.leg_params_map().end() ?
|
||||
spark::LegSpreaderParams::default_instance() :
|
||||
iter->second;
|
||||
|
||||
auto mdtype = leg_params.market_data_type();
|
||||
HandleReadResult r;
|
||||
if (mdtype == spark::LegSpreaderParams::DIRECT) {
|
||||
r = synth.legs[x].get_direct_book(t, count, &bids[0], &asks[0]);
|
||||
}
|
||||
else {
|
||||
r = synth.legs[x].get_book(t, count, &bids[0], &asks[0]);
|
||||
}
|
||||
if (r == HandleReadResult::BUSY || t == TimeStamp::invalid()) return TimeStamp::invalid();
|
||||
|
||||
// NET Change
|
||||
if (leg_params.leg_md_calc() == spark::LegSpreaderParams::NET_CHANGE) {
|
||||
auto settle = synth.legs[x].settlement_price();
|
||||
for (size_t d = 0; d < COCONUT_MAX_DEPTH; ++d) {
|
||||
bids[d] = bids[d] - settle;
|
||||
asks[d] = asks[d] - settle;
|
||||
}
|
||||
}
|
||||
|
||||
rv = std::max(rv, t);
|
||||
const auto& leg = synth.security.synthetic().legs(x);
|
||||
const auto& eff_bid = leg.side() == api::SIDE_BUY ? bids : asks;
|
||||
@@ -186,12 +212,12 @@ TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bi
|
||||
const auto& spec = synth.security.contract_spec();
|
||||
copy_depth(count, api::SIDE_BUY, spec, bid_leg_travs, scratch.data());
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
bid[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size);
|
||||
bid[x] = PriceSize_from_double<7>(scratch[x].price, scratch[x].size);
|
||||
}
|
||||
memset(scratch.data(), 0, sizeof(scratch));
|
||||
copy_depth(count, api::SIDE_SELL, spec, ask_leg_travs, scratch.data());
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
ask[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size);
|
||||
ask[x] = PriceSize_from_double<7>(scratch[x].price, scratch[x].size);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
@@ -200,20 +226,6 @@ TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bi
|
||||
// END SYNTHETIC DEPTH
|
||||
/////////////////////////////
|
||||
|
||||
TimeStamp MDConsumerHandle::last_market_update() const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_VOID:
|
||||
return TimeStamp::invalid();
|
||||
case HANDLETYPE_DEPTH:
|
||||
return book.hot->time();
|
||||
case HANDLETYPE_BBO:
|
||||
return bbo.hot->time();
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
return sane::max<TimeStamp>(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); });
|
||||
}
|
||||
never;
|
||||
}
|
||||
|
||||
bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume) const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_VOID: return false;
|
||||
|
||||
+172
-53
@@ -5,6 +5,7 @@
|
||||
#include "lib/md/Shm/MDStructure.h"
|
||||
#include "lib/sparkproto/SparkProtos.pb.h"
|
||||
#include "UserSymbol.h"
|
||||
#include "lib/utils/sane_algorithm.h"
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
@@ -16,6 +17,7 @@ struct SyntheticStrategy {
|
||||
spark::ExecutionStrategy strategy;
|
||||
api::Security security;
|
||||
std::vector<MDConsumerHandle> legs;
|
||||
TimeStamp updated;
|
||||
};
|
||||
|
||||
TimeStamp copy_bbo(const HotEntry& src, PriceSize& out_bid, PriceSize& out_ask);
|
||||
@@ -28,6 +30,117 @@ enum class HandleReadResult {
|
||||
SUCCESS
|
||||
};
|
||||
|
||||
inline void read_direct(
|
||||
const HotDepthEntry& hot,
|
||||
size_t count,
|
||||
PriceSize* bid, PriceSize* ask) {
|
||||
|
||||
bts_assert_d(count <= COCONUT_MAX_DEPTH);
|
||||
count = std::min(count, (size_t)COCONUT_MAX_DEPTH);
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
hot.read_direct(x, &bid[x], &ask[x]);
|
||||
}
|
||||
}
|
||||
inline void read_implied(
|
||||
const HotDepthEntry& hot,
|
||||
size_t count,
|
||||
PriceSize* bid, PriceSize* ask) {
|
||||
|
||||
bts_assert_d(count <= COCONUT_MAX_IMPLIED_DEPTH);
|
||||
count = std::min(count, (size_t)COCONUT_MAX_IMPLIED_DEPTH);
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
hot.read_implied(x, &bid[x], &ask[x]);
|
||||
}
|
||||
}
|
||||
inline void read_combined(
|
||||
const HotDepthEntry& hot,
|
||||
size_t count,
|
||||
PriceSize* bid, PriceSize* ask) {
|
||||
bts_assert_d(count <= COCONUT_MAX_DEPTH);
|
||||
size_t xda=0, xdb=0, xia=0, xib=0;
|
||||
if (hot.implied[xib].bid().is_nan()) xib = COCONUT_MAX_IMPLIED_DEPTH;
|
||||
if (hot.implied[xia].ask().is_nan()) xia = COCONUT_MAX_IMPLIED_DEPTH;
|
||||
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
if (xib < COCONUT_MAX_IMPLIED_DEPTH) {
|
||||
auto direct = hot.direct[xdb].bid();
|
||||
auto implied = hot.implied[xib].bid();
|
||||
|
||||
// Compare bids
|
||||
if (direct.int_price() == implied.int_price()) {
|
||||
bid[x] = PriceSize(
|
||||
direct.int_price(),
|
||||
direct.size(),
|
||||
ImpliedSize(implied.size()));
|
||||
xdb += 1;
|
||||
xib += 1;
|
||||
}
|
||||
// Bids grow down, take the higher price
|
||||
else if (implied.int_price() < direct.int_price()) {
|
||||
bid[x] = direct;
|
||||
xdb += 1;
|
||||
}
|
||||
else {
|
||||
bid[x] = implied;
|
||||
xib += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
bid[x] = hot.direct[xdb].bid();
|
||||
xdb += 1;
|
||||
}
|
||||
|
||||
if (xia < COCONUT_MAX_IMPLIED_DEPTH) {
|
||||
auto direct = hot.direct[xda].ask();
|
||||
auto implied = hot.implied[xia].ask();
|
||||
|
||||
// Compare asks
|
||||
if (direct.int_price() == implied.int_price()) {
|
||||
ask[x] = PriceSize(
|
||||
direct.int_price(),
|
||||
direct.size(),
|
||||
ImpliedSize(implied.size()));
|
||||
xda += 1;
|
||||
xia += 1;
|
||||
}
|
||||
// asks grow up, take the lower price
|
||||
else if (direct.int_price() < implied.int_price()) {
|
||||
ask[x] = direct;
|
||||
xda += 1;
|
||||
}
|
||||
else {
|
||||
ask[x] = implied;
|
||||
xia += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ask[x] = hot.direct[xda].ask();
|
||||
xda += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class Read>
|
||||
TimeStamp optimistic_copy(
|
||||
Read read,
|
||||
const HotDepthEntry& hot,
|
||||
size_t count,
|
||||
PriceSize* bid, PriceSize* ask)
|
||||
{
|
||||
TimeStamp t;
|
||||
int attempts = 0;
|
||||
const int max_attempts = 1619200;
|
||||
do {
|
||||
t = hot.time();
|
||||
read(hot, count, bid, ask);
|
||||
++attempts;
|
||||
} while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts);
|
||||
if (attempts >= max_attempts) {
|
||||
return TimeStamp::invalid();
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
// Can be an invalid handle if you request a security which has not been discovered yet.
|
||||
class BTS_EXPORT MDConsumerHandle
|
||||
@@ -75,69 +188,44 @@ public:
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
TimeStamp last_market_update() const;
|
||||
|
||||
HandleReadResult get_bbo(TimeStamp& time, PriceSize& bid, PriceSize& ask) const
|
||||
{
|
||||
if (!is_initialized()) return HandleReadResult::NO_CHANGE;
|
||||
|
||||
TimeStamp last_market_update() const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_DEPTH:
|
||||
if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) {
|
||||
TimeStamp temp = copy_depth(*book.hot, 1, &bid, &ask);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
case HANDLETYPE_BBO:
|
||||
if (bbo.hot->time() > time || bbo.hot->time() == TimeStamp::invalid()) {
|
||||
TimeStamp temp = copy_bbo(*bbo.hot, bid, ask);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
return get_depth(time, 1, &bid, &ask);
|
||||
case HANDLETYPE_VOID:
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
}
|
||||
bts_assert_d(false);
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
}
|
||||
|
||||
HandleReadResult get_depth(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const {
|
||||
bts_assert_d(copydepth <= COCONUT_MAX_DEPTH);
|
||||
if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE;
|
||||
|
||||
switch(_type) {
|
||||
case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE;
|
||||
return TimeStamp::invalid();
|
||||
case HANDLETYPE_DEPTH:
|
||||
if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) {
|
||||
TimeStamp temp = copy_depth(*book.hot, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
return book.hot->time();
|
||||
case HANDLETYPE_BBO:
|
||||
return get_bbo(time, *bids, *asks);
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
auto t = last_market_update();
|
||||
if (t > time || t == TimeStamp::invalid()) {
|
||||
TimeStamp temp = copy_depth(*synthetic.def, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
return bbo.hot->time();
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
return std::max(
|
||||
synthetic.def->updated,
|
||||
sane::max<TimeStamp>(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); }));
|
||||
}
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
HandleReadResult get_direct_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const {
|
||||
return get_book(read_direct, time, copydepth, bids, asks);
|
||||
}
|
||||
HandleReadResult get_implied_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const {
|
||||
return get_book(read_implied, time, copydepth, bids, asks);
|
||||
}
|
||||
HandleReadResult get_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const {
|
||||
return get_book(read_combined, time, copydepth, bids, asks);
|
||||
}
|
||||
|
||||
bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const;
|
||||
|
||||
IntPrice settlement_price() const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_DEPTH: return book.hot->settlement;
|
||||
case HANDLETYPE_BBO: return bbo.hot->settlement;
|
||||
default:
|
||||
return INT_NAN;
|
||||
}
|
||||
}
|
||||
datetime::Date settlement_date() const { return this->misc().settlement_date; }
|
||||
|
||||
TimeStamp misc_timestamp() const;
|
||||
MiscData misc() const;
|
||||
|
||||
@@ -162,6 +250,37 @@ public:
|
||||
|
||||
private:
|
||||
friend class MDConsumerInst;
|
||||
|
||||
template <class Read>
|
||||
HandleReadResult get_book(Read read, TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const {
|
||||
bts_assert_d(copydepth <= COCONUT_MAX_DEPTH);
|
||||
if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE;
|
||||
switch(_type) {
|
||||
case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE;
|
||||
case HANDLETYPE_DEPTH:
|
||||
if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) {
|
||||
TimeStamp temp = optimistic_copy(read, *book.hot, copydepth, bids, asks);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
case HANDLETYPE_BBO:
|
||||
bts_assert_msg(false,"Not impelmented");
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
auto t = last_market_update();
|
||||
if (t > time || t == TimeStamp::invalid()) {
|
||||
TimeStamp temp = copy_depth(*synthetic.def, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks);
|
||||
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
|
||||
time = temp;
|
||||
return HandleReadResult::SUCCESS;
|
||||
}
|
||||
return HandleReadResult::NO_CHANGE;
|
||||
}
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
void set_hot(const void* hot) {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_DEPTH:
|
||||
|
||||
@@ -102,16 +102,6 @@ CAPI void md_consumer_set_on_security(MDConsumer* consumer, security_cb cb) {
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef void(*just_security_cb)(security_key, const char*, size_t);
|
||||
CAPI void md_consumer_get_single_security(const ColdEntry* entry, just_security_cb cb) {
|
||||
bts_assert(entry);
|
||||
bts::api::Security security;
|
||||
convert_security(entry->security, &security);
|
||||
std::string buffer;
|
||||
security.SerializePartialToString(&buffer);
|
||||
cb(security.id(), buffer.c_str(), buffer.size());
|
||||
}
|
||||
|
||||
bool is_heart_beat_ok(const ColdHeader &header);
|
||||
CAPI bool md_consumer_is_heart_beating(const ColdHeader* header) {
|
||||
if (header == nullptr) return false;
|
||||
|
||||
@@ -148,16 +148,21 @@ void MDProducerHandle::update_exchange_status(TimeStamp, TradingStatus status) {
|
||||
cheader().exchange_status_count++;
|
||||
}
|
||||
|
||||
void MDProducerHandle::update_settlement(TimeStamp time, Price price, Date date) {
|
||||
void MDProducerHandle::update_settlement(TimeStamp time, IntPrice price, Date date) {
|
||||
if (date > cdata().misc.settlement_date) {
|
||||
cdata().misc.previous_settlement_price = cdata().misc.settlement_price;
|
||||
cdata().misc.previous_settlement_price = has_depth() ? hdepth().settlement : hdata().settlement;
|
||||
cdata().misc.previous_settlement_date = cdata().misc.settlement_date;
|
||||
}
|
||||
cdata().misc.settlement_price = price;
|
||||
if (has_depth()) {
|
||||
hdepth().settlement = price;
|
||||
}
|
||||
else {
|
||||
hdata().settlement = price;
|
||||
}
|
||||
cdata().misc.settlement_date = date;
|
||||
cdata().misc.misc_timestamp = time;
|
||||
}
|
||||
void MDProducerHandle::update_previous_settlement(TimeStamp time, Price price, Date date) {
|
||||
void MDProducerHandle::update_previous_settlement(TimeStamp time, IntPrice price, Date date) {
|
||||
cdata().misc.previous_settlement_price = price;
|
||||
cdata().misc.previous_settlement_date = date;
|
||||
cdata().misc.misc_timestamp = time;
|
||||
@@ -209,10 +214,15 @@ void MDProducerHandle::mark_deleted() {
|
||||
TimeStamp ts = TimeStamp::now();
|
||||
cdata().deleted = true;
|
||||
std::array<PriceSize,COCONUT_MAX_DEPTH> blank;
|
||||
std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH> iblank;
|
||||
for (size_t x = 0; x < blank.size(); ++x) {
|
||||
blank[x] = PriceSize::NO_MARKET;
|
||||
blank[x] = NO_MARKET;
|
||||
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
|
||||
iblank[x] = NO_MARKET;
|
||||
}
|
||||
}
|
||||
update_book(ts, blank, blank, 0);
|
||||
update_direct(ts, blank, blank, 0);
|
||||
update_implied(ts, iblank, iblank, 0);
|
||||
}
|
||||
|
||||
}}//namespace
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include "lib/utils/bts_export.h"
|
||||
#include "Shm/MDStructure.h"
|
||||
@@ -33,9 +33,9 @@ public:
|
||||
return key;
|
||||
}
|
||||
void set_legs(const std::vector<security_key>& legs);
|
||||
|
||||
void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0); // hot
|
||||
void update_book(TimeStamp time, std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint32_t seq=0) {
|
||||
|
||||
void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0);
|
||||
void update_direct(TimeStamp time, const std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, const std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint32_t seq=0) {
|
||||
if (!has_depth()) {
|
||||
update_bbo(time, bids[0], asks[0], seq);
|
||||
}
|
||||
@@ -51,11 +51,12 @@ public:
|
||||
hheader().direct_count += 1;
|
||||
}
|
||||
}
|
||||
void update_implied(TimeStamp time, std::array<PriceSize,COCONUT_IMPLIED_DEPTH>& bids, std::array<PriceSize,COCONUT_IMPLIED_DEPTH>& asks, uint32_t seq=0) {
|
||||
|
||||
void update_implied(TimeStamp time, const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& bids, const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& asks, uint32_t seq=0) {
|
||||
if (has_depth()) {
|
||||
auto& book = hdepth();
|
||||
book.set_time(TimeStamp::invalid());
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
|
||||
book.write_implied(x, bids[x], asks[x]);
|
||||
}
|
||||
book.status = co2::COCONUTSECURITYSTATUS_OK;
|
||||
@@ -64,7 +65,31 @@ public:
|
||||
hheader().implied_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void update_books(TimeStamp time,
|
||||
const std::array<PriceSize, COCONUT_MAX_DEPTH>& bid,
|
||||
const std::array<PriceSize,COCONUT_MAX_DEPTH>& ask,
|
||||
const std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH>& implied_bid,
|
||||
const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& implied_ask,
|
||||
uint32_t seq=0) {
|
||||
|
||||
if (has_depth()) {
|
||||
auto& book = hdepth();
|
||||
book.set_time(TimeStamp::invalid());
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
|
||||
book.write_implied(x, implied_bid[x], implied_ask[x]);
|
||||
}
|
||||
book.write_direct(x, bid[x], ask[x]);
|
||||
}
|
||||
book.status = co2::COCONUTSECURITYSTATUS_OK;
|
||||
book.sequence = seq;
|
||||
book.set_time(time);
|
||||
hheader().direct_count += 1;
|
||||
hheader().implied_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void set_status(co2::CoconutSecurityStatus status); // hot
|
||||
|
||||
// Updates trade log and last trade
|
||||
@@ -72,12 +97,12 @@ public:
|
||||
// Update last_trade without updating trade log
|
||||
void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume);
|
||||
void add_rfq(TimeStamp time, const co2::QuoteRequestMessage& rfq); // cold
|
||||
|
||||
|
||||
void update_exchange_status(TimeStamp time, TradingStatus status); // cold
|
||||
|
||||
// Misc fields
|
||||
void update_settlement(TimeStamp time, Price price, datetime::Date date); // cold
|
||||
void update_previous_settlement(TimeStamp time, Price price, datetime::Date date); // cold
|
||||
void update_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold
|
||||
void update_previous_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold
|
||||
void update_opening(TimeStamp time, Price price, datetime::Date date); // cold
|
||||
void update_closing(TimeStamp time, Price price, datetime::Date date); // cold
|
||||
void update_session_high(TimeStamp time, Price price); // cold
|
||||
@@ -96,7 +121,7 @@ private:
|
||||
friend class tbb::concurrent_queue < MDProducerHandle > ;
|
||||
friend class SecurityIDRecorder;
|
||||
friend struct bts::co2::Work;
|
||||
|
||||
|
||||
HotEntry& hdata() const {
|
||||
bts_assert_d(_hdata && !has_depth());
|
||||
return *static_cast<HotEntry*>(_hdata);
|
||||
@@ -111,7 +136,7 @@ private:
|
||||
bts_assert_d(_hheader);
|
||||
return *_hheader;
|
||||
}
|
||||
|
||||
|
||||
ColdEntry& cdata() const {
|
||||
bts_assert_d(_cdata);
|
||||
return *_cdata;
|
||||
|
||||
@@ -8,8 +8,9 @@ using namespace std;
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
bool enable_depth(const string&, SecurityType type) {
|
||||
return type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_SYNTHETIC;
|
||||
bool enable_depth(const string&, SecurityType) {
|
||||
return true;
|
||||
// return type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_SYNTHETIC;
|
||||
}
|
||||
|
||||
string segment_name(const string& symbol, SecurityType type, Venue exchange, Platform platform) {
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
BTS_EXPORT PriceSize PriceSize::NO_MARKET = PriceSize();
|
||||
IntPrice INT_NAN = IntPrice();
|
||||
PriceSize NO_MARKET = PriceSize();
|
||||
|
||||
int to_exponent(double value) {
|
||||
if (value == 0 && os::is_nan(value)) return 0;
|
||||
|
||||
+80
-131
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "IntPrice.h"
|
||||
#include "SizeTypes.h"
|
||||
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/warn_ctrl.h"
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
@@ -8,6 +11,7 @@
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include "lib/utils/Optimistic.h"
|
||||
|
||||
|
||||
#include <atomic>
|
||||
#include <limits>
|
||||
#include <cmath>
|
||||
@@ -18,107 +22,62 @@ namespace md {
|
||||
typedef double Price;
|
||||
typedef uint32_t Size;
|
||||
|
||||
// #define LOAD(x) x.load(std::memory_order_acquire)
|
||||
// #define STORE(x,value) x.store(value, std::memory_order_release)
|
||||
|
||||
static const double powers_10[] = {
|
||||
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
|
||||
1E0,
|
||||
1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20
|
||||
};
|
||||
static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2;
|
||||
inline double pow10(int power) {
|
||||
const int offset = power + max_abs_power_10;
|
||||
const int powers_10_sz = BTS_ARRAYSIZE(powers_10);
|
||||
bts_assert_d(offset >= 0 && offset < powers_10_sz);
|
||||
if (offset >= 0 && offset < powers_10_sz) {
|
||||
return powers_10[offset];
|
||||
}
|
||||
return std::numeric_limits<double>().quiet_NaN();
|
||||
}
|
||||
int to_exponent(double value);
|
||||
template <class Mantissa, class Exponent>
|
||||
inline bool compact_mantissa(Mantissa& mantissa, Exponent& exponent) {
|
||||
bool result = false;
|
||||
// If the mantissa is larger than 39 bits will allow attempt to shrink it
|
||||
while (mantissa > 0x8000000000 || mantissa < -0x8000000000) {
|
||||
mantissa /= 10;
|
||||
exponent += 1;
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class PriceSize {
|
||||
int64_t _mantissa;
|
||||
int8_t _exponent;
|
||||
IntPrice _price;
|
||||
// merged size
|
||||
uint32_t _size;
|
||||
uint32_t _direct_size;
|
||||
|
||||
public:
|
||||
PriceSize(int64_t mantissa, int8_t exponent, Size size) :
|
||||
_mantissa(mantissa),
|
||||
_exponent(exponent),
|
||||
_size(size)
|
||||
{
|
||||
// bts_assert_d(exponent != 0);
|
||||
}
|
||||
PriceSize() : _mantissa(0), _exponent(), _size(0) { }
|
||||
PriceSize(IntPrice price, Size size) :
|
||||
_price(size == 0 ? INT_NAN : price),
|
||||
_size(size),
|
||||
_direct_size(size)
|
||||
{ }
|
||||
PriceSize(IntPrice price, Size direct_size, ImpliedSize implied_size) :
|
||||
_price(price),
|
||||
_size(direct_size + implied_size.value()),
|
||||
_direct_size(direct_size)
|
||||
{ }
|
||||
PriceSize() : _price(INT_NAN), _size(0), _direct_size(0) {}
|
||||
|
||||
PriceSize(const PriceSize& other) = default;
|
||||
PriceSize& operator=(const PriceSize& other) = default;
|
||||
|
||||
bool operator==(const PriceSize& other) const {
|
||||
return (_size == other._size && _mantissa == other._mantissa && _exponent == other._exponent);
|
||||
return (_size == other._size && _direct_size == other._direct_size && int_price() == other.int_price());
|
||||
}
|
||||
bool operator!=(const PriceSize& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
BTS_EXPORT static PriceSize NO_MARKET;
|
||||
|
||||
template <uint8_t PRECISION>
|
||||
static PriceSize from_double(double price, Size size) {
|
||||
if (size == 0 || os::is_nan(price)) return NO_MARKET;
|
||||
double temp;
|
||||
if (fabs(modf(price, &temp)) != 0.0) {
|
||||
const double rounding = 0.5 * pow10(-PRECISION);
|
||||
price += (price > 0) ? rounding : -rounding;
|
||||
price *= pow10(PRECISION);
|
||||
return PriceSize((int64_t)price, -PRECISION, size);
|
||||
}
|
||||
return PriceSize((int64_t)price, 0, size);
|
||||
PriceSize operator-(IntPrice price) const{
|
||||
PriceSize res=*this;
|
||||
res._price=res._price-price;
|
||||
return res;
|
||||
}
|
||||
static PriceSize from_double(double price, int precision, Size size) {
|
||||
if (size == 0 || os::is_nan(price)) return NO_MARKET;
|
||||
double temp;
|
||||
if (fabs(modf(price, &temp)) != 0.0) {
|
||||
const double rounding = 0.5 * pow10(-precision);
|
||||
price += (price > 0) ? rounding : -rounding;
|
||||
price *= pow10(precision);
|
||||
return PriceSize((int64_t)price, -precision, size);
|
||||
}
|
||||
return PriceSize((int64_t)price, 0, size);
|
||||
}
|
||||
|
||||
//
|
||||
// Accessors
|
||||
|
||||
int64_t mantissa() const {
|
||||
return _mantissa;
|
||||
}
|
||||
int8_t exponent() const {
|
||||
return _exponent;
|
||||
}
|
||||
// This call is relatively slow so don't call it often
|
||||
double price() const {
|
||||
if (*this == NO_MARKET) return NO_PRICE;
|
||||
return mantissa() * pow10(exponent());
|
||||
}
|
||||
Size size() const {
|
||||
return _size;
|
||||
}
|
||||
int64_t mantissa() const { return _price.mantissa(); }
|
||||
int8_t exponent() const { return _price.exponent(); }
|
||||
Size size() const { return _size; }
|
||||
Size direct_size() const { return _direct_size; }
|
||||
Size implied_size() const { return _size - _direct_size; }
|
||||
double price() const { return _price.price(); }
|
||||
IntPrice int_price() const { return _price; }
|
||||
bool is_nan() const { return _price.is_nan(); }
|
||||
};
|
||||
extern PriceSize NO_MARKET;
|
||||
static_assert(sizeof(PriceSize) == 16, "PriceSize should be small");
|
||||
|
||||
template <uint8_t PRECISION>
|
||||
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 };
|
||||
}
|
||||
|
||||
|
||||
typedef utils::Optimistic<PriceSize> AtomicPriceSize;
|
||||
|
||||
@@ -127,63 +86,34 @@ typedef utils::Optimistic<PriceSize> AtomicPriceSize;
|
||||
// This structure can store the same data in 24 bytes so it's more efficient for shared memory
|
||||
struct PriceLevel {
|
||||
PriceLevel() :
|
||||
_mantissa_b(0), _exponent_b(0),
|
||||
_mantissa_a(0), _exponent_a(0),
|
||||
_size_b(0), _size_a(0) {}
|
||||
PriceLevel(
|
||||
int64_t bid_mantissa,
|
||||
int8_t bid_exponent,
|
||||
uint32_t bid_size,
|
||||
int64_t ask_mantissa,
|
||||
int8_t ask_exponent,
|
||||
uint32_t ask_size) :
|
||||
_mantissa_b(bid_mantissa), _exponent_b(bid_exponent),
|
||||
_mantissa_a(ask_mantissa), _exponent_a(ask_exponent),
|
||||
_size_b(bid_size),
|
||||
_size_a(ask_size)
|
||||
{}
|
||||
_bid(), _ask(),
|
||||
_bid_size(0), _ask_size(0) {}
|
||||
PriceLevel(const PriceSize& bid, const PriceSize& ask):
|
||||
_mantissa_b(bid.mantissa()), _exponent_b(bid.exponent()),
|
||||
_mantissa_a(ask.mantissa()), _exponent_a(ask.exponent()),
|
||||
_size_b(bid.size()), _size_a(ask.size())
|
||||
_bid(bid.int_price()), _ask(ask.int_price()),
|
||||
_bid_size(bid.size()), _ask_size(ask.size())
|
||||
{}
|
||||
|
||||
#ifdef _WIN32
|
||||
int64_t _mantissa_b : 56;
|
||||
int64_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int64_t _exponent_a : 8;
|
||||
#else
|
||||
int64_t _mantissa_b : 56;
|
||||
int8_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int8_t _exponent_a : 8;
|
||||
#endif
|
||||
uint32_t _size_b;
|
||||
uint32_t _size_a;
|
||||
IntPrice _bid;
|
||||
IntPrice _ask;
|
||||
uint32_t _bid_size;
|
||||
uint32_t _ask_size;
|
||||
|
||||
uint64_t bid_mantissa() const { return _mantissa_b; }
|
||||
uint64_t ask_mantissa() const { return _mantissa_a; }
|
||||
int8_t bid_exponent() const { return _exponent_b; }
|
||||
int8_t ask_exponent() const { return _exponent_a; }
|
||||
uint32_t bid_size() const { return _size_b; }
|
||||
uint32_t ask_size() const { return _size_a; }
|
||||
const IntPrice& bid_price() const { return _bid; }
|
||||
const IntPrice& ask_price() const { return _ask; }
|
||||
uint32_t bid_size() const { return _bid_size; }
|
||||
uint32_t ask_size() const { return _ask_size; }
|
||||
|
||||
bool operator==(const PriceLevel& other) const {
|
||||
return _size_a == other._size_a &&
|
||||
_size_b == other._size_b &&
|
||||
_mantissa_a == other._mantissa_a &&
|
||||
_mantissa_b == other._mantissa_b &&
|
||||
_exponent_a == other._exponent_a &&
|
||||
_exponent_b == other._exponent_b;
|
||||
return _ask_size == other._ask_size &&
|
||||
_bid_size == other._bid_size &&
|
||||
_bid == other._bid &&
|
||||
_ask == other._ask;
|
||||
}
|
||||
bool operator!=(const PriceLevel& other) const {
|
||||
return _size_a != other._size_a ||
|
||||
_size_b != other._size_b ||
|
||||
_mantissa_a != other._mantissa_a ||
|
||||
_mantissa_b != other._mantissa_b ||
|
||||
_exponent_a != other._exponent_a ||
|
||||
_exponent_b != other._exponent_b;
|
||||
return _ask_size != other._ask_size ||
|
||||
_bid_size != other._bid_size ||
|
||||
_bid != other._bid ||
|
||||
_ask != other._ask;
|
||||
}
|
||||
|
||||
bool update(const PriceLevel& temp) {
|
||||
@@ -195,14 +125,33 @@ struct PriceLevel {
|
||||
}
|
||||
|
||||
PriceSize bid() const {
|
||||
return PriceSize(_mantissa_b, _exponent_b, _size_b);
|
||||
return PriceSize(_bid, _bid_size);
|
||||
}
|
||||
PriceSize ask() const {
|
||||
return PriceSize(_mantissa_a, _exponent_a, _size_a);
|
||||
return PriceSize(_ask, _ask_size);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
|
||||
|
||||
struct ImpliedPriceLevel : private PriceLevel {
|
||||
ImpliedPriceLevel() : PriceLevel() {}
|
||||
ImpliedPriceLevel(const PriceSize& bid, const PriceSize& ask):
|
||||
PriceLevel(bid,ask) {}
|
||||
using PriceLevel::bid_price;
|
||||
using PriceLevel::ask_price;
|
||||
using PriceLevel::bid_size;
|
||||
using PriceLevel::ask_size;
|
||||
using PriceLevel::operator==;
|
||||
using PriceLevel::operator!=;
|
||||
bool operator=(const PriceLevel& other) { return update(other); }
|
||||
PriceSize bid() const {
|
||||
return PriceSize(_bid, 0, ImpliedSize(_bid_size));
|
||||
}
|
||||
PriceSize ask() const {
|
||||
return PriceSize(_ask, 0, ImpliedSize(_ask_size));
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(ImpliedPriceLevel) == sizeof(PriceLevel), "ImpliedPriceLevel should not be bigger than PriceLevel");
|
||||
|
||||
}}//namespace
|
||||
|
||||
@@ -26,7 +26,7 @@ enum WorkType {
|
||||
struct Work {
|
||||
WorkType type;
|
||||
MDProducerHandle handle;
|
||||
Price price;
|
||||
IntPrice price;
|
||||
Date date;
|
||||
};
|
||||
|
||||
@@ -43,10 +43,11 @@ struct SettlementRecorderImpl {
|
||||
sqlite3_stmt_ptr insert_stmt;
|
||||
};
|
||||
|
||||
bool record_settle(sqlite3_stmt* insert, security_key key, Price price, Date date) {
|
||||
bool record_settle(sqlite3_stmt* insert, security_key key, IntPrice price, Date date) {
|
||||
sqlite3_bind_int64(insert, 1, key);
|
||||
sqlite3_bind_int(insert, 2, date.to_short());
|
||||
sqlite3_bind_double(insert, 3, price);
|
||||
sqlite3_bind_int64(insert, 3, price.mantissa());
|
||||
sqlite3_bind_int(insert, 4, price.exponent());
|
||||
auto result = sqlite3_step(insert);
|
||||
sqlite3_reset(insert);
|
||||
return (result == SQLITE_DONE || result == SQLITE_ROW);
|
||||
@@ -56,14 +57,16 @@ void publish_settles(sqlite3_stmt* select, MDProducerHandle handle) {
|
||||
sqlite3_bind_int(select, 1, handle.key());
|
||||
auto result = sqlite3_step(select);
|
||||
if (result == SQLITE_ROW) {
|
||||
Price price = sqlite3_column_double(select, 0);
|
||||
Date date = Date(sqlite3_column_int(select, 1));
|
||||
handle.update_settlement(TimeStamp::now(), price, date);
|
||||
int64_t mantissa = sqlite3_column_int(select, 0);
|
||||
int exponent = sqlite3_column_int(select, 1);
|
||||
Date date = Date(sqlite3_column_int(select, 2));
|
||||
handle.update_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date);
|
||||
result = sqlite3_step(select);
|
||||
if (result == SQLITE_ROW) {
|
||||
price = sqlite3_column_double(select, 0);
|
||||
date = Date(sqlite3_column_int(select, 1));
|
||||
handle.update_previous_settlement(TimeStamp::now(), price, date);
|
||||
int64_t mantissa = sqlite3_column_int(select, 0);
|
||||
int exponent = sqlite3_column_int(select, 1);
|
||||
date = Date(sqlite3_column_int(select, 2));
|
||||
handle.update_previous_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date);
|
||||
}
|
||||
}
|
||||
sqlite3_reset(select);
|
||||
@@ -97,28 +100,29 @@ SettlementRecorder::SettlementRecorder(Platform platform) :
|
||||
sqlite3_exec_nofail(db, "PRAGMA journal_mode=OFF");
|
||||
sqlite3_exec_nofail(db, "PRAGMA syncronous=0");
|
||||
// key=(security_key+date)
|
||||
sqlite3_exec_nofail(db, "CREATE TABLE IF NOT EXISTS settlements (key INTEGER NOT NULL, date INTEGER NOT NULL, price REAL NOT NULL, PRIMARY KEY (key,date));");
|
||||
sqlite3_exec_nofail(db,
|
||||
"CREATE TABLE IF NOT EXISTS settlements2 (key INTEGER NOT NULL, date INTEGER NOT NULL, mantissa INTEGER NOT NULL, exponent INTEGER NOT NULL, PRIMARY KEY (key,date));");
|
||||
|
||||
// Delete any records > 10 days old
|
||||
auto cutoff = datetime::local_now() - boost::gregorian::days(10);
|
||||
auto encoded = datetime::ll_to_date(datetime::ptime_to_ll(cutoff)).to_short();
|
||||
char query[256];
|
||||
sprintf(query, "delete from settlements where date < %i", encoded);
|
||||
sprintf(query, "delete from settlements2 where date < %i", encoded);
|
||||
sqlite3_exec_nofail(db, query);
|
||||
|
||||
// statements
|
||||
_self->select_stmt = prepare_query(db, "select price,date from settlements where key=? order by date desc limit 2");
|
||||
_self->insert_stmt = prepare_query(db, "insert or replace into settlements(key, date, price) values(?,?,?)");
|
||||
_self->select_stmt = prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2");
|
||||
_self->insert_stmt = prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)");
|
||||
}
|
||||
|
||||
SettlementRecorder::~SettlementRecorder() { }
|
||||
|
||||
void SettlementRecorder::restore_settlement(MDProducerHandle handle) {
|
||||
Work work = { SNAPSHOT, handle, 0, Date() };
|
||||
Work work = { SNAPSHOT, handle, INT_NAN, Date() };
|
||||
_self->work_queue.push(work);
|
||||
}
|
||||
|
||||
void SettlementRecorder::add_settlement(MDProducerHandle handle, Price price, datetime::Date date) {
|
||||
void SettlementRecorder::add_settlement(MDProducerHandle handle, IntPrice price, datetime::Date date) {
|
||||
Work work = { UPDATE, handle, price, date };
|
||||
_self->work_queue.push(work);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
// Put settlement update in a queue
|
||||
// in do_work() drain queue and update settlement 'depth'
|
||||
void restore_settlement(MDProducerHandle handle);
|
||||
void add_settlement(MDProducerHandle handle, Price price, datetime::Date date);
|
||||
void add_settlement(MDProducerHandle handle, IntPrice price, datetime::Date date);
|
||||
void do_work();
|
||||
|
||||
private:
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace md {
|
||||
|
||||
using utils::TimeStamp;
|
||||
|
||||
const uint32_t shm_version = 100;
|
||||
const uint32_t shm_version = 101;
|
||||
|
||||
struct ALIGNED_(CACHE_LINE) HotHeader {
|
||||
size_t count;
|
||||
@@ -45,6 +45,7 @@ class HotEntry {
|
||||
PriceLevel book; // 24
|
||||
security_key key; // 4
|
||||
uint32_t sequence; // 4
|
||||
IntPrice settlement; // 8
|
||||
uint8_t status; // 1
|
||||
void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); }
|
||||
TimeStamp time() const { return timestamp.load(std::memory_order_acquire); }
|
||||
@@ -52,7 +53,7 @@ class HotEntry {
|
||||
static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small");
|
||||
|
||||
const uint32_t COCONUT_MAX_DEPTH = 10;
|
||||
const uint32_t COCONUT_IMPLIED_DEPTH = 2;
|
||||
const uint32_t COCONUT_MAX_IMPLIED_DEPTH = 2;
|
||||
class HotDepthEntry {
|
||||
std::atomic<TimeStamp> timestamp; // 8
|
||||
public:
|
||||
@@ -67,45 +68,34 @@ class HotDepthEntry {
|
||||
uint32_t size() const { return sizeof(HotDepthEntry); }
|
||||
security_key key; // 4 NOTE: key is at a different offset than it is in HotEntry
|
||||
uint32_t sequence; // 4
|
||||
IntPrice settlement; // 8
|
||||
std::array<PriceLevel, COCONUT_MAX_DEPTH> direct; // 240
|
||||
std::array<PriceLevel, COCONUT_IMPLIED_DEPTH> implied; // 48
|
||||
std::array<ImpliedPriceLevel, COCONUT_MAX_IMPLIED_DEPTH> implied; // 48
|
||||
uint8_t status; // 1
|
||||
|
||||
const PriceLevel* at(uint32_t depth) const {
|
||||
if (depth < direct.size()) {
|
||||
return &direct[depth];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
void read_direct(uint32_t depth, PriceSize* bid, PriceSize* ask) const {
|
||||
if (depth < direct.size()) {
|
||||
*bid = direct[depth].bid();
|
||||
*ask = direct[depth].ask();
|
||||
}
|
||||
*bid = direct[depth].bid();
|
||||
*ask = direct[depth].ask();
|
||||
}
|
||||
void read_implied(uint32_t depth, PriceSize* bid, PriceSize* ask) const {
|
||||
if (depth < implied.size()) {
|
||||
*bid = implied[depth].bid();
|
||||
*ask = implied[depth].ask();
|
||||
}
|
||||
*bid = implied[depth].bid();
|
||||
*ask = implied[depth].ask();
|
||||
}
|
||||
bool write_direct(uint32_t depth, const PriceSize& bid, const PriceSize& ask) {
|
||||
if (depth < direct.size()) {
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (direct[depth] != temp) {
|
||||
direct[depth] = PriceLevel(bid,ask);
|
||||
return true;
|
||||
}
|
||||
bts_assert_d(depth < direct.size());
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (direct[depth] != temp) {
|
||||
direct[depth] = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool write_implied(uint32_t depth, const PriceSize& bid, const PriceSize& ask) {
|
||||
if (depth < implied.size()) {
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (implied[depth] != temp) {
|
||||
implied[depth] = PriceLevel(bid,ask);
|
||||
return true;
|
||||
}
|
||||
bts_assert_d(depth < implied.size());
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (implied[depth] != temp) {
|
||||
implied[depth] = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -168,9 +158,8 @@ struct ALIGNED_(CACHE_LINE) ColdHeader {
|
||||
struct MiscData {
|
||||
MiscData():
|
||||
misc_timestamp(),
|
||||
settlement_price(NO_PRICE),
|
||||
settlement_date(),
|
||||
previous_settlement_price(NO_PRICE),
|
||||
previous_settlement_price(INT_NAN),
|
||||
previous_settlement_date(),
|
||||
open_price(NO_PRICE),
|
||||
open_date(),
|
||||
@@ -185,9 +174,8 @@ struct MiscData {
|
||||
{ }
|
||||
TimeStamp misc_timestamp;
|
||||
// Settlement
|
||||
Price settlement_price;
|
||||
datetime::Date settlement_date;
|
||||
Price previous_settlement_price;
|
||||
IntPrice previous_settlement_price;
|
||||
datetime::Date previous_settlement_date;
|
||||
|
||||
// Opening/Closing
|
||||
@@ -206,7 +194,6 @@ struct MiscData {
|
||||
|
||||
bool operator==(const MiscData& other) const {
|
||||
return
|
||||
settlement_price == other.settlement_price &&
|
||||
settlement_date == other.settlement_date &&
|
||||
previous_settlement_price == other.previous_settlement_price &&
|
||||
previous_settlement_date == other.previous_settlement_date &&
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "lib/utils/ValueWrapper.h"
|
||||
namespace bts {
|
||||
namespace md {
|
||||
using ImpliedSize = utils::ValueWrapper<struct ImpliedSizeTag,uint32_t>;
|
||||
}}
|
||||
+41
-33
@@ -50,13 +50,13 @@ message MDRequest {
|
||||
repeated SecurityChannel channels = 1;
|
||||
enum Flags {
|
||||
SECURITY = 0;
|
||||
BBO = 0x1;
|
||||
DEPTH = 0x2;
|
||||
BBO = 0x1;
|
||||
DIRECT = 0x2;
|
||||
LAST_TRADE = 0x4;
|
||||
TRADE_LOG = 0x8;
|
||||
RFQ = 0x10;
|
||||
// CUSTOMER_BBO = 0x20;
|
||||
MISC = 0x40;
|
||||
MISC = 0x30;
|
||||
IMPLIED = 0x40;
|
||||
}
|
||||
string host = 2;
|
||||
}
|
||||
@@ -64,7 +64,8 @@ message MDRequest {
|
||||
message ChannelUpdate {
|
||||
uint32 key = 1;
|
||||
bts.api.Security security = 2;
|
||||
BookMessage book = 3;
|
||||
BookMessage direct = 3;
|
||||
BookMessage implied = 4;
|
||||
MiscMessage misc = 5;
|
||||
TradeMessage last_trade = 6;
|
||||
}
|
||||
@@ -81,6 +82,11 @@ message MDReply {
|
||||
repeated ChannelReply channels = 1;
|
||||
}
|
||||
|
||||
message PriceMessage {
|
||||
int64 mantissa = 1;
|
||||
sint32 exponent = 2;
|
||||
}
|
||||
|
||||
message PriceSizeMessage {
|
||||
int64 mantissa = 1;
|
||||
sint32 exponent = 2;
|
||||
@@ -103,20 +109,22 @@ message BookMessage {
|
||||
|
||||
message MiscMessage {
|
||||
// int32 key = 1;
|
||||
double settlement_price = 2;
|
||||
uint32 settlement_date = 3;
|
||||
double previous_settlement_price = 20;
|
||||
uint32 previous_settlement_date = 21;
|
||||
double open_price = 4;
|
||||
uint32 open_date = 5;
|
||||
double close_price = 6;
|
||||
uint32 close_date = 7;
|
||||
double session_high = 8;
|
||||
double session_low = 9;
|
||||
double open_interest = 10;
|
||||
double session_high_bid = 11;
|
||||
double session_low_offer = 12;
|
||||
double fixing_price = 13;
|
||||
reserved 1;
|
||||
reserved 2;
|
||||
PriceMessage settlement = 22;
|
||||
uint32 settlement_date = 3;
|
||||
PriceMessage previous_settlement_price = 23;
|
||||
uint32 previous_settlement_date = 21;
|
||||
double open_price = 4;
|
||||
uint32 open_date = 5;
|
||||
double close_price = 6;
|
||||
uint32 close_date = 7;
|
||||
double session_high = 8;
|
||||
double session_low = 9;
|
||||
double open_interest = 10;
|
||||
double session_high_bid = 11;
|
||||
double session_low_offer = 12;
|
||||
double fixing_price = 13;
|
||||
}
|
||||
|
||||
enum AggressorSide {
|
||||
@@ -139,25 +147,25 @@ message TradeMessage {
|
||||
}
|
||||
message QuoteRequestMessage {
|
||||
enum Side {
|
||||
UNDEFINED = 0;
|
||||
BUY = 49;
|
||||
SELL = 50;
|
||||
CROSS = 99;
|
||||
}
|
||||
uint32 key = 1;
|
||||
int64 time = 2;
|
||||
string request_id = 3;
|
||||
int32 size = 5;
|
||||
Side side = 6;
|
||||
bool tradable = 7;
|
||||
bool gap = 8;
|
||||
UNDEFINED = 0;
|
||||
BUY = 49;
|
||||
SELL = 50;
|
||||
CROSS = 99;
|
||||
}
|
||||
uint32 key = 1;
|
||||
int64 time = 2;
|
||||
string request_id = 3;
|
||||
int32 size = 5;
|
||||
Side side = 6;
|
||||
bool tradable = 7;
|
||||
bool gap = 8;
|
||||
}
|
||||
|
||||
message TradeList {
|
||||
repeated TradeMessage items = 1;
|
||||
repeated TradeMessage items = 1;
|
||||
}
|
||||
message RFQList {
|
||||
repeated QuoteRequestMessage items = 1;
|
||||
repeated QuoteRequestMessage items = 1;
|
||||
}
|
||||
|
||||
message CoconutReplayHeader {
|
||||
|
||||
@@ -133,10 +133,15 @@ void set_price(PriceSize ps, co2::TradeMessage* ptrade) {
|
||||
ptrade->set_size(ps.size());
|
||||
}
|
||||
|
||||
void copy_misc(MiscData misc, co2::MiscMessage* pmisc) {
|
||||
pmisc->set_settlement_price(misc.settlement_price);
|
||||
void set_price(const IntPrice& price, co2::PriceMessage* dest) {
|
||||
dest->set_mantissa(price.mantissa());
|
||||
dest->set_exponent(price.exponent());
|
||||
}
|
||||
|
||||
void copy_misc(IntPrice settlement, MiscData misc, co2::MiscMessage* pmisc) {
|
||||
set_price(settlement, pmisc->mutable_settlement());
|
||||
pmisc->set_settlement_date(misc.settlement_date.to_short());
|
||||
pmisc->set_previous_settlement_price(misc.previous_settlement_price);
|
||||
set_price(misc.previous_settlement_price, pmisc->mutable_previous_settlement_price());
|
||||
pmisc->set_previous_settlement_date(misc.previous_settlement_date.to_short());
|
||||
pmisc->set_open_price(misc.open_price);
|
||||
pmisc->set_open_date(misc.open_date.to_short());
|
||||
@@ -241,20 +246,20 @@ public:
|
||||
pupd->mutable_security()->CopyFrom(handle.get_normalized_security());
|
||||
snapshot = true;
|
||||
}
|
||||
if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DEPTH)) {
|
||||
if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) {
|
||||
std::array<PriceSize, COCONUT_MAX_DEPTH> bids, asks;
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
size_t copy_depth = HASFLAG(flags, MDRequest::DEPTH) ? COCONUT_MAX_DEPTH : 1;
|
||||
auto result = handle.get_depth(time, copy_depth, bids.data(), asks.data());
|
||||
size_t copy_depth = HASFLAG(flags, MDRequest::DIRECT) ? COCONUT_MAX_DEPTH : 1;
|
||||
auto result = handle.get_direct_book(time, copy_depth, bids.data(), asks.data());
|
||||
if (result == HandleReadResult::SUCCESS) {
|
||||
auto* p = pupd->mutable_book();
|
||||
auto* p = pupd->mutable_direct();
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
PriceSize b = bids[x];
|
||||
if (b != PriceSize::NO_MARKET) {
|
||||
if (b != NO_MARKET) {
|
||||
convert(b, p->add_bids());
|
||||
}
|
||||
PriceSize a = asks[x];
|
||||
if (a != PriceSize::NO_MARKET) {
|
||||
if (a != NO_MARKET) {
|
||||
convert(a, p->add_asks());
|
||||
}
|
||||
}
|
||||
@@ -270,6 +275,29 @@ public:
|
||||
reply->mutable_channel()->set_time(chan.time());
|
||||
}
|
||||
}
|
||||
if ((HASFLAG(flags, MDRequest::IMPLIED) || HASFLAG(flags, MDRequest::BBO)) && !handle.is_synthetic()) {
|
||||
std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> bids, asks;
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
size_t copy_depth = HASFLAG(flags, MDRequest::IMPLIED) ? COCONUT_MAX_IMPLIED_DEPTH : 1;
|
||||
auto result = handle.get_implied_book(time, copy_depth, bids.data(), asks.data());
|
||||
if (result == HandleReadResult::SUCCESS) {
|
||||
auto* p = pupd->mutable_implied();
|
||||
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
|
||||
PriceSize b = bids[x];
|
||||
if (b != NO_MARKET) {
|
||||
convert(b, p->add_bids());
|
||||
}
|
||||
PriceSize a = asks[x];
|
||||
if (a != NO_MARKET) {
|
||||
convert(a, p->add_asks());
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (result == HandleReadResult::BUSY) {
|
||||
LOG(md,Info, "Busy read on handle (implied) {}. Retry on next request", instrument.handle.key());
|
||||
reply->mutable_channel()->set_time(chan.time());
|
||||
}
|
||||
}
|
||||
if (HASFLAG(flags, MDRequest::LAST_TRADE)) {
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
PriceSize price;
|
||||
@@ -286,10 +314,11 @@ public:
|
||||
if (HASFLAG(flags, MDRequest::MISC)) {
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
if (handle.misc_timestamp() > time) {
|
||||
copy_misc(handle.misc(), pupd->mutable_misc());
|
||||
copy_misc(handle.settlement_price(), handle.misc(), pupd->mutable_misc());
|
||||
}
|
||||
}
|
||||
if (pupd->has_book() ||
|
||||
if (pupd->has_direct() ||
|
||||
pupd->has_implied() ||
|
||||
pupd->has_last_trade() ||
|
||||
pupd->has_misc() ||
|
||||
pupd->has_security()) {
|
||||
|
||||
@@ -32,17 +32,21 @@ namespace md {
|
||||
using namespace co2;
|
||||
|
||||
PriceSize convert(const PriceSizeMessage& psm) {
|
||||
return psm.size() > 0 ? PriceSize(psm.mantissa(), psm.exponent(), psm.size()) : PriceSize::NO_MARKET;
|
||||
return psm.size() > 0 ? PriceSize({psm.mantissa(), (int8_t)psm.exponent()}, psm.size()) : NO_MARKET;
|
||||
}
|
||||
PriceSize trade_pricesize(const co2::TradeMessage& trade) {
|
||||
return trade.size() > 0 ?
|
||||
PriceSize(trade.mantissa(), trade.exponent(), trade.size()) : PriceSize::NO_MARKET;
|
||||
PriceSize({trade.mantissa(), (int8_t)trade.exponent()}, trade.size()) : NO_MARKET;
|
||||
}
|
||||
PriceSize book_at(const google::protobuf::RepeatedPtrField<PriceSizeMessage>& side, int index) {
|
||||
if (side.size() > index) {
|
||||
return convert(side.Get(index));
|
||||
}
|
||||
return PriceSize::NO_MARKET;
|
||||
return NO_MARKET;
|
||||
}
|
||||
|
||||
IntPrice convert(const PriceMessage& price) {
|
||||
return { price.mantissa(), (int8_t)price.exponent() };
|
||||
}
|
||||
|
||||
class RelayProducer : public IRelayWriter {
|
||||
@@ -54,18 +58,23 @@ class RelayProducer : public IRelayWriter {
|
||||
utils::TimeStamp updated = utils::TimeStamp::now();
|
||||
bool snapshot = update.has_security();
|
||||
// If this is a snapshot an empty book() means "no market" rather than "no change"
|
||||
if (update.has_book() || snapshot) {
|
||||
if (update.has_direct() || update.has_implied() || snapshot) {
|
||||
std::array<PriceSize,COCONUT_MAX_DEPTH> bids, asks;
|
||||
std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH> implied_bids, implied_asks;
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
bids[x] = book_at(update.book().bids(), x);
|
||||
asks[x] = book_at(update.book().asks(), x);
|
||||
bids[x] = book_at(update.direct().bids(), x);
|
||||
asks[x] = book_at(update.direct().asks(), x);
|
||||
}
|
||||
handle.update_book(updated, bids, asks);
|
||||
for (size_t x = 0; x <COCONUT_MAX_IMPLIED_DEPTH; ++x) {
|
||||
implied_bids[x] = book_at(update.implied().bids(), x);
|
||||
implied_asks[x] = book_at(update.implied().asks(), x);
|
||||
}
|
||||
handle.update_books(updated, bids, asks, implied_bids, implied_asks);
|
||||
}
|
||||
if (update.has_misc()) {
|
||||
auto& misc = update.misc();
|
||||
handle.update_settlement(updated, misc.settlement_price(), datetime::Date(misc.settlement_date()));
|
||||
handle.update_previous_settlement(updated, misc.previous_settlement_price(), datetime::Date(misc.previous_settlement_date()));
|
||||
handle.update_settlement(updated, convert(misc.settlement()), datetime::Date(misc.settlement_date()));
|
||||
handle.update_previous_settlement(updated, convert(misc.previous_settlement_price()), datetime::Date(misc.previous_settlement_date()));
|
||||
handle.update_opening(updated, misc.open_price(), datetime::Date(misc.open_date()));
|
||||
handle.update_closing(updated, misc.close_price(), datetime::Date(misc.close_date()));
|
||||
handle.update_session_high(updated, misc.session_high());
|
||||
|
||||
@@ -37,10 +37,11 @@ extern "C" BTS_EXPORT void co2_get_sample_hot(size_t& size, HotEntry& entry) {
|
||||
size = sizeof(HotEntry);
|
||||
entry.key = 1234;
|
||||
entry.book = PriceLevel(
|
||||
PriceSize::from_double<2>(7.2, 101),
|
||||
PriceSize::from_double<2>(-9.7, 245));
|
||||
PriceSize_from_double<2>(7.2, 101),
|
||||
PriceSize_from_double<2>(-9.7, 245));
|
||||
entry.sequence = 777;
|
||||
entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS;
|
||||
entry.settlement = IntPrice_from_double<7>(98.6);
|
||||
entry.set_time(utils::TimeStamp::now());
|
||||
}
|
||||
|
||||
@@ -49,12 +50,18 @@ extern "C" BTS_EXPORT void co2_get_sample_depth(size_t& size, HotDepthEntry& ent
|
||||
entry.key = 1234;
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
entry.write_direct(x,
|
||||
PriceSize::from_double<2>(x + 0.5, x + 1),
|
||||
PriceSize::from_double<2>(x + 0.6, x + 2));
|
||||
PriceSize_from_double<2>(x + 0.5, x + 1),
|
||||
PriceSize_from_double<2>(x + 0.6, x + 2));
|
||||
}
|
||||
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
|
||||
entry.write_implied(x,
|
||||
PriceSize_from_double<3>(77, x + 1),
|
||||
PriceSize_from_double<3>(88, x + 2));
|
||||
}
|
||||
entry.set_time(utils::TimeStamp::now());
|
||||
entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS;
|
||||
entry.sequence = 888;
|
||||
entry.settlement = IntPrice_from_double<7>(98.6);
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) {
|
||||
@@ -62,13 +69,12 @@ extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) {
|
||||
entry.exchange_status = TRADINGSTATUS_NORMAL;
|
||||
|
||||
// Last trade
|
||||
entry.last_trade.store(PriceSize::from_double<2>(4.2, 101));
|
||||
entry.last_trade.store(PriceSize_from_double<2>(4.2, 101));
|
||||
entry.total_volume = 12345;
|
||||
entry.last_trade_time = datetime::ll_now();
|
||||
// Settlement
|
||||
entry.misc.settlement_price = 3.3;
|
||||
entry.misc.settlement_date = datetime::Date(2015, 9, 3);
|
||||
entry.misc.previous_settlement_price = 3.3;
|
||||
entry.misc.previous_settlement_price = IntPrice_from_double<7>(3.3);
|
||||
entry.misc.previous_settlement_date = datetime::Date(2015, 9, 2);
|
||||
// Open/close
|
||||
entry.misc.open_price = 4.4;
|
||||
@@ -102,21 +108,31 @@ extern "C" BTS_EXPORT void co2_get_sample_cold_header(ColdHeader& header) {
|
||||
// Do not write to trade_buffer or rfq_buffer
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT MDProducer* co2_produce_test_md() {
|
||||
struct TestProducer {
|
||||
std::shared_ptr<MDProducer> md;
|
||||
std::vector<MDProducerHandle> handles;
|
||||
};
|
||||
|
||||
extern "C" BTS_EXPORT TestProducer* co2_produce_test_md() {
|
||||
config::internal::initialize_for_test(
|
||||
"PlatformMap {"
|
||||
" value {"
|
||||
" glbx_platform: 'PLATFORM_SIM'"
|
||||
" }"
|
||||
"}");
|
||||
auto* p = new MDProducer(PLATFORM_SIM);
|
||||
auto producers = create_test_securities(*p);
|
||||
auto* tp = new TestProducer();
|
||||
tp->md = std::make_shared<MDProducer>(PLATFORM_SIM, false);
|
||||
tp->handles = create_test_securities(*(tp->md));
|
||||
auto& producers = tp->handles;
|
||||
assign_ids(producers.begin(), producers.end());
|
||||
for (auto& p : producers) {
|
||||
p.update_bbo(utils::TimeStamp::now(), PriceSize::from_double<3>(1.1, 101), PriceSize::from_double<3>(2.2,202));
|
||||
p.add_trade(utils::TimeStamp::now(), PriceSize::from_double<3>(5.5, 505), datetime::ll_from_date(datetime::Date(2015, 9, 22)), AGGRESSOR_SIDE_NONE, 4200);
|
||||
p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 8));
|
||||
p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 9));
|
||||
p.update_bbo(utils::TimeStamp::now(), PriceSize_from_double<3>(1.1, 101), PriceSize_from_double<3>(2.2,202));
|
||||
p.add_trade(utils::TimeStamp::now(),
|
||||
PriceSize_from_double<3>(5.5, 505),
|
||||
datetime::ll_from_date(datetime::Date(2015, 9, 22)),
|
||||
AGGRESSOR_SIDE_NONE, 4200);
|
||||
p.update_settlement(utils::TimeStamp::now(), IntPrice_from_double<7>(6.6), datetime::Date(2000, 8, 8));
|
||||
p.update_settlement(utils::TimeStamp::now(), IntPrice_from_double<7>(6.6), datetime::Date(2000, 8, 9));
|
||||
|
||||
p.update_opening(utils::TimeStamp::now(), 7.7, datetime::Date(2001, 1, 2));
|
||||
auto t = utils::TimeStamp::now();
|
||||
@@ -129,16 +145,24 @@ extern "C" BTS_EXPORT MDProducer* co2_produce_test_md() {
|
||||
p.update_fixing_price(t, 14.14);
|
||||
}
|
||||
|
||||
return p;
|
||||
return tp;
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_destroy_test_md(MDProducer* producer) {
|
||||
extern "C" BTS_EXPORT void co2_destroy_test_md(TestProducer* producer) {
|
||||
if (producer != nullptr) delete producer;
|
||||
config::internal::reset();
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_zero_and_nan_book(PriceLevel& price) {
|
||||
price = { PriceSize::NO_MARKET, PriceSize::from_double<7>(0,1) };
|
||||
price = { NO_MARKET, PriceSize_from_double<7>(0,1) };
|
||||
}
|
||||
extern "C" BTS_EXPORT void co2_get_nan_price_size(PriceSize& ps) {
|
||||
ps = NO_MARKET;
|
||||
}
|
||||
extern "C" BTS_EXPORT void co2_test_get_constants(size_t& max_depth, size_t& max_implied_depth, uint8_t& nan_exponent) {
|
||||
max_depth = COCONUT_MAX_DEPTH;
|
||||
max_implied_depth = COCONUT_MAX_IMPLIED_DEPTH;
|
||||
nan_exponent = INT_NAN.exponent();
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_sample_datetime(datetime::LLDateTime& now, datetime::LLDateTime& utc_now) {
|
||||
|
||||
+1047
-187
File diff suppressed because it is too large.
Load diff
@@ -1,3 +1,4 @@
|
||||
#ifndef _WIN32
|
||||
#include "catch/catch.hpp"
|
||||
#include "../MDConsumerHandle.h"
|
||||
#include "Co2TestInstrument.h"
|
||||
@@ -9,6 +10,10 @@
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
#include <ostream>
|
||||
#define ENABLE_BENCHMARK 0
|
||||
#if ENABLE_BENCHMARK
|
||||
#include <benchmark/benchmark.h>
|
||||
#endif
|
||||
|
||||
|
||||
using namespace std;
|
||||
@@ -35,16 +40,16 @@ void setup_market(MDProducerHandle producer, const vector<PS>& bids, const vecto
|
||||
auto ts = TimeStamp::now();
|
||||
|
||||
std::array<PriceSize,COCONUT_MAX_DEPTH> bids_a = { }, asks_a = { };
|
||||
std::transform(bids.begin(), bids.end(), bids_a.begin(), [](const PS& ps) { return PriceSize::from_double<7>(ps.price, ps.size); });
|
||||
std::transform(asks.begin(), asks.end(), asks_a.begin(), [](const PS& ps) { return PriceSize::from_double<7>(ps.price, ps.size); });
|
||||
std::transform(bids.begin(), bids.end(), bids_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); });
|
||||
std::transform(asks.begin(), asks.end(), asks_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); });
|
||||
|
||||
producer.update_book(ts, bids_a, asks_a);
|
||||
producer.update_direct(ts, bids_a, asks_a);
|
||||
}
|
||||
|
||||
void expect_market(const MDConsumerHandle& handle, const vector<PS> bids, const vector<PS>& asks) {
|
||||
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
|
||||
TimeStamp t;
|
||||
handle.get_depth(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
|
||||
handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
|
||||
vector<PS> src_bids, src_asks;
|
||||
sane::map(raw_bids, src_bids, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()};});
|
||||
sane::map(raw_asks, src_asks, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()}; });
|
||||
@@ -70,11 +75,15 @@ void expect_market(const MDConsumerHandle& handle, const vector<PS> bids, const
|
||||
tuple<shared_ptr<SyntheticStrategy>,vector<Co2TestInstrument>> make_securities(const vector<tuple<double, double,api::Side>>& leg_descr) {
|
||||
vector<Co2TestInstrument> legs;
|
||||
auto synthetic = make_shared<SyntheticStrategy>();
|
||||
uint32_t security_key = 100;
|
||||
for (const auto& ldesc : leg_descr) {
|
||||
double quantity, tick_size;
|
||||
api::Side side;
|
||||
std::tie(quantity, tick_size, side) = ldesc;
|
||||
Co2TestInstrument instr("ES", api::SECURITYTYPE_FUTURE);
|
||||
instr.producer().set_key(security_key);
|
||||
++security_key;
|
||||
|
||||
auto& s = synthetic->security;
|
||||
s.mutable_contract_spec()->set_low_tick(tick_size);
|
||||
s.mutable_contract_spec()->set_high_tick(tick_size);
|
||||
@@ -86,10 +95,16 @@ tuple<shared_ptr<SyntheticStrategy>,vector<Co2TestInstrument>> make_securities(c
|
||||
l->set_side(side);
|
||||
synthetic->legs.push_back(instr.consumer());
|
||||
legs.push_back(instr);
|
||||
|
||||
}
|
||||
return { synthetic, legs };
|
||||
}
|
||||
|
||||
void set_leg_params(SyntheticStrategy& strat, uint security_key, const spark::LegSpreaderParams& params) {
|
||||
auto* map = strat.strategy.mutable_leg_params_map();
|
||||
map->insert({ security_key, params });
|
||||
}
|
||||
|
||||
TEST_CASE("SyntheticBookMerge", "[md][synthetic]") {
|
||||
shared_ptr<SyntheticStrategy> synthetic;
|
||||
vector<Co2TestInstrument> test_instr;
|
||||
@@ -221,6 +236,93 @@ TEST_CASE("SPARK-955", "[md][synthetic]") {
|
||||
{ {5.00, 3}, {5.25, 1}, {6.00,3}, {6.50,6} });
|
||||
}
|
||||
|
||||
TEST_CASE("Synthetic_net_change", "[md][synthetic]") {
|
||||
shared_ptr<SyntheticStrategy> synthetic;
|
||||
vector<Co2TestInstrument> test_instr;
|
||||
tie(synthetic, test_instr) = make_securities({ {1.0, 0.01, api::SIDE_BUY}, {2.0, 0.01, api::SIDE_SELL }});
|
||||
auto es_buy1 = test_instr[0];
|
||||
auto es_sell2 = test_instr[1];
|
||||
MDConsumerHandle synthetic_handle{synthetic};
|
||||
|
||||
// Set legs to use net change
|
||||
spark::LegSpreaderParams lparam;
|
||||
lparam.set_leg_md_calc(spark::LegSpreaderParams::NET_CHANGE);
|
||||
set_leg_params(*synthetic, es_buy1.producer().key(), lparam);
|
||||
set_leg_params(*synthetic, es_sell2.producer().key(), lparam);
|
||||
|
||||
// set settlement prices
|
||||
es_buy1.producer().update_settlement(TimeStamp::now(), { 100, 0 }, datetime::today());
|
||||
es_sell2.producer().update_settlement(TimeStamp::now(), { 50, 0 }, datetime::today());
|
||||
|
||||
SECTION("buy 1 sell 2 Net") {
|
||||
setup_market(es_buy1.producer(),
|
||||
{{190,1}, {180,11}, {170,4}, {160,10}},
|
||||
{{200,2}, {210,6}, {220,8}, {230,12}});
|
||||
setup_market(es_sell2.producer(),
|
||||
{{90,5}, {80,9}, {70,11}, {60,10}, {50,1}},
|
||||
{{100,16}, {110,8}, {120,12}, {130,5}});
|
||||
|
||||
// Validate merge
|
||||
expect_market(synthetic_handle,
|
||||
{{-10,1}, {-20,7}, {-40,4}, {-70,4}, {-80,2}, {-100,2}},
|
||||
{{20,2}, {40,1}, {50,4}, {70,1}, {80,4}, {90,1}, {100,3}, {110,1}, {120,1}});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}}}//namespace
|
||||
//
|
||||
// Benchmarks
|
||||
//
|
||||
#if ENABLE_BENCHMARK
|
||||
void synthetic_get_book_speed2(benchmark::State& state) {
|
||||
shared_ptr<SyntheticStrategy> synthetic;
|
||||
vector<Co2TestInstrument> test_instr;
|
||||
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL } });
|
||||
auto esm8 = test_instr[0];
|
||||
auto esu8 = test_instr[1];
|
||||
MDConsumerHandle synthetic_handle(synthetic);
|
||||
|
||||
setup_market(esm8.producer(),
|
||||
{{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}},
|
||||
{{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}});
|
||||
setup_market(esu8.producer(),
|
||||
{{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}},
|
||||
{{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}});
|
||||
|
||||
for (auto _ : state) {
|
||||
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
|
||||
TimeStamp t;
|
||||
synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
|
||||
}
|
||||
}
|
||||
BENCHMARK(synthetic_get_book_speed2);
|
||||
void synthetic_get_book_speed3(benchmark::State& state) {
|
||||
shared_ptr<SyntheticStrategy> synthetic;
|
||||
vector<Co2TestInstrument> test_instr;
|
||||
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL }, { 1.0, 0.25, api::SIDE_BUY}});
|
||||
auto esm8 = test_instr[0];
|
||||
auto esu8 = test_instr[1];
|
||||
auto esz8 = test_instr[2];
|
||||
MDConsumerHandle synthetic_handle(synthetic);
|
||||
|
||||
setup_market(esm8.producer(),
|
||||
{{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}},
|
||||
{{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}});
|
||||
setup_market(esu8.producer(),
|
||||
{{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}},
|
||||
{{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}});
|
||||
setup_market(esz8.producer(),
|
||||
{{2601.25, 20}, {2600.50, 3}, {2600.25, 100}},
|
||||
{{2607.25, 3}, {2607.50, 1}, {2608.25, 900}});
|
||||
|
||||
for (auto _ : state) {
|
||||
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
|
||||
TimeStamp t;
|
||||
synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
|
||||
}
|
||||
}
|
||||
BENCHMARK(synthetic_get_book_speed3);
|
||||
#endif
|
||||
|
||||
}}}//namespace
|
||||
#endif
|
||||
@@ -82,7 +82,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
|
||||
|
||||
for (auto &h : _handles) {
|
||||
auto &handle = h.handle;
|
||||
if (handle.get_depth(h.last, std::tuple_size<BookSide>(),
|
||||
if (handle.get_book(h.last, std::tuple_size<BookSide>(),
|
||||
&book.bid()[0], &book.ask()[0]) ==
|
||||
md::HandleReadResult::SUCCESS) {
|
||||
book._security_key = SecurityKey(handle.key());
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "MDSupplementarySource.h"
|
||||
#include "lib/orderman/events/SettlementRequestEvent.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/os/os.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
struct HandleRecord {
|
||||
md::MDConsumerHandle handle;
|
||||
md::IntPrice settlement_price;
|
||||
};
|
||||
|
||||
struct MDSupplementarySource::PImpl
|
||||
{
|
||||
PImpl(md::MDConsumer& md_consumer)
|
||||
:
|
||||
md_consumer(md_consumer),
|
||||
handle_map(),
|
||||
settlement_update(){
|
||||
}
|
||||
md::MDConsumer md_consumer;
|
||||
// list of sec subscriptions vs their handles
|
||||
std::map<uint32_t, HandleRecord> handle_map;
|
||||
mercury::Stream<SettlementUpdateEvent> settlement_update;
|
||||
};
|
||||
|
||||
MDSupplementarySource::MDSupplementarySource(md::MDConsumer& md_consumer)
|
||||
:
|
||||
me(new PImpl(md_consumer)){
|
||||
}
|
||||
|
||||
MDSupplementarySource::~MDSupplementarySource(){
|
||||
}
|
||||
|
||||
void MDSupplementarySource::init(){
|
||||
me->settlement_update=make_stream<SettlementUpdateEvent>();
|
||||
subscribe<SettlementRequestEvent>([this](auto const &update) {
|
||||
if (update.events) {
|
||||
for (auto const &ev : *update.events) {
|
||||
auto k = ev.security_key.value();
|
||||
auto it = me->handle_map.find(k);
|
||||
if(it ==me->handle_map.end()){
|
||||
LOG(OrderMan, Info, "MDSupplementarySource::init key:{}", k);
|
||||
me->handle_map[k]={me->md_consumer.get_handle(k),md::INT_NAN};
|
||||
}
|
||||
else {
|
||||
// late joiners
|
||||
auto const &hr = it->second;
|
||||
if (!hr.settlement_price.is_nan()) {
|
||||
SettlementUpdateEvent ev(hr.settlement_price,SecurityKey(k));
|
||||
publish(me->settlement_update,ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MDSupplementarySource::on_poll(){
|
||||
for(auto& kvp : me->handle_map){
|
||||
SecurityKey key(kvp.first);
|
||||
HandleRecord& hr=kvp.second;
|
||||
auto settlement_price = hr.handle.settlement_price();
|
||||
if(!settlement_price.is_nan() &&
|
||||
settlement_price!=hr.settlement_price){
|
||||
LOG(OrderMan, Info,
|
||||
"MDSupplementarySource::on_poll new settle key:{} price:{}",
|
||||
key, hr.settlement_price.price());
|
||||
hr.settlement_price=settlement_price;
|
||||
SettlementUpdateEvent ev(settlement_price,key);
|
||||
publish(me->settlement_update,ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "lib/mercury/actors/Actor.h"
|
||||
#include "lib/mercury/actors/IPollableActor.h"
|
||||
|
||||
namespace bts {
|
||||
|
||||
|
||||
namespace md {class MDConsumer;}
|
||||
namespace orderman {
|
||||
|
||||
// provides non hot-path market data such as settles, daily high-low etc
|
||||
// (only settles for now)
|
||||
// like MDSource, watches for MDRequestEvent's and hence constructs handles
|
||||
// for requested contracts only. these handles are then polled and settles are
|
||||
// published if changed.
|
||||
class MDSupplementarySource : public mercury::Actor, public mercury::IPollableActor
|
||||
{
|
||||
public:
|
||||
MDSupplementarySource(md::MDConsumer& md_consumer);
|
||||
~MDSupplementarySource();
|
||||
|
||||
void init() override;
|
||||
void on_poll() override;
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -370,6 +370,7 @@ void OrderManager::init_order_services() {
|
||||
me->services_data.create_fix_req_pub =
|
||||
make_stream<CreateFIXBufferRequestEvent>();
|
||||
me->services_data.md_request_pub = make_stream<MDRequestEvent>();
|
||||
me->services_data.settlement_request_pub= make_stream<SettlementRequestEvent>();
|
||||
me->services_data.report_order_state_pub =
|
||||
make_stream<OrderStateUpdateEvent>();
|
||||
me->pub_ready = make_stream<OrderManagerReadyEvent>();
|
||||
@@ -382,8 +383,13 @@ void OrderManager::init_order_services() {
|
||||
std::function<void(SecurityKey)> on_md_sub = [this](SecurityKey k) {
|
||||
publish(me->services_data.md_request_pub, k);
|
||||
};
|
||||
std::function<void(SecurityKey)> on_settlement_sub = [this](SecurityKey k) {
|
||||
publish(me->services_data.settlement_request_pub, k);
|
||||
};
|
||||
me->services_data.md_subs =
|
||||
std::make_unique<ContentSubscription<BookUpdateEvent>>(*this, on_md_sub);
|
||||
me->services_data.settlement_subs =
|
||||
std::make_unique<ContentSubscription<SettlementUpdateEvent>>(*this,on_settlement_sub);
|
||||
me->services_data.time_subs =
|
||||
std::make_unique<ContentSubscription<TimerEvent, NoKey>>(*this);
|
||||
me->services_data.order_creation_subs =
|
||||
|
||||
@@ -70,7 +70,9 @@ class SessionServices {
|
||||
class OrderServices {
|
||||
public:
|
||||
OrderServices(Order &owner, OrderServicesData &data)
|
||||
: book_updates(*data.md_subs), child_updates(*data.child_order_subs),
|
||||
: book_updates(*data.md_subs),
|
||||
settlement_updates(*data.settlement_subs),
|
||||
child_updates(*data.child_order_subs),
|
||||
time_change(*data.time_subs),
|
||||
order_creations(*data.order_creation_subs), _owner(owner), _data(data),
|
||||
_sessions() {
|
||||
@@ -79,6 +81,7 @@ class OrderServices {
|
||||
}
|
||||
|
||||
EventStream<BookUpdateEvent> book_updates;
|
||||
EventStream<SettlementUpdateEvent> settlement_updates;
|
||||
EventStream<OrderStateUpdateEvent, ParentKey> child_updates;
|
||||
EventStream<mercury::TimerEvent, mercury::NoKey> time_change;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "lib/mercury/events/TimerEvent.h"
|
||||
#include "lib/orderman/events/ActivateOrderEvent.h"
|
||||
#include "lib/orderman/events/BookUpdateEvent.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/orderman/events/CancelOrderEvent.h"
|
||||
#include "lib/orderman/events/CancelRejectEvent.h"
|
||||
#include "lib/orderman/events/CrackOrderEvent.h"
|
||||
@@ -20,6 +21,7 @@
|
||||
#include "lib/orderman/events/CreateOrderRequestEvent.h"
|
||||
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
|
||||
#include "lib/orderman/events/MDRequestEvent.h"
|
||||
#include "lib/orderman/events/SettlementRequestEvent.h"
|
||||
#include "lib/orderman/events/NewExecutionReportEvent.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/orderman/events/OrderStateUpdateEvent.h"
|
||||
@@ -75,6 +77,7 @@ class OrderServicesData {
|
||||
OrderFactory order_factory;
|
||||
|
||||
std::unique_ptr<mercury::ContentSubscription<BookUpdateEvent>> md_subs;
|
||||
std::unique_ptr<mercury::ContentSubscription<SettlementUpdateEvent>> settlement_subs;
|
||||
std::unique_ptr<
|
||||
mercury::ContentSubscription<OrderStateUpdateEvent, ParentKey>>
|
||||
child_order_subs;
|
||||
@@ -93,6 +96,7 @@ class OrderServicesData {
|
||||
mercury::Stream<CreateOrderRequestEvent> create_order_req_pub;
|
||||
mercury::Stream<CreateFIXBufferRequestEvent> create_fix_req_pub;
|
||||
mercury::Stream<MDRequestEvent> md_request_pub;
|
||||
mercury::Stream<SettlementRequestEvent> settlement_request_pub;
|
||||
mercury::Stream<OrderStateUpdateEvent> report_order_state_pub;
|
||||
|
||||
mercury::TimerEvent last_timer_ev;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class SettlementRequestEvent : public mercury::Event<SettlementRequestEvent> {
|
||||
public:
|
||||
using SynopsisPolicy = mercury::CacheAll<SettlementRequestEvent>;
|
||||
SettlementRequestEvent(SecurityKey k) : security_key(k) {}
|
||||
SettlementRequestEvent &operator=(SettlementRequestEvent const&rhs)
|
||||
{
|
||||
security_key = rhs.security_key;
|
||||
return *this;
|
||||
}
|
||||
SecurityKey security_key;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "lib/mercury/events/Event.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/mercury/synopsis/MergeByKey.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class SettlementUpdateEvent : public mercury::Event<SettlementUpdateEvent> {
|
||||
public:
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
md::IntPrice settlement_price;
|
||||
SecurityKey security_key;
|
||||
SecurityKey key() const { return security_key; }
|
||||
|
||||
SettlementUpdateEvent(const md::IntPrice& price,
|
||||
const SecurityKey& key)
|
||||
:
|
||||
settlement_price(price),
|
||||
security_key(key) {}
|
||||
};
|
||||
}}
|
||||
@@ -18,6 +18,11 @@ public:
|
||||
_book()
|
||||
{}
|
||||
|
||||
Book (SecurityKey key) :
|
||||
_security_key(key),
|
||||
_book()
|
||||
{}
|
||||
|
||||
Book (SecurityKey key,
|
||||
BookSide const&bid,
|
||||
BookSide const&ask) :
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
|
||||
using namespace bts::api::orderman;
|
||||
|
||||
inline bool order_in_error_state(OrderStatus order_status){
|
||||
return order_status == OrderStatus::ORDER_STATUS_ERROR ||
|
||||
order_status == OrderStatus::ORDER_STATUS_DISCONNECTED ||
|
||||
order_status == OrderStatus::ORDER_STATUS_REJECTED_BY_SPARK;
|
||||
}
|
||||
|
||||
inline bool order_in_working_state(OrderStatus order_status){
|
||||
return order_status == OrderStatus::ORDER_STATUS_WORKING ||
|
||||
order_status == OrderStatus::ORDER_STATUS_PENDING_WORKING;
|
||||
}
|
||||
|
||||
inline bool is_exchange_order(const OrderState& state){
|
||||
return state.has_market() ||
|
||||
state.has_limit() ||
|
||||
state.has_stop() ||
|
||||
state.has_stop_limit();
|
||||
}
|
||||
|
||||
inline bool is_parent_order(const OrderRecord& ord){
|
||||
return ord.parent_key()==0;
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h"
|
||||
#include "lib/orderman/events/SettlementUpdateEvent.h"
|
||||
#include "lib/tracing/bts_trace.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
|
||||
@@ -100,6 +101,21 @@ void SpreaderOrder::_on_book_update(const BookUpdateEvent& book_event){
|
||||
trace.spreader_on_book = utils::TimeStamp::now();
|
||||
_state_worker->on_book (book_event.book,trace);
|
||||
}
|
||||
void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent&
|
||||
settlement_event){
|
||||
if (!_active) return;
|
||||
LOG(OrderMan, Info, "SpreaderOrder::_on_settlement_update "
|
||||
"key:{} sec_key:{} state:{} settlement:{}",
|
||||
key(), settlement_event.key(),
|
||||
static_cast<int>(_state_worker->state()),
|
||||
settlement_event.settlement_price.price());
|
||||
// this is a key->index lookup
|
||||
SpreaderLegData& md_leg =
|
||||
_bookkeeping_data().get_leg_data(settlement_event.key());
|
||||
md_leg.update_settlement_price(settlement_event.settlement_price);
|
||||
bts::PerfTrace trace;
|
||||
_state_worker->on_book (md_leg.book(),trace);
|
||||
}
|
||||
void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
auto trace = ord_ev.trace;
|
||||
trace.spreader_on_child_order_update = utils::TimeStamp::now();
|
||||
@@ -114,7 +130,7 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
|
||||
if (!SpreaderUtils::order_acknowledged(status)){
|
||||
LOG(OrderMan, Diag, "SpreaderOrder::_on_child_order_update, unack'ed "
|
||||
"callback for key:{} sec_key:{} order_key:{} "
|
||||
"status:{} ignoring",
|
||||
"status:{} ignoring",
|
||||
key(), sec_key, order_key, status);
|
||||
bts_assert(filled_qty.value() == 0);
|
||||
return;
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace bts {
|
||||
namespace api {namespace orderman {class OrderState; class CreateSpreaderOrder;}}
|
||||
namespace orderman {
|
||||
|
||||
class SettlementUpdateEvent;
|
||||
|
||||
class SpreaderOrder : public Order, public ISpreaderManager
|
||||
{
|
||||
friend class SpreaderStartStateWorker;
|
||||
@@ -124,6 +126,7 @@ private:
|
||||
/////////////////////////////////////////////////
|
||||
void _on_time (const bts::mercury::TimerEvent&);
|
||||
void _on_book_update(const BookUpdateEvent&);
|
||||
void _on_settlement_update(const SettlementUpdateEvent&);
|
||||
void _on_child_order_update(const OrderStateUpdateEvent&);
|
||||
// State Transformations ////////////////////
|
||||
/////////////////////////////////////////////
|
||||
|
||||
@@ -40,6 +40,12 @@ public:
|
||||
bool is_active_quoting() const {return _leg_params.is_active_quoting();}
|
||||
const bts::api::orderman::OrderDefinition& quoter_order_definition() const {return _quoter_order_definition;}
|
||||
const bts::api::orderman::OrderDefinition& hedger_order_definition() const {return _hedger_order_definition;}
|
||||
bts::spark::LegSpreaderParams::LegMarketDataCalculation leg_md_calc() const {
|
||||
return _leg_params.leg_md_calc();
|
||||
}
|
||||
bts::spark::LegSpreaderParams::MarketDataType md_type() const {
|
||||
return _leg_params.market_data_type();
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -28,7 +28,7 @@ _reported_state(){
|
||||
for (const auto& lpd_iter : legs){
|
||||
_leg_prop_data.push_back(lpd_iter);
|
||||
const LegPropertyData& leg_property_data = _leg_prop_data.back();
|
||||
_books.emplace_back();
|
||||
_books.emplace_back(lpd_iter.leg_security_key());
|
||||
_quoter_order_builders.emplace_back(spreader_order_key, leg_property_data.quoter_order_definition(), spreader_pao_services, 10);
|
||||
_hedger_order_builders.emplace_back(spreader_order_key, leg_property_data.hedger_order_definition(), spreader_pao_services, 10);
|
||||
ChildOrder empty_order;
|
||||
|
||||
@@ -19,7 +19,8 @@ _hedger_order_builder (hedger_order_builder),
|
||||
_quoter_child_order_state (quoter_child_order_state),
|
||||
_hedger_data_vector (hedger_data_vector),
|
||||
_book(book),
|
||||
_unhedged_delta(unhedged_delta)
|
||||
_unhedged_delta(unhedged_delta),
|
||||
_settlement_price(md::INT_NAN)
|
||||
{}
|
||||
|
||||
SpreaderLegData::SpreaderLegData(SpreaderLegData&& other)
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
@@ -30,6 +32,7 @@ private:
|
||||
std::vector<SingleFillHedger>& _hedger_data_vector;
|
||||
Book& _book;
|
||||
UnhedgedDelta& _unhedged_delta;
|
||||
md::IntPrice _settlement_price;
|
||||
public:
|
||||
SpreaderLegData (const LegPropertyData& leg_property_data,
|
||||
SingleLegQuoter& quoter,
|
||||
@@ -56,6 +59,7 @@ public:
|
||||
SingleLegQuoter& quoter() {return _quoter;}
|
||||
const SingleLegQuoter& quoter() const {return _quoter;}
|
||||
const bts::orderman::Book& book () const {return _book;}
|
||||
//md::Price settlement_price() const {return _settlement_price;}
|
||||
SpreaderChildOrderState& order_update_message () {return _quoter_child_order_state;}
|
||||
bts::orderman::OrderKey key() {return _quoter_child_order_state.order_key();}
|
||||
bts::orderman::OrderKey order_key() {return _quoter_child_order_state.order_key();}
|
||||
@@ -99,6 +103,21 @@ public:
|
||||
// mutators ////////////////////
|
||||
////////////////////////////////
|
||||
// called for every book update
|
||||
void update_book(const bts::orderman::Book& book_arg) {_book = book_arg;}
|
||||
void update_book(const Book& book) {
|
||||
SpreaderUtils::copy_book(book,
|
||||
_settlement_price,
|
||||
_leg_property_data.md_type(),
|
||||
_leg_property_data.leg_md_calc(),
|
||||
_book);
|
||||
}
|
||||
void update_settlement_price(md::IntPrice price){
|
||||
_settlement_price=price;
|
||||
SpreaderUtils::copy_book(_book,
|
||||
_settlement_price,
|
||||
_leg_property_data.md_type(),
|
||||
_leg_property_data.leg_md_calc(),
|
||||
_book);
|
||||
|
||||
}
|
||||
};
|
||||
}}
|
||||
@@ -136,6 +136,80 @@ public:
|
||||
}
|
||||
bts_assert(out_quantity.value() <= target_quantity.value());
|
||||
}
|
||||
// copies the price/qty content of the source book
|
||||
// into the target, accounting for md_type and leg_calc.
|
||||
// (note that the security_key in the target is left untouched
|
||||
// and should be set by the callee)
|
||||
// this is not very clean for the reason that the spreader
|
||||
// uses the "size()" function in the book, so we copy the
|
||||
// "effective size" to the size var in the Book, which
|
||||
// for different values of md_type would mean different
|
||||
// things. the better way to do it is to make a SpreaderBook
|
||||
// that exposes effective_size() and routes it to the
|
||||
// right size.
|
||||
void static copy_book (
|
||||
const Book& source,
|
||||
md::IntPrice settlement_price,
|
||||
bts::spark::LegSpreaderParams::MarketDataType md_type,
|
||||
bts::spark::LegSpreaderParams::LegMarketDataCalculation leg_calc,
|
||||
Book& target){
|
||||
SecurityKey target_key=target.security_key();
|
||||
if(md_type==bts::spark::LegSpreaderParams::DIRECT){
|
||||
// since the "size()" in Book is the merged size
|
||||
// we put the direct in the size
|
||||
size_t target_i=0;
|
||||
for(size_t source_i=0;source_i<source.n_levels();source_i++){
|
||||
const md::PriceSize& b=source.bid()[source_i];
|
||||
if(b.direct_size()>0){
|
||||
target.bid()[target_i]=md::PriceSize(b.int_price(),b.direct_size());
|
||||
target_i++;
|
||||
}
|
||||
}
|
||||
empty_out_book(target.bid(),target_i,target.n_levels());
|
||||
target_i=0;
|
||||
for(size_t source_i=0;source_i<source.n_levels();source_i++){
|
||||
const md::PriceSize& a=source.ask()[source_i];
|
||||
if(a.direct_size()>0){
|
||||
target.ask()[target_i]=md::PriceSize(a.int_price(),a.direct_size());
|
||||
target_i++;
|
||||
}
|
||||
}
|
||||
empty_out_book(target.ask(),target_i,target.n_levels());
|
||||
}
|
||||
else if(md_type==bts::spark::LegSpreaderParams::MERGED){
|
||||
target.bid()=source.bid();
|
||||
target.ask()=source.ask();
|
||||
}
|
||||
if(leg_calc==bts::spark::LegSpreaderParams::NET_CHANGE){
|
||||
if(settlement_price.is_nan()){
|
||||
empty_out_book(target.bid(),0,target.n_levels());
|
||||
empty_out_book(target.ask(),0,target.n_levels());
|
||||
}
|
||||
else{
|
||||
offset_book(target.bid(), target.n_levels(),settlement_price);
|
||||
offset_book(target.ask(), target.n_levels(),settlement_price);
|
||||
}
|
||||
}
|
||||
bts_assert(target_key==target.security_key());
|
||||
}
|
||||
void static offset_book(BookSide& book_side,
|
||||
size_t n_levels,
|
||||
md::IntPrice offset){
|
||||
for(size_t i=0;i<n_levels;i++){
|
||||
if(book_side[i].size()!=0)
|
||||
book_side[i]=book_side[i]-offset;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
void static empty_out_book (BookSide& book_side,
|
||||
size_t start_index,
|
||||
size_t n_levels){
|
||||
md::PriceSize empty_ps;
|
||||
for(size_t i=start_index;i<n_levels;i++){
|
||||
book_side[i]=empty_ps;
|
||||
}
|
||||
}
|
||||
// CRITICAL PATH (for hedging)
|
||||
// travserses the book to locate the target_price,
|
||||
// and finds the qty at the target price and the
|
||||
|
||||
+4
-1
@@ -94,8 +94,11 @@ void SpreaderStartStateWorker::on_activate(Price price, Quantity quantity, Sprea
|
||||
const LegPropertyData& leg_prop = _spreader->_bookkeeping_data().get_leg_data(l_idx).leg_property_data();
|
||||
LOG(OrderMan, Info, "SpreaderStartStateWorker::on_activate subbing to book sec_key: {}", leg_prop.security_key());
|
||||
order_services.order_services().book_updates.sub(leg_prop.leg_security_key(),
|
||||
[this](const bts::orderman::BookUpdateEvent& bk_event)
|
||||
[this](const BookUpdateEvent& bk_event)
|
||||
{_spreader->_on_book_update (bk_event);});
|
||||
order_services.order_services().settlement_updates.sub(leg_prop.leg_security_key(),
|
||||
[this](const SettlementUpdateEvent& s_event)
|
||||
{_spreader->_on_settlement_update (s_event);});
|
||||
}
|
||||
// 6. sub to time
|
||||
order_services.order_services().time_change.sub([this](const bts::mercury::TimerEvent& t_event){_spreader->_on_time(t_event);});
|
||||
|
||||
@@ -2868,6 +2868,289 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]")
|
||||
std::cout << "4 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl;
|
||||
}
|
||||
}
|
||||
TEST_CASE ("simple book addition", "[spreader]"){
|
||||
SECTION ("direct, book"){
|
||||
SecurityKey key(12345);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
new uint16_t[5] {1, 12, 0, 0, 0},
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(1999.0),
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::BOOK,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
true);
|
||||
}
|
||||
SECTION ("direct, book_holes"){
|
||||
SecurityKey key(12345);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {0, 1, 0, 14, 20}, //directs
|
||||
new uint16_t[5] {1, 12, 0, 0, 0}, //implieds
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(1999.0),
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::BOOK,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {2000.01, 2000.04, 2000.10, os::nan(), os::nan()},
|
||||
new uint16_t[5] {1, 14, 20, 0, 0},
|
||||
true);
|
||||
}
|
||||
SECTION ("direct, net_change"){
|
||||
SecurityKey key(13579);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
new uint16_t[5] {1, 12, 0, 0, 0},
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(1999.0),
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::NET_CHANGE,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {1, 1.01, 1.03, 1.04, 1.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
true);
|
||||
}
|
||||
SECTION ("direct, net_change_nan"){
|
||||
SecurityKey key(13579);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
new uint16_t[5] {1, 12, 0, 0, 0},
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::INT_NAN,
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::NET_CHANGE,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {os::nan(), os::nan(), os::nan(), os::nan(), os::nan()},
|
||||
new uint16_t[5] {0, 0, 0, 0, 0},
|
||||
true);
|
||||
}
|
||||
SECTION ("merged, book"){
|
||||
SecurityKey key(24680);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
new uint16_t[5] {1, 12, 0, 0, 0},
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(1999.0),
|
||||
bts::spark::LegSpreaderParams::MERGED,
|
||||
bts::spark::LegSpreaderParams::BOOK,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {11, 13, 15, 14, 20},
|
||||
true);
|
||||
}
|
||||
SECTION ("merged, net_change"){
|
||||
SecurityKey key(54321);
|
||||
Book in_book (key);
|
||||
fill_book(in_book,
|
||||
new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1},
|
||||
new uint16_t[5] {10, 1, 15, 14, 20},
|
||||
new uint16_t[5] {1, 12, 0, 0, 0},
|
||||
api::Side::SIDE_BUY,
|
||||
true);
|
||||
Book out_book(key);
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(1999.0),
|
||||
bts::spark::LegSpreaderParams::MERGED,
|
||||
bts::spark::LegSpreaderParams::NET_CHANGE,
|
||||
out_book);
|
||||
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
0.01,
|
||||
new double[5] {1, 1.01, 1.03, 1.04, 1.1},
|
||||
new uint16_t[5] {11, 13, 15, 14, 20},
|
||||
true);
|
||||
}
|
||||
}
|
||||
TEST_CASE ("random book addition", "[spreader]"){
|
||||
size_t n_runs=100;
|
||||
// generates random prices, with tick size=0.01
|
||||
size_t n_levels=5;
|
||||
double* bid_prices=new double[n_levels],
|
||||
*ask_prices=new double[n_levels];
|
||||
uint16_t* direct_bid_sizes=new uint16_t[n_levels],
|
||||
*implied_bid_sizes=new uint16_t[n_levels],
|
||||
*direct_ask_sizes=new uint16_t[n_levels],
|
||||
*implied_ask_sizes=new uint16_t[n_levels];
|
||||
double* exp_bid_prices=new double[n_levels],
|
||||
*exp_ask_prices=new double[n_levels];
|
||||
uint16_t* exp_bid_sizes=new uint16_t[n_levels],
|
||||
*exp_ask_sizes=new uint16_t[n_levels];
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
SecurityKey key(gen_rand(10000,99999));
|
||||
Book in_book(key);
|
||||
double tick_size=0.01;
|
||||
double top_bid=gen_rand(100,1000),
|
||||
top_ask=top_bid+tick_size*gen_rand(1,5);
|
||||
generate_random_book(n_levels,
|
||||
tick_size,
|
||||
top_bid,
|
||||
top_ask,
|
||||
bid_prices,
|
||||
direct_bid_sizes,
|
||||
implied_bid_sizes,
|
||||
ask_prices,
|
||||
direct_ask_sizes,
|
||||
implied_ask_sizes,
|
||||
in_book);
|
||||
double settlement_price=top_bid+tick_size*gen_rand(-10,10);
|
||||
Book out_book(key);
|
||||
// direct, book
|
||||
{
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(settlement_price),
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::BOOK,
|
||||
out_book);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
tick_size,
|
||||
bid_prices,
|
||||
direct_bid_sizes,
|
||||
false);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_SELL,
|
||||
tick_size,
|
||||
ask_prices,
|
||||
direct_ask_sizes,
|
||||
false);
|
||||
}
|
||||
// "direct, net_change"
|
||||
{
|
||||
for(size_t i=0;i<n_levels;i++){
|
||||
exp_bid_prices[i]=bid_prices[i]-settlement_price;
|
||||
exp_ask_prices[i]=ask_prices[i]-settlement_price;
|
||||
}
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(settlement_price),
|
||||
bts::spark::LegSpreaderParams::DIRECT,
|
||||
bts::spark::LegSpreaderParams::NET_CHANGE,
|
||||
out_book);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
tick_size,
|
||||
exp_bid_prices,
|
||||
direct_bid_sizes,
|
||||
false);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_SELL,
|
||||
tick_size,
|
||||
exp_ask_prices,
|
||||
direct_ask_sizes,
|
||||
false);
|
||||
}
|
||||
// merged, book
|
||||
{
|
||||
for(size_t i=0;i<n_levels;i++){
|
||||
exp_bid_sizes[i]=direct_bid_sizes[i]+implied_bid_sizes[i];
|
||||
exp_ask_sizes[i]=direct_ask_sizes[i]+implied_ask_sizes[i];
|
||||
}
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(settlement_price),
|
||||
bts::spark::LegSpreaderParams::MERGED,
|
||||
bts::spark::LegSpreaderParams::BOOK,
|
||||
out_book);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
tick_size,
|
||||
bid_prices,
|
||||
exp_bid_sizes,
|
||||
false);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_SELL,
|
||||
tick_size,
|
||||
ask_prices,
|
||||
exp_ask_sizes,
|
||||
false);
|
||||
}
|
||||
// merged, net change
|
||||
{
|
||||
for(size_t i=0;i<n_levels;i++){
|
||||
exp_bid_prices[i]=bid_prices[i]-settlement_price;
|
||||
exp_ask_prices[i]=ask_prices[i]-settlement_price;
|
||||
exp_bid_sizes[i]=direct_bid_sizes[i]+implied_bid_sizes[i];
|
||||
exp_ask_sizes[i]=direct_ask_sizes[i]+implied_ask_sizes[i];
|
||||
}
|
||||
SpreaderUtils::copy_book(in_book,
|
||||
md::IntPrice_from_double<7>(settlement_price),
|
||||
bts::spark::LegSpreaderParams::MERGED,
|
||||
bts::spark::LegSpreaderParams::NET_CHANGE,
|
||||
out_book);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_BUY,
|
||||
tick_size,
|
||||
exp_bid_prices,
|
||||
exp_bid_sizes,
|
||||
false);
|
||||
validate_book(out_book,
|
||||
key,
|
||||
api::Side::SIDE_SELL,
|
||||
tick_size,
|
||||
exp_ask_prices,
|
||||
exp_ask_sizes,
|
||||
false);
|
||||
}
|
||||
}
|
||||
}
|
||||
class RV
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h"
|
||||
#include <google/protobuf/util/message_differencer.h>
|
||||
#include "lib/os/os.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
@@ -250,13 +251,32 @@ SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices _
|
||||
/**********************************************************************/
|
||||
/******************************book************************************/
|
||||
/**********************************************************************/
|
||||
|
||||
void fill_book(bts::orderman::Book& book,
|
||||
double* prices,
|
||||
uint16_t* direct_sizes,
|
||||
uint16_t* implied_sizes,
|
||||
bts::api::Side side,
|
||||
bool delete_arrays){
|
||||
bts::orderman::BookSide& bookside = book.side(side);
|
||||
for (size_t i = 0; i < book.n_levels(); i++)
|
||||
bookside[i] = md::PriceSize(md::IntPrice_from_double<7>(prices[i]),
|
||||
direct_sizes[i],
|
||||
md::ImpliedSize(implied_sizes[i]));
|
||||
if (delete_arrays){
|
||||
delete[] prices;
|
||||
delete[] direct_sizes;
|
||||
delete[] implied_sizes;
|
||||
}
|
||||
}
|
||||
|
||||
void fill_book(bts::orderman::Book& book,
|
||||
double* prices,
|
||||
uint16_t* sizes,
|
||||
bts::api::Side side){
|
||||
bts::orderman::BookSide& bookside = book.side(side);
|
||||
for (size_t i = 0; i < book.n_levels(); i++)
|
||||
bookside[i] = md::PriceSize::from_double<6>(prices[i], sizes[i]);
|
||||
bookside[i] = md::PriceSize_from_double<6>(prices[i], sizes[i]);
|
||||
}
|
||||
void fill_book(bts::orderman::Book& book,
|
||||
double* bid_prices,
|
||||
@@ -266,8 +286,8 @@ void fill_book(bts::orderman::Book& book,
|
||||
bts::orderman::BookSide& bidside = book.side(api::Side::SIDE_BUY);
|
||||
bts::orderman::BookSide& askside = book.side(api::Side::SIDE_SELL);
|
||||
for (size_t i = 0; i < book.n_levels(); i++){
|
||||
bidside[i] = md::PriceSize::from_double<6>(bid_prices[i], bid_sizes[i]);
|
||||
askside[i] = md::PriceSize::from_double<6>(ask_prices[i], ask_sizes[i]);
|
||||
bidside[i] = md::PriceSize_from_double<6>(bid_prices[i], bid_sizes[i]);
|
||||
askside[i] = md::PriceSize_from_double<6>(ask_prices[i], ask_sizes[i]);
|
||||
}
|
||||
}
|
||||
bts::orderman::Book get_book(SecurityKey key,
|
||||
@@ -280,11 +300,11 @@ bts::orderman::Book get_book(SecurityKey key,
|
||||
bool delete_arrays){
|
||||
bts::orderman::BookSide bidside, askside;
|
||||
for (size_t i = 0; i < n_bid_levels; i++){
|
||||
md::PriceSize bid = md::PriceSize::from_double<6>(bid_prices[i], bid_sizes[i]);
|
||||
md::PriceSize bid = md::PriceSize_from_double<6>(bid_prices[i], bid_sizes[i]);
|
||||
bidside[i] = bid;
|
||||
}
|
||||
for (size_t i = 0; i < n_ask_levels; i++){
|
||||
md::PriceSize ask = md::PriceSize::from_double<6>(ask_prices[i], ask_sizes[i]);
|
||||
md::PriceSize ask = md::PriceSize_from_double<6>(ask_prices[i], ask_sizes[i]);
|
||||
askside[i] = ask;
|
||||
}
|
||||
if (delete_arrays){
|
||||
@@ -465,6 +485,30 @@ void validate_book (const bts::orderman::Book& b1, const bts::orderman::Book& b2
|
||||
REQUIRE (b1.ask()[i].size() == b2.ask()[i].size());
|
||||
}
|
||||
}
|
||||
void validate_book (const bts::orderman::Book& book,
|
||||
SecurityKey key,
|
||||
api::Side side,
|
||||
double tick_size,
|
||||
double* prices,
|
||||
uint16_t* sizes,
|
||||
bool delete_arrays){
|
||||
REQUIRE(book.security_key()==key);
|
||||
const BookSide& md=side==api::Side::SIDE_BUY ? book.bid() : book.ask();
|
||||
for(size_t i=0; i<book.n_levels();i++){
|
||||
const md::PriceSize& p=md[i];
|
||||
if(!os::is_nan(prices[i]) && !os::is_nan(p.price()))
|
||||
REQUIRE(securities::test_price_equality(p.price(),prices[i],tick_size));
|
||||
else{
|
||||
REQUIRE(os::is_nan(prices[i]));
|
||||
REQUIRE(os::is_nan(p.price()));
|
||||
}
|
||||
REQUIRE(p.size()==sizes[i]);
|
||||
}
|
||||
if(delete_arrays){
|
||||
delete[] prices;
|
||||
delete[] sizes;
|
||||
}
|
||||
}
|
||||
// fires the book update to the spreader legs
|
||||
// and then an order update
|
||||
void set_spreader_orders (SpreaderOrder& spreader, double* leg_1_bid_prices, uint16_t* leg_1_bid_sizes,
|
||||
@@ -527,4 +571,36 @@ void generate_driver_set(std::vector<QuoteLegDriver>& drivers, size_t n_legs){
|
||||
drivers.push_back(d);
|
||||
}
|
||||
}
|
||||
void generate_random_book(size_t n_levels,
|
||||
double tick_size,
|
||||
double top_book,
|
||||
double top_ask,
|
||||
// out vars
|
||||
double* bid_prices,
|
||||
uint16_t* direct_bid_sizes,
|
||||
uint16_t* implied_bid_sizes,
|
||||
double* ask_prices,
|
||||
uint16_t* direct_ask_sizes,
|
||||
uint16_t* implied_ask_sizes,
|
||||
Book& book){
|
||||
double b=top_book,
|
||||
a=top_ask;
|
||||
for(size_t i=0;i<n_levels;i++){
|
||||
bid_prices[i]=b;
|
||||
direct_bid_sizes[i]=gen_rand(10,100);
|
||||
implied_bid_sizes[i]=gen_rand(10,100);
|
||||
b-=gen_rand(1,10)*tick_size;
|
||||
|
||||
ask_prices[i]=a;
|
||||
direct_ask_sizes[i]=gen_rand(10,100);
|
||||
implied_ask_sizes[i]=gen_rand(10,100);
|
||||
a+=gen_rand(1,10)*tick_size;
|
||||
}
|
||||
fill_book(book,
|
||||
bid_prices,direct_bid_sizes,implied_bid_sizes,
|
||||
bts::api::Side::SIDE_BUY,false);
|
||||
fill_book(book,
|
||||
ask_prices,direct_ask_sizes,implied_ask_sizes,
|
||||
bts::api::Side::SIDE_SELL,false);
|
||||
}
|
||||
}}
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "lib/orderman/actors/ExecutionGateway.h"
|
||||
#include "lib/orderman/actors/FillStore.h"
|
||||
#include "lib/orderman/actors/MDSource.h"
|
||||
#include "lib/orderman/actors/MDSupplementarySource.h"
|
||||
#include "lib/orderman/actors/OrderStore.h"
|
||||
#include "lib/orderman/actors/SecuritySrc.h"
|
||||
#include "lib/orderman/actors/ServiceBridge.h"
|
||||
@@ -90,8 +91,7 @@ struct OrderServiceServer::PImpl {
|
||||
: service_bridge(), service_ready(false), known_orders(), known_fills(),
|
||||
publish_orders(), publish_fills(),
|
||||
admin_service(ctx.locate<api::admin::AdminService>()), md_consumer(ss),
|
||||
stats(stats)
|
||||
{}
|
||||
stats(stats) {}
|
||||
|
||||
ServiceBridge *service_bridge;
|
||||
|
||||
@@ -111,9 +111,8 @@ struct OrderServiceServer::PImpl {
|
||||
OrderServiceServer::OrderServiceServer(
|
||||
bts::api::ServiceContext &ctx,
|
||||
std::shared_ptr<api::ISyntheticSecurityService> &ss,
|
||||
metrics::MetricsContainer &stats,
|
||||
PerfTraceLog &log)
|
||||
: api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, ss, stats)) {
|
||||
metrics::MetricsContainer &stats, PerfTraceLog &log)
|
||||
: api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, ss, stats)) {
|
||||
|
||||
me->publish_orders = make_orders_publisher();
|
||||
me->publish_fills = make_fills_publisher();
|
||||
@@ -186,6 +185,14 @@ OrderServiceServer::OrderServiceServer(
|
||||
"the OrdermanCfg key");
|
||||
thread->executor.spawn<SecuritySrc>(me->md_consumer);
|
||||
|
||||
// MDSupplementarySource
|
||||
thread = find_thread_for_actor("MDSupplementarySource", me->actor_threads);
|
||||
bts_assert_msg(
|
||||
thread,
|
||||
"Could not find a configured thread for MDSupplementarySource. See "
|
||||
"the OrdermanCfg key");
|
||||
thread->executor.spawn<MDSupplementarySource>(me->md_consumer);
|
||||
|
||||
// ServiceBridge
|
||||
thread = find_thread_for_actor("ServiceBridge", me->actor_threads);
|
||||
bts_assert_msg(thread,
|
||||
|
||||
@@ -28,8 +28,17 @@ message RulePriority
|
||||
int32 priority = 20;
|
||||
}
|
||||
|
||||
|
||||
message LegSpreaderParams
|
||||
{
|
||||
enum MarketDataType {
|
||||
MERGED = 0;
|
||||
DIRECT = 1;
|
||||
}
|
||||
enum LegMarketDataCalculation{
|
||||
BOOK=0;
|
||||
NET_CHANGE=1;
|
||||
}
|
||||
/// Legs can be quoted actively, or sent in reponse
|
||||
/// to fills
|
||||
bool is_active_quoting = 20;
|
||||
@@ -37,6 +46,8 @@ message LegSpreaderParams
|
||||
QuoteParams quote_params = 40;
|
||||
PreHedgeParams pre_hedge_params = 50;
|
||||
PostHedgeParams post_hedge_params = 60;
|
||||
MarketDataType market_data_type = 70;
|
||||
LegMarketDataCalculation leg_md_calc = 80;
|
||||
}
|
||||
|
||||
// /*************************************************************************************************
|
||||
|
||||
+442
-442
@@ -1,446 +1,446 @@
|
||||
#include "datetime.h"
|
||||
|
||||
#include "bts_assert.h"
|
||||
#include "TimeStamp.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace datetime {
|
||||
|
||||
const boost::gregorian::date anchor (1970, boost::gregorian::Jan, 1);
|
||||
const uint64_t anchor_ticks = 621355968000000000;
|
||||
const uint64_t micros_per_day = 86400000000;
|
||||
const uint64_t ticks_per_micro = 10;
|
||||
#include "datetime.h"
|
||||
|
||||
#include "bts_assert.h"
|
||||
#include "TimeStamp.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace datetime {
|
||||
|
||||
const boost::gregorian::date anchor (1970, boost::gregorian::Jan, 1);
|
||||
const uint64_t anchor_ticks = 621355968000000000;
|
||||
const uint64_t micros_per_day = 86400000000;
|
||||
const uint64_t ticks_per_micro = 10;
|
||||
const uint64_t ticks_per_day = micros_per_day * ticks_per_micro;
|
||||
const uint64_t ticks_mask = 0x3FFFFFFFFFFFFFFF;
|
||||
// DateTimeKinds
|
||||
// DateTimeKinds
|
||||
const uint64_t KindUnspecified = 0x0000000000000000;
|
||||
const uint64_t KindUtc = 0x4000000000000000;
|
||||
const uint64_t KindLocal = 0x8000000000000000;
|
||||
|
||||
unsigned long long ptime_to_ll(PTime const &t, uint64_t const &kind) {
|
||||
uint64_t days = (t.date() - anchor).days();
|
||||
uint64_t usecs = t.time_of_day().total_microseconds();
|
||||
uint64_t ticks = anchor_ticks + usecs * ticks_per_micro + days * ticks_per_day;
|
||||
return (ticks & ticks_mask) | kind;
|
||||
}
|
||||
|
||||
unsigned long long ptime_to_ll(PTime const &t) {
|
||||
return ptime_to_ll(t, KindUnspecified);
|
||||
}
|
||||
|
||||
LLDateTime ll_now() {
|
||||
return ptime_to_ll(boost::posix_time::microsec_clock::universal_time(), KindUtc);
|
||||
}
|
||||
|
||||
LLDateTime ll_local_now() {
|
||||
return ptime_to_ll(boost::posix_time::microsec_clock::local_time (), KindLocal);
|
||||
}
|
||||
|
||||
long unix_now() {
|
||||
return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)).total_microseconds() / (long)1E6;
|
||||
}
|
||||
|
||||
long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2)
|
||||
{
|
||||
#ifndef NO_CPP0X
|
||||
auto p1 = ll_to_ptime (ll1);
|
||||
auto p2 = ll_to_ptime (ll2); // NOLINT
|
||||
return (p2 - p1).total_microseconds ();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
long long ptime_diff_in_us (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_microseconds ();
|
||||
}
|
||||
|
||||
long long ptime_diff_in_ms (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_milliseconds ();
|
||||
}
|
||||
|
||||
long long ptime_diff_in_secs (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_seconds ();
|
||||
}
|
||||
|
||||
PTime timestamp_to_ptime(utils::TimeStamp const&t)
|
||||
{
|
||||
auto now_ts = utils::TimeStamp::now();
|
||||
auto now = datetime::local_now();
|
||||
|
||||
auto delta = (now_ts - t).milliseconds();
|
||||
return now - boost::posix_time::milliseconds(delta);
|
||||
}
|
||||
|
||||
utils::TimeStamp ptime_to_timestamp(PTime const &t)
|
||||
{
|
||||
auto now_ts = utils::TimeStamp::now();
|
||||
auto now = datetime::now();
|
||||
|
||||
auto delta = (now - t).total_milliseconds();
|
||||
return now_ts.shift_ms (delta);
|
||||
}
|
||||
|
||||
long long usecs_since (PTime const &t1)
|
||||
{
|
||||
return (boost::posix_time::microsec_clock::universal_time () - t1).total_microseconds ();
|
||||
}
|
||||
|
||||
long long msecs_since (PTime const &t1)
|
||||
{
|
||||
return (boost::posix_time::microsec_clock::universal_time () - t1).total_milliseconds ();
|
||||
}
|
||||
|
||||
PTime now ()
|
||||
{
|
||||
return boost::posix_time::microsec_clock::universal_time ();
|
||||
}
|
||||
|
||||
PTime local_now ()
|
||||
{
|
||||
return boost::posix_time::microsec_clock::local_time ();
|
||||
}
|
||||
|
||||
PTime make_ptime (int year, int month, int day)
|
||||
{
|
||||
// January=1
|
||||
bts_assert_d(month > 0);
|
||||
boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day);
|
||||
return PTime (dt);
|
||||
}
|
||||
|
||||
PTime make_ptime (int year, int month, int day, int hour, int minute, int second, int millisecond)
|
||||
{
|
||||
boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day);
|
||||
return PTime (dt, boost::posix_time::time_duration(hour, minute, second) + boost::posix_time::milliseconds(millisecond));
|
||||
}
|
||||
|
||||
PTime ll_to_ptime (LLDateTime ll) {
|
||||
ll &= ticks_mask;
|
||||
uint64_t ticks_part = ll % ticks_per_day;
|
||||
uint64_t days_part = (ll - anchor_ticks) / ticks_per_day;
|
||||
return PTime(anchor + boost::gregorian::date_duration(days_part),
|
||||
boost::posix_time::time_duration(boost::posix_time::microseconds(ticks_part / ticks_per_micro)));
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz)
|
||||
{
|
||||
const int str_sz = sizeof ("20121204-14:44:02.000");
|
||||
bts_assert (buffer_sz >= str_sz);
|
||||
auto str = FIX_timestamp (ll_to_ptime (ll));
|
||||
bts_assert (str.size () == str_sz-1);
|
||||
memcpy (buffer,&str[0],str.size ());
|
||||
buffer[str.size ()] = '\0';
|
||||
return str.size ();
|
||||
}
|
||||
|
||||
std::string time_string (PTime const &t)
|
||||
{
|
||||
int hours = t.time_of_day ().hours ();
|
||||
int minutes = t.time_of_day ().minutes ();
|
||||
int seconds = t.time_of_day ().seconds ();
|
||||
int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60;
|
||||
int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms;
|
||||
std::ostringstream out;
|
||||
out << std::setw( 2 ) << std::setfill( '0' ) << hours << ":"
|
||||
<< std::setw( 2 ) << std::setfill( '0' ) << minutes << ":"
|
||||
<< std::setw( 2 ) << std::setfill( '0' ) << seconds << "."
|
||||
<< std::setw( 3 ) << std::setfill( '0' ) << frac_ms;
|
||||
return out.str ();
|
||||
}
|
||||
|
||||
std::string date_string (PTime const &t)
|
||||
{
|
||||
std::string result (10,' ');
|
||||
sprintf (&result[0], "%02d/%02d/%04d",
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
(unsigned short) t.date ().year ());
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string iso_date_time_string (PTime const &t)
|
||||
{
|
||||
return boost::posix_time::to_iso_extended_string(t);
|
||||
}
|
||||
|
||||
std::string simple_date_time_string (PTime const &t)
|
||||
{
|
||||
return boost::posix_time::to_simple_string(t);
|
||||
}
|
||||
|
||||
std::string FIX_date_time_string (PTime const &t)
|
||||
{
|
||||
std::string result (strlen ("20121204-14:44:02"),' ');
|
||||
sprintf (&result[0],"%d%02d%02d-%02d:%02d:%02d",
|
||||
(unsigned short) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
t.time_of_day ().hours (),
|
||||
t.time_of_day ().minutes (),
|
||||
t.time_of_day ().seconds ());
|
||||
return result;
|
||||
}
|
||||
|
||||
void FIX_timestamp (PTime const &t, std::string *out)
|
||||
{
|
||||
int hours = t.time_of_day ().hours ();
|
||||
int minutes = t.time_of_day ().minutes ();
|
||||
int seconds = t.time_of_day ().seconds ();
|
||||
int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60;
|
||||
int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms;
|
||||
out->resize(sizeof ("20121204-14:44:02.000"));
|
||||
|
||||
#ifdef WIN32
|
||||
_snprintf (&(*out)[0],out->size (),
|
||||
"%04d%02d%02d-%02d:%02d:%02d.%03d",
|
||||
(int) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
hours,
|
||||
minutes,
|
||||
seconds,
|
||||
frac_ms);
|
||||
#else
|
||||
snprintf (&(*out)[0],out->size (),
|
||||
"%04d%02d%02d-%02d:%02d:%02d.%03d",
|
||||
(int) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
hours,
|
||||
minutes,
|
||||
seconds,
|
||||
frac_ms);
|
||||
#endif
|
||||
out->resize (out->size ()-1);
|
||||
}
|
||||
|
||||
std::string FIX_timestamp (PTime const &t)
|
||||
{
|
||||
std::string result;
|
||||
FIX_timestamp(t,&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_from_today(PTime const &time)
|
||||
{
|
||||
return (time.date() == boost::posix_time::microsec_clock::universal_time().date());
|
||||
}
|
||||
|
||||
bool is_at_least_msecs_old (PTime const &time, uint64_t ms)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return (now - time).total_milliseconds () >= (int64_t) ms;
|
||||
}
|
||||
|
||||
bool is_at_least_secs_old (PTime const &time, uint64_t sec)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return (now - time).total_seconds () >= (int64_t) sec;
|
||||
}
|
||||
|
||||
bool is_at_least_mins_old (PTime const &time, uint64_t mins)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return ((now - time).total_seconds ()/60) >= (int64_t) mins;
|
||||
}
|
||||
|
||||
BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hours)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
int64_t secs = (int64_t) ((now - time).total_seconds ());
|
||||
return (secs/60/60) >= (int64_t) hours;
|
||||
}
|
||||
|
||||
BTS_EXPORT bool is_at_least_days_old (PTime const &time, uint64_t days)
|
||||
{
|
||||
return is_at_least_hours_old (time, days *24);
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_unix_epoch(long seconds)
|
||||
{
|
||||
PTime result(anchor, boost::posix_time::seconds(seconds));
|
||||
return ptime_to_ll(result, KindUnspecified);
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_unix_epoch_ms(long long milliseconds)
|
||||
{
|
||||
PTime result(anchor, boost::posix_time::millisec(milliseconds));
|
||||
return ptime_to_ll(result, KindUnspecified);
|
||||
}
|
||||
|
||||
BTS_EXPORT void ll_to_parts(LLDateTime ll_time, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond)
|
||||
{
|
||||
PTime t = ll_to_ptime(ll_time);
|
||||
year = t.date().year();
|
||||
month = t.date().month();
|
||||
day = t.date().day();
|
||||
hour = t.time_of_day().hours();
|
||||
minute = t.time_of_day().minutes();
|
||||
second = t.time_of_day().seconds();
|
||||
int whole_ms = second * 1000 + minute * 1000 * 60 + hour * 1000 * 60 * 60;
|
||||
millisecond = t.time_of_day().total_milliseconds() - whole_ms;
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_parts(int year, int month, int day, int hour, int minute, int second, int millisecond)
|
||||
{
|
||||
return ptime_to_ll(make_ptime(year, month, day, hour, minute, second, millisecond)); // NOLINT
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_date(const Date& date) {
|
||||
if (date.year == 0 && date.day == 0 && date.month == 0) {
|
||||
return 0;
|
||||
}
|
||||
return ll_from_parts(date.year, date.month, date.day, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
Date ll_to_date(LLDateTime time) {
|
||||
Date date;
|
||||
int year, month, day, unused;
|
||||
ll_to_parts(time, year, month, day, unused, unused, unused, unused);
|
||||
date.year = year;
|
||||
date.month = month;
|
||||
date.day = day;
|
||||
return date;
|
||||
}
|
||||
|
||||
struct UtcTimeStamp {
|
||||
int hour;
|
||||
int min;
|
||||
int sec;
|
||||
int msec;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
};
|
||||
|
||||
struct UtcTimeStampConvertor
|
||||
{
|
||||
static UtcTimeStamp convert( const std::string& value)
|
||||
{
|
||||
bool haveMilliseconds = false;
|
||||
|
||||
switch( value.size() )
|
||||
{
|
||||
case 21: haveMilliseconds = true;
|
||||
case 17: break;
|
||||
default: bts_assert(false);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
int c = 0;
|
||||
for( c = 0; c < 8; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if (value[i++] != '-') bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if( value[i++] != ':' ) bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if( value[i++] != ':' ) bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
|
||||
if( haveMilliseconds )
|
||||
{
|
||||
if( value[i++] != '.' ) bts_assert(false);
|
||||
for( c = 0; c < 3; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
}
|
||||
|
||||
int year, mon, mday, hour, min, sec, millis;
|
||||
|
||||
i = 0;
|
||||
|
||||
year = value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
|
||||
mon = value[i++] - '0';
|
||||
mon = 10 * mon + value[i++] - '0';
|
||||
if( mon < 1 || 12 < mon ) bts_assert(false);
|
||||
|
||||
mday = value[i++] - '0';
|
||||
mday = 10 * mday + value[i++] - '0';
|
||||
if( mday < 1 || 31 < mday ) bts_assert(false);
|
||||
|
||||
++i; // skip '-'
|
||||
|
||||
hour = value[i++] - '0';
|
||||
hour = 10 * hour + value[i++] - '0';
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 23 < hour ) bts_assert(false);
|
||||
|
||||
++i; // skip ':'
|
||||
|
||||
min = value[i++] - '0';
|
||||
min = 10 * min + value[i++] - '0';
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 59 < min ) bts_assert(false);
|
||||
|
||||
++i; // skip ':'
|
||||
|
||||
sec = value[i++] - '0';
|
||||
sec = 10 * sec + value[i++] - '0';
|
||||
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 60 < sec ) bts_assert(false);
|
||||
|
||||
if( haveMilliseconds )
|
||||
{
|
||||
millis = (100 * (value[i+1] - '0')
|
||||
+ 10 * (value[i+2] - '0')
|
||||
+ (value[i+3] - '0'));
|
||||
}
|
||||
else
|
||||
millis = 0;
|
||||
|
||||
return UtcTimeStamp {hour, min, sec, millis, mday, mon, year};
|
||||
}
|
||||
};
|
||||
|
||||
PTime parse_FIX_timestamp (const std::string &ts_str)
|
||||
{
|
||||
auto ts = UtcTimeStampConvertor::convert (ts_str);
|
||||
boost::gregorian::date dt(ts.year, ts.month, ts.day);
|
||||
// fucking boost. unbelieavable.
|
||||
auto ticks_per_second = boost::posix_time::time_duration::rep_type::res_adjust();
|
||||
boost::posix_time::time_duration td (ts.hour, ts.min, ts.sec, ts.msec * (ticks_per_second / 1000));
|
||||
return PTime(dt, td);
|
||||
}
|
||||
|
||||
#ifndef NO_CPP0X
|
||||
extern "C" BTS_EXPORT LLDateTime ll_today() {
|
||||
return datetime::ll_from_date(today());
|
||||
}
|
||||
|
||||
BTS_EXPORT Date today() {
|
||||
auto now = datetime::now();
|
||||
Date date;
|
||||
date.year = now.date().year();
|
||||
date.month = now.date().month();
|
||||
date.day = now.date().day();
|
||||
return date;
|
||||
}
|
||||
BTS_EXPORT Date add_days(Date indate, int days) {
|
||||
boost::gregorian::date dt(indate.year, indate.month, indate.day);
|
||||
auto adjusted_dt = dt + boost::gregorian::days(days);
|
||||
Date date(adjusted_dt.year(), adjusted_dt.month(), adjusted_dt.day());
|
||||
return date;
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT LLDateTime ll_yesterday() {
|
||||
auto now = datetime::now() - boost::gregorian::days(1);
|
||||
return datetime::ll_from_parts(now.date().year(), now.date().month(), now.date().day(), 0, 0, 0, 0);
|
||||
}
|
||||
#endif
|
||||
}}
|
||||
const uint64_t KindLocal = 0x8000000000000000;
|
||||
|
||||
unsigned long long ptime_to_ll(PTime const &t, uint64_t const &kind) {
|
||||
uint64_t days = (t.date() - anchor).days();
|
||||
uint64_t usecs = t.time_of_day().total_microseconds();
|
||||
uint64_t ticks = anchor_ticks + usecs * ticks_per_micro + days * ticks_per_day;
|
||||
return (ticks & ticks_mask) | kind;
|
||||
}
|
||||
|
||||
unsigned long long ptime_to_ll(PTime const &t) {
|
||||
return ptime_to_ll(t, KindUnspecified);
|
||||
}
|
||||
|
||||
LLDateTime ll_now() {
|
||||
return ptime_to_ll(boost::posix_time::microsec_clock::universal_time(), KindUtc);
|
||||
}
|
||||
|
||||
LLDateTime ll_local_now() {
|
||||
return ptime_to_ll(boost::posix_time::microsec_clock::local_time (), KindLocal);
|
||||
}
|
||||
|
||||
long unix_now() {
|
||||
return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)).total_microseconds() / (long)1E6;
|
||||
}
|
||||
|
||||
long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2)
|
||||
{
|
||||
#ifndef NO_CPP0X
|
||||
auto p1 = ll_to_ptime (ll1);
|
||||
auto p2 = ll_to_ptime (ll2); // NOLINT
|
||||
return (p2 - p1).total_microseconds ();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
long long ptime_diff_in_us (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_microseconds ();
|
||||
}
|
||||
|
||||
long long ptime_diff_in_ms (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_milliseconds ();
|
||||
}
|
||||
|
||||
long long ptime_diff_in_secs (PTime const &t1, PTime const &t2)
|
||||
{
|
||||
return (t1 - t2).total_seconds ();
|
||||
}
|
||||
|
||||
PTime timestamp_to_ptime(utils::TimeStamp const&t)
|
||||
{
|
||||
auto now_ts = utils::TimeStamp::now();
|
||||
auto now = datetime::local_now();
|
||||
|
||||
auto delta = (now_ts - t).milliseconds();
|
||||
return now - boost::posix_time::milliseconds(delta);
|
||||
}
|
||||
|
||||
utils::TimeStamp ptime_to_timestamp(PTime const &t)
|
||||
{
|
||||
auto now_ts = utils::TimeStamp::now();
|
||||
auto now = datetime::now();
|
||||
|
||||
auto delta = (now - t).total_milliseconds();
|
||||
return now_ts.shift_ms (delta);
|
||||
}
|
||||
|
||||
long long usecs_since (PTime const &t1)
|
||||
{
|
||||
return (boost::posix_time::microsec_clock::universal_time () - t1).total_microseconds ();
|
||||
}
|
||||
|
||||
long long msecs_since (PTime const &t1)
|
||||
{
|
||||
return (boost::posix_time::microsec_clock::universal_time () - t1).total_milliseconds ();
|
||||
}
|
||||
|
||||
PTime now ()
|
||||
{
|
||||
return boost::posix_time::microsec_clock::universal_time ();
|
||||
}
|
||||
|
||||
PTime local_now ()
|
||||
{
|
||||
return boost::posix_time::microsec_clock::local_time ();
|
||||
}
|
||||
|
||||
PTime make_ptime (int year, int month, int day)
|
||||
{
|
||||
// January=1
|
||||
bts_assert_d(month > 0);
|
||||
boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day);
|
||||
return PTime (dt);
|
||||
}
|
||||
|
||||
PTime make_ptime (int year, int month, int day, int hour, int minute, int second, int millisecond)
|
||||
{
|
||||
boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day);
|
||||
return PTime (dt, boost::posix_time::time_duration(hour, minute, second) + boost::posix_time::milliseconds(millisecond));
|
||||
}
|
||||
|
||||
PTime ll_to_ptime (LLDateTime ll) {
|
||||
ll &= ticks_mask;
|
||||
uint64_t ticks_part = ll % ticks_per_day;
|
||||
uint64_t days_part = (ll - anchor_ticks) / ticks_per_day;
|
||||
return PTime(anchor + boost::gregorian::date_duration(days_part),
|
||||
boost::posix_time::time_duration(boost::posix_time::microseconds(ticks_part / ticks_per_micro)));
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz)
|
||||
{
|
||||
const int str_sz = sizeof ("20121204-14:44:02.000");
|
||||
bts_assert (buffer_sz >= str_sz);
|
||||
auto str = FIX_timestamp (ll_to_ptime (ll));
|
||||
bts_assert (str.size () == str_sz-1);
|
||||
memcpy (buffer,&str[0],str.size ());
|
||||
buffer[str.size ()] = '\0';
|
||||
return str.size ();
|
||||
}
|
||||
|
||||
std::string time_string (PTime const &t)
|
||||
{
|
||||
int hours = t.time_of_day ().hours ();
|
||||
int minutes = t.time_of_day ().minutes ();
|
||||
int seconds = t.time_of_day ().seconds ();
|
||||
int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60;
|
||||
int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms;
|
||||
std::ostringstream out;
|
||||
out << std::setw( 2 ) << std::setfill( '0' ) << hours << ":"
|
||||
<< std::setw( 2 ) << std::setfill( '0' ) << minutes << ":"
|
||||
<< std::setw( 2 ) << std::setfill( '0' ) << seconds << "."
|
||||
<< std::setw( 3 ) << std::setfill( '0' ) << frac_ms;
|
||||
return out.str ();
|
||||
}
|
||||
|
||||
std::string date_string (PTime const &t)
|
||||
{
|
||||
std::string result (10,' ');
|
||||
sprintf (&result[0], "%02d/%02d/%04d",
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
(unsigned short) t.date ().year ());
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string iso_date_time_string (PTime const &t)
|
||||
{
|
||||
return boost::posix_time::to_iso_extended_string(t);
|
||||
}
|
||||
|
||||
std::string simple_date_time_string (PTime const &t)
|
||||
{
|
||||
return boost::posix_time::to_simple_string(t);
|
||||
}
|
||||
|
||||
std::string FIX_date_time_string (PTime const &t)
|
||||
{
|
||||
std::string result (strlen ("20121204-14:44:02"),' ');
|
||||
sprintf (&result[0],"%d%02d%02d-%02d:%02d:%02d",
|
||||
(unsigned short) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
t.time_of_day ().hours (),
|
||||
t.time_of_day ().minutes (),
|
||||
t.time_of_day ().seconds ());
|
||||
return result;
|
||||
}
|
||||
|
||||
void FIX_timestamp (PTime const &t, std::string *out)
|
||||
{
|
||||
int hours = t.time_of_day ().hours ();
|
||||
int minutes = t.time_of_day ().minutes ();
|
||||
int seconds = t.time_of_day ().seconds ();
|
||||
int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60;
|
||||
int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms;
|
||||
out->resize(sizeof ("20121204-14:44:02.000"));
|
||||
|
||||
#ifdef WIN32
|
||||
_snprintf (&(*out)[0],out->size (),
|
||||
"%04d%02d%02d-%02d:%02d:%02d.%03d",
|
||||
(int) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
hours,
|
||||
minutes,
|
||||
seconds,
|
||||
frac_ms);
|
||||
#else
|
||||
snprintf (&(*out)[0],out->size (),
|
||||
"%04d%02d%02d-%02d:%02d:%02d.%03d",
|
||||
(int) t.date ().year (),
|
||||
t.date ().month ().as_number (),
|
||||
t.date ().day ().as_number (),
|
||||
hours,
|
||||
minutes,
|
||||
seconds,
|
||||
frac_ms);
|
||||
#endif
|
||||
out->resize (out->size ()-1);
|
||||
}
|
||||
|
||||
std::string FIX_timestamp (PTime const &t)
|
||||
{
|
||||
std::string result;
|
||||
FIX_timestamp(t,&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_from_today(PTime const &time)
|
||||
{
|
||||
return (time.date() == boost::posix_time::microsec_clock::universal_time().date());
|
||||
}
|
||||
|
||||
bool is_at_least_msecs_old (PTime const &time, uint64_t ms)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return (now - time).total_milliseconds () >= (int64_t) ms;
|
||||
}
|
||||
|
||||
bool is_at_least_secs_old (PTime const &time, uint64_t sec)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return (now - time).total_seconds () >= (int64_t) sec;
|
||||
}
|
||||
|
||||
bool is_at_least_mins_old (PTime const &time, uint64_t mins)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
return ((now - time).total_seconds ()/60) >= (int64_t) mins;
|
||||
}
|
||||
|
||||
BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hours)
|
||||
{
|
||||
PTime now = boost::posix_time::microsec_clock::universal_time ();
|
||||
int64_t secs = (int64_t) ((now - time).total_seconds ());
|
||||
return (secs/60/60) >= (int64_t) hours;
|
||||
}
|
||||
|
||||
BTS_EXPORT bool is_at_least_days_old (PTime const &time, uint64_t days)
|
||||
{
|
||||
return is_at_least_hours_old (time, days *24);
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_unix_epoch(long seconds)
|
||||
{
|
||||
PTime result(anchor, boost::posix_time::seconds(seconds));
|
||||
return ptime_to_ll(result, KindUnspecified);
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_unix_epoch_ms(long long milliseconds)
|
||||
{
|
||||
PTime result(anchor, boost::posix_time::millisec(milliseconds));
|
||||
return ptime_to_ll(result, KindUnspecified);
|
||||
}
|
||||
|
||||
BTS_EXPORT void ll_to_parts(LLDateTime ll_time, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond)
|
||||
{
|
||||
PTime t = ll_to_ptime(ll_time);
|
||||
year = t.date().year();
|
||||
month = t.date().month();
|
||||
day = t.date().day();
|
||||
hour = t.time_of_day().hours();
|
||||
minute = t.time_of_day().minutes();
|
||||
second = t.time_of_day().seconds();
|
||||
int whole_ms = second * 1000 + minute * 1000 * 60 + hour * 1000 * 60 * 60;
|
||||
millisecond = t.time_of_day().total_milliseconds() - whole_ms;
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_parts(int year, int month, int day, int hour, int minute, int second, int millisecond)
|
||||
{
|
||||
return ptime_to_ll(make_ptime(year, month, day, hour, minute, second, millisecond)); // NOLINT
|
||||
}
|
||||
|
||||
BTS_EXPORT LLDateTime ll_from_date(const Date& date) {
|
||||
if (date.year == 0 && date.day == 0 && date.month == 0) {
|
||||
return 0;
|
||||
}
|
||||
return ll_from_parts(date.year, date.month, date.day, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
Date ll_to_date(LLDateTime time) {
|
||||
Date date;
|
||||
int year, month, day, unused;
|
||||
ll_to_parts(time, year, month, day, unused, unused, unused, unused);
|
||||
date.year = year;
|
||||
date.month = month;
|
||||
date.day = day;
|
||||
return date;
|
||||
}
|
||||
|
||||
struct UtcTimeStamp {
|
||||
int hour;
|
||||
int min;
|
||||
int sec;
|
||||
int msec;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
};
|
||||
|
||||
struct UtcTimeStampConvertor
|
||||
{
|
||||
static UtcTimeStamp convert( const std::string& value)
|
||||
{
|
||||
bool haveMilliseconds = false;
|
||||
|
||||
switch( value.size() )
|
||||
{
|
||||
case 21: haveMilliseconds = true;
|
||||
case 17: break;
|
||||
default: bts_assert(false);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
int c = 0;
|
||||
for( c = 0; c < 8; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if (value[i++] != '-') bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if( value[i++] != ':' ) bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
if( value[i++] != ':' ) bts_assert(false);
|
||||
for( c = 0; c < 2; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
|
||||
if( haveMilliseconds )
|
||||
{
|
||||
if( value[i++] != '.' ) bts_assert(false);
|
||||
for( c = 0; c < 3; ++c )
|
||||
if( !isdigit(value[i++]) ) bts_assert(false);
|
||||
}
|
||||
|
||||
int year, mon, mday, hour, min, sec, millis;
|
||||
|
||||
i = 0;
|
||||
|
||||
year = value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
year = 10 * year + value[i++] - '0';
|
||||
|
||||
mon = value[i++] - '0';
|
||||
mon = 10 * mon + value[i++] - '0';
|
||||
if( mon < 1 || 12 < mon ) bts_assert(false);
|
||||
|
||||
mday = value[i++] - '0';
|
||||
mday = 10 * mday + value[i++] - '0';
|
||||
if( mday < 1 || 31 < mday ) bts_assert(false);
|
||||
|
||||
++i; // skip '-'
|
||||
|
||||
hour = value[i++] - '0';
|
||||
hour = 10 * hour + value[i++] - '0';
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 23 < hour ) bts_assert(false);
|
||||
|
||||
++i; // skip ':'
|
||||
|
||||
min = value[i++] - '0';
|
||||
min = 10 * min + value[i++] - '0';
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 59 < min ) bts_assert(false);
|
||||
|
||||
++i; // skip ':'
|
||||
|
||||
sec = value[i++] - '0';
|
||||
sec = 10 * sec + value[i++] - '0';
|
||||
|
||||
// No check for >= 0 as no '-' are converted here
|
||||
if( 60 < sec ) bts_assert(false);
|
||||
|
||||
if( haveMilliseconds )
|
||||
{
|
||||
millis = (100 * (value[i+1] - '0')
|
||||
+ 10 * (value[i+2] - '0')
|
||||
+ (value[i+3] - '0'));
|
||||
}
|
||||
else
|
||||
millis = 0;
|
||||
|
||||
return UtcTimeStamp {hour, min, sec, millis, mday, mon, year};
|
||||
}
|
||||
};
|
||||
|
||||
PTime parse_FIX_timestamp (const std::string &ts_str)
|
||||
{
|
||||
auto ts = UtcTimeStampConvertor::convert (ts_str);
|
||||
boost::gregorian::date dt(ts.year, ts.month, ts.day);
|
||||
// fucking boost. unbelieavable.
|
||||
auto ticks_per_second = boost::posix_time::time_duration::rep_type::res_adjust();
|
||||
boost::posix_time::time_duration td (ts.hour, ts.min, ts.sec, ts.msec * (ticks_per_second / 1000));
|
||||
return PTime(dt, td);
|
||||
}
|
||||
|
||||
#ifndef NO_CPP0X
|
||||
extern "C" BTS_EXPORT LLDateTime ll_today() {
|
||||
return datetime::ll_from_date(today());
|
||||
}
|
||||
|
||||
BTS_EXPORT Date today() {
|
||||
auto now = datetime::now();
|
||||
Date date;
|
||||
date.year = now.date().year();
|
||||
date.month = now.date().month();
|
||||
date.day = now.date().day();
|
||||
return date;
|
||||
}
|
||||
BTS_EXPORT Date add_days(Date indate, int days) {
|
||||
boost::gregorian::date dt(indate.year, indate.month, indate.day);
|
||||
auto adjusted_dt = dt + boost::gregorian::days(days);
|
||||
Date date(adjusted_dt.year(), adjusted_dt.month(), adjusted_dt.day());
|
||||
return date;
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT LLDateTime ll_yesterday() {
|
||||
auto now = datetime::now() - boost::gregorian::days(1);
|
||||
return datetime::ll_from_parts(now.date().year(), now.date().month(), now.date().day(), 0, 0, 0, 0);
|
||||
}
|
||||
#endif
|
||||
}}
|
||||
Reference in new issue
Block a user