Made Limiter more robust for orders with undiscovered securities.

This commit is contained in:
Pedro Pinto committed 2019-06-07 13:50:44 -04:00
1 parent baac6a893d
commit 929492c2f0
12 files changed
+123 -67

No files matched your search

+3 -1
View File
@@ -8,7 +8,9 @@ insert_anchor_links = "right"
### User Visible Changes
- Made our orders easier to spot in MBO display
- Changed middle-click on center column to recenter ladder and introduced Alt middle-click on center to bring up the MBO popup
- Fixed double counting of spread legs in limits
- Removed expired fills from the system
- Fixed limit validation of orders with yet undiscovered securities
## 2.1.4
### User Visible Changes
- Fixed handling of cancel button in security picker
+18 -8
View File
@@ -3,6 +3,7 @@
#include "lib/jetfix/common/OutFIX.h"
#include "lib/jetfix/gateway/JetFIXGateway.h"
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/events/CancelRejectEvent.h"
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
@@ -24,10 +25,11 @@ using JFGw =
fix::RejectMsg, fix::CancelRejectMsg>;
struct ExecutionGateway::PImpl {
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l)
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l,
md::SecurityOracle o)
: gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES),
securities(), securities_by_descriptor(), gw_name(cfg.name()),
cfg(cfg), perf_log(l) {}
cfg(cfg), perf_log(l), sec_oracle(o) {}
JFGw gw;
boost::object_pool<ExecutionReport> exe_report_pool;
@@ -41,11 +43,12 @@ struct ExecutionGateway::PImpl {
OnGwStatusChange on_gw_status_change;
OnCancelReject on_cancel_reject;
trace::PerfLog &perf_log;
md::SecurityOracle sec_oracle;
};
ExecutionGateway::ExecutionGateway(config::JetFIX::Connection const &cfg,
trace::PerfLog &log)
: me(new PImpl(cfg, log)) {
trace::PerfLog &log, md::SecurityOracle o)
: me(new PImpl(cfg, log, o)) {
me->state = State::WAITING_FOR_SECURITIES;
}
ExecutionGateway::~ExecutionGateway() {}
@@ -111,11 +114,17 @@ void ExecutionGateway::on_exe_report(
SecurityKey sk;
decode_clorid(msg.get<fix::ClOrdID>(), &ok, &sk);
auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), "ExecutionGateway::on_exe_report", ok.value());
auto scoped_log =
trace::log_scope(me->perf_log, msg.trace(),
"ExecutionGateway::on_exe_report", ok.value());
LOG(Latency, Info, "Started handling of execution report for order {}",
ok.value());
auto sec = me->securities[sk.value()];
auto &sec = me->securities[sk.value()];
if (!sec) {
sec = me->sec_oracle.get_security(SecurityKey(sk.value()));
me->securities_by_descriptor[securities::descriptor(*sec)] = sec;
}
if (!sec)
LOG(OrderMan, Error,
@@ -303,8 +312,9 @@ void ExecutionGateway::dispatch_incoming_fix_messages() {
void ExecutionGateway::send_fix(jetfix::OutFIXPtr const &fix,
trace::CorID trace) {
auto scoped_log = trace::log_scope(me->perf_log,trace,"ExecutionGateway::send_fix");
me->gw.send(fix,trace);
auto scoped_log =
trace::log_scope(me->perf_log, trace, "ExecutionGateway::send_fix");
me->gw.send(fix, trace);
me->gw.dispatch_events();
}
+3 -1
View File
@@ -9,6 +9,7 @@
#include <memory>
namespace bts {
namespace md { class SecurityOracle;}
namespace trace {struct CorID;class PerfLog;}
namespace orderman {
class NewSecuritiesEvent;
@@ -22,7 +23,8 @@ using OnCancelReject = std::function<void(OrderKey, std::string const &)>;
// fix execution gateway for a single FIX session
class ExecutionGateway {
public:
ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &);
ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &,
md::SecurityOracle);
~ExecutionGateway();
void start();
@@ -16,7 +16,7 @@ OrderServicesData::OrderServicesData(OrderManager &owner,
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
sessions.push_back(std::make_unique<SessionServicesData>(c, l));
sessions.push_back(std::make_unique<SessionServicesData>(c, l,o));
}
} // namespace orderman
} // namespace bts
@@ -48,10 +48,10 @@ struct ParentKey {
struct SessionServicesData {
SessionServicesData(config::JetFIX::Connection const &cfg,
trace::PerfLog &log)
trace::PerfLog &log, md::SecurityOracle o)
: session(cfg.name()), gw_connected(false), cfg(cfg),
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
gw(cfg, log) {}
gw(cfg, log,o) {}
std::string session;
std::vector<jetfix::OutFIXPtr> free_buffers;
+11 -11
View File
@@ -98,7 +98,7 @@ public:
//iv. account should be activated
Limiter::LimiterQueryResult can_trade(UserID user_id,
AccountID account_id) const{
Limiter::LimiterQueryResult result; result.ok = true;
Limiter::LimiterQueryResult result; result.result = Limiter::OK;
auto user_it = _user_data_map.find(user_id.value());
if (user_it != _user_data_map.end()){
const UserData& user_data = user_it->second;
@@ -106,23 +106,23 @@ public:
const api::admin::BTSAccount* account_ptr = user_data.find_account(account_id);
if (account_ptr != nullptr){
if (!account_ptr->is_active()){
result.ok=false;
result.result=Limiter::ACCOUNT_DEACTIVATED;
result.error_msg = "Account is de-activated";
}
}
else{
result.ok=false;
result.result=Limiter::NO_SUCH_ACCOUNT;
result.error_msg = "Can not find account";
}
}
else
{
result.ok=false;
result.result=Limiter::USER_DEACTIVATED;
result.error_msg = "User is de-activated";
}
}
else{
result.ok=false;
result.result=Limiter::NO_SUCH_USER;
result.error_msg = "Can not find user";
}
return result;
@@ -131,11 +131,11 @@ public:
const api::Security& security,
api::Side side,
Quantity quantity) const{
Limiter::LimiterQueryResult rv; rv.ok = true;
Limiter::LimiterQueryResult rv; rv.result = Limiter::OK;
const SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
rv = sec_tree->can_send_new_order(account_id, side, quantity);
if (rv.ok && security.has_spread()){
if (rv.ok() && security.has_spread()){
// if its rejected we dont need to validate further,
// however if its ok and its a spread we need to check
// the legs
@@ -143,7 +143,7 @@ public:
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_qty = Quantity (quantity.value() * std::abs(leg.quantity()));
rv = can_send_new_order (account_id, leg.security(), effective_leg_side, effective_leg_qty);
if (!rv.ok)
if (!rv.ok())
return rv;
}
}
@@ -153,18 +153,18 @@ public:
const api::Security& security,
api::Side side,
Quantity from, Quantity to) const{
Limiter::LimiterQueryResult rv; rv.ok = true;
Limiter::LimiterQueryResult rv; rv.result = Limiter::OK;
const SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
rv = sec_tree->can_update_order(account_id, side, from, to);
// same logic for the spread legs as above
if (rv.ok && security.has_spread()){
if (rv.ok() && security.has_spread()){
for (const api::ContractLeg& leg : security.spread().legs()){
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_from_qty = Quantity (from.value() * std::abs(leg.quantity()));
Quantity effective_leg_to_qty = Quantity (to.value() * std::abs(leg.quantity()));
rv = can_update_order (account_id, leg.security(), effective_leg_side, effective_leg_from_qty, effective_leg_to_qty);
if (!rv.ok)
if (!rv.ok())
return rv;
}
}
+17 -6
View File
@@ -80,21 +80,32 @@ Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id,
}
Limiter::LimiterQueryResult
Limiter::can_send_new_order(AccountID account_id, api::Security const &security,
api::Side side, Quantity quantity) const {
LimiterQueryResult rv = me->limit_store->can_send_new_order(
api::Side side, Quantity quantity) {
LimiterQueryResult rv =
me->limit_store->can_send_new_order(
account_id, security, side, quantity);
// rv.ok = true;
if (rv.result == Limiter::NO_SUCH_SECURITY) {
add_security(security);
rv = me->limit_store->can_send_new_order(
account_id, security, side, quantity);
}
return rv;
}
Limiter::LimiterQueryResult
Limiter::can_update_order(AccountID account_id, api::Security const &security,
api::Side side, Quantity from, Quantity to) const {
api::Side side, Quantity from, Quantity to) {
LimiterQueryResult rv;
if (from == to)
rv.ok = true;
else
rv.result = Limiter::OK;
else {
rv = me->limit_store->can_update_order(account_id, security, side, from,
to);
if (rv.result == Limiter::NO_SUCH_SECURITY) {
add_security(security);
rv = me->limit_store->can_update_order(account_id, security, side, from,
to);
}
}
return rv;
}
void Limiter::_add_account_and_reset(AccountID account_id) {
+26 -7
View File
@@ -24,26 +24,45 @@ class Limiter {
void add_securities(std::vector<api::Security const *> const &secs);
void add_security(api::Security const &new_sec);
void add(api::Security const &, AccountID, api::Side, Quantity, bool recurse);
void remove(api::Security const &, AccountID, api::Side, Quantity, bool recurse);
void add(api::Security const &, AccountID, api::Side, Quantity,
bool recurse);
void remove(api::Security const &, AccountID, api::Side, Quantity,
bool recurse);
void add(api::Security const &, std::string const & /* account_name */,
api::Side, Quantity, bool recurse);
void remove(api::Security const &, std::string const & /* account_name */,
api::Side, Quantity, bool recurse);
struct LimiterQueryResult {
bool ok;
std::string error_msg;
enum LimiterErrorCode {
OK,
NO_SUCH_SECURITY,
SEC_LONG_POS_BREACHED,
PROD_LONG_POS_BREACHED,
SEC_SHORT_POS_BREACHED,
PROD_SHORT_POS_BREACHED,
CLIP_SIZE_BREACHED,
ACCOUNT_DEACTIVATED,
NO_SUCH_ACCOUNT,
USER_DEACTIVATED,
NO_SUCH_USER
};
struct LimiterQueryResult {
std::string error_msg;
LimiterErrorCode result;
bool ok() const { return result == OK; }
bool error() const { return result != OK; }
};
LimiterQueryResult can_trade(UserID user, AccountID into_account) const;
LimiterQueryResult can_send_new_order(AccountID into_account,
api::Security const &sec,
api::Side side,
Quantity quantity) const;
Quantity quantity) ;
LimiterQueryResult can_update_order(AccountID account,
api::Security const &sec, api::Side side,
Quantity from, Quantity to) const;
Quantity from, Quantity to) ;
~Limiter();
// for testing purposes only
LimitStore &limit_store();
+32 -20
View File
@@ -16,7 +16,7 @@ public:
int short_pos_limit,
int net_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
bts_assert(long_pos_limit >= 0);
bts_assert(short_pos_limit <= 0);
int net_pos_new = 0;
@@ -25,18 +25,24 @@ public:
else if(side == api::Side::SIDE_SELL)
net_pos_new = net_pos - quantity.value();
if (net_pos_new > long_pos_limit && side==api::Side::SIDE_BUY){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Security Long Position (Max {} Current {})", long_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Product Long Position (Max {} Current {})", long_pos_limit, net_pos);
}
}
else if (net_pos_new < short_pos_limit && side==api::Side::SIDE_SELL){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Security Short Position (Max {} Current {})", short_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Product Short Position (Max {} Current {})", short_pos_limit, net_pos);
}
}
}
static void can_update_order_net (api::Side side,
@@ -47,7 +53,7 @@ public:
int short_pos_limit,
int net_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if (to.value() > from.value()){
int net_pos_new = 0;
uint32_t diff = to.value() - from.value();
@@ -56,18 +62,24 @@ public:
else if(side == api::Side::SIDE_SELL)
net_pos_new = net_pos - diff;
if (net_pos_new > long_pos_limit){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Security Long Position (Max {} Current {})", long_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Product Long Position (Max {} Current {})", long_pos_limit, net_pos);
}
}
else if (net_pos_new < short_pos_limit){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Security Short Position (Max {} Current {})", short_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Product Short Position (Max {} Current {})", short_pos_limit, net_pos);
}
}
}
}
@@ -78,13 +90,13 @@ public:
uint32_t long_pos,
uint32_t short_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if(side == api::Side::SIDE_BUY){
if(long_pos + quantity.value() > long_pos_limit){
out_res.error_msg = fmt::format("Long Position (Abs) "
"(Max {} Current {})",
long_pos_limit,long_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
}
}
else{
@@ -92,7 +104,7 @@ public:
out_res.error_msg = fmt::format("Short Position (Abs) "
"(Max {} Current {})",
short_pos_limit,short_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
}
}
}
@@ -104,7 +116,7 @@ public:
uint32_t long_pos,
uint32_t short_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if (to.value() > from.value()){
uint32_t diff = to.value() - from.value();
if(side == api::Side::SIDE_BUY){
@@ -112,7 +124,7 @@ public:
out_res.error_msg = fmt::format("Long Position (Abs) "
"(Max {} Current {})",
long_pos_limit,long_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
}
}
else{
@@ -120,7 +132,7 @@ public:
out_res.error_msg = fmt::format("Short Position (Abs) "
"(Max {} Current {})",
short_pos_limit,short_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
}
}
}
+6 -6
View File
@@ -64,9 +64,9 @@ public:
// risk limit checks
////////////////////
Limiter::LimiterQueryResult can_send_new_order(api::Side side, Quantity quantity) const{
Limiter::LimiterQueryResult result;result.ok=true;
Limiter::LimiterQueryResult result;result.result= Limiter::OK;
if (quantity.value() > _clip_size){
result.ok = false;
result.result = Limiter::CLIP_SIZE_BREACHED;
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, quantity.value());
return result;
}
@@ -74,14 +74,14 @@ public:
LimiterUtils::can_send_new_order_net (side, quantity, true,
_long_pos_limit, _short_pos_limit,
_net_pos, result);
if (result.ok)
if (result.ok())
result = _product_node.can_send_new_order(side, quantity);
return result;
}
Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to) const{
Limiter::LimiterQueryResult result;result.ok=true;
Limiter::LimiterQueryResult result;result.result=Limiter::OK;
if (to.value() > _clip_size){
result.ok = false;
result.result = Limiter::CLIP_SIZE_BREACHED;
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, to.value());
return result;
}
@@ -89,7 +89,7 @@ public:
LimiterUtils::can_update_order_net (side, from, to, true,
_long_pos_limit, _short_pos_limit,
_net_pos, result);
if (result.ok)
if (result.ok())
result = _product_node.can_update_order(side, from, to);
return result;
}
+3 -3
View File
@@ -369,7 +369,7 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
auto lqr = me->services->limiter().can_trade(
UserID(me->definition.user_id()),
AccountID(me->definition.account_id()));
if (!lqr.ok) {
if (!lqr.ok()) {
LOG(OrderMan, reject_level,
"Unauthorized order attempted. User: {} on "
"Account: {}. Msg: {} Order: {}. Dropping order",
@@ -386,7 +386,7 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
AccountID(me->definition.account_id()), *me->sec, me->definition.side(),
q);
if (!lqr.ok) {
if (!lqr.ok()) {
LOG(OrderMan, reject_level,
"New order would breach limit. User: {} Account: "
"{} Msg: {} Order: {}. Dropping order",
@@ -586,7 +586,7 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
Quantity(current_working_qty));
// are we under the position limits?
if (!lqr.ok) {
if (!lqr.ok()) {
auto msg = fmt::format("Cannot update order due to limit breach: {}",
lqr.error_msg);
LOG(OrderMan, Error,
+1 -1
View File
@@ -1 +1 @@
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/cssstyle/scripts/run_tests.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)